# Python : Database : pogo-migrate - Inspired by [yoyo](https://pypi.org/project/yoyo-migrations/) and [dbmate](https://github.com/amacneil/dbmate/). - Uses [asyngpg](https://pypi.org/project/asyncpg/). - Supports both SQL and Python migrations. - API and CLI ## CLI ``` pogo new # create empty migration and open in editor --sql # create as SQL instead of Python --no-interactive # don't open editor -m "..." # set as migration docstring and incorporated into autogenerated filename pogo apply pogo rollback -c | --count N # defaults to 1 pogo mark # for each unapplied migration, prompts to mark as applied --no-interactive # yes-to-all pogo unmark pogo history --unapplied # show only unapplied migrations --simple # raw data instead of pretty table ``` ``` Common Options -d | --database DSN ``` Check for unapplied migrations in a script: ```bash $ pogo history --unapplied --simple | wc -l ``` ## Config ```toml # pyproject.toml [ tool.pogo ] migrations = "./migrations" database_config = "postgres://{USER}:{PASS}@{HOST}:{PORT}/{DATABASE}" ``` ## Migrations Auto-generated names: - `20240926_01_1qmub-foo` - `20240926_02_aivos-bar` #### Python ```python """ foo """ __depends__ = [] #__depends__ = ["20240926_01_1qmub-foo"] async def apply( db ): ... async def rollback( db ): ... ``` #### SQL ```sql -- a descriptive message -- depends: 20210101_01_abcdef-previous-migration -- migrate: apply CREATE TABLE foo ( id INT, bar VARCHAR( 20 ), PRIMARY KEY ( id ) ); -- migrate: rollback DROP TABLE foo; ``` ## API ```python # conftest.py import asyncpg import pogo_migrate.testing @pytest.fixture( scope="session", autouse=True ) async def _database(): conn = await asyncpg.connect( DSN ) await pogo_migrate.testing.apply( conn ) yield await pogo_migrate.testing.rollback( conn ) ```