-
Notifications
You must be signed in to change notification settings - Fork 561
Refactor Postgres migrator #6573
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
base: main
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -53,3 +53,13 @@ You can then use the following commands to apply/revert your postgresql migratio | |
| sqlx migrate run --database-url postgres://quickwit-dev:quickwit-dev@localhost:5432/quickwit-metastore-dev --source migrations/postgresql | ||
| sqlx migrate revert --database-url postgres://quickwit-dev:quickwit-dev@localhost:5432/quickwit-metastore-dev --source migrations/postgresql | ||
| ``` | ||
|
|
||
| ## Deferred migrations | ||
|
|
||
| `migrations/postgresql_deferred` holds long-running, degrade-gracefully migrations (e.g. `CREATE INDEX CONCURRENTLY`). | ||
| They run in a background task after readiness, elected across pods by a Postgres advisory lock, and share the | ||
| `_sqlx_migrations` table with the required track, so version numbers must be globally unique across both dirs and each | ||
| migration must be idempotent. See `migrations/postgresql_deferred/README.md` for authoring rules. | ||
| ``` | ||
| sqlx migrate run --database-url postgres://quickwit-dev:quickwit-dev@localhost:5432/quickwit-metastore-dev --source migrations/postgresql_deferred | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
Because the deferred directory shares Useful? React with 👍 / 👎. |
||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| # Deferred PostgreSQL migrations | ||
|
nadav-govari marked this conversation as resolved.
|
||
|
|
||
| Long, degrade-gracefully-only migrations (e.g. `CREATE INDEX CONCURRENTLY`) run in a background task after readiness. Both tracks share the `_sqlx_migrations` table. | ||
|
|
||
| - Version must be globally unique across both dirs (continue the single sequence). | ||
| - Must be idempotent. A statement that can't run in a transaction (e.g. `CREATE INDEX CONCURRENTLY`) needs `-- no-transaction` as the first line, then the DDL. Each statement auto-commits, so a killed migration must be safe to re-run (`CREATE INDEX CONCURRENTLY` can leave an invalid index, so drop it first). For example: | ||
|
|
||
| 29_add_foo_index.up.sql | ||
| ```sql | ||
| -- no-transaction | ||
| DROP INDEX CONCURRENTLY IF EXISTS foo_idx; | ||
| CREATE INDEX CONCURRENTLY IF NOT EXISTS foo_idx ON foo (bar); | ||
|
Comment on lines
+11
to
+12
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This example still puts Useful? React with 👍 / 👎. |
||
| ``` | ||
|
|
||
| 29_add_foo_index.down.sql | ||
| ```sql | ||
| -- no-transaction | ||
| DROP INDEX CONCURRENTLY IF EXISTS foo_idx; | ||
| ``` | ||
|
|
||
| The `DROP` before the `CREATE` looks like it would drop the index on every run, but it does not: sqlx applies each migration at most once (tracked by version in `_sqlx_migrations`) and never re-runs one that already succeeded. The drop only matters on a retry after a partial failure. If a `CREATE INDEX CONCURRENTLY` is killed midway, Postgres leaves an *invalid* index and sqlx records nothing (the bookkeeping row is written only on success), so the next attempt re-runs the migration. A bare `CREATE INDEX CONCURRENTLY IF NOT EXISTS` would then see that invalid index and skip it forever, so we drop it first to force a clean rebuild. | ||
| - Never depend on an unshipped required migration; required migrations must never depend on a deferred one. | ||
Uh oh!
There was an error while loading. Please reload this page.