Skip to main content

dbschema

Pypi Build Status codecov MIT licensed

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
$

Release files for dbschema 1.4.3

For a detailed explanation of source distributions (sdists) and built distributions (wheels), please see the package formats documentation.

Source distribution (sdist)

Source distribution for dbschema 1.4.3
File Size Uploaded
dbschema-1.4.3.tar.gz 6.0 kB Details

Built distribution (wheel)

Table of built distributions (wheels) for dbschema 1.4.3
File Interpreter ABI Platform
dbschema-1.4.3-py2.py3-none-any.whl Python 3, Python 2 none any Details

Total release size: 13.2 kB

Release files / dbschema-1.4.3.tar.gz

Download URL dbschema-1.4.3.tar.gz
Size 6.0 kB
Tags Source
SHA-256 checksum
How to use checksums
ee984c4e2dfd56c49b7ac444cc20ca80b38a4f62d5b3117c29b9f45ec46bf761
BLAKE2b-256 checksum
How to use checksums
31a9de084490940fb2a532bf9f662a1d31553d92ed532143083e8d74193fa3b5
Upload date
Uploaded using Trusted Publishing?
What is 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

Release files / dbschema-1.4.3-py2.py3-none-any.whl

Download URL dbschema-1.4.3-py2.py3-none-any.whl
Size 7.2 kB
Tags Python 2 Python 3
SHA-256 checksum
How to use checksums
f35333580c28db9cdec26de2696978786974e587c2d261a401549de4303bccd1
BLAKE2b-256 checksum
How to use checksums
8a19ed13c3f12e873f84be54220e57a6890ce3253885a42123c84f5ac4c600a7
Upload date
Uploaded using Trusted Publishing?
What is 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

Release history Release notifications | RSS feed

This release

1.4.3 This release

2 release files

1.4.2

2 release files

1.4

2 release files

1.3

2 release files

1.2.5

2 release files

1.2.4

2 release files

1.2.3

2 release files

1.2.2

2 release files

1.2.1

2 release files

1.2

2 release files

1.1.2

2 release files

1.1.1

2 release files

1.1

2 release files

1.0.2

2 release files

1.0.1

2 release files

1.0.0

2 release files

Anthropic, PBC Visionary sponsor Bloomberg Visionary sponsor Hudson River Trading Visionary sponsor Meta Visionary sponsor NVIDIA Visionary sponsor Microsoft Sustainability sponsor Depot Continuous Integration AWS Cloud computing and Security Sponsor Datadog Monitoring Fastly CDN Google Download Analytics Sentry Error logging StatusPage Status page