diff --git a/pkg/dal/dao/artifact.go b/pkg/dal/dao/artifact.go index 7bed5d16..dff854b6 100644 --- a/pkg/dal/dao/artifact.go +++ b/pkg/dal/dao/artifact.go @@ -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 } @@ -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. diff --git a/pkg/dal/dao/tag.go b/pkg/dal/dao/tag.go index 59f9e93f..ea842818 100644 --- a/pkg/dal/dao/tag.go +++ b/pkg/dal/dao/tag.go @@ -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() @@ -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)) } diff --git a/pkg/dal/migrations/mysql/0002_upgrade.down.sql b/pkg/dal/migrations/mysql/0002_upgrade.down.sql new file mode 100644 index 00000000..f6d0b33a --- /dev/null +++ b/pkg/dal/migrations/mysql/0002_upgrade.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE `artifact_artifacts` RENAME COLUMN `artifact_sub_id` TO `artifact_index_id`; + diff --git a/pkg/dal/migrations/mysql/0002_upgrade.up.sql b/pkg/dal/migrations/mysql/0002_upgrade.up.sql new file mode 100644 index 00000000..cae72cb5 --- /dev/null +++ b/pkg/dal/migrations/mysql/0002_upgrade.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE `artifact_artifacts` RENAME COLUMN `artifact_index_id` TO `artifact_sub_id`; + diff --git a/pkg/dal/migrations/postgresql/0002_upgrade.down.sql b/pkg/dal/migrations/postgresql/0002_upgrade.down.sql new file mode 100644 index 00000000..f5cd0848 --- /dev/null +++ b/pkg/dal/migrations/postgresql/0002_upgrade.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE "artifact_artifacts" RENAME COLUMN "artifact_sub_id" TO "artifact_index_id"; + diff --git a/pkg/dal/migrations/postgresql/0002_upgrade.up.sql b/pkg/dal/migrations/postgresql/0002_upgrade.up.sql new file mode 100644 index 00000000..f5baf346 --- /dev/null +++ b/pkg/dal/migrations/postgresql/0002_upgrade.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE "artifact_artifacts" RENAME COLUMN "artifact_index_id" TO "artifact_sub_id"; + diff --git a/pkg/dal/migrations/sqlite3/0002_upgrade.down.sql b/pkg/dal/migrations/sqlite3/0002_upgrade.down.sql new file mode 100644 index 00000000..f6d0b33a --- /dev/null +++ b/pkg/dal/migrations/sqlite3/0002_upgrade.down.sql @@ -0,0 +1,2 @@ +ALTER TABLE `artifact_artifacts` RENAME COLUMN `artifact_sub_id` TO `artifact_index_id`; + diff --git a/pkg/dal/migrations/sqlite3/0002_upgrade.up.sql b/pkg/dal/migrations/sqlite3/0002_upgrade.up.sql new file mode 100644 index 00000000..cae72cb5 --- /dev/null +++ b/pkg/dal/migrations/sqlite3/0002_upgrade.up.sql @@ -0,0 +1,2 @@ +ALTER TABLE `artifact_artifacts` RENAME COLUMN `artifact_index_id` TO `artifact_sub_id`; + diff --git a/pkg/dal/models/artifact.go b/pkg/dal/models/artifact.go index 649f9e2b..2fc7bd12 100644 --- a/pkg/dal/models/artifact.go +++ b/pkg/dal/models/artifact.go @@ -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 ... @@ -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) } diff --git a/pkg/dal/query/artifact_sboms.gen.go b/pkg/dal/query/artifact_sboms.gen.go index beae22ca..982c20fc 100644 --- a/pkg/dal/query/artifact_sboms.gen.go +++ b/pkg/dal/query/artifact_sboms.gen.go @@ -182,10 +182,10 @@ func newArtifactSbom(db *gorm.DB, opts ...gen.DOOption) artifactSbom { RelationField: field.NewRelation("Artifact.Tags.Artifact", "models.Artifact"), }, }, - ArtifactIndexes: struct { + ArtifactSubs: struct { field.RelationField }{ - RelationField: field.NewRelation("Artifact.ArtifactIndexes", "models.Artifact"), + RelationField: field.NewRelation("Artifact.ArtifactSubs", "models.Artifact"), }, Blobs: struct { field.RelationField @@ -356,7 +356,7 @@ type artifactSbomBelongsToArtifact struct { field.RelationField } } - ArtifactIndexes struct { + ArtifactSubs struct { field.RelationField } Blobs struct { diff --git a/pkg/dal/query/artifact_vulnerabilities.gen.go b/pkg/dal/query/artifact_vulnerabilities.gen.go index 3d913204..c2653bf7 100644 --- a/pkg/dal/query/artifact_vulnerabilities.gen.go +++ b/pkg/dal/query/artifact_vulnerabilities.gen.go @@ -183,10 +183,10 @@ func newArtifactVulnerability(db *gorm.DB, opts ...gen.DOOption) artifactVulnera RelationField: field.NewRelation("Artifact.Tags.Artifact", "models.Artifact"), }, }, - ArtifactIndexes: struct { + ArtifactSubs: struct { field.RelationField }{ - RelationField: field.NewRelation("Artifact.ArtifactIndexes", "models.Artifact"), + RelationField: field.NewRelation("Artifact.ArtifactSubs", "models.Artifact"), }, Blobs: struct { field.RelationField @@ -360,7 +360,7 @@ type artifactVulnerabilityBelongsToArtifact struct { field.RelationField } } - ArtifactIndexes struct { + ArtifactSubs struct { field.RelationField } Blobs struct { diff --git a/pkg/dal/query/artifacts.gen.go b/pkg/dal/query/artifacts.gen.go index c4dfe4ef..6a220e7a 100644 --- a/pkg/dal/query/artifacts.gen.go +++ b/pkg/dal/query/artifacts.gen.go @@ -96,7 +96,7 @@ func newArtifact(db *gorm.DB, opts ...gen.DOOption) artifact { field.RelationField } } - ArtifactIndexes struct { + ArtifactSubs struct { field.RelationField } Blobs struct { @@ -239,10 +239,10 @@ func newArtifact(db *gorm.DB, opts ...gen.DOOption) artifact { RelationField: field.NewRelation("Vulnerability.Artifact.Tags.Artifact", "models.Artifact"), }, }, - ArtifactIndexes: struct { + ArtifactSubs: struct { field.RelationField }{ - RelationField: field.NewRelation("Vulnerability.Artifact.ArtifactIndexes", "models.Artifact"), + RelationField: field.NewRelation("Vulnerability.Artifact.ArtifactSubs", "models.Artifact"), }, Blobs: struct { field.RelationField @@ -284,10 +284,10 @@ func newArtifact(db *gorm.DB, opts ...gen.DOOption) artifact { RelationField: field.NewRelation("Referrer", "models.Artifact"), } - _artifact.ArtifactIndexes = artifactManyToManyArtifactIndexes{ + _artifact.ArtifactSubs = artifactManyToManyArtifactSubs{ db: db.Session(&gorm.Session{}), - RelationField: field.NewRelation("ArtifactIndexes", "models.Artifact"), + RelationField: field.NewRelation("ArtifactSubs", "models.Artifact"), } _artifact.Blobs = artifactManyToManyBlobs{ @@ -332,7 +332,7 @@ type artifact struct { Referrer artifactBelongsToReferrer - ArtifactIndexes artifactManyToManyArtifactIndexes + ArtifactSubs artifactManyToManyArtifactSubs Blobs artifactManyToManyBlobs @@ -475,7 +475,7 @@ type artifactHasOneVulnerability struct { field.RelationField } } - ArtifactIndexes struct { + ArtifactSubs struct { field.RelationField } Blobs struct { @@ -836,13 +836,13 @@ func (a artifactBelongsToReferrerTx) Count() int64 { return a.tx.Count() } -type artifactManyToManyArtifactIndexes struct { +type artifactManyToManyArtifactSubs struct { db *gorm.DB field.RelationField } -func (a artifactManyToManyArtifactIndexes) Where(conds ...field.Expr) *artifactManyToManyArtifactIndexes { +func (a artifactManyToManyArtifactSubs) Where(conds ...field.Expr) *artifactManyToManyArtifactSubs { if len(conds) == 0 { return &a } @@ -855,27 +855,27 @@ func (a artifactManyToManyArtifactIndexes) Where(conds ...field.Expr) *artifactM return &a } -func (a artifactManyToManyArtifactIndexes) WithContext(ctx context.Context) *artifactManyToManyArtifactIndexes { +func (a artifactManyToManyArtifactSubs) WithContext(ctx context.Context) *artifactManyToManyArtifactSubs { a.db = a.db.WithContext(ctx) return &a } -func (a artifactManyToManyArtifactIndexes) Session(session *gorm.Session) *artifactManyToManyArtifactIndexes { +func (a artifactManyToManyArtifactSubs) Session(session *gorm.Session) *artifactManyToManyArtifactSubs { a.db = a.db.Session(session) return &a } -func (a artifactManyToManyArtifactIndexes) Model(m *models.Artifact) *artifactManyToManyArtifactIndexesTx { - return &artifactManyToManyArtifactIndexesTx{a.db.Model(m).Association(a.Name())} +func (a artifactManyToManyArtifactSubs) Model(m *models.Artifact) *artifactManyToManyArtifactSubsTx { + return &artifactManyToManyArtifactSubsTx{a.db.Model(m).Association(a.Name())} } -type artifactManyToManyArtifactIndexesTx struct{ tx *gorm.Association } +type artifactManyToManyArtifactSubsTx struct{ tx *gorm.Association } -func (a artifactManyToManyArtifactIndexesTx) Find() (result []*models.Artifact, err error) { +func (a artifactManyToManyArtifactSubsTx) Find() (result []*models.Artifact, err error) { return result, a.tx.Find(&result) } -func (a artifactManyToManyArtifactIndexesTx) Append(values ...*models.Artifact) (err error) { +func (a artifactManyToManyArtifactSubsTx) Append(values ...*models.Artifact) (err error) { targetValues := make([]interface{}, len(values)) for i, v := range values { targetValues[i] = v @@ -883,7 +883,7 @@ func (a artifactManyToManyArtifactIndexesTx) Append(values ...*models.Artifact) return a.tx.Append(targetValues...) } -func (a artifactManyToManyArtifactIndexesTx) Replace(values ...*models.Artifact) (err error) { +func (a artifactManyToManyArtifactSubsTx) Replace(values ...*models.Artifact) (err error) { targetValues := make([]interface{}, len(values)) for i, v := range values { targetValues[i] = v @@ -891,7 +891,7 @@ func (a artifactManyToManyArtifactIndexesTx) Replace(values ...*models.Artifact) return a.tx.Replace(targetValues...) } -func (a artifactManyToManyArtifactIndexesTx) Delete(values ...*models.Artifact) (err error) { +func (a artifactManyToManyArtifactSubsTx) Delete(values ...*models.Artifact) (err error) { targetValues := make([]interface{}, len(values)) for i, v := range values { targetValues[i] = v @@ -899,11 +899,11 @@ func (a artifactManyToManyArtifactIndexesTx) Delete(values ...*models.Artifact) return a.tx.Delete(targetValues...) } -func (a artifactManyToManyArtifactIndexesTx) Clear() error { +func (a artifactManyToManyArtifactSubsTx) Clear() error { return a.tx.Clear() } -func (a artifactManyToManyArtifactIndexesTx) Count() int64 { +func (a artifactManyToManyArtifactSubsTx) Count() int64 { return a.tx.Count() } @@ -1011,13 +1011,13 @@ func (a artifactDo) ArtifactSizeByRepository(repositoryID int64) (result models. return } -// 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 func (a artifactDo) ArtifactAssociated(artifactID int64) (result map[string]interface{}, err error) { var params []interface{} var generateSQL strings.Builder params = append(params, artifactID) - generateSQL.WriteString("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=? ") + generateSQL.WriteString("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=? ") result = make(map[string]interface{}) var executeSQL *gorm.DB diff --git a/pkg/dal/query/blobs.gen.go b/pkg/dal/query/blobs.gen.go index 91861438..4ac0e201 100644 --- a/pkg/dal/query/blobs.gen.go +++ b/pkg/dal/query/blobs.gen.go @@ -182,10 +182,10 @@ func newBlob(db *gorm.DB, opts ...gen.DOOption) blob { RelationField: field.NewRelation("Artifacts.Tags.Artifact", "models.Artifact"), }, }, - ArtifactIndexes: struct { + ArtifactSubs: struct { field.RelationField }{ - RelationField: field.NewRelation("Artifacts.ArtifactIndexes", "models.Artifact"), + RelationField: field.NewRelation("Artifacts.ArtifactSubs", "models.Artifact"), }, Blobs: struct { field.RelationField @@ -349,7 +349,7 @@ type blobManyToManyArtifacts struct { field.RelationField } } - ArtifactIndexes struct { + ArtifactSubs struct { field.RelationField } Blobs struct { diff --git a/pkg/dal/query/tags.gen.go b/pkg/dal/query/tags.gen.go index 1283fba8..c1ae0d41 100644 --- a/pkg/dal/query/tags.gen.go +++ b/pkg/dal/query/tags.gen.go @@ -166,10 +166,10 @@ func newTag(db *gorm.DB, opts ...gen.DOOption) tag { RelationField: field.NewRelation("Artifact.Tags.Artifact", "models.Artifact"), }, }, - ArtifactIndexes: struct { + ArtifactSubs: struct { field.RelationField }{ - RelationField: field.NewRelation("Artifact.ArtifactIndexes", "models.Artifact"), + RelationField: field.NewRelation("Artifact.ArtifactSubs", "models.Artifact"), }, Blobs: struct { field.RelationField @@ -407,7 +407,7 @@ type tagBelongsToArtifact struct { field.RelationField } } - ArtifactIndexes struct { + ArtifactSubs struct { field.RelationField } Blobs struct { diff --git a/pkg/handlers/distribution/manifest/manifest_put.go b/pkg/handlers/distribution/manifest/manifest_put.go index d6878491..de5c0d12 100644 --- a/pkg/handlers/distribution/manifest/manifest_put.go +++ b/pkg/handlers/distribution/manifest/manifest_put.go @@ -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) diff --git a/pkg/handlers/tags/tags_get.go b/pkg/handlers/tags/tags_get.go index 014161e9..417e5593 100644 --- a/pkg/handlers/tags/tags_get.go +++ b/pkg/handlers/tags/tags_get.go @@ -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, diff --git a/pkg/handlers/tags/tags_list.go b/pkg/handlers/tags/tags_list.go index 05732ed3..8ecfe1f8 100644 --- a/pkg/handlers/tags/tags_list.go +++ b/pkg/handlers/tags/tags_list.go @@ -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,