Merge pull request #14455 from wy65701436/fixes-14454

fix catalog duplicate result
This commit is contained in:
Daniel Jiang 2021-03-16 15:19:13 +08:00 committed by GitHub
commit 7694c131a4
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
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) {