Skip to content

Commit

Permalink
SQL: Define primary key for tables without it (#22255)
Browse files Browse the repository at this point in the history
The annotation_tag and alert_rule_tag tables did not have
PRIMARY KEY defined what cause problems with migration to
MariaDB with Galera setup with
 innodb_force_primary_key=1
Or MySQL > 8.0.13 with
 sql_require_primary_key=ON

Which can manifest as follows:

MariaDB
Error 1173: This table type requires a primary key

MySQL
ERROR 3750 (HY000): Unable to create or change a table
without a primary key, when the system variable 'sql_require_primary_key' is set.

Extra reading for curious:
https://jfg-mysql.blogspot.com/2017/08/danger-no-pk-with-RBR-and-mariadb-protection.html

Fixes #12971

Co-authored-by: Marcus Efraimsson <marcus.efraimsson@gmail.com>
  • Loading branch information
2 people authored and ryantxu committed Nov 18, 2020
1 parent 76bddaf commit 6c5e957
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
29 changes: 29 additions & 0 deletions pkg/services/sqlstore/migrations/alert_mig.go
Expand Up @@ -58,6 +58,35 @@ func addAlertMigrations(mg *Migrator) {
mg.AddMigration("Create alert_rule_tag table v1", NewAddTableMigration(alertRuleTagTable))
mg.AddMigration("Add unique index alert_rule_tag.alert_id_tag_id", NewAddIndexMigration(alertRuleTagTable, alertRuleTagTable.Indices[0]))

// drop alert_rule_tag indexes
addDropAllIndicesMigrations(mg, "v1", alertRuleTagTable)
// rename table
addTableRenameMigration(mg, "alert_rule_tag", "alert_rule_tag_v1", "v1")

// alert_rule_tag V2
alertRuleTagTableV2 := Table{
Name: "alert_rule_tag",
Columns: []*Column{
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
{Name: "alert_id", Type: DB_BigInt, Nullable: false},
{Name: "tag_id", Type: DB_BigInt, Nullable: false},
},
Indices: []*Index{
{Cols: []string{"alert_id", "tag_id"}, Type: UniqueIndex},
},
}
// recreate table
mg.AddMigration("Create alert_rule_tag table v2", NewAddTableMigration(alertRuleTagTableV2))
// recreate indices
addTableIndicesMigrations(mg, "Add unique index alert_rule_tag.alert_id_tag_id V2", alertRuleTagTableV2)
// copy data
mg.AddMigration("copy alert_rule_tag v1 to v2", NewCopyTableDataMigration("alert_rule_tag", "alert_rule_tag_v1", map[string]string{
"alert_id": "alert_id",
"tag_id": "tag_id",
}))

mg.AddMigration("drop table alert_rule_tag_v1", NewDropTableMigration("alert_rule_tag_v1"))

alert_notification := Table{
Name: "alert_notification",
Columns: []*Column{
Expand Down
30 changes: 30 additions & 0 deletions pkg/services/sqlstore/migrations/annotation_mig.go
Expand Up @@ -82,6 +82,36 @@ func addAnnotationMig(mg *Migrator) {
mg.AddMigration("Create annotation_tag table v2", NewAddTableMigration(annotationTagTable))
mg.AddMigration("Add unique index annotation_tag.annotation_id_tag_id", NewAddIndexMigration(annotationTagTable, annotationTagTable.Indices[0]))

// drop dashboard indexes
addDropAllIndicesMigrations(mg, "v2", annotationTagTable)
// rename table
addTableRenameMigration(mg, "annotation_tag", "annotation_tag_v2", "v2")

// annotation_tag v3
annotationTagTableV3 := Table{
Name: "annotation_tag",
Columns: []*Column{
{Name: "id", Type: DB_BigInt, IsPrimaryKey: true, IsAutoIncrement: true},
{Name: "annotation_id", Type: DB_BigInt, Nullable: false},
{Name: "tag_id", Type: DB_BigInt, Nullable: false},
},
Indices: []*Index{
{Cols: []string{"annotation_id", "tag_id"}, Type: UniqueIndex},
},
}

// recreate table
mg.AddMigration("Create annotation_tag table v3", NewAddTableMigration(annotationTagTableV3))
// recreate indices
addTableIndicesMigrations(mg, "Add unique index annotation_tag.annotation_id_tag_id V3", annotationTagTableV3)
// copy data
mg.AddMigration("copy annotation_tag v2 to v3", NewCopyTableDataMigration("annotation_tag", "annotation_tag_v2", map[string]string{
"annotation_id": "annotation_id",
"tag_id": "tag_id",
}))

mg.AddMigration("drop table annotation_tag_v2", NewDropTableMigration("annotation_tag_v2"))

//
// clear alert text
//
Expand Down

0 comments on commit 6c5e957

Please sign in to comment.