Skip to content

Commit

Permalink
[ENT] fix issue with index on artifact (#1835)
Browse files Browse the repository at this point in the history
* fix issue with index on artifact

Signed-off-by: pxp928 <parth.psu@gmail.com>

* add test to capture this usecase

Signed-off-by: pxp928 <parth.psu@gmail.com>

* remove digest only index in arrango

Signed-off-by: pxp928 <parth.psu@gmail.com>

---------

Signed-off-by: pxp928 <parth.psu@gmail.com>
  • Loading branch information
pxp928 committed Apr 11, 2024
1 parent d0c51f5 commit 2ec6bc9
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 13 deletions.
15 changes: 15 additions & 0 deletions internal/testing/backend/artifact_test.go
Expand Up @@ -66,6 +66,21 @@ func TestArtifacts(t *testing.T) {
Digest: "7a8f47318e4676dacb0142afa0b83029cd7befd9",
}},
wantErr: false,
}, {
name: "sha-1",
artifactInput: &model.ArtifactInputSpec{
Algorithm: "SHA-1",
Digest: "7A8F47318E4676DACB0142AFA0B83029CD7BEFD9",
},
artifactSpec: &model.ArtifactSpec{
Algorithm: ptrfrom.String("SHA-1"),
Digest: ptrfrom.String("7A8F47318E4676DACB0142AFA0B83029CD7BEFD9"),
},
want: []*model.Artifact{{
Algorithm: "sha-1",
Digest: "7a8f47318e4676dacb0142afa0b83029cd7befd9",
}},
wantErr: false,
}, {
name: "sha512",
artifactInput: &model.ArtifactInputSpec{
Expand Down
3 changes: 1 addition & 2 deletions pkg/assembler/backends/arangodb/backend.go
Expand Up @@ -64,7 +64,7 @@ func init() {

func initIndex(name string, fields []string, unique bool) index {
return index{
name: name,
name: name,
fields: fields,
unique: unique,
}
Expand Down Expand Up @@ -514,7 +514,6 @@ func getCollectionIndexMap() map[string][]index {
collectionIndexMap := make(map[string][]index)

collectionIndexMap[artifactsStr] = []index{
initIndex("byDigest", []string{"digest"}, true),
initIndex("byArtAndDigest", []string{"algorithm", "digest"}, true),
}

Expand Down
9 changes: 7 additions & 2 deletions pkg/assembler/backends/ent/backend/artifact.go
Expand Up @@ -96,6 +96,8 @@ func upsertBulkArtifact(ctx context.Context, tx *ent.Tx, artInputs []*model.IDor
batches := chunk(artInputs, MaxBatchSize)
ids := make([]string, 0)

conflictColumns := []string{artifact.FieldAlgorithm, artifact.FieldDigest}

for _, artifacts := range batches {
creates := make([]*ent.ArtifactCreate, len(artifacts))
for i, art := range artifacts {
Expand All @@ -108,7 +110,7 @@ func upsertBulkArtifact(ctx context.Context, tx *ent.Tx, artInputs []*model.IDor

err := tx.Artifact.CreateBulk(creates...).
OnConflict(
sql.ConflictColumns(artifact.FieldDigest),
sql.ConflictColumns(conflictColumns...),
).
DoNothing().
Exec(ctx)
Expand All @@ -128,11 +130,14 @@ func generateArtifactCreate(tx *ent.Tx, artifactID *uuid.UUID, art *model.IDorAr
}

func upsertArtifact(ctx context.Context, tx *ent.Tx, art *model.IDorArtifactInput) (*string, error) {

conflictColumns := []string{artifact.FieldAlgorithm, artifact.FieldDigest}

artifactID := generateUUIDKey([]byte(helpers.GetKey[*model.ArtifactInputSpec, string](art.ArtifactInput, helpers.ArtifactServerKey)))
insert := generateArtifactCreate(tx, &artifactID, art)
err := insert.
OnConflict(
sql.ConflictColumns(artifact.FieldDigest),
sql.ConflictColumns(conflictColumns...),
).
DoNothing().
Exec(ctx)
Expand Down
9 changes: 2 additions & 7 deletions pkg/assembler/backends/ent/migrate/schema.go

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

3 changes: 1 addition & 2 deletions pkg/assembler/backends/ent/schema/artifact.go
Expand Up @@ -72,7 +72,6 @@ func (Artifact) Edges() []ent.Edge {
// to query all artifacts using a specific algorithm.
func (Artifact) Indexes() []ent.Index {
return []ent.Index{
index.Fields("algorithm"),
index.Fields("digest").Unique(),
index.Fields("algorithm", "digest").Unique(),
}
}

0 comments on commit 2ec6bc9

Please sign in to comment.