github.com/systematiccaos/gorm@v1.22.6/migrator.go (about)

     1  package gorm
     2  
     3  import (
     4  	"github.com/systematiccaos/gorm/clause"
     5  	"github.com/systematiccaos/gorm/schema"
     6  )
     7  
     8  // Migrator returns migrator
     9  func (db *DB) Migrator() Migrator {
    10  	tx := db.getInstance()
    11  
    12  	// apply scopes to migrator
    13  	for len(tx.Statement.scopes) > 0 {
    14  		scopes := tx.Statement.scopes
    15  		tx.Statement.scopes = nil
    16  		for _, scope := range scopes {
    17  			tx = scope(tx)
    18  		}
    19  	}
    20  
    21  	return tx.Dialector.Migrator(tx.Session(&Session{}))
    22  }
    23  
    24  // AutoMigrate run auto migration for given models
    25  func (db *DB) AutoMigrate(dst ...interface{}) error {
    26  	return db.Migrator().AutoMigrate(dst...)
    27  }
    28  
    29  // ViewOption view option
    30  type ViewOption struct {
    31  	Replace     bool
    32  	CheckOption string
    33  	Query       *DB
    34  }
    35  
    36  type ColumnType interface {
    37  	Name() string
    38  	DatabaseTypeName() string
    39  	Length() (length int64, ok bool)
    40  	DecimalSize() (precision int64, scale int64, ok bool)
    41  	Nullable() (nullable bool, ok bool)
    42  }
    43  
    44  type Migrator interface {
    45  	// AutoMigrate
    46  	AutoMigrate(dst ...interface{}) error
    47  
    48  	// Database
    49  	CurrentDatabase() string
    50  	FullDataTypeOf(*schema.Field) clause.Expr
    51  
    52  	// Tables
    53  	CreateTable(dst ...interface{}) error
    54  	DropTable(dst ...interface{}) error
    55  	HasTable(dst interface{}) bool
    56  	RenameTable(oldName, newName interface{}) error
    57  	GetTables() (tableList []string, err error)
    58  
    59  	// Columns
    60  	AddColumn(dst interface{}, field string) error
    61  	DropColumn(dst interface{}, field string) error
    62  	AlterColumn(dst interface{}, field string) error
    63  	MigrateColumn(dst interface{}, field *schema.Field, columnType ColumnType) error
    64  	HasColumn(dst interface{}, field string) bool
    65  	RenameColumn(dst interface{}, oldName, field string) error
    66  	ColumnTypes(dst interface{}) ([]ColumnType, error)
    67  
    68  	// Views
    69  	CreateView(name string, option ViewOption) error
    70  	DropView(name string) error
    71  
    72  	// Constraints
    73  	CreateConstraint(dst interface{}, name string) error
    74  	DropConstraint(dst interface{}, name string) error
    75  	HasConstraint(dst interface{}, name string) bool
    76  
    77  	// Indexes
    78  	CreateIndex(dst interface{}, name string) error
    79  	DropIndex(dst interface{}, name string) error
    80  	HasIndex(dst interface{}, name string) bool
    81  	RenameIndex(dst interface{}, oldName, newName string) error
    82  }