Operation Reference¶
This file provides documentation on Alembic migration directives.
The directives here are used within user-defined migration files,
within the upgrade()
and downgrade()
functions, as well as
any functions further invoked by those.
All directives exist as methods on a class called Operations
.
When migration scripts are run, this object is made available
to the script via the alembic.op
datamember, which is
a proxy to an actual instance of Operations
.
Currently, alembic.op
is a real Python module, populated
with individual proxies for each method on Operations
,
so symbols can be imported safely from the alembic.op
namespace.
The Operations
system is also fully extensible. See
Operation Plugins for details on this.
A key design philosophy to the Operation Directives methods is that
to the greatest degree possible, they internally generate the
appropriate SQLAlchemy metadata, typically involving
Table
and Constraint
objects. This so that migration instructions can be
given in terms of just the string names and/or flags involved.
The exceptions to this
rule include the add_column()
and create_table()
directives, which require full Column
objects, though the table metadata is still generated here.
The functions here all require that a MigrationContext
has been
configured within the env.py
script first, which is typically
via EnvironmentContext.configure()
. Under normal
circumstances they are called from an actual migration script, which
itself would be invoked by the EnvironmentContext.run_migrations()
method.
-
class
alembic.operations.
Operations
(migration_context, impl=None)¶ Define high level migration operations.
Each operation corresponds to some schema migration operation, executed against a particular
MigrationContext
which in turn represents connectivity to a database, or a file output stream.While
Operations
is normally configured as part of theEnvironmentContext.run_migrations()
method called from anenv.py
script, a standaloneOperations
instance can be made for use cases external to regular Alembic migrations by passing in aMigrationContext
:from alembic.migration import MigrationContext from alembic.operations import Operations conn = myengine.connect() ctx = MigrationContext.configure(conn) op = Operations(ctx) op.alter_column("t", "c", nullable=True)
Note that as of 0.8, most of the methods on this class are produced dynamically using the
Operations.register_operation()
method.Construct a new
Operations
- Parameters
migration_context¶ – a
MigrationContext
instance.
-
class
alembic.operations.
BatchOperations
(migration_context, impl=None)¶ Modifies the interface
Operations
for batch mode.This basically omits the
table_name
andschema
parameters from associated methods, as these are a given when running under batch mode.See also
Operations.batch_alter_table()
Note that as of 0.8, most of the methods on this class are produced dynamically using the
Operations.register_operation()
method.Construct a new
Operations
- Parameters
migration_context¶ – a
MigrationContext
instance.