Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: migrator run with nil schema (#5795) #6303

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions migrator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,8 @@ func (m Migrator) RunWithValue(value interface{}, fc func(*gorm.Statement) error

if table, ok := value.(string); ok {
stmt.Table = table
// set schema to avoid panic
stmt.Schema = &schema.Schema{}
} else if err := stmt.ParseWithSpecialTableName(value, stmt.Table); err != nil {
return err
}
Expand Down
22 changes: 19 additions & 3 deletions tests/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -616,7 +616,7 @@ func TestMigrateColumns(t *testing.T) {
}

if err := DB.Table("column_structs").Migrator().DropColumn(&NewColumnStruct{}, "NewName"); err != nil {
t.Fatalf("Failed to add column, got %v", err)
t.Fatalf("Failed to drop column, got %v", err)
}

if DB.Table("column_structs").Migrator().HasColumn(&NewColumnStruct{}, "NewName") {
Expand All @@ -628,20 +628,36 @@ func TestMigrateColumns(t *testing.T) {
}

if err := DB.Table("column_structs").Migrator().RenameColumn(&NewColumnStruct{}, "NewName", "new_new_name"); err != nil {
t.Fatalf("Failed to add column, got %v", err)
t.Fatalf("Failed to rename column, got %v", err)
}

if !DB.Table("column_structs").Migrator().HasColumn(&NewColumnStruct{}, "new_new_name") {
t.Fatalf("Failed to found renamed column")
}

if err := DB.Table("column_structs").Migrator().DropColumn(&NewColumnStruct{}, "new_new_name"); err != nil {
t.Fatalf("Failed to add column, got %v", err)
t.Fatalf("Failed to drop column, got %v", err)
}

if DB.Table("column_structs").Migrator().HasColumn(&NewColumnStruct{}, "new_new_name") {
t.Fatalf("Found deleted column")
}

if err := DB.Table("column_structs").Migrator().AddColumn(&NewColumnStruct{}, "NewName"); err != nil {
t.Fatalf("Failed to add column, got %v", err)
}

if err := DB.Migrator().RenameColumn("column_structs", "new_name", "new_new_name"); err != nil {
t.Fatalf("Failed to rename column, got %v", err)
}

if !DB.Migrator().HasColumn("column_structs", "new_new_name") {
t.Fatalf("Failed to find added column")
}

if err := DB.Migrator().DropColumn("column_structs", "new_new_name"); err != nil {
t.Fatalf("Failed to drop column, got %v", err)
}
}

func TestMigrateConstraint(t *testing.T) {
Expand Down