Thanks to visit codestin.com
Credit goes to github.com

Skip to content

chore: improve error when db migration fails #15969

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

Closed
wants to merge 1 commit into from
Closed
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
4 changes: 4 additions & 0 deletions cli/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -2157,6 +2157,10 @@ func ConnectToPostgres(ctx context.Context, logger slog.Logger, driver string, d

err = migrations.Up(sqlDB)
if err != nil {
// Checks for database schema version mismatch
if errors.Is(err, os.ErrNotExist) && strings.Contains(err.Error(), "no migration found for version") {
Copy link
Contributor Author

Choose a reason for hiding this comment

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

golang-migrate does not provide a better way to handle these errors, it returns the error message as a string as seen here

https://github.com/golang-migrate/migrate/blob/c378583d782e026f472dff657bfd088bf2510038/migrate.go#L809

Copy link
Member

Choose a reason for hiding this comment

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

What about migrations.CheckLatestVersion? Would this not accomplish something similar?

Copy link
Member

Choose a reason for hiding this comment

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

I like @johnstcn's suggestion of checking the version first before trying to run the migrations over the string matching which could break. 👍

Copy link
Contributor Author

@joobisb joobisb Jan 2, 2025

Choose a reason for hiding this comment

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

Suggested change
if errors.Is(err, os.ErrNotExist) && strings.Contains(err.Error(), "no migration found for version") {
if errors.Is(err, os.ErrNotExist) {
if cleanErr := migrations.EnsureClean(sqlDB); cleanErr != nil {
return nil, xerrors.New("Current database schema requires a newer version of Coder!")
}
}

@mafredri @johnstcn I could do something like this. This check cannot precede migrations.Up(sqlDB) cause we want to run the migrations if the executable is the latest and the migrations have not yet been executed.

wdyt?

PS: this would throw Current database schema requires a newer version of Coder! irrespective of error coming from EnsureClean or we should provide a custom error for CheckLatestVersion inside EnsureClean

Copy link
Member

Choose a reason for hiding this comment

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

heads-up: we're making some parallel changes here #15980

return nil, xerrors.New("Current database schema requires a newer version of Coder!")
}
return nil, xerrors.Errorf("migrate up: %w", err)
}
// The default is 0 but the request will fail with a 500 if the DB
Expand Down
Loading