Add migrator

This commit is contained in:
Jinzhu 2020-01-28 23:01:35 +08:00
parent f0d514e330
commit 8eae7e4ab9
6 changed files with 112 additions and 0 deletions

1
.gitignore vendored
View File

@ -1,3 +1,4 @@
TODO
documents documents
coverage.txt coverage.txt
_book _book

2
go.mod
View File

@ -1 +1,3 @@
module github.com/jinzhu/gorm module github.com/jinzhu/gorm
go 1.13

46
gorm.go Normal file
View File

@ -0,0 +1,46 @@
package gorm
import (
"time"
"github.com/jinzhu/gorm/logger"
)
// Config GORM config
type Config struct {
// Set true to use singular table name, by default, GORM will pluralize your struct's name as table name
// Refer https://github.com/jinzhu/inflection for inflection rules
SingularTable bool
// GORM perform single create, update, delete operations in transactions by default to ensure database data integrity
// You can cancel it by setting `SkipDefaultTransaction` to true
SkipDefaultTransaction bool
// Logger
Logger logger.Interface
// NowFunc the function to be used when creating a new timestamp
NowFunc func() time.Time
}
// Model a basic GoLang struct which includes the following fields: ID, CreatedAt, UpdatedAt, DeletedAt
// It may be embeded into your model or you may build your own model without it
// type User struct {
// gorm.Model
// }
type Model struct {
ID uint `gorm:"primary_key"`
CreatedAt time.Time
UpdatedAt time.Time
DeletedAt *time.Time `gorm:"index"`
}
// Dialector GORM database dialector
type Dialector interface {
Migrator() Migrator
}
// DB GORM DB definition
type DB struct {
*Config
}

5
logger/logger.go Normal file
View File

@ -0,0 +1,5 @@
package logger
// Interface logger interface
type Interface interface {
}

44
migrator.go Normal file
View File

@ -0,0 +1,44 @@
package gorm
import (
"database/sql"
)
// ViewOption view option
type ViewOption struct {
Replace bool
CheckOption string
Query *DB
}
type Migrator interface {
// AutoMigrate
AutoMigrate(dst ...interface{}) error
// Tables
CreateTable(dst ...interface{}) error
DropTable(dst ...interface{}) error
HasTable(dst ...interface{}) error
RenameTable(oldName, newName string) error
// Columns
AddColumn(dst interface{}, field string) error
DropColumn(dst interface{}, field string) error
AlterColumn(dst interface{}, field string) error
RenameColumn(dst interface{}, oldName, field string) error
ColumnTypes(dst interface{}) ([]*sql.ColumnType, error)
// Views
CreateView(name string, option ViewOption) error
DropView(name string) error
// Constraints
CreateConstraint(dst interface{}, name string) error
DropConstraint(dst interface{}, name string) error
// Indexes
CreateIndex(dst interface{}, name string) error
DropIndex(dst interface{}, name string) error
HasIndex(dst interface{}, name string) error
RenameIndex(dst interface{}, oldName, newName string) error
}

14
migrator/migrator.go Normal file
View File

@ -0,0 +1,14 @@
package migrator
import "github.com/jinzhu/gorm"
// Migrator migrator struct
type Migrator struct {
*Config
}
// Config schema config
type Config struct {
CheckExistsBeforeDropping bool
DB *gorm.DB
}