Skip to main content

Like diff but for PostgreSQL schemas

Project description

Migrations are without doubt the most cumbersome and annoying part of working with SQL databases. So much so that some people think that schemas themselves are bad!

But schemas are actually good. Enforcing data consistency and structure is a good thing. It’s the migration tooling that is bad, because it’s harder to use than it should be. migra is an attempt to change that, and make migrations easy, safe, and reliable instead of something to dread.

How it Works

Think of migra as a diff tool for schemas. Say database A and database B have a similar but slightly different schemas. migra will detect the differences and output the SQL needed to transform A to B.

Being a python library, you can use it programmatically and use it to build your own migration scripts, tools, etc. Installing migra also installs the migra command, so you can use it as follows:

$ migra postgresql:///a postgresql:///b
alter table "public"."products" add column newcolumn text;
alter table "public"."products" add constraint "x" CHECK ((price > (0)::numeric));

If b is the target schema, then a new column and constraint needs to be applied to a to make it match b’s schema. Once we’ve reviewed the autogenerated SQL and we’re happy with it, we can apply these changes as easily as:

$ migra --unsafe postgresql:///a postgresql:///b > migration_script.sql
# Then after careful review (obviously)...
$ psql a --single-transaction -f migration_script.sql

Migration complete!

IMPORTANT: Practice safe migrations

Migrations can never be fully automatic. As noted above ALWAYS REVIEW MIGRATION SCRIPTS CAREFULLY, ESPECIALLY WHEN DROPPING TABLES IS INVOLVED.

migra is in the alpha stage. Even more reason not trust it too much, and review and test your migrations thoroughly.

migra will deliberately throw an error if any generated statements feature the word “drop”. This safety feature is by no means bulletproof either, but might prevent a few obvious blunders.

If you want to generate “drop …” statements, you need to use the –unsafe flag if using the command, or if using the python package directly, set_safety( to false on your Migration object.

Python Code

Here’s how the migra command is implemented under the hood (with a few irrelevant lines removed).

As you can see, it’s pretty simple (S here is a context manager that creates a database session from a database URL).

from migra import Migration
from sqlbag import S

with S(args.dburl_from) as s0, S(args.dburl_target) as s1:
    m = Migration(s0, s1)
    m.set_safety(False)
    m.add_all_changes()
    print(m.sql)

Here the code just opens connections to both databases for the Migration object to analyse. m.add_all_changes() generates the SQL statements for the changes required, and adds to the migration object’s list of pending changes. The necessary SQL is now available as a property.

Documentation

migra is in early alpha and documentation is scarce so far. We are working on remedying this. Watch this space.

Features and Limitations

Migra will detect changes to tables, views, materialized views, indexes, constraints, enums, sequences, and which extensions are installed.

In terms of specific PostgreSQL feature limitations, migra is only confirmed to work with SQL/PLPGSQL functions so far.

Installation

Assuming you have pip installed, all you need to do is install as follows:

$ pip install migra

If you don’t have psycopg2 (the PostgreSQL driver) installed yet, you can install this at the same time with:

$ pip install migra[pg]

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

migra-0.1.1470841985.tar.gz (5.6 kB view details)

Uploaded Source

Built Distribution

migra-0.1.1470841985-py2.py3-none-any.whl (9.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file migra-0.1.1470841985.tar.gz.

File metadata

File hashes

Hashes for migra-0.1.1470841985.tar.gz
Algorithm Hash digest
SHA256 103692a578fa32f5676fe2ba1456a7bf503f5acf22f1a1b8beb4c3b59a1aa386
MD5 9f32da347a5120e04cb0c47abb0bd026
BLAKE2b-256 842bc4c1365fe6c14c4f4dac0d8cb4810316ffa5056d809f3fd11b09e6abb93b

See more details on using hashes here.

File details

Details for the file migra-0.1.1470841985-py2.py3-none-any.whl.

File metadata

File hashes

Hashes for migra-0.1.1470841985-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 89bf4f19892ce14ca514a69567d3bbcfb469a5f5528ca86b0c7f6654c412521e
MD5 d21bddfafd5f513d3d482c2e898da2dd
BLAKE2b-256 18e6d647bd2d2b8fed56bc8ad4a447a74ecb8514577bcd8ac7703f3f06b07575

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