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: Running AutoMigrate concurrently on the same model fails with various race conditions #6680

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
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
4 changes: 4 additions & 0 deletions migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -108,4 +108,8 @@ type Migrator interface {
HasIndex(dst interface{}, name string) bool
RenameIndex(dst interface{}, oldName, newName string) error
GetIndexes(dst interface{}) ([]Index, error)

// Locking
ObtainLock() error
ReleaseLock() error
}
152 changes: 90 additions & 62 deletions migrator/migrator.go
Original file line number Diff line number Diff line change
Expand Up @@ -110,87 +110,105 @@
return
}

// AutoMigrate auto migrate values
func (m Migrator) AutoMigrate(values ...interface{}) error {
for _, value := range m.ReorderModels(values, true) {
queryTx := m.DB.Session(&gorm.Session{})
execTx := queryTx
if m.DB.DryRun {
queryTx.DryRun = false
execTx = m.DB.Session(&gorm.Session{Logger: &printSQLLogger{Interface: m.DB.Logger}})
func (m Migrator) migrateTable(queryTx, execTx *gorm.DB, value interface{}) (err error) {

Check failure on line 113 in migrator/migrator.go

View workflow job for this annotation

GitHub Actions / runner / golangci-lint

[golangci] reported by reviewdog 🐶 calculated cyclomatic complexity for function migrateTable is 27, max is 10 (cyclop) Raw Output: migrator/migrator.go:113:1: calculated cyclomatic complexity for function migrateTable is 27, max is 10 (cyclop) func (m Migrator) migrateTable(queryTx, execTx *gorm.DB, value interface{}) (err error) { ^
if err = execTx.Migrator().ObtainLock(); err != nil {
return
}
defer func() {
releaseErr := execTx.Migrator().ReleaseLock()
if err == nil {
err = releaseErr
}
}()

if !queryTx.Migrator().HasTable(value) {
if err = execTx.Migrator().CreateTable(value); err != nil {
return err
}
if !queryTx.Migrator().HasTable(value) {
if err := execTx.Migrator().CreateTable(value); err != nil {
} else {
if err = m.RunWithValue(value, func(stmt *gorm.Statement) error {
columnTypes, err := queryTx.Migrator().ColumnTypes(value)
if err != nil {
return err
}
} else {
if err := m.RunWithValue(value, func(stmt *gorm.Statement) error {
columnTypes, err := queryTx.Migrator().ColumnTypes(value)
if err != nil {
return err
}
var (
parseIndexes = stmt.Schema.ParseIndexes()
parseCheckConstraints = stmt.Schema.ParseCheckConstraints()
)
for _, dbName := range stmt.Schema.DBNames {
var foundColumn gorm.ColumnType

for _, columnType := range columnTypes {
if columnType.Name() == dbName {
foundColumn = columnType
break
}
}
var (
parseIndexes = stmt.Schema.ParseIndexes()
parseCheckConstraints = stmt.Schema.ParseCheckConstraints()
)
for _, dbName := range stmt.Schema.DBNames {
var foundColumn gorm.ColumnType

if foundColumn == nil {
// not found, add column
if err = execTx.Migrator().AddColumn(value, dbName); err != nil {
return err
}
} else {
// found, smartly migrate
field := stmt.Schema.FieldsByDBName[dbName]
if err = execTx.Migrator().MigrateColumn(value, field, foundColumn); err != nil {
return err
}
for _, columnType := range columnTypes {
if columnType.Name() == dbName {
foundColumn = columnType
break
}
}

if !m.DB.DisableForeignKeyConstraintWhenMigrating && !m.DB.IgnoreRelationshipsWhenMigrating {
for _, rel := range stmt.Schema.Relationships.Relations {
if rel.Field.IgnoreMigration {
continue
}
if constraint := rel.ParseConstraint(); constraint != nil &&
constraint.Schema == stmt.Schema && !queryTx.Migrator().HasConstraint(value, constraint.Name) {
if err := execTx.Migrator().CreateConstraint(value, constraint.Name); err != nil {
return err
}
}
if foundColumn == nil {
// not found, add column
if err = execTx.Migrator().AddColumn(value, dbName); err != nil {
return err
}
} else {
// found, smartly migrate
field := stmt.Schema.FieldsByDBName[dbName]
if err = execTx.Migrator().MigrateColumn(value, field, foundColumn); err != nil {
return err
}
}
}

for _, chk := range parseCheckConstraints {
if !queryTx.Migrator().HasConstraint(value, chk.Name) {
if err := execTx.Migrator().CreateConstraint(value, chk.Name); err != nil {
if !m.DB.DisableForeignKeyConstraintWhenMigrating && !m.DB.IgnoreRelationshipsWhenMigrating {
for _, rel := range stmt.Schema.Relationships.Relations {
if rel.Field.IgnoreMigration {
continue
}
if constraint := rel.ParseConstraint(); constraint != nil &&
constraint.Schema == stmt.Schema && !queryTx.Migrator().HasConstraint(value, constraint.Name) {
if err := execTx.Migrator().CreateConstraint(value, constraint.Name); err != nil {
return err
}
}
}
}

for _, idx := range parseIndexes {
if !queryTx.Migrator().HasIndex(value, idx.Name) {
if err := execTx.Migrator().CreateIndex(value, idx.Name); err != nil {
return err
}
for _, chk := range parseCheckConstraints {
if !queryTx.Migrator().HasConstraint(value, chk.Name) {
if err := execTx.Migrator().CreateConstraint(value, chk.Name); err != nil {
return err
}
}
}

return nil
}); err != nil {
return err
for _, idx := range parseIndexes {
if !queryTx.Migrator().HasIndex(value, idx.Name) {
if err := execTx.Migrator().CreateIndex(value, idx.Name); err != nil {
return err
}
}
}

return nil
}); err != nil {
return err
}
}
return nil
}

// AutoMigrate auto migrate values
func (m Migrator) AutoMigrate(values ...interface{}) error {
for _, value := range m.ReorderModels(values, true) {
queryTx := m.DB.Session(&gorm.Session{})
execTx := queryTx
if m.DB.DryRun {
queryTx.DryRun = false
execTx = m.DB.Session(&gorm.Session{Logger: &printSQLLogger{Interface: m.DB.Logger}})
}

if err := m.migrateTable(queryTx, execTx, value); err != nil {
return err
}
}

Expand Down Expand Up @@ -985,3 +1003,13 @@
func (m Migrator) TableType(dst interface{}) (gorm.TableType, error) {
return nil, errors.New("not support")
}

// ObtainLock obtains a global migration lock
func (m Migrator) ObtainLock() error {
return nil
}

// ReleaseLock releases the global migration lock
func (m Migrator) ReleaseLock() error {
return nil
}
26 changes: 26 additions & 0 deletions tests/migrate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1685,3 +1685,29 @@ func TestTableType(t *testing.T) {
t.Fatalf("expected comment %s got %s", tblComment, comment)
}
}

func TestMigrateRaceCondition(t *testing.T) {
type TestTable struct {
gorm.Model
}

for a := 0; a < 2; a++ {
t.Run("drop and migrate", func(t *testing.T) {
t.Run("drop", func(t *testing.T) {
if err := DB.Migrator().DropTable(&TestTable{}); err != nil {
t.Fatalf("failed to drop table: %v", err)
}
})

for i := 0; i < 2; i++ {
t.Run("migrate", func(t *testing.T) {
t.Parallel()

if err := DB.AutoMigrate(&TestTable{}); err != nil {
t.Fatalf("failed to migrate: %v", err)
}
})
}
})
}
}
Loading