(bug) - remove unique index on template name (#3226)

* (bug) - remove unique index on template names
This commit is contained in:
Eoin McAfee 2022-06-09 14:30:18 +01:00 committed by GitHub
parent 0fcf2536d2
commit f7a3c375a2
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
6 changed files with 141 additions and 2 deletions

View file

@ -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
})

View file

@ -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;
`

View file

@ -0,0 +1,4 @@
-- name: amend-table-templates
ALTER TABLE templates
DROP CONSTRAINT templates_template_name_key;

View file

@ -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;
`

View file

@ -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;

View file

@ -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]))
}
}
}