Obtain lock before auto-migrating table
This commit is contained in:
parent
34440dafd8
commit
4d6c09b83e
@ -108,4 +108,8 @@ type Migrator interface {
|
|||||||
HasIndex(dst interface{}, name string) bool
|
HasIndex(dst interface{}, name string) bool
|
||||||
RenameIndex(dst interface{}, oldName, newName string) error
|
RenameIndex(dst interface{}, oldName, newName string) error
|
||||||
GetIndexes(dst interface{}) ([]Index, error)
|
GetIndexes(dst interface{}) ([]Index, error)
|
||||||
|
|
||||||
|
// Locking
|
||||||
|
ObtainLock() error
|
||||||
|
ReleaseLock() error
|
||||||
}
|
}
|
||||||
|
@ -119,6 +119,12 @@ func (m Migrator) AutoMigrate(values ...interface{}) error {
|
|||||||
queryTx.DryRun = false
|
queryTx.DryRun = false
|
||||||
execTx = m.DB.Session(&gorm.Session{Logger: &printSQLLogger{Interface: m.DB.Logger}})
|
execTx = m.DB.Session(&gorm.Session{Logger: &printSQLLogger{Interface: m.DB.Logger}})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if err := execTx.Migrator().ObtainLock(); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
defer execTx.Migrator().ReleaseLock()
|
||||||
|
|
||||||
if !queryTx.Migrator().HasTable(value) {
|
if !queryTx.Migrator().HasTable(value) {
|
||||||
if err := execTx.Migrator().CreateTable(value); err != nil {
|
if err := execTx.Migrator().CreateTable(value); err != nil {
|
||||||
return err
|
return err
|
||||||
@ -985,3 +991,13 @@ func (m Migrator) GetTypeAliases(databaseTypeName string) []string {
|
|||||||
func (m Migrator) TableType(dst interface{}) (gorm.TableType, error) {
|
func (m Migrator) TableType(dst interface{}) (gorm.TableType, error) {
|
||||||
return nil, errors.New("not support")
|
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
|
||||||
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user