synchronize migrations with actual schema (#9965)
Этот коммит содержится в:
коммит произвёл
Christopher Speller
родитель
ef04692b18
Коммит
204dde9f48
@@ -59,6 +59,7 @@ type SqlStore interface {
|
||||
RenameColumnIfExists(tableName string, oldColumnName string, newColumnName string, colType string) bool
|
||||
GetMaxLengthOfColumnIfExists(tableName string, columnName string) string
|
||||
AlterColumnTypeIfExists(tableName string, columnName string, mySqlColType string, postgresColType string) bool
|
||||
AlterColumnDefaultIfExists(tableName string, columnName string, mySqlColDefault *string, postgresColDefault *string) bool
|
||||
CreateUniqueIndexIfNotExists(indexName string, tableName string, columnName string) bool
|
||||
CreateIndexIfNotExists(indexName string, tableName string, columnName string) bool
|
||||
CreateCompositeIndexIfNotExists(indexName string, tableName string, columnNames []string) bool
|
||||
|
||||
@@ -677,6 +677,52 @@ func (ss *SqlSupplier) AlterColumnTypeIfExists(tableName string, columnName stri
|
||||
return true
|
||||
}
|
||||
|
||||
func (ss *SqlSupplier) AlterColumnDefaultIfExists(tableName string, columnName string, mySqlColDefault *string, postgresColDefault *string) bool {
|
||||
if !ss.DoesColumnExist(tableName, columnName) {
|
||||
return false
|
||||
}
|
||||
|
||||
var defaultValue = ""
|
||||
if ss.DriverName() == model.DATABASE_DRIVER_MYSQL {
|
||||
// Some column types in MySQL cannot have defaults, so don't try to configure anything.
|
||||
if mySqlColDefault == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
defaultValue = *mySqlColDefault
|
||||
} else if ss.DriverName() == model.DATABASE_DRIVER_POSTGRES {
|
||||
// Postgres doesn't have the same limitation, but preserve the interface.
|
||||
if postgresColDefault == nil {
|
||||
return true
|
||||
}
|
||||
|
||||
tableName = strings.ToLower(tableName)
|
||||
columnName = strings.ToLower(columnName)
|
||||
defaultValue = *postgresColDefault
|
||||
} else {
|
||||
mlog.Critical("Failed to alter column default because of missing driver")
|
||||
time.Sleep(time.Second)
|
||||
os.Exit(EXIT_GENERIC_FAILURE)
|
||||
return false
|
||||
}
|
||||
|
||||
var err error
|
||||
if defaultValue == "" {
|
||||
_, err = ss.GetMaster().ExecNoTimeout("ALTER TABLE " + tableName + " ALTER COLUMN " + columnName + " DROP DEFAULT")
|
||||
} else {
|
||||
_, err = ss.GetMaster().ExecNoTimeout("ALTER TABLE " + tableName + " ALTER COLUMN " + columnName + " SET DEFAULT " + defaultValue)
|
||||
}
|
||||
|
||||
if err != nil {
|
||||
mlog.Critical(fmt.Sprintf("Failed to alter column %s.%s default %s: %v", tableName, columnName, defaultValue, err))
|
||||
time.Sleep(time.Second)
|
||||
os.Exit(EXIT_GENERIC_FAILURE)
|
||||
return false
|
||||
}
|
||||
|
||||
return true
|
||||
}
|
||||
|
||||
func (ss *SqlSupplier) CreateUniqueIndexIfNotExists(indexName string, tableName string, columnName string) bool {
|
||||
return ss.createIndexIfNotExists(indexName, tableName, []string{columnName}, INDEX_TYPE_DEFAULT, true)
|
||||
}
|
||||
|
||||
@@ -92,6 +92,7 @@ func UpgradeDatabase(sqlStore SqlStore) {
|
||||
UpgradeDatabaseToVersion55(sqlStore)
|
||||
UpgradeDatabaseToVersion56(sqlStore)
|
||||
UpgradeDatabaseToVersion57(sqlStore)
|
||||
UpgradeDatabaseToVersion58(sqlStore)
|
||||
|
||||
// If the SchemaVersion is empty this this is the first time it has ran
|
||||
// so lets set it to the current version.
|
||||
@@ -536,9 +537,31 @@ func UpgradeDatabaseToVersion56(sqlStore SqlStore) {
|
||||
}
|
||||
|
||||
func UpgradeDatabaseToVersion57(sqlStore SqlStore) {
|
||||
// TODO: Uncomment following condition when version 5.5.0 is released
|
||||
// TODO: Uncomment following condition when version 5.7.0 is released
|
||||
// if shouldPerformUpgrade(sqlStore, VERSION_5_6_0, VERSION_5_7_0) {
|
||||
|
||||
// saveSchemaVersion(sqlStore, VERSION_5_7_0)
|
||||
// }
|
||||
}
|
||||
|
||||
func UpgradeDatabaseToVersion58(sqlStore SqlStore) {
|
||||
// TODO: Uncomment following condition when version 5.8.0 is released
|
||||
// if shouldPerformUpgrade(sqlStore, VERSION_5_7_0, VERSION_5_8_0) {
|
||||
|
||||
// idx_channels_txt was removed in `UpgradeDatabaseToVersion50`, but merged as part of
|
||||
// v5.1, so the migration wouldn't apply to anyone upgrading from v5.0. Remove it again to
|
||||
// bring the upgraded (from v5.0) and fresh install schemas back in sync.
|
||||
sqlStore.RemoveIndexIfExists("idx_channels_txt", "Channels")
|
||||
|
||||
// Fix column types and defaults where gorp converged on a different schema value than the
|
||||
// original migration.
|
||||
sqlStore.AlterColumnTypeIfExists("OutgoingWebhooks", "Description", "text", "VARCHAR(500)")
|
||||
sqlStore.AlterColumnTypeIfExists("IncomingWebhooks", "Description", "text", "VARCHAR(500)")
|
||||
sqlStore.AlterColumnTypeIfExists("OutgoingWebhooks", "IconURL", "text", "VARCHAR(1024)")
|
||||
sqlStore.AlterColumnDefaultIfExists("OutgoingWebhooks", "Username", model.NewString("NULL"), model.NewString(""))
|
||||
sqlStore.AlterColumnDefaultIfExists("OutgoingWebhooks", "IconURL", nil, model.NewString(""))
|
||||
sqlStore.AlterColumnDefaultIfExists("PluginKeyValueStore", "ExpireAt", model.NewString("NULL"), model.NewString("NULL"))
|
||||
|
||||
// saveSchemaVersion(sqlStore, VERSION_5_8_0)
|
||||
// }
|
||||
}
|
||||
|
||||
Ссылка в новой задаче
Block a user