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

Conversation

joobisb
Copy link
Contributor

@joobisb joobisb commented Dec 28, 2024

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:

2024-08-12 17:51:10.327 [erro]  connect to postgres failed ...
    error= migrate up:
               github.com/coder/coder/v2/cli.ConnectToPostgres
                   /home/runner/work/coder/coder/cli/server.go:2157
             - up:
               github.com/coder/coder/v2/coderd/database/migrations.UpWithFS
                   /home/runner/work/coder/coder/coderd/database/migrations/migrate.go:82
             - no migration found for version 237: read down for version 237 .: file does not exist

Proposal:

2024-08-12 17:51:10.327 [erro]  connect to postgres failed ...
    error= Current database requires Coder version 2.14.0 or greater

@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.

@cdr-bot cdr-bot bot added the community Pull Requests and issues created by the community. label Dec 28, 2024
@@ -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

@johnstcn johnstcn requested review from mafredri and johnstcn January 2, 2025 10:24
@github-actions github-actions bot added the stale This issue is like stale bread. label Jan 11, 2025
@github-actions github-actions bot closed this Jan 15, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
community Pull Requests and issues created by the community. stale This issue is like stale bread.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Improve error when DB migrations fail
3 participants