@@ -21,8 +21,8 @@ func (m Migrator) GetTables() (tableList []string, err error) {
21
21
}
22
22
23
23
func getTableSchemaName (schema * schema.Schema ) string {
24
- //return the schema name if it is explicitly provided in the table name
25
- //otherwise return a sql wildcard -> use any table_schema
24
+ // return the schema name if it is explicitly provided in the table name
25
+ // otherwise return a sql wildcard -> use any table_schema
26
26
if schema == nil || ! strings .Contains (schema .Table , "." ) {
27
27
return ""
28
28
}
@@ -32,11 +32,11 @@ func getTableSchemaName(schema *schema.Schema) string {
32
32
33
33
func splitFullQualifiedName (name string ) (string , string , string ) {
34
34
nameParts := strings .Split (name , "." )
35
- if len (nameParts ) == 1 { //[table_name]
35
+ if len (nameParts ) == 1 { // [table_name]
36
36
return "" , "" , nameParts [0 ]
37
- } else if len (nameParts ) == 2 { //[table_schema].[table_name]
37
+ } else if len (nameParts ) == 2 { // [table_schema].[table_name]
38
38
return "" , nameParts [0 ], nameParts [1 ]
39
- } else if len (nameParts ) == 3 { //[table_catalog].[table_schema].[table_name]
39
+ } else if len (nameParts ) == 3 { // [table_catalog].[table_schema].[table_name]
40
40
return nameParts [0 ], nameParts [1 ], nameParts [2 ]
41
41
}
42
42
return "" , "" , ""
@@ -130,8 +130,10 @@ func (m Migrator) HasColumn(value interface{}, field string) bool {
130
130
m .RunWithValue (value , func (stmt * gorm.Statement ) error {
131
131
currentDatabase := m .DB .Migrator ().CurrentDatabase ()
132
132
name := field
133
- if field := stmt .Schema .LookUpField (field ); field != nil {
134
- name = field .DBName
133
+ if stmt .Schema != nil {
134
+ if field := stmt .Schema .LookUpField (field ); field != nil {
135
+ name = field .DBName
136
+ }
135
137
}
136
138
137
139
return m .DB .Raw (
@@ -145,31 +147,34 @@ func (m Migrator) HasColumn(value interface{}, field string) bool {
145
147
146
148
func (m Migrator ) AlterColumn (value interface {}, field string ) error {
147
149
return m .RunWithValue (value , func (stmt * gorm.Statement ) error {
148
- if field := stmt .Schema .LookUpField (field ); field != nil {
149
- fieldType := clause.Expr {SQL : m .DataTypeOf (field )}
150
- if field .NotNull {
151
- fieldType .SQL += " NOT NULL"
152
- } else {
153
- fieldType .SQL += " NULL"
154
- }
150
+ if stmt .Schema != nil {
151
+ if field := stmt .Schema .LookUpField (field ); field != nil {
152
+ fieldType := clause.Expr {SQL : m .DataTypeOf (field )}
153
+ if field .NotNull {
154
+ fieldType .SQL += " NOT NULL"
155
+ } else {
156
+ fieldType .SQL += " NULL"
157
+ }
155
158
156
- return m .DB .Exec (
157
- "ALTER TABLE ? ALTER COLUMN ? ?" ,
158
- clause.Table {Name : stmt .Table }, clause.Column {Name : field .DBName }, fieldType ,
159
- ).Error
159
+ return m .DB .Exec (
160
+ "ALTER TABLE ? ALTER COLUMN ? ?" ,
161
+ clause.Table {Name : stmt .Table }, clause.Column {Name : field .DBName }, fieldType ,
162
+ ).Error
163
+ }
160
164
}
161
165
return fmt .Errorf ("failed to look up field with name: %s" , field )
162
166
})
163
167
}
164
168
165
169
func (m Migrator ) RenameColumn (value interface {}, oldName , newName string ) error {
166
170
return m .RunWithValue (value , func (stmt * gorm.Statement ) error {
167
- if field := stmt .Schema .LookUpField (oldName ); field != nil {
168
- oldName = field .DBName
169
- }
170
-
171
- if field := stmt .Schema .LookUpField (newName ); field != nil {
172
- newName = field .DBName
171
+ if stmt .Schema != nil {
172
+ if field := stmt .Schema .LookUpField (oldName ); field != nil {
173
+ oldName = field .DBName
174
+ }
175
+ if field := stmt .Schema .LookUpField (newName ); field != nil {
176
+ newName = field .DBName
177
+ }
173
178
}
174
179
175
180
return m .DB .Exec (
@@ -287,7 +292,10 @@ func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) {
287
292
288
293
func (m Migrator ) CreateIndex (value interface {}, name string ) error {
289
294
return m .RunWithValue (value , func (stmt * gorm.Statement ) error {
290
- idx := stmt .Schema .LookIndex (name )
295
+ var idx * schema.Index
296
+ if stmt .Schema != nil {
297
+ idx = stmt .Schema .LookIndex (name )
298
+ }
291
299
if idx == nil {
292
300
return fmt .Errorf ("failed to create index with name %s" , name )
293
301
}
@@ -316,8 +324,10 @@ func (m Migrator) CreateIndex(value interface{}, name string) error {
316
324
func (m Migrator ) HasIndex (value interface {}, name string ) bool {
317
325
var count int
318
326
m .RunWithValue (value , func (stmt * gorm.Statement ) error {
319
- if idx := stmt .Schema .LookIndex (name ); idx != nil {
320
- name = idx .Name
327
+ if stmt .Schema != nil {
328
+ if idx := stmt .Schema .LookIndex (name ); idx != nil {
329
+ name = idx .Name
330
+ }
321
331
}
322
332
323
333
return m .DB .Raw (
0 commit comments