From f7a3c375a2058d0f6b704c500290ef73f1b33428 Mon Sep 17 00:00:00 2001 From: Eoin McAfee <83226740+eoinmcafee00@users.noreply.github.com> Date: Thu, 9 Jun 2022 14:30:18 +0100 Subject: [PATCH] (bug) - remove unique index on template name (#3226) * (bug) - remove unique index on template names --- store/shared/db/dbtest/dbtest.go | 2 + store/shared/migrate/postgres/ddl_gen.go | 13 ++++ .../files/020_amend_table_templates.sql | 4 ++ store/shared/migrate/sqlite/ddl_gen.go | 37 ++++++++++++ .../files/019_amend_table_templates.sql | 28 +++++++++ store/template/template_test.go | 59 ++++++++++++++++++- 6 files changed, 141 insertions(+), 2 deletions(-) create mode 100644 store/shared/migrate/postgres/files/020_amend_table_templates.sql create mode 100644 store/shared/migrate/sqlite/files/019_amend_table_templates.sql diff --git a/store/shared/db/dbtest/dbtest.go b/store/shared/db/dbtest/dbtest.go index b67ded9e..21a578e2 100644 --- a/store/shared/db/dbtest/dbtest.go +++ b/store/shared/db/dbtest/dbtest.go @@ -48,6 +48,7 @@ func Connect() (*db.DB, error) { func Reset(d *db.DB) { d.Lock(func(tx db.Execer, _ db.Binder) error { tx.Exec("DELETE FROM cron") + tx.Exec("DELETE FROM cards") tx.Exec("DELETE FROM logs") tx.Exec("DELETE FROM steps") tx.Exec("DELETE FROM stages") @@ -56,6 +57,7 @@ func Reset(d *db.DB) { tx.Exec("DELETE FROM perms") tx.Exec("DELETE FROM repos") tx.Exec("DELETE FROM users") + tx.Exec("DELETE FROM templates") tx.Exec("DELETE FROM orgsecrets") return nil }) diff --git a/store/shared/migrate/postgres/ddl_gen.go b/store/shared/migrate/postgres/ddl_gen.go index 37ae33e2..40c5c001 100644 --- a/store/shared/migrate/postgres/ddl_gen.go +++ b/store/shared/migrate/postgres/ddl_gen.go @@ -196,6 +196,10 @@ var migrations = []struct { name: "create-new-table-cards", stmt: createNewTableCards, }, + { + name: "amend-table-templates", + stmt: amendTableTemplates, + }, } // Migrate performs the database migration. If the migration fails @@ -763,3 +767,12 @@ CREATE TABLE IF NOT EXISTS cards FOREIGN KEY (card_id) REFERENCES steps (step_id) ON DELETE CASCADE ); ` + +// +// 020_amend_table_templates.sql +// + +var amendTableTemplates = ` +ALTER TABLE templates +DROP CONSTRAINT templates_template_name_key; +` diff --git a/store/shared/migrate/postgres/files/020_amend_table_templates.sql b/store/shared/migrate/postgres/files/020_amend_table_templates.sql new file mode 100644 index 00000000..30b3cfbd --- /dev/null +++ b/store/shared/migrate/postgres/files/020_amend_table_templates.sql @@ -0,0 +1,4 @@ +-- name: amend-table-templates + +ALTER TABLE templates +DROP CONSTRAINT templates_template_name_key; diff --git a/store/shared/migrate/sqlite/ddl_gen.go b/store/shared/migrate/sqlite/ddl_gen.go index 1df96562..76afe275 100644 --- a/store/shared/migrate/sqlite/ddl_gen.go +++ b/store/shared/migrate/sqlite/ddl_gen.go @@ -196,6 +196,10 @@ var migrations = []struct { name: "create-new-table-cards", stmt: createNewTableCards, }, + { + name: "amend-templates-table", + stmt: amendTemplatesTable, + }, } // Migrate performs the database migration. If the migration fails @@ -765,3 +769,36 @@ CREATE TABLE IF NOT EXISTS cards FOREIGN KEY (card_id) REFERENCES steps (step_id) ON DELETE CASCADE ); ` + +// +// 019_amend_table_templates.sql +// + +var amendTemplatesTable = ` +PRAGMA foreign_keys=off; + +BEGIN TRANSACTION; + +ALTER TABLE templates RENAME TO _templates_old; + +CREATE TABLE IF NOT EXISTS templates ( + template_id INTEGER PRIMARY KEY AUTOINCREMENT + ,template_name TEXT + ,template_namespace TEXT COLLATE NOCASE + ,template_data BLOB + ,template_created INTEGER + ,template_updated INTEGER + ,UNIQUE(template_name, template_namespace) +); + +INSERT INTO templates (template_id, template_name, template_namespace, template_data, template_created, template_updated) +SELECT template_id, template_name, template_namespace, template_data, template_created, template_updated +FROM _templates_old; + +COMMIT; + +CREATE INDEX IF NOT EXISTS ix_template_namespace ON templates (template_namespace); +DROP TABLE _templates_old; + +PRAGMA foreign_keys=on; +` diff --git a/store/shared/migrate/sqlite/files/019_amend_table_templates.sql b/store/shared/migrate/sqlite/files/019_amend_table_templates.sql new file mode 100644 index 00000000..16b094d0 --- /dev/null +++ b/store/shared/migrate/sqlite/files/019_amend_table_templates.sql @@ -0,0 +1,28 @@ +-- name: amend-templates-table + +PRAGMA foreign_keys=off; + +BEGIN TRANSACTION; + +ALTER TABLE templates RENAME TO _templates_old; + +CREATE TABLE IF NOT EXISTS templates ( + template_id INTEGER PRIMARY KEY AUTOINCREMENT + ,template_name TEXT + ,template_namespace TEXT COLLATE NOCASE + ,template_data BLOB + ,template_created INTEGER + ,template_updated INTEGER + ,UNIQUE(template_name, template_namespace) +); + +INSERT INTO templates (template_id, template_name, template_namespace, template_data, template_created, template_updated) +SELECT template_id, template_name, template_namespace, template_data, template_created, template_updated +FROM _templates_old; + +COMMIT; + +CREATE INDEX IF NOT EXISTS ix_template_namespace ON templates (template_namespace); +DROP TABLE _templates_old; + +PRAGMA foreign_keys=on; \ No newline at end of file diff --git a/store/template/template_test.go b/store/template/template_test.go index 4e777f9c..cec90191 100644 --- a/store/template/template_test.go +++ b/store/template/template_test.go @@ -2,6 +2,7 @@ // Use of this source code is governed by the Drone Non-Commercial License // that can be found in the LICENSE file. +//go:build !oss // +build !oss package template @@ -29,7 +30,7 @@ func TestTemplate(t *testing.T) { }() store := New(conn).(*templateStore) - t.Run("Create", testTemplateCreate(store)) + t.Run("TestTemplates", testTemplateCreate(store)) } func testTemplateCreate(store *templateStore) func(t *testing.T) { @@ -50,6 +51,8 @@ func testTemplateCreate(store *templateStore) func(t *testing.T) { t.Errorf("Want template Id assigned, got %d", item.Id) } + t.Run("CreateSameNameDiffOrg", testCreateWithSameNameDiffOrg(store)) + t.Run("CreateSameNameSameOrgShouldError", testCreateSameNameSameOrgShouldError(store)) t.Run("Find", testTemplateFind(store, item)) t.Run("FindName", testTemplateFindName(store)) t.Run("ListAll", testTemplateListAll(store)) @@ -59,6 +62,43 @@ func testTemplateCreate(store *templateStore) func(t *testing.T) { } } +func testCreateWithSameNameDiffOrg(store *templateStore) func(t *testing.T) { + return func(t *testing.T) { + item := &core.Template{ + Id: 1, + Name: "my_template", + Namespace: "my_org2", + Data: "some_template_data", + Created: 1, + Updated: 2, + } + err := store.Create(noContext, item) + if err != nil { + t.Error(err) + } + if item.Id == 0 { + t.Errorf("Want template Id assigned, got %d", item.Id) + } + } +} + +func testCreateSameNameSameOrgShouldError(store *templateStore) func(t *testing.T) { + return func(t *testing.T) { + item := &core.Template{ + Id: 3, + Name: "my_template", + Namespace: "my_org2", + Data: "some_template_data", + Created: 1, + Updated: 2, + } + err := store.Create(noContext, item) + if err == nil { + t.Error(err) + } + } +} + func testTemplateFind(store *templateStore, template *core.Template) func(t *testing.T) { return func(t *testing.T) { item, err := store.Find(noContext, template.Id) @@ -95,6 +135,20 @@ func testTemplate(item *core.Template) func(t *testing.T) { } } +func testTemplate2(item *core.Template) func(t *testing.T) { + return func(t *testing.T) { + if got, want := item.Name, "my_template"; got != want { + t.Errorf("Want template name %q, got %q", want, got) + } + if got, want := item.Data, "some_template_data"; got != want { + t.Errorf("Want template data %q, got %q", want, got) + } + if got, want := item.Namespace, "my_org2"; got != want { + t.Errorf("Want template org %q, got %q", want, got) + } + } +} + func testTemplateListAll(store *templateStore) func(t *testing.T) { return func(t *testing.T) { list, err := store.ListAll(noContext) @@ -102,10 +156,11 @@ func testTemplateListAll(store *templateStore) func(t *testing.T) { t.Error(err) return } - if got, want := len(list), 1; got != want { + if got, want := len(list), 2; got != want { t.Errorf("Want count %d, got %d", want, got) } else { t.Run("Fields", testTemplate(list[0])) + t.Run("Fields", testTemplate2(list[1])) } } }