Skip to main content

No project description provided

Project description

migri

A simple Python async migration tool. You can use the CLI (run yourself or from a shell script) or import the exposed functions and run programatically. Currently supports PostgreSQL (asyncpg), SQLite (aiosqlite), and MySQL (aiomysql).

migri is currently in alpha and although unlikely, the implementation may change

Motivation

Using async database libraries is useful when a service/application is already using an async library. It's extra overhead to install a synchronous library just to run migrations. Practically speaking, though, there isn't much benefit to running migrations asynchronously since migrations must be applied synchronously. Besides, the number of migrations for a service is generally small.

Getting started

Install migri

pip install migri[mysql]
pip install migri[postgresql]
pip install migri[sqlite]

Create migrations

Create a migrations directory and add your migrations. Migrations are applied in lexicographical order (e.g. 0001_initial.sql then 0002_add_user_data.py and so on).

Currently .sql and .py files are supported. If you write a Python migration file, ensure that it contains an async function migrate. An instance of asyncpg's Connection class will be passed into the function.

async def migrate(conn) -> bool:
    await conn.execute("INSERT INTO categories (name) VALUES ($1)", "Animals")
    return True

Migrate

Run migri migrate. Provide database credentials via arguments or environment variables:

  • --db-name or DB_NAME (required)
  • --db-user or DB_USER
  • --db-pass or DB_PASS
  • --db-host or DB_HOST
  • --db-port or DB_PORT

Other options:

  • -d, --dialect or DB_DIALECT (mysql, postgresql, sqlite, note that currently only postgresql is supported. If not set, migri will attempt to infer the dialect (and library to use) using the database port.)
  • -l, --log-level or LOG_LEVEL (default error)

When you run migrate, migri will create a table called applied_migration (if it doesn't exist). This is how migri tracks which migrations have already been applied.

Dry run mode

If you want to test your migrations without applying them, you can use the dry run flag: --dry-run.

Unfortunately, dry run mode does not work with SQLite at this moment. If you want to try to get it to work, see the issue.

Dry run mode also doesn't work w/ MySQL because DDL statements implicitly commit.

Migrate programmatically

Migri can be called with a shell script (e.g. when a container is starting) or you can apply migrations from your application:

from migri import apply_migrations, PostgreSQLConnection

async def migrate():
    conn = PostgreSQLConnection(
        "sampledb",
        db_user="user",
        db_pass="passpass",
        db_host="localhost",
        db_port=5432
    )
    async with conn:
        await apply_migrations("migrations", conn)

Testing

  1. Set up local Python versions (e.g. pyenv local 3.7.7 3.8.3)
  2. Run docker-compose up to start Postgresql.
  3. Install nox with pip install nox.
  4. Run nox.

Docs

Docstrings are formatted in the Sphinx format.

Todos

  • Don't record empty migrations - warn user
  • Add dry run mode for testing migrations
  • Output migration results
  • Test modules not found
  • Test/handle incorrect migrate function signature (in migration Python files)
  • Add colorful output 🍭 for enhanced readability
  • Make error output more readable

Project details


Download files

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

Source Distribution

migri-0.5.0.tar.gz (16.5 kB view details)

Uploaded Source

Built Distribution

migri-0.5.0-py3-none-any.whl (24.2 kB view details)

Uploaded Python 3

File details

Details for the file migri-0.5.0.tar.gz.

File metadata

  • Download URL: migri-0.5.0.tar.gz
  • Upload date:
  • Size: 16.5 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.7

File hashes

Hashes for migri-0.5.0.tar.gz
Algorithm Hash digest
SHA256 19663bc8cab71bb4f028d1c553f017b68217e8de9025b51c558b5a080410b180
MD5 0c76008924179e651da47f99b5594f69
BLAKE2b-256 0df7f9267eb8ac951b816fa58ebcfaa294321778fe43e432dd42fefe8c43a5b3

See more details on using hashes here.

File details

Details for the file migri-0.5.0-py3-none-any.whl.

File metadata

  • Download URL: migri-0.5.0-py3-none-any.whl
  • Upload date:
  • Size: 24.2 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/41.2.0 requests-toolbelt/0.9.1 tqdm/4.48.0 CPython/3.7.7

File hashes

Hashes for migri-0.5.0-py3-none-any.whl
Algorithm Hash digest
SHA256 7d9873cd32870e1086198c7c40063530e614357f895b88f2d91be8d33e0e4d94
MD5 49b3e46f44aed345fb3fc02fdb04c96c
BLAKE2b-256 35b8be89a8d9d46a55844b29192eae37db9725f8d6981822618e30245745c495

See more details on using hashes here.

Supported by

AWS AWS Cloud computing and Security Sponsor Datadog Datadog Monitoring Fastly Fastly CDN Google Google Download Analytics Microsoft Microsoft PSF Sponsor Pingdom Pingdom Monitoring Sentry Sentry Error logging StatusPage StatusPage Status page