migrator.go 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111
  1. package gorm
  2. import (
  3. "reflect"
  4. "gorm.io/gorm/clause"
  5. "gorm.io/gorm/schema"
  6. )
  7. // Migrator returns migrator
  8. func (db *DB) Migrator() Migrator {
  9. tx := db.getInstance()
  10. // apply scopes to migrator
  11. for len(tx.Statement.scopes) > 0 {
  12. tx = tx.executeScopes()
  13. }
  14. return tx.Dialector.Migrator(tx.Session(&Session{}))
  15. }
  16. // AutoMigrate run auto migration for given models
  17. func (db *DB) AutoMigrate(dst ...interface{}) error {
  18. return db.Migrator().AutoMigrate(dst...)
  19. }
  20. // ViewOption view option
  21. type ViewOption struct {
  22. Replace bool // If true, exec `CREATE`. If false, exec `CREATE OR REPLACE`
  23. CheckOption string // optional. e.g. `WITH [ CASCADED | LOCAL ] CHECK OPTION`
  24. Query *DB // required subquery.
  25. }
  26. // ColumnType column type interface
  27. type ColumnType interface {
  28. Name() string
  29. DatabaseTypeName() string // varchar
  30. ColumnType() (columnType string, ok bool) // varchar(64)
  31. PrimaryKey() (isPrimaryKey bool, ok bool)
  32. AutoIncrement() (isAutoIncrement bool, ok bool)
  33. Length() (length int64, ok bool)
  34. DecimalSize() (precision int64, scale int64, ok bool)
  35. Nullable() (nullable bool, ok bool)
  36. Unique() (unique bool, ok bool)
  37. ScanType() reflect.Type
  38. Comment() (value string, ok bool)
  39. DefaultValue() (value string, ok bool)
  40. }
  41. type Index interface {
  42. Table() string
  43. Name() string
  44. Columns() []string
  45. PrimaryKey() (isPrimaryKey bool, ok bool)
  46. Unique() (unique bool, ok bool)
  47. Option() string
  48. }
  49. // TableType table type interface
  50. type TableType interface {
  51. Schema() string
  52. Name() string
  53. Type() string
  54. Comment() (comment string, ok bool)
  55. }
  56. // Migrator migrator interface
  57. type Migrator interface {
  58. // AutoMigrate
  59. AutoMigrate(dst ...interface{}) error
  60. // Database
  61. CurrentDatabase() string
  62. FullDataTypeOf(*schema.Field) clause.Expr
  63. GetTypeAliases(databaseTypeName string) []string
  64. // Tables
  65. CreateTable(dst ...interface{}) error
  66. DropTable(dst ...interface{}) error
  67. HasTable(dst interface{}) bool
  68. RenameTable(oldName, newName interface{}) error
  69. GetTables() (tableList []string, err error)
  70. TableType(dst interface{}) (TableType, error)
  71. // Columns
  72. AddColumn(dst interface{}, field string) error
  73. DropColumn(dst interface{}, field string) error
  74. AlterColumn(dst interface{}, field string) error
  75. MigrateColumn(dst interface{}, field *schema.Field, columnType ColumnType) error
  76. // MigrateColumnUnique migrate column's UNIQUE constraint, it's part of MigrateColumn.
  77. MigrateColumnUnique(dst interface{}, field *schema.Field, columnType ColumnType) error
  78. HasColumn(dst interface{}, field string) bool
  79. RenameColumn(dst interface{}, oldName, field string) error
  80. ColumnTypes(dst interface{}) ([]ColumnType, error)
  81. // Views
  82. CreateView(name string, option ViewOption) error
  83. DropView(name string) error
  84. // Constraints
  85. CreateConstraint(dst interface{}, name string) error
  86. DropConstraint(dst interface{}, name string) error
  87. HasConstraint(dst interface{}, name string) bool
  88. // Indexes
  89. CreateIndex(dst interface{}, name string) error
  90. DropIndex(dst interface{}, name string) error
  91. HasIndex(dst interface{}, name string) bool
  92. RenameIndex(dst interface{}, oldName, newName string) error
  93. GetIndexes(dst interface{}) ([]Index, error)
  94. }