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.5.tar.gz (10.4 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.5-py3-none-any.whl (9.0 kB view details)

Uploaded Python 3

File details

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

File metadata

  • Download URL: migreat-0.1.5.tar.gz
  • Upload date:
  • Size: 10.4 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.5.tar.gz
Algorithm Hash digest
SHA256 8108b83c464d6694c4343aade9cb3f4ee3f39b1b89d8780687b6b683e9a61153
MD5 05d5492e0c766b39889c6950c43ac097
BLAKE2b-256 dbe71e855b82c7c705f73e546d3aec8ede9e5a8608142351f9dddf34c2914251

See more details on using hashes here.

File details

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

File metadata

  • Download URL: migreat-0.1.5-py3-none-any.whl
  • Upload date:
  • Size: 9.0 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.5-py3-none-any.whl
Algorithm Hash digest
SHA256 a877cf8b76c9fe9f27c789c95b2173c61b49ba6c57eb4ef20baade97ae271c62
MD5 ac48b0177b3bc0c3d6d68db7c31ed1d8
BLAKE2b-256 6843da2655c44b3a6f5918d730b87be17ef882f31abaefff3c63785a39554f65

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