mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
SQLStore: Add deprecation comments for breaking migrations (#49740)
* Migrator: Extend support to rename columns * SQLStore: Add deprecation comments for breaking migrations
This commit is contained in:
parent
36c3398c6d
commit
5f1305d280
@ -20,6 +20,8 @@ func addTableIndicesMigrations(mg *Migrator, versionSuffix string, table Table)
|
||||
}
|
||||
}
|
||||
|
||||
// addTableRenameMigration may cause breaking changes.
|
||||
// DEPRECATED: It should no longer be used. Kept only for legacy reasons.
|
||||
func addTableRenameMigration(mg *Migrator, oldName string, newName string, versionSuffix string) {
|
||||
migrationId := fmt.Sprintf("Rename table %s to %s - %s", oldName, newName, versionSuffix)
|
||||
mg.AddMigration(migrationId, NewRenameTableMigration(oldName, newName))
|
||||
|
@ -44,7 +44,7 @@ func addSecretsMigration(mg *migrator.Migrator) {
|
||||
mg.AddMigration("create secrets table", migrator.NewAddTableMigration(secretsV1))
|
||||
|
||||
mg.AddMigration("rename data_keys name column to id", migrator.NewRenameColumnMigration(
|
||||
dataKeysV1, "name", "id",
|
||||
dataKeysV1, dataKeysV1.Columns[0], "id",
|
||||
))
|
||||
|
||||
mg.AddMigration("add name column into data_keys", migrator.NewAddColumnMigration(
|
||||
|
@ -36,8 +36,12 @@ type Dialect interface {
|
||||
DropTable(tableName string) string
|
||||
DropIndexSQL(tableName string, index *Index) string
|
||||
|
||||
// RenameTable is deprecated, its use cause breaking changes
|
||||
// so, it should no longer be used. Kept for legacy reasons.
|
||||
RenameTable(oldName string, newName string) string
|
||||
RenameColumn(table Table, oldName, newName string) string
|
||||
// RenameColumn is deprecated, its use cause breaking changes
|
||||
// so, it should no longer be used. Kept for legacy reasons.
|
||||
RenameColumn(table Table, column *Column, newName string) string
|
||||
|
||||
UpdateTableSQL(tableName string, columns []*Column) string
|
||||
|
||||
@ -211,9 +215,12 @@ func (b *BaseDialect) RenameTable(oldName string, newName string) string {
|
||||
return fmt.Sprintf("ALTER TABLE %s RENAME TO %s", quote(oldName), quote(newName))
|
||||
}
|
||||
|
||||
func (b *BaseDialect) RenameColumn(table Table, oldName, newName string) string {
|
||||
func (b *BaseDialect) RenameColumn(table Table, column *Column, newName string) string {
|
||||
quote := b.dialect.Quote
|
||||
return fmt.Sprintf("ALTER TABLE %s RENAME COLUMN %s TO %s", quote(table.Name), quote(oldName), quote(newName))
|
||||
return fmt.Sprintf(
|
||||
"ALTER TABLE %s RENAME COLUMN %s TO %s",
|
||||
quote(table.Name), quote(column.Name), quote(newName),
|
||||
)
|
||||
}
|
||||
|
||||
func (b *BaseDialect) ColumnCheckSQL(tableName, columnName string) (string, []interface{}) {
|
||||
|
@ -31,6 +31,9 @@ type RawSQLMigration struct {
|
||||
sql map[string]string
|
||||
}
|
||||
|
||||
// NewRawSQLMigration should be used carefully, the usage
|
||||
// of SQL statements that cause breaking changes like renaming
|
||||
// a table or a column, or changing a column type should not be used.
|
||||
func NewRawSQLMigration(sql string) *RawSQLMigration {
|
||||
m := &RawSQLMigration{}
|
||||
if sql != "" {
|
||||
@ -111,12 +114,14 @@ func (m *AddColumnMigration) SQL(dialect Dialect) string {
|
||||
type RenameColumnMigration struct {
|
||||
MigrationBase
|
||||
table Table
|
||||
oldName string
|
||||
column *Column
|
||||
newName string
|
||||
}
|
||||
|
||||
func NewRenameColumnMigration(table Table, oldName, newName string) *RenameColumnMigration {
|
||||
return &RenameColumnMigration{table: table, oldName: oldName, newName: newName}
|
||||
// NewRenameColumnMigration may cause breaking changes.
|
||||
// DEPRECATED: It should no longer be used. Kept only for legacy reasons.
|
||||
func NewRenameColumnMigration(table Table, column *Column, newName string) *RenameColumnMigration {
|
||||
return &RenameColumnMigration{table: table, column: column, newName: newName}
|
||||
}
|
||||
|
||||
func (m *RenameColumnMigration) Table(table Table) *RenameColumnMigration {
|
||||
@ -124,14 +129,18 @@ func (m *RenameColumnMigration) Table(table Table) *RenameColumnMigration {
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *RenameColumnMigration) Rename(oldName string, newName string) *RenameColumnMigration {
|
||||
m.oldName = oldName
|
||||
func (m *RenameColumnMigration) Column(column *Column) *RenameColumnMigration {
|
||||
m.column = column
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *RenameColumnMigration) Rename(newName string) *RenameColumnMigration {
|
||||
m.newName = newName
|
||||
return m
|
||||
}
|
||||
|
||||
func (m *RenameColumnMigration) SQL(d Dialect) string {
|
||||
return d.RenameColumn(m.table, m.oldName, m.newName)
|
||||
return d.RenameColumn(m.table, m.column, m.newName)
|
||||
}
|
||||
|
||||
type AddIndexMigration struct {
|
||||
@ -211,6 +220,8 @@ type RenameTableMigration struct {
|
||||
newName string
|
||||
}
|
||||
|
||||
// NewRenameTableMigration may cause breaking changes.
|
||||
// DEPRECATED: It should no longer be used. Kept only for legacy reasons.
|
||||
func NewRenameTableMigration(oldName string, newName string) *RenameTableMigration {
|
||||
return &RenameTableMigration{oldName: oldName, newName: newName}
|
||||
}
|
||||
|
@ -118,18 +118,12 @@ func (db *MySQLDialect) ColumnCheckSQL(tableName, columnName string) (string, []
|
||||
return sql, args
|
||||
}
|
||||
|
||||
func (db *MySQLDialect) RenameColumn(table Table, oldName, newName string) string {
|
||||
var colType string
|
||||
for _, col := range table.Columns {
|
||||
if col.Name == oldName {
|
||||
colType = db.SQLType(col)
|
||||
break
|
||||
}
|
||||
}
|
||||
|
||||
func (db *MySQLDialect) RenameColumn(table Table, column *Column, newName string) string {
|
||||
quote := db.dialect.Quote
|
||||
|
||||
return fmt.Sprintf("ALTER TABLE %s CHANGE %s %s %s", quote(table.Name), quote(oldName), quote(newName), colType)
|
||||
return fmt.Sprintf(
|
||||
"ALTER TABLE %s CHANGE %s %s %s",
|
||||
quote(table.Name), quote(column.Name), quote(newName), db.SQLType(column),
|
||||
)
|
||||
}
|
||||
|
||||
func (db *MySQLDialect) CleanDB() error {
|
||||
|
Loading…
Reference in New Issue
Block a user