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
This commit is contained in:
Wenkai Yin 2017-12-19 22:15:56 +08:00
parent befd169c00
commit 73babbf1ab
4 changed files with 23 additions and 4 deletions

View File

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

View File

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

View File

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

View File

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