Skip to content

Commit e816c4f

Browse files
committed
migrations: add missing index creating migration
While the sql command was added to claircore (and the admin command was added to clairctl) to create an index it was never actually reference in the migrations. This should be a no-op for all instances that ran the associated clairctl admin command. Also, added a test to try and avoid this in the future. Signed-off-by: crozzy <[email protected]>
1 parent 6bbe356 commit e816c4f

File tree

2 files changed

+72
-0
lines changed

2 files changed

+72
-0
lines changed

datastore/postgres/migrations/migrations.go

+4
Original file line numberDiff line numberDiff line change
@@ -57,6 +57,10 @@ var IndexerMigrations = []migrate.Migration{
5757
ID: 7,
5858
Up: runFile("indexer/07-index-manifest_index.sql"),
5959
},
60+
{
61+
ID: 8,
62+
Up: runFile("indexer/08-index-manifest_layer.sql"),
63+
},
6064
}
6165

6266
var MatcherMigrations = []migrate.Migration{
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
package migrations
2+
3+
import (
4+
"bufio"
5+
iofs "io/fs"
6+
"os"
7+
"path"
8+
"path/filepath"
9+
"regexp"
10+
"slices"
11+
"testing"
12+
13+
"github.com/google/go-cmp/cmp"
14+
)
15+
16+
func TestMigrationsMismatch(t *testing.T) {
17+
var migrations, files []string
18+
19+
// Get referenced migrations
20+
migrationLine, err := regexp.Compile(`runFile\(\"(.*)\"\)`)
21+
if err != nil {
22+
t.Fatal(err)
23+
}
24+
f, err := os.Open("migrations.go")
25+
if err != nil {
26+
t.Fatal(err)
27+
}
28+
defer f.Close()
29+
30+
s := bufio.NewScanner(f)
31+
for s.Scan() {
32+
ms := migrationLine.FindSubmatch(s.Bytes())
33+
switch {
34+
case ms == nil, len(ms) == 1:
35+
continue
36+
case len(ms) == 2:
37+
migrations = append(migrations, path.Clean(string(ms[1])))
38+
}
39+
}
40+
if err := s.Err(); err != nil {
41+
t.Error(err)
42+
}
43+
slices.Sort(migrations)
44+
45+
// Get migration files
46+
err = iofs.WalkDir(os.DirFS("."), ".", func(p string, d iofs.DirEntry, err error) error {
47+
switch {
48+
case err != nil:
49+
return err
50+
case d.IsDir():
51+
return nil
52+
case filepath.Ext(d.Name()) != ".sql":
53+
return nil
54+
}
55+
files = append(files, p)
56+
return nil
57+
})
58+
if err != nil {
59+
t.Error(err)
60+
}
61+
slices.Sort(files)
62+
63+
// Check referenced files exist and existing files are referenced.
64+
if !cmp.Equal(migrations, files) {
65+
t.Log("error mismatch of migrations to entries:")
66+
t.Error(cmp.Diff(migrations, files))
67+
}
68+
}

0 commit comments

Comments
 (0)