Schema migration made easy
Project description
dbschema is a tool to run MySQL or PostgreSQL migrations automatically. Using a table, it keeps a state of previous migrations to avoid duplicates.
Features:
Support for MySQL and PostgreSQL
Optional pre and post-migration queries (for example to update privileges)
Multiple migrations in multiple databases can be processed as one.
Installation
Install dbschema
pip3 install dbschema
Create a config file
Create the file ~/.dbschema.yml and add your databases configuration. See example
Create migrations table
dbschema uses a table called migrations_applied to keep track of migrations already applied to avoid duplication. See the schema for MySQL or PostgreSQL.
Migrations folder structure
For each database, you need to have a migration path (setting path in the migration file).
Within that path you need to create one folder per migration. This folder must contain a file called up.sql with the SQL queries and optionally a file called down.sql for rollbacks.
/path/to/migrations/db1/ |-- migration1/ | |-- up.sql | |-- down.sql |-- migration2/ | |-- up.sql |... /path/to/migrations/db2/ |-- migration1/ | |-- up.sql |-- migration2/ | |-- up.sql | |-- down.sql |...
Usage
Apply pending migrations
dbschema
# or to specify a config file path
dbschema --config /path/to/config.yml
# or to migrate only a specific database
dbschema --tag db1
Rollback
dbschema --tag db1 --rollback migration1
Example
$ dbschema
* Applying migrations for postgresql -> `test`
-> Migration `migration1` applied
-> Migration `migration2` applied
-> Migration `migration3` applied
* Migrations applied
* Applying migrations for mysql -> `test`
-> Migration `migration1` applied
-> Migration `migration2` applied
-> Migration `migration3` applied
* Migrations applied
$
$ dbschema --tag db2 --rollback migration1
* Rolling back mysql -> `migration1`
-> Migration `migration1` has been rolled back
$
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
Hashes for dbschema-1.1-py2.py3-none-any.whl
Algorithm | Hash digest | |
---|---|---|
SHA256 | 2d0a39f31666a095ac5a6bf449d6043d278188a7391511574d0e3e6f6c943e46 |
|
MD5 | 87036052f9ca056b516e99e18ea139df |
|
BLAKE2b-256 | 324bb70f0687cef99c94debfad3afa971d3359b143a9ed74fcfb35b4062b80ea |