8000 chore: prevent db migrations from running on all cli commands by Emyrk · Pull Request #15980 · coder/coder · GitHub
[go: up one dir, main page]

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
Merged
Changes from 1 commit
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
Prev Previous commit
Next Next commit
reuse ConnectToPostgres
  • Loading branch information
Emyrk committed Jan 3, 2025
commit 177d34105dacef21cd0b1b8e1b766fef1c0f7988
42 changes: 30 additions & 12 deletions cli/resetpassword.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,27 @@
package cli

import (
"database/sql"
"fmt"

"golang.org/x/xerrors"

"cdr.dev/slog"
"cdr.dev/slog/sloggers/sloghuman"
"github.com/coder/coder/v2/coderd/database/awsiamrds"
"github.com/coder/coder/v2/codersdk"
"github.com/coder/pretty"
"github.com/coder/serpent"

"github.com/coder/coder/v2/cli/cliui"
"github.com/coder/coder/v2/coderd/database"
"github.com/coder/coder/v2/coderd/database/migrations"
"github.com/coder/coder/v2/coderd/userpassword"
)

func (*RootCmd) resetPassword() *serpent.Command {
var postgresURL string
var (
postgresURL string
postgresAuth string
)

root := &serpent.Command{
Use: "reset-password <username>",
Expand All @@ -27,20 +32,25 @@ func (*RootCmd) resetPassword() *serpent.Command {
Handler: func(inv *serpent.Invocation) error {
username := inv.Args[0]

sqlDB, err := sql.Open("postgres", postgresURL)
if err != nil {
return xerrors.Errorf("dial postgres: %w", err)
logger := slog.Make(sloghuman.Sink(inv.Stdout))
if ok, _ := inv.ParsedFlags().GetBool("verbose"); ok {
logger = logger.Leveled(slog.LevelDebug)
}
defer sqlDB.Close()
err = sqlDB.Ping()
if err != nil {
return xerrors.Errorf("ping postgres: %w", err)

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 👍🏻

if err != nil {
return xerrors.Errorf("database needs migration: %w", err)
return xerrors.Errorf("dial postgres: %w", err)
}

db := database.New(sqlDB)

user, err := db.GetUserByEmailOrUsername(inv.Context(), database.GetUserByEmailOrUsernameParams{
Expand Down Expand Up @@ -97,6 +107,14 @@ func (*RootCmd) resetPassword() *serpent.Command {
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...),
},
}

return root
Expand Down
0