Skip to content

Commit 32a66cb

Browse files
authored
Merge pull request #12 from jhoward-lm/unique-index-fixes
Fix unique index definitions
2 parents 61d03bc + 78130c1 commit 32a66cb

29 files changed

+606
-872
lines changed

Makefile

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
# SPDX-FileType: SOURCE
44
# SPDX-License-Identifier: Apache-2.0
55
# --------------------------------------------------------------
6-
BASH := ${shell type -p bash}
7-
SHELL := ${BASH}
86

97
# ANSI color escape codes
108
BOLD := \033[1m

backends/ent/backend.go

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ func (backend *Backend) CloseClient() {
7777

7878
func (backend *Backend) Debug() *Backend {
7979
backend.Options.Debug = true
80-
backend.client.Debug()
8180

8281
return backend
8382
}

backends/ent/store.go

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -13,15 +13,12 @@ import (
1313
"github.com/protobom/protobom/pkg/storage"
1414

1515
"github.com/protobom/storage/internal/backends/ent"
16-
"github.com/protobom/storage/internal/backends/ent/document"
1716
"github.com/protobom/storage/internal/backends/ent/documenttype"
1817
"github.com/protobom/storage/internal/backends/ent/edgetype"
1918
"github.com/protobom/storage/internal/backends/ent/externalreference"
2019
"github.com/protobom/storage/internal/backends/ent/hashesentry"
2120
"github.com/protobom/storage/internal/backends/ent/identifiersentry"
22-
"github.com/protobom/storage/internal/backends/ent/metadata"
2321
"github.com/protobom/storage/internal/backends/ent/node"
24-
"github.com/protobom/storage/internal/backends/ent/nodelist"
2522
"github.com/protobom/storage/internal/backends/ent/purpose"
2623
)
2724

@@ -49,40 +46,23 @@ func (backend *Backend) Store(doc *sbom.Document, opts *storage.StoreOptions) er
4946
return fmt.Errorf("%w", errInvalidEntOptions)
5047
}
5148

52-
if err := backend.StoreMetadata(doc.Metadata); err != nil {
53-
return err
54-
}
55-
56-
if err := backend.StoreNodeList(doc.NodeList); err != nil {
57-
return err
58-
}
59-
60-
nodeListID, ok := backend.ctx.Value(nodeListIDKey{}).(int)
61-
if !ok {
62-
var err error
63-
64-
nodeListID, err = backend.client.NodeList.Query().
65-
Where(
66-
nodelist.Or(
67-
nodelist.HasDocumentWith(document.HasMetadataWith(metadata.IDEQ(doc.Metadata.Id))),
68-
nodelist.Not(nodelist.HasDocument()),
69-
)).
70-
OnlyID(backend.ctx)
71-
if err != nil {
72-
return fmt.Errorf("querying node lists: %w", err)
73-
}
74-
}
75-
7649
err := backend.client.Document.Create().
77-
SetMetadataID(doc.Metadata.Id).
78-
SetNodeListID(nodeListID).
50+
SetID(doc.Metadata.Id).
7951
OnConflict().
8052
Ignore().
8153
Exec(backend.ctx)
8254
if err != nil && !ent.IsConstraintError(err) {
8355
return fmt.Errorf("ent.Document: %w", err)
8456
}
8557

58+
if err := backend.StoreMetadata(doc.Metadata); err != nil {
59+
return err
60+
}
61+
62+
if err := backend.StoreNodeList(doc.NodeList); err != nil {
63+
return err
64+
}
65+
8666
return nil
8767
}
8868

@@ -234,6 +214,7 @@ func (backend *Backend) StoreMetadata(md *sbom.Metadata) error {
234214

235215
newMetadata := backend.client.Metadata.Create().
236216
SetID(md.Id).
217+
SetDocumentID(md.Id).
237218
SetVersion(md.Version).
238219
SetName(md.Name).
239220
SetComment(md.Comment).
@@ -269,6 +250,10 @@ func (backend *Backend) StoreNodeList(nodeList *sbom.NodeList) error {
269250
newNodeList := backend.client.NodeList.Create().
270251
SetRootElements(nodeList.RootElements)
271252

253+
if documentID, ok := backend.ctx.Value(metadataIDKey{}).(string); ok {
254+
newNodeList.SetDocumentID(documentID)
255+
}
256+
272257
id, err := newNodeList.OnConflict().Ignore().ID(backend.ctx)
273258
if err != nil && !ent.IsConstraintError(err) {
274259
return fmt.Errorf("ent.NodeList: %w", err)

internal/backends/ent/client.go

Lines changed: 8 additions & 8 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/backends/ent/document.go

Lines changed: 6 additions & 29 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

internal/backends/ent/document/document.go

Lines changed: 6 additions & 22 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)