migrator
v1.31.2
Documentation
pkg.go.dev ↗Package migrator provides the interface and implementation for database schema migration in GORM.
Types
BuildIndexOptionsInterface
type BuildIndexOptionsInterface interface {
BuildIndexOptions([]schema.IndexOption, *gorm.Statement) []interface{}
}
BuildIndexOptionsInterface build index options interface
ColumnType
type ColumnType struct {
SQLColumnType *sql.ColumnType
NameValue sql.NullString
DataTypeValue sql.NullString
ColumnTypeValue sql.NullString
PrimaryKeyValue sql.NullBool
UniqueValue sql.NullBool
AutoIncrementValue sql.NullBool
LengthValue sql.NullInt64
DecimalSizeValue sql.NullInt64
ScaleValue sql.NullInt64
NullableValue sql.NullBool
ScanTypeValue reflect.Type
CommentValue sql.NullString
DefaultValueValue sql.NullString
}
ColumnType column type implements ColumnType interface
ColumnType.AutoIncrement
func (ct ColumnType) AutoIncrement() (isAutoIncrement bool, ok bool)
AutoIncrement returns the column is auto increment or not.
ColumnType.ColumnType
func (ct ColumnType) ColumnType() (columnType string, ok bool)
ColumnType returns the database type of the column. like `varchar(16)`
ColumnType.Comment
func (ct ColumnType) Comment() (value string, ok bool)
Comment returns the comment of current column.
ColumnType.DatabaseTypeName
func (ct ColumnType) DatabaseTypeName() string
DatabaseTypeName returns the database system name of the column type. If an empty string is returned, then the driver type name is not supported. Consult your driver documentation for a list of driver data types. Length specifiers are not included. Common type names include "VARCHAR", "TEXT", "NVARCHAR", "DECIMAL", "BOOL", "INT", and "BIGINT".
ColumnType.DecimalSize
func (ct ColumnType) DecimalSize() (precision int64, scale int64, ok bool)
DecimalSize returns the scale and precision of a decimal type.
ColumnType.DefaultValue
func (ct ColumnType) DefaultValue() (value string, ok bool)
DefaultValue returns the default value of current column.
ColumnType.Length
func (ct ColumnType) Length() (length int64, ok bool)
Length returns the column type length for variable length column types
ColumnType.Name
func (ct ColumnType) Name() string
Name returns the name or alias of the column.
ColumnType.Nullable
func (ct ColumnType) Nullable() (nullable bool, ok bool)
Nullable reports whether the column may be null.
ColumnType.PrimaryKey
func (ct ColumnType) PrimaryKey() (isPrimaryKey bool, ok bool)
PrimaryKey returns the column is primary key or not.
ColumnType.ScanType
func (ct ColumnType) ScanType() reflect.Type
ScanType returns a Go type suitable for scanning into using Rows.Scan.
ColumnType.Unique
func (ct ColumnType) Unique() (unique bool, ok bool)
Unique reports whether the column may be unique.
Config
type Config struct {
CreateIndexAfterCreateTable bool
DB *gorm.DB
gorm.Dialector
}
Config schema config
GormDataTypeInterface
type GormDataTypeInterface interface {
GormDBDataType(*gorm.DB, *schema.Field) string
}
GormDataTypeInterface gorm data type interface
Index
type Index struct {
TableName string
NameValue string
ColumnList []string
PrimaryKeyValue sql.NullBool
UniqueValue sql.NullBool
OptionValue string
}
Index implements gorm.Index interface
Index.Columns
func (idx Index) Columns() []string
Columns return the columns of the index
Index.Name
func (idx Index) Name() string
Name return the name of the index.
Index.Option
func (idx Index) Option() string
Option return the optional attribute of the index
Index.PrimaryKey
func (idx Index) PrimaryKey() (isPrimaryKey bool, ok bool)
PrimaryKey returns the index is primary key or not.
Index.Table
func (idx Index) Table() string
Table return the table name of the index.
Index.Unique
func (idx Index) Unique() (unique bool, ok bool)
Unique returns whether the index is unique or not.
Migrator
type Migrator struct {
Config
}
Migrator m struct
Migrator.AddColumn
func (m Migrator) AddColumn(value interface{}, name string) error
AddColumn create `name` column for value
Migrator.AlterColumn
func (m Migrator) AlterColumn(value interface{}, field string) error
AlterColumn alter value's `field` column's type based on schema definition
Migrator.AutoMigrate
func (m Migrator) AutoMigrate(values ...interface{}) error
AutoMigrate auto migrate values
Migrator.BuildIndexOptions
func (m Migrator) BuildIndexOptions(opts []schema.IndexOption, stmt *gorm.Statement) (results []interface{})
BuildIndexOptions build index options
Migrator.ColumnTypes
func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error)
ColumnTypes return columnTypes []gorm.ColumnType and execErr error
Migrator.CreateConstraint
func (m Migrator) CreateConstraint(value interface{}, name string) error
CreateConstraint create constraint
Migrator.CreateIndex
func (m Migrator) CreateIndex(value interface{}, name string) error
CreateIndex create index `name`
Migrator.CreateTable
func (m Migrator) CreateTable(values ...interface{}) error
CreateTable create table in database for values
Migrator.CreateView
func (m Migrator) CreateView(name string, option gorm.ViewOption) error
CreateView create view from Query in gorm.ViewOption. Query in gorm.ViewOption is a subquery
// CREATE VIEW `user_view` AS SELECT * FROM `users` WHERE age > 20
q := DB.Model(&User{}).Where("age > ?", 20)
DB.Debug().Migrator().CreateView("user_view", gorm.ViewOption{Query: q})
// CREATE OR REPLACE VIEW `users_view` AS SELECT * FROM `users` WITH CHECK OPTION
q := DB.Model(&User{})
DB.Debug().Migrator().CreateView("user_view", gorm.ViewOption{Query: q, Replace: true, CheckOption: "WITH CHECK OPTION"})
Migrator.CurrentDatabase
func (m Migrator) CurrentDatabase() (name string)
CurrentDatabase returns current database name
Migrator.CurrentTable
func (m Migrator) CurrentTable(stmt *gorm.Statement) interface{}
CurrentTable returns current statement's table expression
Migrator.DataTypeOf
func (m Migrator) DataTypeOf(field *schema.Field) string
DataTypeOf return field's db data type
Migrator.DropColumn
func (m Migrator) DropColumn(value interface{}, name string) error
DropColumn drop value's `name` column
Migrator.DropConstraint
func (m Migrator) DropConstraint(value interface{}, name string) error
DropConstraint drop constraint
Migrator.DropIndex
func (m Migrator) DropIndex(value interface{}, name string) error
DropIndex drop index `name`
Migrator.DropTable
func (m Migrator) DropTable(values ...interface{}) error
DropTable drop table for values
Migrator.DropView
func (m Migrator) DropView(name string) error
DropView drop view
Migrator.FullDataTypeOf
func (m Migrator) FullDataTypeOf(field *schema.Field) (expr clause.Expr)
FullDataTypeOf returns field's db full data type
Migrator.GetIndexes
func (m Migrator) GetIndexes(dst interface{}) ([]gorm.Index, error)
GetIndexes return Indexes []gorm.Index and execErr error
Migrator.GetQueryAndExecTx
func (m Migrator) GetQueryAndExecTx() (queryTx, execTx *gorm.DB)
Migrator.GetTables
func (m Migrator) GetTables() (tableList []string, err error)
GetTables returns tables
Migrator.GetTypeAliases
func (m Migrator) GetTypeAliases(databaseTypeName string) []string
GetTypeAliases return database type aliases
Migrator.GuessConstraintAndTable
func (m Migrator) GuessConstraintAndTable(stmt *gorm.Statement, name string) (*schema.Constraint, *schema.CheckConstraint, string)
GuessConstraintAndTable guess statement's constraint and it's table based on name
Deprecated: use GuessConstraintInterfaceAndTable instead.
Migrator.GuessConstraintInterfaceAndTable
func (m Migrator) GuessConstraintInterfaceAndTable(stmt *gorm.Statement, name string) (_ schema.ConstraintInterface, table string)
GuessConstraintInterfaceAndTable guess statement's constraint and it's table based on name nolint:cyclop
Migrator.HasColumn
func (m Migrator) HasColumn(value interface{}, field string) bool
HasColumn check has column `field` for value or not
Migrator.HasConstraint
func (m Migrator) HasConstraint(value interface{}, name string) bool
HasConstraint check has constraint or not
Migrator.HasIndex
func (m Migrator) HasIndex(value interface{}, name string) bool
HasIndex check has index `name` or not
Migrator.HasTable
func (m Migrator) HasTable(value interface{}) bool
HasTable returns table exists or not for value, value could be a struct or string
Migrator.MigrateColumn
func (m Migrator) MigrateColumn(value interface{}, field *schema.Field, columnType gorm.ColumnType) error
MigrateColumn migrate column
Migrator.MigrateColumnUnique
func (m Migrator) MigrateColumnUnique(value interface{}, field *schema.Field, columnType gorm.ColumnType) error
Migrator.RenameColumn
func (m Migrator) RenameColumn(value interface{}, oldName, newName string) error
RenameColumn rename value's field name from oldName to newName
Migrator.RenameIndex
func (m Migrator) RenameIndex(value interface{}, oldName, newName string) error
RenameIndex rename index from oldName to newName
Migrator.RenameTable
func (m Migrator) RenameTable(oldName, newName interface{}) error
RenameTable rename table from oldName to newName
Migrator.ReorderModels
func (m Migrator) ReorderModels(values []interface{}, autoAdd bool) (results []interface{})
ReorderModels reorder models according to constraint dependencies
Migrator.RunWithValue
func (m Migrator) RunWithValue(value interface{}, fc func(*gorm.Statement) error) error
RunWithValue run migration with statement value
Migrator.TableType
func (m Migrator) TableType(dst interface{}) (gorm.TableType, error)
TableType return tableType gorm.TableType and execErr error
TableType
type TableType struct {
SchemaValue string
NameValue string
TypeValue string
CommentValue sql.NullString
}
TableType table type implements TableType interface
TableType.Comment
func (ct TableType) Comment() (comment string, ok bool)
Comment returns the comment of current table.
TableType.Name
func (ct TableType) Name() string
Name returns the name of the table.
TableType.Schema
func (ct TableType) Schema() string
Schema returns the schema of the table.
TableType.Type
func (ct TableType) Type() string
Type returns the type of the table.