migrator.go 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970
  1. package gorm
  2. import (
  3. "gorm.io/gorm/clause"
  4. "gorm.io/gorm/schema"
  5. )
  6. // Migrator returns migrator
  7. func (db *DB) Migrator() Migrator {
  8. return db.Dialector.Migrator(db.Session(&Session{}))
  9. }
  10. // AutoMigrate run auto migration for given models
  11. func (db *DB) AutoMigrate(dst ...interface{}) error {
  12. return db.Migrator().AutoMigrate(dst...)
  13. }
  14. // ViewOption view option
  15. type ViewOption struct {
  16. Replace bool
  17. CheckOption string
  18. Query *DB
  19. }
  20. type ColumnType interface {
  21. Name() string
  22. DatabaseTypeName() string
  23. Length() (length int64, ok bool)
  24. DecimalSize() (precision int64, scale int64, ok bool)
  25. Nullable() (nullable bool, ok bool)
  26. }
  27. type Migrator interface {
  28. // AutoMigrate
  29. AutoMigrate(dst ...interface{}) error
  30. // Database
  31. CurrentDatabase() string
  32. FullDataTypeOf(*schema.Field) clause.Expr
  33. // Tables
  34. CreateTable(dst ...interface{}) error
  35. DropTable(dst ...interface{}) error
  36. HasTable(dst interface{}) bool
  37. RenameTable(oldName, newName interface{}) error
  38. // Columns
  39. AddColumn(dst interface{}, field string) error
  40. DropColumn(dst interface{}, field string) error
  41. AlterColumn(dst interface{}, field string) error
  42. MigrateColumn(dst interface{}, field *schema.Field, columnType ColumnType) error
  43. HasColumn(dst interface{}, field string) bool
  44. RenameColumn(dst interface{}, oldName, field string) error
  45. ColumnTypes(dst interface{}) ([]ColumnType, error)
  46. // Views
  47. CreateView(name string, option ViewOption) error
  48. DropView(name string) error
  49. // Constraints
  50. CreateConstraint(dst interface{}, name string) error
  51. DropConstraint(dst interface{}, name string) error
  52. HasConstraint(dst interface{}, name string) bool
  53. // Indexes
  54. CreateIndex(dst interface{}, name string) error
  55. DropIndex(dst interface{}, name string) error
  56. HasIndex(dst interface{}, name string) bool
  57. RenameIndex(dst interface{}, oldName, newName string) error
  58. }