-
Notifications
You must be signed in to change notification settings - Fork 887
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
Conversation
cli/resetpassword.go
Outdated
|
||
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) |
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.
We should really dedupe this code and flags.
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.
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 👍🏻
026a875
to
5646ab6
Compare
cli/server.go
Outdated
// 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) { |
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.
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)
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 passing the migration function here, we could always run ensure clean whether or not up is run as well to further simplify usage.
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 passed in the migration function and always run 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.
@Emyrk did you push the change? Not seeing it yet. 🤔
cli/server_test.go
Outdated
sqlDB, err := cli.ConnectToPostgres(ctx, log, "postgres", dbURL) | ||
sqlDB, err := cli.ConnectToPostgres(ctx, log, true, "postgres", dbURL) |
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 we do the control flag approach, we should add a separate test here to ensure that no migrations are run if migrate=false
.
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.
OK to merge once linter+tests are happy 👍
6957027
to
9c74819
Compare
Error when running future version of date coder cli to the coderd version
Note
It is unfortunate we have to add these options manually on each command that uses a database.
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