From 73babbf1ab9a4b91698586b3060405b2e05f4cad Mon Sep 17 00:00:00 2001 From: Wenkai Yin Date: Tue, 19 Dec 2017 22:15:56 +0800 Subject: [PATCH] Modify unique constraint of table harbor_label Add unique constraint to column name, scope and project_id of table harbor_label to make creating same name labels under different projects valid --- make/photon/db/registry.sql | 2 +- make/photon/db/registry_sqlite.sql | 2 +- src/common/dao/label_test.go | 21 ++++++++++++++++++++- tools/migration/db/db_meta.py | 2 +- 4 files changed, 23 insertions(+), 4 deletions(-) diff --git a/make/photon/db/registry.sql b/make/photon/db/registry.sql index 023ef70ec..17dbf9fa9 100644 --- a/make/photon/db/registry.sql +++ b/make/photon/db/registry.sql @@ -278,7 +278,7 @@ create table harbor_label ( creation_time timestamp default CURRENT_TIMESTAMP, update_time timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, PRIMARY KEY(id), - CONSTRAINT unique_name_and_scope UNIQUE (name,scope) + CONSTRAINT unique_label UNIQUE (name,scope, project_id) ); create table harbor_resource_label ( diff --git a/make/photon/db/registry_sqlite.sql b/make/photon/db/registry_sqlite.sql index c6e4ed2e9..722a58d08 100644 --- a/make/photon/db/registry_sqlite.sql +++ b/make/photon/db/registry_sqlite.sql @@ -260,7 +260,7 @@ create table harbor_label ( project_id int, creation_time timestamp default CURRENT_TIMESTAMP, update_time timestamp default CURRENT_TIMESTAMP, - UNIQUE(name, scope) + UNIQUE(name, scope, project_id) ); create table harbor_resource_label ( diff --git a/src/common/dao/label_test.go b/src/common/dao/label_test.go index 60b389e1b..d6c0f1d49 100644 --- a/src/common/dao/label_test.go +++ b/src/common/dao/label_test.go @@ -25,8 +25,9 @@ import ( ) func TestMethodsOfLabel(t *testing.T) { + labelName := "test" label := &models.Label{ - Name: "test", + Name: labelName, Level: common.LabelLevelUser, Scope: common.LabelScopeProject, ProjectID: 1, @@ -37,6 +38,24 @@ func TestMethodsOfLabel(t *testing.T) { require.Nil(t, err) label.ID = id + // add a label which has the same name to another project + projectID, err := AddProject(models.Project{ + OwnerID: 1, + Name: "project_for_label_test", + }) + require.Nil(t, err) + defer GetOrmer().QueryTable(&models.Project{}). + Filter("project_id", projectID).Delete() + + id2, err := AddLabel(&models.Label{ + Name: labelName, + Level: common.LabelLevelUser, + Scope: common.LabelScopeProject, + ProjectID: projectID, + }) + require.Nil(t, err) + defer DeleteLabel(id2) + // get l, err := GetLabel(id) require.Nil(t, err) diff --git a/tools/migration/db/db_meta.py b/tools/migration/db/db_meta.py index 4fc2b5232..c012f9ff3 100644 --- a/tools/migration/db/db_meta.py +++ b/tools/migration/db/db_meta.py @@ -259,7 +259,7 @@ class HarborLabel(Base): creation_time = sa.Column(mysql.TIMESTAMP, server_default = sa.text("CURRENT_TIMESTAMP")) update_time = sa.Column(mysql.TIMESTAMP, server_default = sa.text("CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP")) - __table_args__ = (sa.UniqueConstraint('name', 'scope', name='unique_name_and_scope'),) + __table_args__ = (sa.UniqueConstraint('name', 'scope', 'project_id', name='unique_label'),) class HarborResourceLabel(Base):