Like `diff` but for PostgreSQL schemas
Project description
migra: Like diff but for Postgres schemas
-
compare schemas
-
autogenerate migration scripts
-
autosync your development database from your application models
-
make your schema changes testable, robust, and (mostly) automatic
migra
is a schema diff tool for PostgreSQL, written in Python. Use it in your python scripts, or from the command line like this:
$ migra postgresql:///a postgresql:///b
alter table "public"."products" add column newcolumn text;
alter table "public"."products" add constraint "x" CHECK ((price > (0)::numeric));
migra
magically figures out all the statements required to get from A to B.
You can also detect changes for a single specific schema only with --schema myschema
.
Migra supports PostgreSQL >= 10 only. Known issues exist with earlier versions. Development resources are limited, and feature support rather than backwards compatibility is prioritised.
Support migra
's maintenance and future development
This project isn't sponsored by my employer or any other organisation: It's been built with many hours of voluntary unpaid work.
I've recently set up a Patreon to help support future development of migra
and related projects.
If you or your employer uses migra
, please consider becoming a subscriber.
If you require specific features or support, more formal commercial arrangements can be discussed (email me at the author email specified in this repo's pyproject.toml
)
Folks, schemas are good
Schema 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.
Full documentation
Documentation is at migra.djrobstep.com.
How it Works
Think of migra
as a diff tool for schemas. Suppose database A and database B have similar but slightly different schemas. migra
will detect the differences and output the SQL needed to transform A to B.
This includes changes to tables, views, functions, indexes, constraints, enums, sequences, and installed extensions.
You can also use migra
as a library to build your own migration scripts, tools, and custom migration flows.
With migra, a typical database migration is a simple three step process.
-
Autogenerate:
$ migra --unsafe postgresql:///a postgresql:///b > migration_script.sql
-
Review (and tweak if necessary).
# If you need to move data about during your script, you can add those changes to your script.
-
Apply:
$ 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
manages schema changes but not your data. If you need to move data around, as part of a migration, you'll need to handle that by editing the script or doing it separately before/after the schema changes.
Best practice is to run your migrations against a copy of your production database first. This helps verify correctness and spot any performance issues before they cause interruptions and downtime on your production database.
migra
will deliberately throw an error if any generated statements feature the word "drop". This safety feature is by no means idiot-proof, 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.
Features and Limitations
Table of supported features:
Feature | Supported | Notes/limitations |
---|---|---|
tables | ✔ | |
partitioned tables | ✔ | NEW! |
constraints | ✔ | |
views | ✔ | |
functions | ✔ | Dependency-aware. All languages except C/INTERNAL |
sequences | ✔ | Does not track sequence numbers |
schemas | ✔ | |
extensions | ✔ | |
enums | ✔ | |
privileges | ✔ | Not exhaustive. Requires --with-privileges flag |
row-level security | ✔ | NEW! Doesn't include role management |
triggers | ✔ | NEW! |
custom types/domains | In progress |
migra
plays nicely with extensions. Schema contents belonging to extensions will be ignored and left to the extension to manage.
migra
plays nicely with view/function dependencies, and will drop/create them in the correct order.
Only SQL/PLPGSQL functions are confirmed to work so far. migra
ignores functions that use other languages.
Installation
Assuming you have pip, all you need to do is install as follows:
$ pip install migra
If you don't have psycopg2-binary (the PostgreSQL driver) installed yet, you can install this at the same time with:
$ pip install migra[pg]
In python
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)
if args.unsafe:
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.
Contributing
Contributing is easy. Jump into the issues, find a feature or fix you'd like to work on, and get involved. Or create a new issue and suggest something completely different. If you're unsure about any aspect of the process, just ask.
Credits
- https://github.com/djrobstep: initial development, maintenance
- https://github.com/alvarogzp: privileges support
- https://github.com/seblucas: docker improvements
- https://github.com/MOZGIII: docker support
- https://github.com/mshahbazi: misc fixes and enhancements
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 migra-1.0.1548031590.tar.gz
.
File metadata
- Download URL: migra-1.0.1548031590.tar.gz
- Upload date:
- Size: 11.8 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.11 CPython/3.7.0 Linux/4.4.0-141-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | 0ed05ef2a730dd8747a345ac3e127decc4045c2a5a8065a0a9f6676b7e94e8fd |
|
MD5 | e0256ca87b3dd6e3ef9a5f1e255e5a16 |
|
BLAKE2b-256 | 86f3e187771bad7f9680a46702447abf8616672f4bf537ebb1d8aebf13ed8c98 |
File details
Details for the file migra-1.0.1548031590-py2.py3-none-any.whl
.
File metadata
- Download URL: migra-1.0.1548031590-py2.py3-none-any.whl
- Upload date:
- Size: 24.9 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: poetry/0.12.11 CPython/3.7.0 Linux/4.4.0-141-generic
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | fa2a764371f7ef48bc1bd66e2ab7621e39db2a6101c8419d3a255914ef4e4136 |
|
MD5 | 638992d8a1dde3987b763fb92e3c82fb |
|
BLAKE2b-256 | 50c7add6067ea1e90748a38aaf001ea412e9d36002cb83bb1ac07db91207a8ff |