From fc4018048e7be1a4473ddb50b8c610017e1b4f36 Mon Sep 17 00:00:00 2001 From: "Grot (@grafanabot)" <43478413+grafanabot@users.noreply.github.com> Date: Fri, 3 Jun 2022 12:02:27 -0400 Subject: [PATCH] SQLStore: Add deprecation comments for breaking migrations (#49740) (#50183) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Migrator: Extend support to rename columns * SQLStore: Add deprecation comments for breaking migrations (cherry picked from commit 5f1305d280cb1c72e8d9833dd03d9d6b9f9e4788) Co-authored-by: Joan López de la Franca Beltran <5459617+joanlopez@users.noreply.github.com> --- pkg/services/sqlstore/migrations/common.go | 2 ++ .../sqlstore/migrations/secrets_mig.go | 2 +- pkg/services/sqlstore/migrator/dialect.go | 13 ++++++++--- pkg/services/sqlstore/migrator/migrations.go | 23 ++++++++++++++----- .../sqlstore/migrator/mysql_dialect.go | 16 ++++--------- 5 files changed, 35 insertions(+), 21 deletions(-) diff --git a/pkg/services/sqlstore/migrations/common.go b/pkg/services/sqlstore/migrations/common.go index cc31b1d4580..7f8c4498e7a 100644 --- a/pkg/services/sqlstore/migrations/common.go +++ b/pkg/services/sqlstore/migrations/common.go @@ -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)) diff --git a/pkg/services/sqlstore/migrations/secrets_mig.go b/pkg/services/sqlstore/migrations/secrets_mig.go index d98e4e5aca8..5baadca3eb8 100644 --- a/pkg/services/sqlstore/migrations/secrets_mig.go +++ b/pkg/services/sqlstore/migrations/secrets_mig.go @@ -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( diff --git a/pkg/services/sqlstore/migrator/dialect.go b/pkg/services/sqlstore/migrator/dialect.go index 1c61389f074..740704c6f40 100644 --- a/pkg/services/sqlstore/migrator/dialect.go +++ b/pkg/services/sqlstore/migrator/dialect.go @@ -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{}) { diff --git a/pkg/services/sqlstore/migrator/migrations.go b/pkg/services/sqlstore/migrator/migrations.go index d2cc23cb837..b26e68fd12f 100644 --- a/pkg/services/sqlstore/migrator/migrations.go +++ b/pkg/services/sqlstore/migrator/migrations.go @@ -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} } diff --git a/pkg/services/sqlstore/migrator/mysql_dialect.go b/pkg/services/sqlstore/migrator/mysql_dialect.go index e217dec6656..819dc45bcab 100644 --- a/pkg/services/sqlstore/migrator/mysql_dialect.go +++ b/pkg/services/sqlstore/migrator/mysql_dialect.go @@ -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 {