Skip to main content

Write migrations in SQL

Project description

SQL-migrate

SQL is a fantastic language — one of the most successful programming languages in the world.

We can use SQL to manage the schema of our database and migrate data. We believe this is better than trying to replace SQL with a bespoke DSL for migrations.

Managing the database schema of an app in development, however, can be challenging. We need some assistance to manage the complexity:

  • Generate migration files for upward and reverse migrations.
  • Track migration dependencies: ensure that migrations are run in the correct order.
  • Merge migrations from multiple branches: Ensure that schema changes in two or more different branches do not conflict and are resolved satisfactorily.

Behold SQL-migrate:

  • Create migrations
    • automatic migration names based on either timestamp (default, like Diesel) or sequence number (option, like Django).
    • optional descriptive label added to the migration filenames
    • configurable to use a single folder (like Django) or subfolders (like Diesel)
    • three files per migration:
      • NAME.up.sql (or NAME/up.sql) – the SQL script to do the upward migration
      • NAME.dn.sql (or NAME/dn.sql) – the SQL script to do the downward migration
      • NAME.conf.yaml (or NAME/conf.yaml) – configuration for the migration. For now, this includes:
        • the "name" of the migration
        • any "description" that the author has chosen to include
        • a list of the migration's "base" dependencies (so that an DAG of migrations can be modeled to ensure that migrations are applied correctly.)
        • a list of "up" SQL scripts in this migration (default *up.sql)
        • a list of "dn" SQL scripts in this migration (default *dn.sql)
    • support for COPY from an external file for easily loading / updating data.
    • support from COPY from the results of a query to ease bulk data migration.
  • Apply migrations:
    • Creates a migrations table to track which migrations were applied and when.
    • Ensures that migrations are applied in dependency-order (which is not necessarily the same as filesystem order).
    • Disallows migrating a database if there are unmerged branches – ensure that branch conflicts have been resolved in a merge before continuing.
    • Each migration is run within its own transaction, treating it as a unified changeset. It's all or nothing with a given migration.
  • Merge migrations:
    • When creating a new migration where there are unmerged branches, creates a new migration that has the tip of each branch as a dependency, and prompts the user to review the migrations to ensure that conflicts are resolved.

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

sql-migrate-0.1.0.tar.gz (2.8 kB view hashes)

Uploaded Source

Built Distribution

sql_migrate-0.1.0-py2.py3-none-any.whl (8.0 kB view hashes)

Uploaded Python 2 Python 3

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