Debian Codegraph @

migrator.go

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