Merge pull request #4802 from ywk253100/180427_label_db

Modify unique constraint of table harbor_label
This commit is contained in:
Wenkai Yin 2018-04-27 17:10:05 +08:00 committed by GitHub
commit f77e4167ac
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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, creation_time timestamp default CURRENT_TIMESTAMP,
update_time timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP, update_time timestamp default CURRENT_TIMESTAMP on update CURRENT_TIMESTAMP,
PRIMARY KEY(id), PRIMARY KEY(id),
CONSTRAINT unique_name_and_scope UNIQUE (name,scope) CONSTRAINT unique_label UNIQUE (name,scope, project_id)
); );
create table harbor_resource_label ( create table harbor_resource_label (

View File

@ -260,7 +260,7 @@ create table harbor_label (
project_id int, project_id int,
creation_time timestamp default CURRENT_TIMESTAMP, creation_time timestamp default CURRENT_TIMESTAMP,
update_time timestamp default CURRENT_TIMESTAMP, update_time timestamp default CURRENT_TIMESTAMP,
UNIQUE(name, scope) UNIQUE(name, scope, project_id)
); );
create table harbor_resource_label ( create table harbor_resource_label (

View File

@ -25,8 +25,9 @@ import (
) )
func TestMethodsOfLabel(t *testing.T) { func TestMethodsOfLabel(t *testing.T) {
labelName := "test"
label := &models.Label{ label := &models.Label{
Name: "test", Name: labelName,
Level: common.LabelLevelUser, Level: common.LabelLevelUser,
Scope: common.LabelScopeProject, Scope: common.LabelScopeProject,
ProjectID: 1, ProjectID: 1,
@ -37,6 +38,24 @@ func TestMethodsOfLabel(t *testing.T) {
require.Nil(t, err) require.Nil(t, err)
label.ID = id 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 // get
l, err := GetLabel(id) l, err := GetLabel(id)
require.Nil(t, err) 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")) 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")) 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): class HarborResourceLabel(Base):