Skip to content

Commit

Permalink
fix: address database error when running 'perseus update' against unt…
Browse files Browse the repository at this point in the history
…idy modules

Merge pull request #87 from CrowdStrike/fix/update-error-when-duplicate-deps-exist-in-go-mod
  • Loading branch information
Dylan Bourque authored Jun 2, 2023
2 parents 2ce2565 + d844a8b commit 780a6d3
Showing 1 changed file with 10 additions and 0 deletions.
10 changes: 10 additions & 0 deletions internal/store/pg.go
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,10 @@ func (p *PostgresClient) SaveModuleDependencies(ctx context.Context, mod Version
cmd := psql.
Insert("module_dependency").
Columns("dependent_id", "dependee_id")
// it's possible for a given dependency to appear in a module's go.mod more than once if it hasn't
// been 'go mod tidy'-ed, so we skip any duplicates here to avoid updating the same row in the
// database multiple times in a single command
uniqueDeps := map[string]struct{}{}
for _, d := range deps {
p.log("saving dependency", "moduleName", d.ModuleID, "version", d.SemVer)
pkey, err := writeModule(ctx, txn, d.ModuleID, "")
Expand All @@ -147,7 +151,13 @@ func (p *PostgresClient) SaveModuleDependencies(ctx context.Context, mod Version
if err != nil {
return err
}
k := fmt.Sprintf("%d-%d", versionIDs[0], vids[0])
if _, found := uniqueDeps[k]; found {
p.log("skipping duplicate dependency", "dependency", d.ModuleID+"@"+d.SemVer)
continue
}
cmd = cmd.Values(versionIDs[0], vids[0])
uniqueDeps[k] = struct{}{}
}
sql, args, err := cmd.Suffix("ON CONFLICT (dependent_id, dependee_id) DO UPDATE SET dependent_id = EXCLUDED.dependent_id").ToSql()
p.log("upsert module dependencies", "sql", sql, "args", args, "err", err)
Expand Down

0 comments on commit 780a6d3

Please sign in to comment.