Skip to content

korbase.migrate

The migration helpers: write, review and apply SQL migration files.

migrate

Generate, apply and list SQL migrations.

make compares your models with the database and writes the SQL file that closes the gap; apply runs the reviewed files; status lists them. From a shell: python -m korbase.migrate make --models app.models --dir migrations. The DSN comes from --dsn, or else the KORBASE_DSN environment variable; --dsn wins. Prefer the variable: a command-line argument shows up in ps.

apply async

apply(
    db: Database, directory: str | PathLike[str]
) -> list[str]

Apply the pending migration files in directory and return their names.

Runs on one connection, so concurrent callers apply each file once: on Postgres under an advisory lock, on SQLite because each file runs in a write transaction that first checks the version table. Refuses, applying nothing, when an applied file is missing or changed, when a pending file sorts before the last applied one, or when a pending file contains -- korbase:review. A file whose first line is -- korbase:no-transaction runs outside a transaction. It stops at the first failing file; earlier files stay applied.

On SQLite a no-transaction file is claimed first: its version row is inserted in a short write transaction, then the file runs. If it fails, the row is deleted. If the process crashes in between, the row stays and the file counts as applied; check what the file did, then delete its row from korbase_migrations to run it again.

status async

status(
    db: Database, directory: str | PathLike[str]
) -> list[tuple[str, str]]

Return (name, state) per migration: applied, pending, changed or missing.

create_all async

create_all(db: Database, models: Iterable[type]) -> None

Create the tables of models in one transaction; handy in tests.

make async

make(
    db: Database,
    models: Iterable[type],
    directory: str | PathLike[str],
    message: str,
    all_tables: bool = False,
) -> Optional[Path]

Write the next migration file for the difference between models and public.

Only the tables of models are compared, so other tables in public are left alone. Pass all_tables=True to compare the whole public schema: that is how you drop the table of a model you removed (the drop is flagged for review, and so is the drop of every other table the models do not know).

Returns the new file's path, or None when nothing differs. The database is left untouched: the comparison runs in a transaction that is rolled back.

dev async

dev(
    db: Database,
    models: Iterable[type],
    directory: str | PathLike[str],
    message: str = "dev",
) -> Optional[Path]

Make a migration and apply it at once; for local development only.

Returns the new file's path, or None when nothing differs. When the file holds a -- korbase:review line, nothing is applied and KorbaseError names the file and lists those lines: review it, then run apply.

collect_models

collect_models(module_path: str) -> list[type]

Import the module module_path and return its @table classes.

Classes defined in it or imported into it count; each appears once. Raises KorbaseError when the module holds none.

main

main(argv: Optional[Sequence[str]] = None) -> int

Run the command line; return the exit code (0 on success, 1 on error).