Migrations¶
Forward-only SQL migration runner.
migrate ¶
Migration runner.
Forward-only by design. The runner:
- Discovers
*.sqlfiles under :mod:taskq.migrationsin lexicographic order — the naming convention is{ver}_{nn}_{pre|post}_{description}.sql. - Substitutes the
{schema}placeholder with the configured schema name. - Applies migrations not already recorded in
{schema}.schema_migrations, recording a SHA-256 checksum of the rendered SQL after each successful apply.
There is no down operation. To revert, restore from a database backup.
__all__
module-attribute
¶
__all__ = [
"Migration",
"Phase",
"apply_pending",
"apply_pending_locked",
"discover",
"list_applied",
"render",
]
discover ¶
Return all bundled migrations sorted by version, then pre before post.
Source code in src/taskq/migrate.py
render ¶
Substitute {schema} in a SQL template.
SQL files escape literal curly braces by doubling them ({{ → {)
because :func:str.format is the substitution engine.
Source code in src/taskq/migrate.py
list_applied
async
¶
Return {version}:{phase} keys recorded in schema_migrations.
Returns an empty set if the schema or table does not yet exist — a
fresh database is the common case on first migrate up.
Source code in src/taskq/migrate.py
apply_pending
async
¶
apply_pending(
conn: Connection,
*,
schema: str,
phase: Phase | None = None,
target: str | None = None,
max_steps: int | None = None,
) -> list[Migration]
Apply pending migrations.
Each migration runs in its own transaction so a failure in one file does not leave a half-applied schema.
:param phase: restrict to pre or post migrations only.
:param target: stop after applying this version (inclusive).
:param max_steps: stop after this many applies.
:returns: migrations that were applied (in order).
Source code in src/taskq/migrate.py
apply_pending_locked
async
¶
apply_pending_locked(
dsn: str,
*,
schema: str,
phase: Phase | None = None,
target: str | None = None,
max_steps: int | None = None,
) -> list[Migration]
Apply pending migrations under a session-level advisory lock.
Opens its own connection, acquires pg_advisory_lock to prevent
concurrent startup races, applies pending migrations, and closes the
connection. Raises :class:SystemExit on failure so the calling
process aborts cleanly.
This is the recommended entry point for CLI --migrate and admin
sidecar TASKQ_MIGRATE_ON_START paths.