No project description provided
Project description
migri
A super simple PostgreSQL migration tool that uses asyncpg. You can use the CLI yourself, in a shell script, or from your Python application.
Coming soon!
MySql (aiomysql) and SQLite (aiosqlite) support will be added in version 1.0.0 (no later than September 23, 2020).
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
Run pip install migri[postgresql]
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
orDB_NAME
(required)--db-user
orDB_USER
--db-pass
orDB_PASS
--db-host
orDB_HOST
--db-port
orDB_PORT
Other options:
-d, --dialect
orDB_DIALECT
(mysql
,postgresql
,sqlite
, note that currently onlypostgresql
is supported. If not set,migri
will attempt to infer the dialect (and library to use) using the database port.)-l, --log-level
orLOG_LEVEL
(defaulterror
)
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
.
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
- Run
docker-compose up
to start Postgresql. - Install nox with
pip install nox
. - Run
nox
.
Docs
Docstrings are formatted in the Sphinx format.
Todos
- Ensure that
migrate
command is safe to run even when no action need to be taken (e.g. helpful for container startup scripts) - 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
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
File details
Details for the file migri-0.3.0.tar.gz
.
File metadata
- Download URL: migri-0.3.0.tar.gz
- Upload date:
- Size: 12.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.2.0 pkginfo/1.5.0.1 requests/2.24.0 setuptools/47.3.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | cbafa36477e85616095189181b2a91e6ce8815ade02139b5268659e6e2a7db60 |
|
MD5 | e8984ff36e80a8957b7e1c3837a8f719 |
|
BLAKE2b-256 | d621add24cd1db4ee1bd2abcb1765aee42f9f42a1d47a75968d7c77d85b9da49 |
File details
Details for the file migri-0.3.0-py3-none-any.whl
.
File metadata
- Download URL: migri-0.3.0-py3-none-any.whl
- Upload date:
- Size: 15.6 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/47.3.1 requests-toolbelt/0.9.1 tqdm/4.47.0 CPython/3.7.7
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 3fea9cbf5308d2bbec267b3e4e85a4036b77332c437636a3a383ea72766462fd |
|
MD5 | a97603e3b890530df6102a68e4777505 |
|
BLAKE2b-256 | 2d0d308c44ab1259837c66cbd88bc3b8494fb998db410fa775bb14aeb2a63989 |