gorm/migrator.go

94 lines
2.5 KiB
Go
Raw Normal View History

2020-01-28 18:01:35 +03:00
package gorm
import (
"reflect"
"gorm.io/gorm/clause"
"gorm.io/gorm/schema"
2020-01-28 18:01:35 +03:00
)
2020-02-20 18:04:03 +03:00
// Migrator returns migrator
func (db *DB) Migrator() Migrator {
2021-03-19 11:34:51 +03:00
tx := db.getInstance()
2021-03-11 05:29:52 +03:00
// apply scopes to migrator
2021-03-19 11:34:51 +03:00
for len(tx.Statement.scopes) > 0 {
scopes := tx.Statement.scopes
tx.Statement.scopes = nil
2021-03-19 08:44:25 +03:00
for _, scope := range scopes {
2021-03-19 11:34:51 +03:00
tx = scope(tx)
2021-03-19 08:44:25 +03:00
}
2021-03-11 05:29:52 +03:00
}
2021-03-19 11:34:51 +03:00
return tx.Dialector.Migrator(tx.Session(&Session{}))
2020-02-20 18:04:03 +03:00
}
2020-06-08 08:45:41 +03:00
// AutoMigrate run auto migration for given models
func (db *DB) AutoMigrate(dst ...interface{}) error {
return db.Migrator().AutoMigrate(dst...)
}
2020-01-28 18:01:35 +03:00
// ViewOption view option
type ViewOption struct {
Replace bool
CheckOption string
Query *DB
}
// ColumnType column type interface
type ColumnType interface {
Name() string
DatabaseTypeName() string // varchar
ColumnType() (columnType string, ok bool) // varchar(64)
PrimaryKey() (isPrimaryKey bool, ok bool)
AutoIncrement() (isAutoIncrement bool, ok bool)
Length() (length int64, ok bool)
DecimalSize() (precision int64, scale int64, ok bool)
Nullable() (nullable bool, ok bool)
Unique() (unique bool, ok bool)
ScanType() reflect.Type
Comment() (value string, ok bool)
DefaultValue() (value string, ok bool)
}
// Migrator migrator interface
2020-01-28 18:01:35 +03:00
type Migrator interface {
// AutoMigrate
AutoMigrate(dst ...interface{}) error
2020-02-20 18:04:03 +03:00
// Database
CurrentDatabase() string
FullDataTypeOf(*schema.Field) clause.Expr
2020-02-20 18:04:03 +03:00
2020-01-28 18:01:35 +03:00
// Tables
CreateTable(dst ...interface{}) error
DropTable(dst ...interface{}) error
2020-02-22 12:53:57 +03:00
HasTable(dst interface{}) bool
2020-05-31 05:24:49 +03:00
RenameTable(oldName, newName interface{}) error
GetTables() (tableList []string, err error)
2020-01-28 18:01:35 +03:00
// Columns
AddColumn(dst interface{}, field string) error
DropColumn(dst interface{}, field string) error
AlterColumn(dst interface{}, field string) error
MigrateColumn(dst interface{}, field *schema.Field, columnType ColumnType) error
2020-02-22 08:09:57 +03:00
HasColumn(dst interface{}, field string) bool
2020-01-28 18:01:35 +03:00
RenameColumn(dst interface{}, oldName, field string) error
ColumnTypes(dst interface{}) ([]ColumnType, error)
2020-01-28 18:01:35 +03:00
// Views
CreateView(name string, option ViewOption) error
DropView(name string) error
// Constraints
CreateConstraint(dst interface{}, name string) error
DropConstraint(dst interface{}, name string) error
2020-02-22 08:09:57 +03:00
HasConstraint(dst interface{}, name string) bool
2020-01-28 18:01:35 +03:00
// Indexes
CreateIndex(dst interface{}, name string) error
DropIndex(dst interface{}, name string) error
2020-02-20 18:04:03 +03:00
HasIndex(dst interface{}, name string) bool
2020-01-28 18:01:35 +03:00
RenameIndex(dst interface{}, oldName, newName string) error
}