Skip to main content

A database migration tool for SQLAlchemy.

Project description

Alembic is a database migrations tool written by the author of SQLAlchemy. A migrations tool offers the following functionality:

  • Can emit ALTER statements to a database in order to change the structure of tables and other constructs

  • Provides a system whereby “migration scripts” may be constructed; each script indicates a particular series of steps that can “upgrade” a target database to a new version, and optionally a series of steps that can “downgrade” similarly, doing the same steps in reverse.

  • Allows the scripts to execute in some sequential manner.

The goals of Alembic are:

  • Very open ended and transparent configuration and operation. A new Alembic environment is generated from a set of templates which is selected among a set of options when setup first occurs. The templates then deposit a series of scripts that define fully how database connectivity is established and how migration scripts are invoked; the migration scripts themselves are generated from a template within that series of scripts. The scripts can then be further customized to define exactly how databases will be interacted with and what structure new migration files should take.

  • Full support for transactional DDL. The default scripts ensure that all migrations occur within a transaction - for those databases which support this (Postgresql, Microsoft SQL Server), migrations can be tested with no need to manually undo changes upon failure.

  • Minimalist script construction. Basic operations like renaming tables/columns, adding/removing columns, changing column attributes can be performed through one line commands like alter_column(), rename_table(), add_constraint(). There is no need to recreate full SQLAlchemy Table structures for simple operations like these - the functions themselves generate minimalist schema structures behind the scenes to achieve the given DDL sequence.

  • “auto generation” of migrations. While real world migrations are far more complex than what can be automatically determined, Alembic can still eliminate the initial grunt work in generating new migration directives from an altered schema. The --autogenerate feature will inspect the current status of a database using SQLAlchemy’s schema inspection capabilities, compare it to the current state of the database model as specified in Python, and generate a series of “candidate” migrations, rendering them into a new migration script as Python directives. The developer then edits the new file, adding additional directives and data migrations as needed, to produce a finished migration. Table and column level changes can be detected, with constraints and indexes to follow as well.

  • Full support for migrations generated as SQL scripts. Those of us who work in corporate environments know that direct access to DDL commands on a production database is a rare privilege, and DBAs want textual SQL scripts. Alembic’s usage model and commands are oriented towards being able to run a series of migrations into a textual output file as easily as it runs them directly to a database. Care must be taken in this mode to not invoke other operations that rely upon in-memory SELECTs of rows - Alembic tries to provide helper constructs like bulk_insert() to help with data-oriented operations that are compatible with script-based DDL.

  • Non-linear, dependency-graph versioning. Scripts are given UUID identifiers similarly to a DVCS, and the linkage of one script to the next is achieved via human-editable markers within the scripts themselves. The structure of a set of migration files is considered as a directed-acyclic graph, meaning any migration file can be dependent on any other arbitrary set of migration files, or none at all. Through this open-ended system, migration files can be organized into branches, multiple roots, and mergepoints, without restriction. Commands are provided to produce new branches, roots, and merges of branches automatically.

  • Provide a library of ALTER constructs that can be used by any SQLAlchemy application. The DDL constructs build upon SQLAlchemy’s own DDLElement base and can be used standalone by any application or script.

  • At long last, bring SQLite and its inablity to ALTER things into the fold, but in such a way that SQLite’s very special workflow needs are accommodated in an explicit way that makes the most of a bad situation, through the concept of a “batch” migration, where multiple changes to a table can be batched together to form a series of instructions for a single, subsequent “move-and-copy” workflow. You can even use “move-and-copy” workflow for other databases, if you want to recreate a table in the background on a busy system.

Documentation and status of Alembic is at https://alembic.sqlalchemy.org/

The SQLAlchemy Project

Alembic is part of the SQLAlchemy Project and adheres to the same standards and conventions as the core project.

Development / Bug reporting / Pull requests

Please refer to the SQLAlchemy Community Guide for guidelines on coding and participating in this project.

Code of Conduct

Above all, SQLAlchemy places great emphasis on polite, thoughtful, and constructive communication between users and developers. Please see our current Code of Conduct at Code of Conduct.

License

Alembic is distributed under the MIT license.

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

alembic-1.6.4.tar.gz (1.2 MB view details)

Uploaded Source

Built Distribution

alembic-1.6.4-py2.py3-none-any.whl (164.7 kB view details)

Uploaded Python 2 Python 3

File details

Details for the file alembic-1.6.4.tar.gz.

File metadata

  • Download URL: alembic-1.6.4.tar.gz
  • Upload date:
  • Size: 1.2 MB
  • Tags: Source
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.9.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.5

File hashes

Hashes for alembic-1.6.4.tar.gz
Algorithm Hash digest
SHA256 becb572c6701c90ca249f97fc1ae231468cc9516df367a350901eeb9310a8d43
MD5 60d2e85ec95002c755dfb2428257b977
BLAKE2b-256 1ecf6f5961d2723f0d97fafeaaee8e25a6d9f9257cea89fd9e0b2573e3fb6342

See more details on using hashes here.

File details

Details for the file alembic-1.6.4-py2.py3-none-any.whl.

File metadata

  • Download URL: alembic-1.6.4-py2.py3-none-any.whl
  • Upload date:
  • Size: 164.7 kB
  • Tags: Python 2, Python 3
  • Uploaded using Trusted Publishing? No
  • Uploaded via: twine/3.4.1 importlib_metadata/3.9.1 pkginfo/1.7.0 requests/2.25.1 requests-toolbelt/0.9.1 tqdm/4.59.0 CPython/3.9.5

File hashes

Hashes for alembic-1.6.4-py2.py3-none-any.whl
Algorithm Hash digest
SHA256 dd0fd7109f82cd1d7ea64b26f287534a1ad1bc5deab79807926d5f3f5b3b517c
MD5 6262ab313c2a18ced813ad1ba691453a
BLAKE2b-256 ebbdc3486fd57a3eec5162a2e32e8f05880c990f0d92b03d268342d2e8fe7032

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