No project description provided
Project description
Migri is a simple async Python 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-nameorDB_NAME(required)--db-userorDB_USER--db-passorDB_PASS--db-hostorDB_HOST--db-portorDB_PORT
Other options:
-d, --dialectorDB_DIALECT(mysql,postgresql,sqlite, note that currently onlypostgresqlis supported. If not set,migriwill attempt to infer the dialect (and library to use) using the database port.)-l, --log-levelorLOG_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.
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
- Set up local Python versions (e.g.
pyenv local 3.7.7 3.8.3) - Run
docker-compose upto start Postgresql. - Install nox with
pip install nox. - 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
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
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file migri-0.7.0.tar.gz.
File metadata
- Download URL: migri-0.7.0.tar.gz
- Upload date:
- Size: 20.3 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
d6662e91719dc13d6f22cddbacaf582367ed2d0d9d4d981b4da7d2fe7c81fcfb
|
|
| MD5 |
075688a47aad0cb2a88b25ef28a59e2d
|
|
| BLAKE2b-256 |
8f95754b7065e7b5cdbff02a245adc1e1ee5634381a582ff53c69119cb2d2e4b
|
File details
Details for the file migri-0.7.0-py3-none-any.whl.
File metadata
- Download URL: migri-0.7.0-py3-none-any.whl
- Upload date:
- Size: 28.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/3.8.0 pkginfo/1.8.2 readme-renderer/32.0 requests/2.27.1 requests-toolbelt/0.9.1 urllib3/1.26.8 tqdm/4.62.3 importlib-metadata/4.11.1 keyring/23.5.0 rfc3986/2.0.0 colorama/0.4.4 CPython/3.9.10
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
82309400e095be88e43d697746ac307a003eb30b381262d078da414ad55dd16d
|
|
| MD5 |
32b74c951a514d44b4ba4dd9d4431373
|
|
| BLAKE2b-256 |
b8549ca08055d6963e4b810daacd395d02e2b200860a3e81629b53c82232d33f
|