fix catalog duplicate result

Add distinct to the sql to avoid duplicate records be returned

Signed-off-by: Wang Yan <wangyan@vmware.com>
This commit is contained in:
Wang Yan 2021-03-16 12:37:22 +08:00
parent ac7a0f8841
commit ad47007438
2 changed files with 25 additions and 10 deletions

View File

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

View File

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