Skip to main content

Simple Postgres Schema Versioning

Project description

Simple Postgres Schema Versioning

There already exists many tools to manage database schema versions, such as sqitch, or alembic. Please consider them first to check whether they fit your needs before considering this one. In contrast to these tools, pg-schema-version emphasizes a simple approach based on a single plain SQL scripts and no configuration, to provide limited but useful features with safety in mind. The application schema status is maintained in one table to detect reruns. Several application can share the same setup.

Status Tests Coverage Python Version License Badges

Usage

  1. Write a sequence of incremental postgres SQL data definition scripts

    • initial schema creation create_000.sql

      CREATE TABLE AcmeData(aid SERIAL PRIMARY KEY, data TEXT UNIQUE NOT NULL);
      
    • first schema upgrade create_001.sql

      CREATE TABLE AcmeType(atid SERIAL PRIMARY KEY, atype TEXT UNIQUE NOT NULL);
      INSERT INTO AcmeType(atype) VALUES ('great'), ('super');
      ALTER TABLE AcmeData ADD COLUMN atid INT NOT NULL DEFAULT 1 REFERENCES AcmeType;
      
    • second schema upgrade create_002.sql

      INSERT INTO AcmeType(atype) VALUES ('wow'), ('incredible');
      
  2. Generate a psql-script from these for the target application:

    pg-schema-version -a acme create_*.sql > acme.sql
    
  3. Execute the script against a database to bring its schema up to date.

    # first time MUST use command create
    psql -v psv=create acme < acme.sql
    # psv command set to create
    # psv dry create for acme, enable with -v psv=create:wet
    # psv script will create infra, register acme and execute all steps
    
    psql -v psv=create:wet < acme.sql
    # psv wet run for acme
    # psv creating psv infra
    # psv registering app acme
    # psv applying acme 1
    # psv applying acme 2
    # psv applying acme 3
    # psv acme version: 3
    # psv wet run for acme done
    
    # on rerun, do nothing
    psql -v psv=wet < acme.sql
    # psv wet run for app acme
    # psv skipping acme 1
    # psv skipping acme 2
    # psv skipping acme 3
    # psv acme version: 3
    # psv wet run for acme done
    

Features

The python script generates a reasonably safe re-entrant idempotent SQL script driven by psql-variable psv with value command:moist

  • available commands are (default is run):
    • init just initialize an empty psv infrastructure if needed.
    • register register new application in the psv infrastructure if needed.
    • run apply required steps on an already registered application.
    • create do all of the above.
    • remove drop psv infrastructure.
    • help show some help.
  • available moistures are (default is dry):
    • dry meaning that no changes are applied.
    • wet to trigger actual changes.

The only way is forward: there is no provision to go back to a previous state. However, note that schema steps are performed in a transaction, so that it can only fail one full step at a time.

Caveats

There is no magic involved, you can still shot yourself in the foot, although with an effort.

To be safe, SQL schema creation scripts must NOT:

  • include backslash commands which may interfere with the script owns.
  • include SQL transaction commands.

Imperfect checks are performed to detect the above issues. They can be circumvented with option --trust-scripts.

Test your scripts with care before applying it to production data.

Versions

TODO

  • reverse?
  • point to?
  • verbose mode?
  • add description?

0.2 on 2024-10-15

  • activate GitHub pages
  • working GitHub CI
  • add coverage check
  • add markdown check
  • use exit code 3 for output file

0.1 on 2024-10-14

  • initial beta version for testing

License

This code is Public Domain.

All software has bug, this is software, hence… Beware that you may lose your hairs or your friends because of it. If you like it, feel free to send a postcard to the author.

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

pg_schema_version-0.2.tar.gz (13.8 kB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pg_schema_version-0.2-py3-none-any.whl (8.8 kB view details)

Uploaded Python 3

File details

Details for the file pg_schema_version-0.2.tar.gz.

File metadata

  • Download URL: pg_schema_version-0.2.tar.gz
  • Upload date:
  • Size: 13.8 kB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/5.1.1 CPython/3.12.3

File hashes

Hashes for pg_schema_version-0.2.tar.gz
Algorithm Hash digest
SHA256 8344500ddce2e4ed0355e00d09b5654d54004aa5cadd13752af54e0febef69a8
MD5 f1f5725d451a392835ffbccc1fbb8bd3
BLAKE2b-256 efa77818b92e804455aacba20911414d53784a15670910980c0d9a1161e50881

See more details on using hashes here.

File details

Details for the file pg_schema_version-0.2-py3-none-any.whl.

File metadata

File hashes

Hashes for pg_schema_version-0.2-py3-none-any.whl
Algorithm Hash digest
SHA256 dce74837be88188f959b061fecd6526664c7413ef7ec577a868b1dc8795b1d17
MD5 2901dd7ca01426901e941af1605c655e
BLAKE2b-256 95272c62434ce6a8fc0af9553cef0f68897f247794021b09fa086fc81388f933

See more details on using hashes here.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page