Skip to content

Commit 5de5c9b

Browse files
committed
datastore: add id column to package_scanartifact
Signed-off-by: RTann <[email protected]> rh-pre-commit.version: 2.3.2 rh-pre-commit.check-secrets: ENABLED
1 parent 349a50e commit 5de5c9b

File tree

3 files changed

+56
-5
lines changed

3 files changed

+56
-5
lines changed
Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
-- The package table only save one copy for each unique name+version of a package (and a few more fields).
2+
-- It is possible an image has several packages with the same name+version (and the other fields)
3+
-- in different locations. Without this migration, the IndexReport only tracks one of those packages
4+
-- instead of a package for each unique location.
5+
ALTER TABLE IF EXISTS package_scanartifact ADD COLUMN id BIGSERIAL;

datastore/postgres/migrations/migrations.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,10 @@ var IndexerMigrations = []migrate.Migration{
6565
ID: 9,
6666
Up: runFile("indexer/09-delete-scannerlist.sql"),
6767
},
68+
{
69+
ID: 10,
70+
Up: runFile("indexer/10-package_scanartifact-id.sql"),
71+
},
6872
}
6973

7074
var MatcherMigrations = []migrate.Migration{

datastore/postgres/migrations/migrations_test.go

Lines changed: 47 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -2,22 +2,65 @@ package migrations
22

33
import (
44
"bufio"
5+
"fmt"
56
iofs "io/fs"
67
"os"
7-
"path"
88
"path/filepath"
99
"regexp"
1010
"slices"
1111
"testing"
1212

1313
"github.com/google/go-cmp/cmp"
14+
"github.com/remind101/migrate"
1415
)
1516

17+
func TestBasicIndexerMigrations(t *testing.T) {
18+
testMigrations(t, "indexer", IndexerMigrations)
19+
}
20+
21+
func TestBasicMatchMigrations(t *testing.T) {
22+
testMigrations(t, "matcher", MatcherMigrations)
23+
}
24+
25+
func testMigrations(t *testing.T, root string, migrations []migrate.Migration) {
26+
var fileMigrations []string
27+
err := iofs.WalkDir(fs, root, func(path string, d iofs.DirEntry, err error) error {
28+
if err != nil {
29+
return err
30+
}
31+
if d.Name() == root {
32+
return nil
33+
}
34+
if !d.Type().IsRegular() {
35+
return fmt.Errorf("%s is not a regular file", path)
36+
}
37+
if filepath.Ext(d.Name()) != ".sql" {
38+
return fmt.Errorf("%s is not a .sql file", path)
39+
}
40+
41+
fileMigrations = append(fileMigrations, path)
42+
return nil
43+
})
44+
if err != nil {
45+
t.Fatal(err)
46+
}
47+
48+
if len(fileMigrations) != len(migrations) {
49+
t.Error(cmp.Diff(len(fileMigrations), len(migrations)))
50+
}
51+
52+
for i, m := range migrations {
53+
if m.ID != i+1 {
54+
t.Error(cmp.Diff(m.ID, i+1))
55+
}
56+
}
57+
}
58+
1659
func TestMigrationsMismatch(t *testing.T) {
1760
var migrations, files []string
1861

1962
// Get referenced migrations
20-
migrationLine, err := regexp.Compile(`runFile\(\"(.*)\"\)`)
63+
migrationLine, err := regexp.Compile(`runFile\("(.*)"\)`)
2164
if err != nil {
2265
t.Fatal(err)
2366
}
@@ -34,16 +77,15 @@ func TestMigrationsMismatch(t *testing.T) {
3477
case ms == nil, len(ms) == 1:
3578
continue
3679
case len(ms) == 2:
37-
migrations = append(migrations, path.Clean(string(ms[1])))
80+
migrations = append(migrations, filepath.Clean(string(ms[1])))
3881
}
3982
}
4083
if err := s.Err(); err != nil {
4184
t.Error(err)
4285
}
4386
slices.Sort(migrations)
44-
4587
// Get migration files
46-
err = iofs.WalkDir(os.DirFS("."), ".", func(p string, d iofs.DirEntry, err error) error {
88+
err = iofs.WalkDir(fs, ".", func(p string, d iofs.DirEntry, err error) error {
4789
switch {
4890
case err != nil:
4991
return err

0 commit comments

Comments
 (0)