Skip to content

update tables #1515

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 3 commits into
base: main
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- This table is not used, so no need to keep it around.
DROP TABLE IF EXISTS scannerlist CASCADE;
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
-- The package table only save one copy for each unique name+version of a package (and a few more fields).
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ignore this comment for now

-- It is possible an image has several packages with the same name+version (and the other fields)
-- in different locations. Without this migration, the IndexReport only tracks one of those packages
-- instead of a package for each unique location.
ALTER TABLE IF EXISTS package_scanartifact ADD COLUMN id BIGSERIAL;
8 changes: 8 additions & 0 deletions datastore/postgres/migrations/migrations.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,14 @@ var IndexerMigrations = []migrate.Migration{
ID: 8,
Up: runFile("indexer/08-index-manifest_layer.sql"),
},
{
ID: 9,
Up: runFile("indexer/09-delete-scannerlist.sql"),
},
{
ID: 10,
Up: runFile("indexer/10-package_scanartifact-id.sql"),
},
}

var MatcherMigrations = []migrate.Migration{
Expand Down
52 changes: 47 additions & 5 deletions datastore/postgres/migrations/migrations_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,22 +2,65 @@ package migrations

import (
"bufio"
"fmt"
iofs "io/fs"
"os"
"path"
"path/filepath"
"regexp"
"slices"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/remind101/migrate"
)

func TestBasicIndexerMigrations(t *testing.T) {
testMigrations(t, "indexer", IndexerMigrations)
}

func TestBasicMatchMigrations(t *testing.T) {
testMigrations(t, "matcher", MatcherMigrations)
}

func testMigrations(t *testing.T, root string, migrations []migrate.Migration) {
var fileMigrations []string
err := iofs.WalkDir(fs, root, func(path string, d iofs.DirEntry, err error) error {
if err != nil {
return err
}
if d.Name() == root {
return nil
}
if !d.Type().IsRegular() {
return fmt.Errorf("%s is not a regular file", path)
}
if filepath.Ext(d.Name()) != ".sql" {
return fmt.Errorf("%s is not a .sql file", path)
}

fileMigrations = append(fileMigrations, path)
return nil
})
if err != nil {
t.Fatal(err)
}

if len(fileMigrations) != len(migrations) {
t.Error(cmp.Diff(len(fileMigrations), len(migrations)))
}

for i, m := range migrations {
if m.ID != i+1 {
t.Error(cmp.Diff(m.ID, i+1))
}
}
}

func TestMigrationsMismatch(t *testing.T) {
var migrations, files []string

// Get referenced migrations
migrationLine, err := regexp.Compile(`runFile\(\"(.*)\"\)`)
migrationLine, err := regexp.Compile(`runFile\("(.*)"\)`)
if err != nil {
t.Fatal(err)
}
Expand All @@ -34,16 +77,15 @@ func TestMigrationsMismatch(t *testing.T) {
case ms == nil, len(ms) == 1:
continue
case len(ms) == 2:
migrations = append(migrations, path.Clean(string(ms[1])))
migrations = append(migrations, filepath.Clean(string(ms[1])))
}
}
if err := s.Err(); err != nil {
t.Error(err)
}
slices.Sort(migrations)

// Get migration files
err = iofs.WalkDir(os.DirFS("."), ".", func(p string, d iofs.DirEntry, err error) error {
err = iofs.WalkDir(fs, ".", func(p string, d iofs.DirEntry, err error) error {
switch {
case err != nil:
return err
Expand Down
4 changes: 3 additions & 1 deletion datastore/postgres/packagesbylayer.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,9 +48,11 @@ FROM
WHERE
name = $1 AND version = $2 AND kind = $3;
`
// TODO: It'd be nice to just use package_scanartifact.id, but what to do about the source_package ID?
// Just use the source package ID for now I guess?
query = `
SELECT
package.id,
package_scanartifact.id,
package.name,
package.kind,
package.version,
Expand Down
27 changes: 0 additions & 27 deletions test/postgres/scannerlist.go

This file was deleted.

Loading