Repository navigation
feat: allow migrating postgres via a kubernetes job - #3594
Conversation
📝 WalkthroughWalkthroughThis PR adds a "migration-job" AutoMigrate mode, a CLI migrate subcommand, and a migration-wait mechanism: configuration accepts the new mode, the app can call WaitForMigrationJob to poll migration completion, and the CLI exposes a migrate command to trigger or validate migration modes. Changes
Sequence DiagramsequenceDiagram
participant CLI as CLI Command
participant Config as Config
participant Migrator as Migrator
participant DB as Database
participant WaitFn as WaitForMigration
Note over CLI: User runs migrate command with mode flag
CLI->>Config: Validate migration mode
Config-->>CLI: Mode valid or error
CLI->>Migrator: Set Migrator.Config.AutoMigrate(mode)
Migrator->>DB: Migrator.Migrate(ctx)
alt mode == "migration-job"
DB->>WaitFn: WaitForMigrationJob(dbURL, logger)
loop retry
WaitFn->>DB: Query current migration version
DB-->>WaitFn: current version / dirty flag
WaitFn->>WaitFn: Compare with LatestVersion()
alt match
WaitFn-->>DB: return success
else dirty or mismatch
WaitFn->>WaitFn: sleep & retry
end
end
else other modes
DB->>DB: Apply migrations directly
end
DB-->>Migrator: result
Migrator-->>CLI: success or error
CLI->>CLI: log outcome
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
|
Important Review skippedDraft detected. Please check the settings in the CodeRabbit UI or the You can disable this status message by setting the ✨ Finishing touches🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
37db5c7 to
ad7cfc8
Compare
There was a problem hiding this comment.
Actionable comments posted: 3
Caution
Some comments are outside the diff and can’t be posted inline due to platform limitations.
⚠️ Outside diff range comments (1)
app/config/postgres.go (1)
20-25: Document the newmigration-joboptionThe inline docs still list only
false,ent, andmigration. Folks reading this comment won’t discover the newmigration-jobmode, which makes it harder to configure correctly.A quick tweak keeps the comment up to date:
-// - "migration" to use the migrations directory +// - "migration" to use the migrations directory +// - "migration-job" to wait for an external migration job to finish provisioning the schema
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (5)
app/common/database.go(1 hunks)app/config/postgres.go(2 hunks)cmd/jobs/main.go(2 hunks)cmd/jobs/migrate/migrate.go(1 hunks)tools/migrate/migrate.go(4 hunks)
🧰 Additional context used
📓 Path-based instructions (1)
**/*.go
⚙️ CodeRabbit configuration file
**/*.go: In general when reviewing the Golang code make readability and maintainability a priority, even potentially suggest restructuring the code to improve them.Performance should be a priority in critical code paths. Anything related to event ingestion, message processing, database operations (regardless of database) should be vetted for potential performance bottlenecks.
Files:
app/common/database.gocmd/jobs/main.goapp/config/postgres.gocmd/jobs/migrate/migrate.gotools/migrate/migrate.go
🧬 Code graph analysis (3)
app/common/database.go (2)
app/config/postgres.go (1)
AutoMigrateMigrationJob(71-71)tools/migrate/migrate.go (1)
WaitForMigrationJob(142-188)
cmd/jobs/main.go (1)
cmd/jobs/migrate/migrate.go (1)
RootCommand(12-43)
cmd/jobs/migrate/migrate.go (2)
app/config/postgres.go (3)
AutoMigrate(66-66)AutoMigrateMigrationJob(71-71)AutoMigrateMigration(70-70)app/common/database.go (1)
Migrator(31-35)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (7)
- GitHub Check: Quickstart
- GitHub Check: E2E
- GitHub Check: Code Generators
- GitHub Check: Build
- GitHub Check: Lint
- GitHub Check: Test
- GitHub Check: Analyze (go)
GAlexIHU
left a comment
There was a problem hiding this comment.
I don't really like the >= N check as the default (and only) behavior... to solve those "older version not getting stuck with newer db if we fail" scenarios you called out
- we'd be currently failing on those and as bad as they sound i'm not sure we need them based on the operation experience of the past year
- if we really want to solve them i think we should do that on the deploy level (e.g. make app somehow aware it's "restarting" and not "starting"). ik that sounds like a hack, but assuming the app will run with the wrong db version is already a hack
otherwise if you need this urgently i'm ok with this being merged
| return err | ||
| } | ||
|
|
||
| err = retry.Do( |
There was a problem hiding this comment.
nit: do we need a lib for this?
| ) | ||
|
|
||
| if ver >= currentBinaryTargetVersion { | ||
| // We are at least on the version that is required by the binary => good to go |
There was a problem hiding this comment.
I know leniency in version enforcement is comfortable, but there are reasons app version N wouldn't work with db version N+1 :D
I think it's fine if the migration tooling has a method that polls for version >= X, but at least in the app startup we should have more control... still the related issues mostly come about in relation to lack of easy rollbacks and dirty states....
| return fmt.Errorf("failed to migrate db: %w", err) | ||
| } | ||
| case config.AutoMigrateMigrationJob: | ||
| if err := migrate.WaitForMigrationJob(m.Config.AsURL(), m.Logger); err != nil { |
There was a problem hiding this comment.
nit: if we want a scoped logger let's add a constructor to migrate instead
| rootCmd.AddCommand(entitlement.RootCommand()) | ||
| rootCmd.AddCommand(billing.Cmd) | ||
| rootCmd.AddCommand(quickstart.Cmd) | ||
| rootCmd.AddCommand(migrate.RootCommand()) |
There was a problem hiding this comment.
FYI, we have a very basic migrate job in the cloud repo for the Tilt migration setup, we should consolidate that too to (though its not exactly the same thing as this)
Overview
This change introduces the support to migrate database using an external job, which is more reliable for large scale deployments than the current implementation.
To do so, the backend services can have their autoMigrate set to
migration-job. In this mode the service waits until the database schema's version becomes >= than the currently embedded latest migration.This approach allows for running older versions of backend against newer version of the database (e.g. blue/green deployment scenarios).
The polling of the database timeouts after
30s. This is enough as the readiness probe of the services will restart the container faster either ways.Notes for reviewer
Summary by CodeRabbit