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
# Install required packages
apt-get update
apt-get install --yes libpq-dev gcc python3-dev
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 db1 (`test` on postgresql)
-> Migration `migration1` applied
-> Migration `migration2` applied
-> Migration `migration3` applied
* Migrations applied
* Applying migrations for db2 (`test` on mysql)
-> 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
File details
Details for the file dbschema-1.4.3.tar.gz
.
File metadata
- Download URL: dbschema-1.4.3.tar.gz
- Upload date:
- Size: 6.0 kB
- Tags: Source
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | ee984c4e2dfd56c49b7ac444cc20ca80b38a4f62d5b3117c29b9f45ec46bf761 |
|
MD5 | 9cbef720ee1ea9f839002a4bc2349787 |
|
BLAKE2b-256 | 31a9de084490940fb2a532bf9f662a1d31553d92ed532143083e8d74193fa3b5 |
File details
Details for the file dbschema-1.4.3-py2.py3-none-any.whl
.
File metadata
- Download URL: dbschema-1.4.3-py2.py3-none-any.whl
- Upload date:
- Size: 7.2 kB
- Tags: Python 2, Python 3
- Uploaded using Trusted Publishing? No
- Uploaded via: twine/1.15.0 pkginfo/1.5.0.1 requests/2.22.0 setuptools/41.0.1 requests-toolbelt/0.9.1 tqdm/4.36.1 CPython/3.7.4
File hashes
Algorithm | Hash digest | |
---|---|---|
SHA256 | f35333580c28db9cdec26de2696978786974e587c2d261a401549de4303bccd1 |
|
MD5 | 2726718c6f703d4ec0d024f9b96c05e0 |
|
BLAKE2b-256 | 8a19ed13c3f12e873f84be54220e57a6890ce3253885a42123c84f5ac4c600a7 |