Skip to main content

A PostgreSQL specific migration tool

Project description

Tusker

A PostgreSQL specific migration tool

Elevator pitch

Do you want to write your database schema directly as SQL which is understood by PostgreSQL?

Do you want to be able to make changes to this schema and generate the SQL which is required to migrate between the old and new schema version?

Tusker does exactly this.

Installation

pip3 install tusker

Now you should be able to run tusker. Give it a try:

tusker --help

Getting started

Once tusker is installed create a new file called schema.sql:

CREATE TABLE fruit (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY,
    name TEXT NOT NULL UNIQUE
);

Tusker also needs an empty migrations directory:

mkdir migrations

Now you should be able to create your first migration:

tusker diff

The migration is printed to the console and all you need to do is copy and paste the output into a new file in the migrations directory. Alternatively you can also pipe the output of tusker diff into the target file:

tusker diff > migrations/0001_initial.sql

After that check that your schema.sql and your migrations are in sync:

tusker diff

This should give you an empty output. This means that there is no difference between applying the migrations in order and the target schema.

If you want to change the schema in the future simply change the schema.sql and run tusker diff to create the migration for you.

Give it a try and change the schema.sql:

CREATE TABLE fruit (
    id BIGINT GENERATED BY DEFAULT AS IDENTITY,
    name TEXT NOT NULL UNIQUE,
    color TEXT NOT NULL DEFAULT ''
);

Create a new migration:

tusker diff > migrations/0002_fruit_color.sql

Congratulations! You are now using SQL to write your migrations. You are no longer limited by a 3rd party data definition language or an object relational wrapper.

Configuration

In order to run tusker you do not need a configuration file. The following defaults are assumed:

  • The file containing your database schema is called schema.sql
  • The directory containing the migrations is called migrations
  • Your current user can connect to the database using a unix domain socket without a password.

You can also create a configuration file called tusker.toml. The default configuration looks like that:

[schema]
filename = "schema.sql"

[migrations]
directory = "migrations"

[database]
#host = ""
#port = 5432
#user = ""
#password = ""
dbname = "tusker"

Instead of the exploded form of host, port, etc. it is also possible to pass a connection URL:

[schema]
filename = "schema.sql"

[migrations]
directory = "migrations"

[database]
url = "postgresql:///my_awesome_db"

How can I use the generated SQL files?

The resulting SQL files can either be applied to the database by hand or by using one of the many great tools and libraries which support applying SQL files in order.

Some recommendations are:

How does it work?

Upon startup tusker reads all files from the migration directory and runs them on an empty database. Another empty database is created and the target schema is created. Then those two schemas are diffed using the excellent migra tool and the output printed to the console.

FAQ

Tusker printed an error and left the temporary databases behind. How can I remove them?

Run tusker clean. This will remove all databases which were created by previous runs of tusker. Tusker only removes databases which are marked with a CREATED BY TUSKER comment.

What does the dbname setting in tusker.toml mean?

Tusker creates two temporary databases and uses this as prefix. The two databases are called {dbname}_{timestamp}_schema and {dbname}_{timestamp}_migrations.

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

tusker-0.1.1.tar.gz (6.6 kB view details)

Uploaded Source

Built Distribution

tusker-0.1.1-py3-none-any.whl (6.7 kB view details)

Uploaded Python 3

File details

Details for the file tusker-0.1.1.tar.gz.

File metadata

  • Download URL: tusker-0.1.1.tar.gz
  • Upload date:
  • Size: 6.6 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.7.5 Linux/5.3.11-100.fc29.x86_64

File hashes

Hashes for tusker-0.1.1.tar.gz
Algorithm Hash digest
SHA256 0e196796a22cd46cababdda50d097d4605b28c1663d8b211521b4694f8b94d69
MD5 890f196f4a95fde7bb23cc9bd5d7e7d8
BLAKE2b-256 848fc88078ad620c9858a4b002d628f71c59df882d93294d9fdc1a7cf37cd570

See more details on using hashes here.

File details

Details for the file tusker-0.1.1-py3-none-any.whl.

File metadata

  • Download URL: tusker-0.1.1-py3-none-any.whl
  • Upload date:
  • Size: 6.7 kB
  • Tags: Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: poetry/1.0.5 CPython/3.7.5 Linux/5.3.11-100.fc29.x86_64

File hashes

Hashes for tusker-0.1.1-py3-none-any.whl
Algorithm Hash digest
SHA256 dacb8cf02073efc1f29f45f6c8ddecbdad6ebaf185a36fc25aa0718cb23a1fc1
MD5 6c89616df7641312e47d46e797eafc18
BLAKE2b-256 f75a91948588e468c7ef835ae1853f02feb38c0a5d4daff65e8ea5a3d4931b3c

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