From ad47007438ecf749db96c46d69f7924302fae84b Mon Sep 17 00:00:00 2001 From: Wang Yan Date: Tue, 16 Mar 2021 12:37:22 +0800 Subject: [PATCH] fix catalog duplicate result Add distinct to the sql to avoid duplicate records be returned Signed-off-by: Wang Yan --- src/pkg/repository/dao/dao.go | 2 +- src/pkg/repository/dao/dao_test.go | 33 ++++++++++++++++++++++-------- 2 files changed, 25 insertions(+), 10 deletions(-) diff --git a/src/pkg/repository/dao/dao.go b/src/pkg/repository/dao/dao.go index 5df942f20..58a4a0749 100644 --- a/src/pkg/repository/dao/dao.go +++ b/src/pkg/repository/dao/dao.go @@ -160,7 +160,7 @@ func (d *dao) NonEmptyRepos(ctx context.Context) ([]*models.RepoRecord, error) { return nil, err } - sql := fmt.Sprintf(`select r.* from repository as r LEFT JOIN tag as t on r.repository_id = t.repository_id where t.repository_id is not null;`) + sql := fmt.Sprintf(`select distinct r.* from repository as r LEFT JOIN tag as t on r.repository_id = t.repository_id where t.repository_id is not null;`) _, err = ormer.Raw(sql).QueryRows(&repos) if err != nil { return repos, err diff --git a/src/pkg/repository/dao/dao_test.go b/src/pkg/repository/dao/dao_test.go index 4dd1d11d5..39567f3b7 100644 --- a/src/pkg/repository/dao/dao_test.go +++ b/src/pkg/repository/dao/dao_test.go @@ -190,9 +190,9 @@ func (d *daoTestSuite) TestAddPullCount() { d.dao.Delete(d.ctx, id) } -func (d *daoTestSuite) TestEmptyRepos() { +func (d *daoTestSuite) TestNonEmptyRepos() { repository := &models.RepoRecord{ - Name: "TestEmptyRepos", + Name: "TestNonEmptyRepos", ProjectID: 10, Description: "test pull count", PullCount: 1, @@ -215,30 +215,45 @@ func (d *daoTestSuite) TestEmptyRepos() { afID, err := d.afDao.Create(d.ctx, art) d.Require().Nil(err) - tag := &tag.Tag{ + // Same repository with two tags, the result should only contain one repository record. + tag1 := &tag.Tag{ RepositoryID: id, ArtifactID: afID, - Name: "latest", + Name: "tag1", PushTime: time.Now(), PullTime: time.Now(), } - _, err = d.tagDao.Create(d.ctx, tag) + _, err = d.tagDao.Create(d.ctx, tag1) + d.Require().Nil(err) + tag2 := &tag.Tag{ + RepositoryID: id, + ArtifactID: afID, + Name: "tag2", + PushTime: time.Now(), + PullTime: time.Now(), + } + _, err = d.tagDao.Create(d.ctx, tag2) d.Require().Nil(err) repos, err := d.dao.NonEmptyRepos(d.ctx) d.Require().Nil(err) var success bool + var count int for _, repo := range repos { - if repo.Name == "TestEmptyRepos" { + if repo.Name == "TestNonEmptyRepos" { success = true - break + count++ } } - if !success { - d.Fail("TestEmptyRepos failure") + d.Fail("TestNonEmptyRepos failure: no NonEmpty repository found") } + + if count != 1 { + d.Fail("TestNonEmptyRepos failure: duplicate repository record") + } + } func TestDaoTestSuite(t *testing.T) {