Skip to content

Commit 8a56aca

Browse files
authored
fix: migrator run with nil schema (#97)
1 parent 047cd00 commit 8a56aca

File tree

1 file changed

+37
-27
lines changed

1 file changed

+37
-27
lines changed

migrator.go

Lines changed: 37 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -21,8 +21,8 @@ func (m Migrator) GetTables() (tableList []string, err error) {
2121
}
2222

2323
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
2626
if schema == nil || !strings.Contains(schema.Table, ".") {
2727
return ""
2828
}
@@ -32,11 +32,11 @@ func getTableSchemaName(schema *schema.Schema) string {
3232

3333
func splitFullQualifiedName(name string) (string, string, string) {
3434
nameParts := strings.Split(name, ".")
35-
if len(nameParts) == 1 { //[table_name]
35+
if len(nameParts) == 1 { // [table_name]
3636
return "", "", nameParts[0]
37-
} else if len(nameParts) == 2 { //[table_schema].[table_name]
37+
} else if len(nameParts) == 2 { // [table_schema].[table_name]
3838
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]
4040
return nameParts[0], nameParts[1], nameParts[2]
4141
}
4242
return "", "", ""
@@ -130,8 +130,10 @@ func (m Migrator) HasColumn(value interface{}, field string) bool {
130130
m.RunWithValue(value, func(stmt *gorm.Statement) error {
131131
currentDatabase := m.DB.Migrator().CurrentDatabase()
132132
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+
}
135137
}
136138

137139
return m.DB.Raw(
@@ -145,31 +147,34 @@ func (m Migrator) HasColumn(value interface{}, field string) bool {
145147

146148
func (m Migrator) AlterColumn(value interface{}, field string) error {
147149
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+
}
155158

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+
}
160164
}
161165
return fmt.Errorf("failed to look up field with name: %s", field)
162166
})
163167
}
164168

165169
func (m Migrator) RenameColumn(value interface{}, oldName, newName string) error {
166170
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+
}
173178
}
174179

175180
return m.DB.Exec(
@@ -287,7 +292,10 @@ func (m Migrator) ColumnTypes(value interface{}) ([]gorm.ColumnType, error) {
287292

288293
func (m Migrator) CreateIndex(value interface{}, name string) error {
289294
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+
}
291299
if idx == nil {
292300
return fmt.Errorf("failed to create index with name %s", name)
293301
}
@@ -316,8 +324,10 @@ func (m Migrator) CreateIndex(value interface{}, name string) error {
316324
func (m Migrator) HasIndex(value interface{}, name string) bool {
317325
var count int
318326
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+
}
321331
}
322332

323333
return m.DB.Raw(

0 commit comments

Comments
 (0)