Skip to content

Commit

Permalink
✨ Rename artifact_artifacts sub artifact field (#337)
Browse files Browse the repository at this point in the history
  • Loading branch information
tosone authored Mar 11, 2024
1 parent e28bf37 commit ea725b4
Show file tree
Hide file tree
Showing 17 changed files with 67 additions and 56 deletions.
8 changes: 4 additions & 4 deletions pkg/dal/dao/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -141,13 +141,13 @@ func (s *artifactService) FindAssociateWithArtifact(ctx context.Context, ids []i
if err != nil {
return nil, err
}
var artifactIndexes []int64
err = s.tx.Blob.WithContext(ctx).UnderlyingDB().Raw("SELECT artifact_index_id FROM artifact_artifacts WHERE artifact_index_id in (?)", ids).Scan(&artifactIndexes).Error
var artifactSubs []int64
err = s.tx.Blob.WithContext(ctx).UnderlyingDB().Raw("SELECT artifact_sub_id FROM artifact_artifacts WHERE artifact_sub_id in (?)", ids).Scan(&artifactSubs).Error
if err != nil {
return nil, err
}
resultSet := mapset.NewSet(artifacts...)
resultSet.Append(artifactIndexes...)
resultSet.Append(artifactSubs...)
return resultSet.ToSlice(), err
}

Expand Down Expand Up @@ -209,7 +209,7 @@ func (s *artifactService) AssociateBlobs(ctx context.Context, artifact *models.A

// AssociateArtifact ...
func (s *artifactService) AssociateArtifact(ctx context.Context, artifact *models.Artifact, artifacts []*models.Artifact) error {
return s.tx.Artifact.ArtifactIndexes.WithContext(ctx).Model(artifact).Append(artifacts...)
return s.tx.Artifact.ArtifactSubs.WithContext(ctx).Model(artifact).Append(artifacts...)
}

// Incr increases the pull times of the artifact.
Expand Down
14 changes: 7 additions & 7 deletions pkg/dal/dao/tag.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,9 +171,9 @@ func (s *tagService) FindWithDayCursor(ctx context.Context, repositoryID int64,
// GetByID gets the tag with the specified tag ID.
func (s *tagService) GetByID(ctx context.Context, tagID int64) (*models.Tag, error) {
q := s.tx.Tag.WithContext(ctx).Where(s.tx.Tag.ID.Eq(tagID))
q.UnderlyingDB().Preload("Artifact.ArtifactIndexes.Vulnerability")
q.UnderlyingDB().Preload("Artifact.ArtifactIndexes.Sbom")
q.Preload(s.tx.Tag.Artifact.ArtifactIndexes)
q.UnderlyingDB().Preload("Artifact.ArtifactSubs.Vulnerability")
q.UnderlyingDB().Preload("Artifact.ArtifactSubs.Sbom")
q.Preload(s.tx.Tag.Artifact.ArtifactSubs)
q.Preload(s.tx.Tag.Artifact.Vulnerability)
q.Preload(s.tx.Tag.Artifact.Sbom)
return q.First()
Expand Down Expand Up @@ -270,14 +270,14 @@ func (s *tagService) ListTag(ctx context.Context, repositoryID int64, name *stri
q = q.Order(s.tx.Tag.UpdatedAt.Desc())
}
if len(types) > 0 {
q = q.Preload(s.tx.Tag.Artifact.ArtifactIndexes.On(s.tx.Artifact.Type.In(mTypes...)))
q = q.Preload(s.tx.Tag.Artifact.ArtifactSubs.On(s.tx.Artifact.Type.In(mTypes...)))
} else {
q = q.Preload(s.tx.Tag.Artifact.ArtifactIndexes)
q = q.Preload(s.tx.Tag.Artifact.ArtifactSubs)
}
q = q.Preload(s.tx.Tag.Artifact.Vulnerability)
q = q.Preload(s.tx.Tag.Artifact.Sbom)
q.UnderlyingDB().Preload("Artifact.ArtifactIndexes.Vulnerability")
q.UnderlyingDB().Preload("Artifact.ArtifactIndexes.Sbom")
q.UnderlyingDB().Preload("Artifact.ArtifactSubs.Vulnerability")
q.UnderlyingDB().Preload("Artifact.ArtifactSubs.Sbom")
return q.FindByPage(ptr.To(pagination.Limit)*(ptr.To(pagination.Page)-1), ptr.To(pagination.Limit))
}

Expand Down
2 changes: 2 additions & 0 deletions pkg/dal/migrations/mysql/0002_upgrade.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE `artifact_artifacts` RENAME COLUMN `artifact_sub_id` TO `artifact_index_id`;

2 changes: 2 additions & 0 deletions pkg/dal/migrations/mysql/0002_upgrade.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE `artifact_artifacts` RENAME COLUMN `artifact_index_id` TO `artifact_sub_id`;

2 changes: 2 additions & 0 deletions pkg/dal/migrations/postgresql/0002_upgrade.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "artifact_artifacts" RENAME COLUMN "artifact_sub_id" TO "artifact_index_id";

2 changes: 2 additions & 0 deletions pkg/dal/migrations/postgresql/0002_upgrade.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE "artifact_artifacts" RENAME COLUMN "artifact_index_id" TO "artifact_sub_id";

2 changes: 2 additions & 0 deletions pkg/dal/migrations/sqlite3/0002_upgrade.down.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE `artifact_artifacts` RENAME COLUMN `artifact_sub_id` TO `artifact_index_id`;

2 changes: 2 additions & 0 deletions pkg/dal/migrations/sqlite3/0002_upgrade.up.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
ALTER TABLE `artifact_artifacts` RENAME COLUMN `artifact_index_id` TO `artifact_sub_id`;

13 changes: 6 additions & 7 deletions pkg/dal/models/artifact.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,12 +52,11 @@ type Artifact struct {
ReferrerID *int64
Referrer *Artifact

// ArtifactIndexes Perhaps the variable naming here is not ideal, but we don't want to change it.
// In the artifact_artifacts table, artifact_id refers to the upper-level artifact index,
// and artifact_index_id refers to the lower-level artifact.
ArtifactIndexes []*Artifact `gorm:"many2many:artifact_artifacts;"`
Blobs []*Blob `gorm:"many2many:artifact_blobs;"`
Tags []*Tag `gorm:"foreignKey:ArtifactID;"`
// ArtifactSubs In the artifact_artifacts table, artifact_id refers to the upper-level artifact index,
// and artifact_sub_id refers to the lower-level artifact.
ArtifactSubs []*Artifact `gorm:"many2many:artifact_artifacts;"`
Blobs []*Blob `gorm:"many2many:artifact_blobs;"`
Tags []*Tag `gorm:"foreignKey:ArtifactID;"`
}

// ArtifactSizeByNamespaceOrRepository ...
Expand All @@ -71,7 +70,7 @@ type ArtifactSizeByNamespaceOrRepository interface {

// ArtifactAssociated ...
type ArtifactAssociated interface {
// SELECT COUNT(artifact_id) as count FROM artifact_artifacts LEFT JOIN artifacts ON artifacts.id = artifact_artifacts.artifact_id WHERE artifacts.deleted_at = 0 AND artifact_index_id=@artifactID
// SELECT COUNT(artifact_id) as count FROM artifact_artifacts LEFT JOIN artifacts ON artifacts.id = artifact_artifacts.artifact_id WHERE artifacts.deleted_at = 0 AND artifact_sub_id=@artifactID
ArtifactAssociated(artifactID int64) (gen.M, error)
}

Expand Down
6 changes: 3 additions & 3 deletions pkg/dal/query/artifact_sboms.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pkg/dal/query/artifact_vulnerabilities.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

44 changes: 22 additions & 22 deletions pkg/dal/query/artifacts.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pkg/dal/query/blobs.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 3 additions & 3 deletions pkg/dal/query/tags.gen.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion pkg/handlers/distribution/manifest/manifest_put.go
Original file line number Diff line number Diff line change
Expand Up @@ -336,7 +336,7 @@ func (h *handler) putManifestIndex(ctx context.Context, user *models.User, diges
return xerrors.DSErrCodeUnknown
}

artifactObj.ArtifactIndexes = artifactObjs
artifactObj.ArtifactSubs = artifactObjs

err = query.Q.Transaction(func(tx *query.Query) error {
artifactService := h.artifactServiceFactory.New(tx)
Expand Down
4 changes: 2 additions & 2 deletions pkg/handlers/tags/tags_get.go
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ func (h *handler) GetTag(c echo.Context) error {
return xerrors.NewHTTPError(c, xerrors.HTTPErrCodeInternalError, err.Error())
}

var artifacts = make([]types.TagItemArtifact, 0, len(tag.Artifact.ArtifactIndexes))
for _, item := range tag.Artifact.ArtifactIndexes {
var artifacts = make([]types.TagItemArtifact, 0, len(tag.Artifact.ArtifactSubs))
for _, item := range tag.Artifact.ArtifactSubs {
artifacts = append(artifacts, types.TagItemArtifact{
ID: item.ID,
Digest: item.Digest,
Expand Down
2 changes: 1 addition & 1 deletion pkg/handlers/tags/tags_list.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ func (h *handler) ListTag(c echo.Context) error {
continue
}
var artifacts []types.TagItemArtifact
for _, item := range tag.Artifact.ArtifactIndexes {
for _, item := range tag.Artifact.ArtifactSubs {
artifacts = append(artifacts, types.TagItemArtifact{
ID: item.ID,
Digest: item.Digest,
Expand Down

0 comments on commit ea725b4

Please sign in to comment.