-
Notifications
You must be signed in to change notification settings - Fork 887
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
Conversation
@@ -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") { |
There was a problem hiding this comment.
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
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
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. 👍
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
There was a problem hiding this comment.
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
Closes #14248
When older coder executables encounter database schemas from newer builds that have newer migrations, they produce a somewhat inscrutable error that doesn't provide the user a meaningful call to action.
Current:
Proposal:
@angrycub @johnstcn
I did see a script here mapping releases and migrations.
But I'm not sure how to have the version to upgrade to be specified in the error message. So I have changed the error message to
Current database schema requires a newer version of Coder!
. Please do let me know if I'm missing anything.