From 2456ac160979d6fc3ff66df2b984391da637d0f9 Mon Sep 17 00:00:00 2001 From: Alexey Zhiltsov Date: Tue, 7 Jan 2020 16:51:45 +0100 Subject: [PATCH] SQL: define primary key for tables without it 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 --- pkg/services/sqlstore/migrations/alert_mig.go | 29 ++++++++++++++++++ .../sqlstore/migrations/annotation_mig.go | 30 +++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/pkg/services/sqlstore/migrations/alert_mig.go b/pkg/services/sqlstore/migrations/alert_mig.go index 97aa3cdf60d4..c7a23f41e29b 100644 --- a/pkg/services/sqlstore/migrations/alert_mig.go +++ b/pkg/services/sqlstore/migrations/alert_mig.go @@ -59,6 +59,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{ diff --git a/pkg/services/sqlstore/migrations/annotation_mig.go b/pkg/services/sqlstore/migrations/annotation_mig.go index fb7fe9f55734..902fc99c63d0 100644 --- a/pkg/services/sqlstore/migrations/annotation_mig.go +++ b/pkg/services/sqlstore/migrations/annotation_mig.go @@ -83,6 +83,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 //