Skip to main content

A flexible SQL migration runner.

Project description

migreat

A flexible SQL migration runner.

Right now, supports only PostgreSQL via psycopg2, as it's still an experiment. Support for any database with a DBAPI implementation is planned.

Install

migreat supports Python 3.7, 3.8, and 3.9. Since it currently only supports PostgreSQL via psycopg2, it will also be installed.

pip install migreat

Mental Model

migreat runs SQL migrations with a DB cursor provided by your code via a function, and stores migration metadata in a table with the datetime it was run, the ID of the user who ran it, the name of the migration, and a hash of its contents at the time it was run. The hash of a migration will be checked against if it is ever rolled back, to make sure we are rolling back the same migration that was once run.

Usage

Migrations are simple SQL files placed inside a directory and following a name pattern. The name pattern is YEAR-MONTH-DAY-SEQNUM-arbitrary-name.sql. YEAR-MONTH-DAY is the day's date following ISO 8601. The SEQNUM is a 2-digit sequential number for migrations released in the same day. Some valid names:

2020-01-10-03-create-users-table.sql
2020-03-18-01-remove-foreign-keys-from-audit-tables.sql

migreat will run the migrations in sequence of date and sequence number.

Create the migrations table:

$ migreat create-migrations-table \
    --cursor-factory yourapp.db.atomic

Run migrations:

$ migreat run \
    --migrations-dir migrations \
    --user-id 42 \
    --last-migration 2020-01-01-01 \
    --cursor-factory yourapp.db.atomic \
    --rollback

This will:

  • Look for the migrations in directory migrations relative to the current working directory;
    • If this is not supplied, the default value will be migrations;
  • Run the migrations as user with ID 42;
  • Run migrations rolling them back, only back up to migration 2020-01-01-01;
  • Run the function yourapp.db.atomic with no arguments expecting it to return a DBAPI cursor (see tests for an example).

migreat allows you to constrain the user ID to real user IDs you might have in some table in your database. To enable it:

$ migreat create-user-id-foreign-key users id \
    --cursor-factory yourapp.db.atomic

This will constrain user IDs to values in the column id of table users.

Finally, you can drop the user ID foreign key constraint:

$ migreat drop-user-id-foreign-key \
    --cursor-factory yourapp.db.atomic

And drop the migrations table:

$ migreat drop-migrations-table \
    --cursor-factory yourapp.db.atomic

All above commands also accept:

--cursor-factory-args=value1,value2
--cursor-factory-kwargs=key1,value1,key2,value2

Values are valid CSV strings and can contain nested commas inside proper delimiters.

Development

Clone the source code from GitHub, have a Docker Engine reachable, have all supported Python interpreters in your PATH (we recommend that you use pyenv to manage different Python interpreters and environments) and create a new virtual environment with poetry install.

Run the tests with:

./run test-db
# Wait for the database to be ready
./run all-tests

To run a specific test:

./run test-db
# Wait for the database to be ready
./run test <test-address>

To make sure your code abides to our quality standards, run:

./run quality

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

migreat-0.1.4.tar.gz (9.8 kB view details)

Uploaded Source

Built Distribution

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

migreat-0.1.4-py3-none-any.whl (8.7 kB view details)

Uploaded Python 3

File details

Details for the file migreat-0.1.4.tar.gz.

File metadata

  • Download URL: migreat-0.1.4.tar.gz
  • Upload date:
  • Size: 9.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.6 Linux/5.11.0-7620-generic

File hashes

Hashes for migreat-0.1.4.tar.gz
Algorithm Hash digest
SHA256 1e5784fd148941f71b143f0a6dec38f6424c49d52c570b783c2320eaeb78b31a
MD5 0ee4cf5af736612f5c17b591cedf1e35
BLAKE2b-256 a415f61625fc4364e3f0f28d24426e736084d4acace09986e726d138ab8abbfe

See more details on using hashes here.

File details

Details for the file migreat-0.1.4-py3-none-any.whl.

File metadata

  • Download URL: migreat-0.1.4-py3-none-any.whl
  • Upload date:
  • Size: 8.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.1.4 CPython/3.9.6 Linux/5.11.0-7620-generic

File hashes

Hashes for migreat-0.1.4-py3-none-any.whl
Algorithm Hash digest
SHA256 6e7bcc5d8b4c14823c9f14f6d6f08694a0a14814717ae2efb19984ae5e7f3ca5
MD5 4168d5651b62bf74739e6ea15111f5e6
BLAKE2b-256 23fd54c9e8b9e62159db050b1ef2f5272bcbd7691175d3a0bba5b33c393262b9

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 Pingdom Monitoring Sentry Error logging StatusPage Status page