Skip to content
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

migrations: add missing index creating migration #1460

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

crozzy
Copy link
Contributor

@crozzy crozzy commented Jan 3, 2025

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.

@crozzy crozzy requested a review from a team as a code owner January 3, 2025 19:44
@crozzy crozzy requested review from RTann and hdonnay and removed request for a team and RTann January 3, 2025 19:44
case ms == nil, len(ms) == 1:
continue
case len(ms) == 2:
migrations = append(migrations, path.Clean(string(ms[1])))
Copy link
Contributor

Choose a reason for hiding this comment

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

filepath? I guess it is always /, though

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I will change it but yeah

sort.Strings(files)

// Check referenced files exist and existing files are referenced.
if !cmp.Equal(migrations, files) {
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe this will fail on Windows machines due to the \? If so, is that ok?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

We don't support running on Windows so I think it's ok

var migrations, files []string

// Get referenced migrations
migrationLine, err := regexp.Compile(`runFile\(\"(.*)\"\)`)
Copy link
Contributor

Choose a reason for hiding this comment

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

This feels brittle. An idea can be to create a base slice like

const indexerMigrations = []string{
	"filename1",
	"filename2",
	...
}

and then have

func init() {
	for i, migration := range indexerMigrations {
		IndexerMigrations = append(IndexerMigrations, {
			ID: i+1,
			Up: runFile(migration),
		})
	}
}

something like that so we can just ensure indexerMigrations lists all the files

Copy link
Contributor Author

@crozzy crozzy Jan 6, 2025

Choose a reason for hiding this comment

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

I think one thing we'd want to keep is the ID being explicitly associated with the migration file so a user can check their DB migration version and know which was the last file being run (without having to work it out).

It is brittle in the sense that the runFile might change, but the proposed solution doesn't address fact that a sql file might exist on the FS but not be referenced in the go code, maybe this isn't actually an issue if you aren't forgetful (like myself) and remember to reference the migration. The reason this case was slightly different was that there was an admin command added to clairctl to pre-create the index so when testing the code in stage and production the index was already created and hence, it didn't look like anything was missing.

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]>
@crozzy crozzy force-pushed the bug-add-migration branch from 31f43b0 to e816c4f Compare January 6, 2025 21:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants