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

Skip to content

chore: prevent db migrations from running on all cli commands #15980

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

Merged
merged 10 commits into from
Jan 3, 2025

Conversation

Emyrk
Copy link
Member

@Emyrk Emyrk commented Dec 30, 2024

Error when running future version of date coder cli to the coderd version

 $ coder server create-admin-user --postgres-url="postgres://postgres:postgres@localhost:5432/postgres?sslmode=disable"
2024-12-30 21:25:24.436 [erro]  connect to postgres failed ...
    error= migrate up:
               github.com/coder/coder/v2/cli.ConnectToPostgres
                   /home/steven/go/src/github.com/coder/coder/cli/server.go:2169
             - database needs migration:
               github.com/coder/coder/v2/coderd/database/migrations.EnsureClean
                   /home/steven/go/src/github.com/coder/coder/coderd/database/migrations/migrate.go:185
             - current version is 272, but later version 273 exists:
               github.com/coder/coder/v2/coderd/database/migrations.CheckLatestVersion
                   /home/steven/go/src/github.com/coder/coder/coderd/database/migrations/migrate.go:200
Encountered an error running "coder server create-admin-user", see "coder server create-admin-user --help" for more information
error: connect to postgres: migrate up: database needs migration: current version is 272, but later version 273 exists

Note

It is unfortunate we have to add these options manually on each command that uses a database.

	root.Options = serpent.OptionSet{
		{
			Flag:        "postgres-url",
			Description: "URL of a PostgreSQL database to connect to.",
			Env:         "CODER_PG_CONNECTION_URL",
			Value:       serpent.StringOf(&postgresURL),
		},
		serpent.Option{
			Name:        "Postgres Connection Auth",
			Description: "Type of auth to use when connecting to postgres.",
			Flag:        "postgres-connection-auth",
			Env:         "CODER_PG_CONNECTION_AUTH",
			Default:     "password",
			Value:       serpent.EnumOf(&postgresAuth, codersdk.PostgresAuthDrivers...),
		},
	}

I would rather see this all implemented in some standard way for the cli. From the flags to the opening of the database, to the migrations.

Closes #15976

Comment on lines 39 to 49

sqlDriver := "postgres"
if codersdk.PostgresAuth(postgresAuth) == codersdk.PostgresAuthAWSIAMRDS {
var err error
sqlDriver, err = awsiamrds.Register(inv.Context(), sqlDriver)
if err != nil {
return xerrors.Errorf("register aws rds iam auth: %w", err)
}
}

err = migrations.EnsureClean(sqlDB)
sqlDB, err := ConnectToPostgres(inv.Context(), logger, false, sqlDriver, postgresURL)
Copy link
Member Author

Choose a reason for hiding this comment

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

We should really dedupe this code and flags.

Copy link
Member

Choose a reason for hiding this comment

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

By my count we call it 6 times not including tests, I'd be fine with not deduping it for the time being but up to you 👍🏻

@Emyrk Emyrk marked this pull request as ready for review December 31, 2024 00:09
@Emyrk Emyrk force-pushed the stevenmasley/no_migrations_cli branch from 026a875 to 5646ab6 Compare January 2, 2025 14:05
@Emyrk Emyrk changed the title chore: do not run db migrations on all cli commands chore: prevent db migrations from running on all cli commands Jan 2, 2025
@Emyrk Emyrk requested a review from johnstcn January 2, 2025 20:59
cli/server.go Outdated
Comment on lines 2093 to 2099
// ConnectToPostgres takes a control flag "migrate". If true, `migrations.Up` will be applied
// to the database, potentially making schema changes.
// If set to false, no database changes will be applied, however the migration version
// will be checked. If the database is not fully up to date with its migrations, then
// an error will be returned.
// nolint:revive // 'migrate' is a control flag.
func ConnectToPostgres(ctx context.Context, logger slog.Logger, migrate bool, driver string, dbURL string) (sqlDB *sql.DB, err error) {
Copy link
Member

Choose a reason for hiding this comment

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

Instead of having a boolean control flag, how about ...func(*sql.DB) error?

This would then become e.g.

  ConnectToPostgres(ctx, logger, driver, dbURL, migrations.EnsureClean, migrations.Up)

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 passing the migration function here, we could always run ensure clean whether or not up is run as well to further simplify usage.

Copy link
Member Author

Choose a reason for hiding this comment

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

I passed in the migration function and always run EnsureClean

Copy link
Member

Choose a reason for hiding this comment

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

@Emyrk did you push the change? Not seeing it yet. 🤔

sqlDB, err := cli.ConnectToPostgres(ctx, log, "postgres", dbURL)
sqlDB, err := cli.ConnectToPostgres(ctx, log, true, "postgres", dbURL)
Copy link
Member

Choose a reason for hiding this comment

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

If we do the control flag approach, we should add a separate test here to ensure that no migrations are run if migrate=false.

Copy link
Member

@johnstcn johnstcn left a comment

Choose a reason for hiding this comment

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

OK to merge once linter+tests are happy 👍

@Emyrk Emyrk force-pushed the stevenmasley/no_migrations_cli branch from 6957027 to 9c74819 Compare January 3, 2025 15:53
@Emyrk Emyrk merged commit a7ed977 into main Jan 3, 2025
32 checks passed
@Emyrk Emyrk deleted the stevenmasley/no_migrations_cli branch January 3, 2025 17:15
@github-actions github-actions bot locked and limited conversation to collaborators Jan 3, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

CLI admin command should not run migrations
3 participants