Skip to main content

asyncpg-trek: simple migrations for asyncpg

A simple library for managing migrations.

Target audience

Me. But maybe if you use [asyncpg] and prefer to write migrations as raw SQL (i.e. you're not using SQLAlchemy/Alembic) then you as well.

Features

  • async: migrations usually don't benefit from being async, but you benefit from using the same database driver in as your application uses (only [asyncpg] is supported).
  • simple: you just create .sql or Python files in a folder of your choosing and point this tool at that folder. No need to fight a new API to write migrations in.
  • API centric: there is no CLI to figure out, you decide how migrations get called, you control how the database connection gets created. This makes it trivial to run migrations in tests, wrap them in a CLI or run them via an exposed HTTP endpoint.
  • declarative: just specify the version you want and the library figures out if it needs an upgrade, downgrade or no action.

Example usage

from pathlib import Path

import asyncpg
from asyncpg_trek import plan, execute, Direction
from asyncpg_trek.asyncpg import AsyncpgBackend

MIGRATIONS_DIR = Path(__file__).parent / "migrations"

async def migrate(
    conn: asyncpg.Connection,
    target_revision: str,
) -> None:
    backend = AsyncpgBackend(conn)
    async with backend.connect() as conn:
        planned = await plan(conn, backend, MIGRATIONS_DIR, target_revision=target_revision, direction=Direction.up)
        await execute(conn, backend, planned)

You could make this an entrypoint in a docker image, an admin endpoint in your API or a helper function in your tests (or all of the above). How you run your migrations depends on the complexity of your system. For example, for simple systems it may be easy to run migrations on app startup based on a hardcoded revision. For more complex systems you may want to run migrations manually or via an admin API.

See this release on GitHub: v0.2.1

Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

asyncpg_trek-0.2.1.tar.gz (6.7 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

asyncpg_trek-0.2.1-py3-none-any.whl (8.1 kB view details)

Uploaded Python 3

File details

Details for the file asyncpg_trek-0.2.1.tar.gz.

File metadata

  • Download URL: asyncpg_trek-0.2.1.tar.gz
  • Upload date:
  • Size: 6.7 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.11.0 Linux/5.15.0-1024-azure

File hashes

Hashes for asyncpg_trek-0.2.1.tar.gz
Algorithm Hash digest
SHA256 2649ae68fee14acab21e0efabf809b2abeeb59ccc1c40934967dea1285264577
MD5 c762601e1ce471d03d9ede9b316a7dd9
BLAKE2b-256 d6f25dfad12853fba6f22fc94d7e1d744aeecacda001df25006fca7b6971cbde

See more details on using hashes here.

File details

Details for the file asyncpg_trek-0.2.1-py3-none-any.whl.

File metadata

  • Download URL: asyncpg_trek-0.2.1-py3-none-any.whl
  • Upload date:
  • Size: 8.1 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.2.2 CPython/3.11.0 Linux/5.15.0-1024-azure

File hashes

Hashes for asyncpg_trek-0.2.1-py3-none-any.whl
Algorithm Hash digest
SHA256 918737f5e34a0217e58798bad471696d252ab4489a85cd68df540f354703ebc5
MD5 e6c8f8e50c765e60246778e3fb9aeb6c
BLAKE2b-256 0e0548933753b84eb45d703a4a376fabb6e94bef0d58d2d5c799ef18a5f4befd

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page