Skip to content

Commit 31f43b0

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 31f43b0

File tree

2 files changed

+71
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)