Operation Directives — Alembic 0.9.5 documentation

# Operation Directives

Note

this section discusses the internal API of Alembic as regards the internal system of defining migration operation directives. This section is only useful for developers who wish to extend the capabilities of Alembic. For end-user guidance on Alembic migration operations, please see Operation Reference.

Within migration scripts, actual database migration operations are handled via an instance of `Operations`. The `Operations` class lists out available migration operations that are linked to a `MigrationContext`, which communicates instructions originated by the `Operations` object into SQL that is sent to a database or SQL output stream.

Most methods on the `Operations` class are generated dynamically using a “plugin” system, described in the next section Operation Plugins. Additionally, when Alembic migration scripts actually run, the methods on the current `Operations` object are proxied out to the `alembic.op` module, so that they are available using module-style access.

For an overview of how to use an `Operations` object directly in programs, as well as for reference to the standard operation methods as well as “batch” methods, see Operation Reference.

## Operation Plugins

The Operations object is extensible using a plugin system. This system allows one to add new `op.<some_operation>` methods at runtime. The steps to use this system are to first create a subclass of `MigrateOperation`, register it using the `Operations.register_operation()` class decorator, then build a default “implementation” function which is established using the `Operations.implementation_for()` decorator.

New in version 0.8.0: - the `Operations` class is now an open namespace that is extensible via the creation of new `MigrateOperation` subclasses.

Below we illustrate a very simple operation `CreateSequenceOp` which will implement a new method `op.create_sequence()` for use in migration scripts:

    from alembic.operations import Operations, MigrateOperation

    @Operations.register_operation("create_sequence")
    class CreateSequenceOp(MigrateOperation):
        """Create a SEQUENCE."""

        def __init__(self, sequence_name, schema=None):
            self.sequence_name = sequence_name
            self.schema = schema

        @classmethod
        def create_sequence(cls, operations, sequence_name, **kw):
            """Issue a "CREATE SEQUENCE" instruction."""

            op = CreateSequenceOp(sequence_name, **kw)
            return operations.invoke(op)

        def reverse(self):
            # only needed to support autogenerate
            return DropSequenceOp(self.sequence_name, schema=self.schema)

    @Operations.register_operation("drop_sequence")
    class DropSequenceOp(MigrateOperation):
        """Drop a SEQUENCE."""

        def __init__(self, sequence_name, schema=None):
            self.sequence_name = sequence_name
            self.schema = schema

        @classmethod
        def drop_sequence(cls, operations, sequence_name, **kw):
            """Issue a "DROP SEQUENCE" instruction."""

            op = DropSequenceOp(sequence_name, **kw)
            return operations.invoke(op)

        def reverse(self):
            # only needed to support autogenerate
            return CreateSequenceOp(self.sequence_name, schema=self.schema)

Above, the `CreateSequenceOp` and `DropSequenceOp` classes represent new operations that will be available as `op.create_sequence()` and `op.drop_sequence()`. The reason the operations are represented as stateful classes is so that an operation and a specific set of arguments can be represented generically; the state can then correspond to different kinds of operations, such as invoking the instruction against a database, or autogenerating Python code for the operation into a script.

In order to establish the migrate-script behavior of the new operations, we use the `Operations.implementation_for()` decorator:

    @Operations.implementation_for(CreateSequenceOp)
    def create_sequence(operations, operation):
        if operation.schema is not None:
            name = "%s.%s" % (operation.schema, operation.sequence_name)
        else:
            name = operation.sequence_name
        operations.execute("CREATE SEQUENCE %s" % name)

    @Operations.implementation_for(DropSequenceOp)
    def drop_sequence(operations, operation):
        if operation.schema is not None:
            name = "%s.%s" % (operation.schema, operation.sequence_name)
        else:
            name = operation.sequence_name
        operations.execute("DROP SEQUENCE %s" % name)

Above, we use the simplest possible technique of invoking our DDL, which is just to call `Operations.execute()` with literal SQL. If this is all a custom operation needs, then this is fine. However, options for more comprehensive support include building out a custom SQL construct, as documented at Custom SQL Constructs and Compilation Extension.

With the above two steps, a migration script can now use new methods `op.create_sequence()` and `op.drop_sequence()` that will proxy to our object as a classmethod:

    def upgrade():
        op.create_sequence("my_sequence")

    def downgrade():
        op.drop_sequence("my_sequence")

The registration of new operations only needs to occur in time for the `env.py` script to invoke `MigrationContext.run_migrations()`; within the module level of the `env.py` script is sufficient.

See also

Autogenerating Custom Operation Directives - how to add autogenerate support to custom operations.

New in version 0.8: - the migration operations available via the `Operations` class as well as the `alembic.op` namespace is now extensible using a plugin system.

## Built-in Operation Objects

The migration operations present on `Operations` are themselves delivered via operation objects that represent an operation and its arguments. All operations descend from the `MigrateOperation` class, and are registered with the `Operations` class using the `Operations.register_operation()` class decorator. The `MigrateOperation` objects also serve as the basis for how the autogenerate system renders new migration scripts.

See also

Operation Plugins

Customizing Revision Generation

The built-in operation objects are listed below.

class `alembic.operations.ops.` `AddColumnOp`(table_name, column, schema=None)

  Represent an add column operation.

  classmethod `add_column`(operations, table_name, column, schema=None)

    This method is proxied on the `Operations` class, via the `Operations.add_column()` method.

  classmethod `batch_add_column`(operations, column)

    This method is proxied on the `BatchOperations` class, via the `BatchOperations.add_column()` method.

class `alembic.operations.ops.` `AddConstraintOp`

  Represent an add constraint operation.

class `alembic.operations.ops.` `AlterColumnOp`(table_name, column_name, schema=None, existing_type=None, existing_server_default=False, existing_nullable=None, modify_nullable=None, modify_server_default=False, modify_name=None, modify_type=None, **kw)

  Represent an alter column operation.

  classmethod `alter_column`(operations, table_name, column_name, nullable=None, server_default=False, new_column_name=None, type_=None, existing_type=None, existing_server_default=False, existing_nullable=None, schema=None, **kw)

    This method is proxied on the `Operations` class, via the `Operations.alter_column()` method.

  classmethod `batch_alter_column`(operations, column_name, nullable=None, server_default=False, new_column_name=None, type_=None, existing_type=None, existing_server_default=False, existing_nullable=None, **kw)

    This method is proxied on the `BatchOperations` class, via the `BatchOperations.alter_column()` method.

class `alembic.operations.ops.` `AlterTableOp`(table_name, schema=None)

  Represent an alter table operation.

class `alembic.operations.ops.` `BulkInsertOp`(table, rows, multiinsert=True)

  Represent a bulk insert operation.

  classmethod `bulk_insert`(operations, table, rows, multiinsert=True)

    This method is proxied on the `Operations` class, via the `Operations.bulk_insert()` method.

class `alembic.operations.ops.` `CreateCheckConstraintOp`(constraint_name, table_name, condition, schema=None, _orig_constraint=None, **kw)

  Represent a create check constraint operation.

  classmethod `batch_create_check_constraint`(operations, constraint_name, condition, **kw)

    This method is proxied on the `BatchOperations` class, via the `BatchOperations.create_check_constraint()` method.

  classmethod `create_check_constraint`(operations, constraint_name, table_name, condition, schema=None, **kw)

    This method is proxied on the `Operations` class, via the `Operations.create_check_constraint()` method.

class `alembic.operations.ops.` `CreateForeignKeyOp`(constraint_name, source_table, referent_table, local_cols, remote_cols, _orig_constraint=None, **kw)

  Represent a create foreign key constraint operation.

  classmethod `batch_create_foreign_key`(operations, constraint_name, referent_table, local_cols, remote_cols, referent_schema=None, onupdate=None, ondelete=None, deferrable=None, initially=None, match=None, **dialect_kw)

    This method is proxied on the `BatchOperations` class, via the `BatchOperations.create_foreign_key()` method.

  classmethod `create_foreign_key`(operations, constraint_name, source_table, referent_table, local_cols, remote_cols, onupdate=None, ondelete=None, deferrable=None, initially=None, match=None, source_schema=None, referent_schema=None, **dialect_kw)

    This method is proxied on the `Operations` class, via the `Operations.create_foreign_key()` method.

class `alembic.operations.ops.` `CreateIndexOp`(index_name, table_name, columns, schema=None, unique=False, _orig_index=None, **kw)

  Represent a create index operation.

  classmethod `batch_create_index`(operations, index_name, columns, **kw)

    This method is proxied on the `BatchOperations` class, via the `BatchOperations.create_index()` method.

  classmethod `create_index`(operations, index_name, table_name, columns, schema=None, unique=False, **kw)

    This method is proxied on the `Operations` class, via the `Operations.create_index()` method.

class `alembic.operations.ops.` `CreatePrimaryKeyOp`(constraint_name, table_name, columns, schema=None, _orig_constraint=None, **kw)

  Represent a create primary key operation.

  classmethod `batch_create_primary_key`(operations, constraint_name, columns)

    This method is proxied on the `BatchOperations` class, via the `BatchOperations.create_primary_key()` method.

  classmethod `create_primary_key`(operations, constraint_name, table_name, columns, schema=None)

    This method is proxied on the `Operations` class, via the `Operations.create_primary_key()` method.

class `alembic.operations.ops.` `CreateTableOp`(table_name, columns, schema=None, _orig_table=None, **kw)

  Represent a create table operation.

  classmethod `create_table`(operations, table_name, *columns, **kw)

    This method is proxied on the `Operations` class, via the `Operations.create_table()` method.

class `alembic.operations.ops.` `CreateUniqueConstraintOp`(constraint_name, table_name, columns, schema=None, _orig_constraint=None, **kw)

  Represent a create unique constraint operation.

  classmethod `batch_create_unique_constraint`(operations, constraint_name, columns, **kw)

    This method is proxied on the `BatchOperations` class, via the `BatchOperations.create_unique_constraint()` method.

  classmethod `create_unique_constraint`(operations, constraint_name, table_name, columns, schema=None, **kw)

    This method is proxied on the `Operations` class, via the `Operations.create_unique_constraint()` method.

class `alembic.operations.ops.` `DowngradeOps`(ops=(), downgrade_token='downgrades')

  contains a sequence of operations that would apply to the ‘downgrade’ stream of a script.

  See also

  Customizing Revision Generation

class `alembic.operations.ops.` `DropColumnOp`(table_name, column_name, schema=None, _orig_column=None, **kw)

  Represent a drop column operation.

  classmethod `batch_drop_column`(operations, column_name, **kw)

    This method is proxied on the `BatchOperations` class, via the `BatchOperations.drop_column()` method.

  classmethod `drop_column`(operations, table_name, column_name, schema=None, **kw)

    This method is proxied on the `Operations` class, via the `Operations.drop_column()` method.

class `alembic.operations.ops.` `DropConstraintOp`(constraint_name, table_name, type_=None, schema=None, _orig_constraint=None)

  Represent a drop constraint operation.

  classmethod `batch_drop_constraint`(operations, constraint_name, type_=None)

    This method is proxied on the `BatchOperations` class, via the `BatchOperations.drop_constraint()` method.

  classmethod `drop_constraint`(operations, constraint_name, table_name, type_=None, schema=None)

    This method is proxied on the `Operations` class, via the `Operations.drop_constraint()` method.

class `alembic.operations.ops.` `DropIndexOp`(index_name, table_name=None, schema=None, _orig_index=None, **kw)

  Represent a drop index operation.

  classmethod `batch_drop_index`(operations, index_name, **kw)

    This method is proxied on the `BatchOperations` class, via the `BatchOperations.drop_index()` method.

  classmethod `drop_index`(operations, index_name, table_name=None, schema=None, **kw)

    This method is proxied on the `Operations` class, via the `Operations.drop_index()` method.

class `alembic.operations.ops.` `DropTableOp`(table_name, schema=None, table_kw=None, _orig_table=None)

  Represent a drop table operation.

  classmethod `drop_table`(operations, table_name, schema=None, **kw)

    This method is proxied on the `Operations` class, via the `Operations.drop_table()` method.

class `alembic.operations.ops.` `ExecuteSQLOp`(sqltext, execution_options=None)

  Represent an execute SQL operation.

  classmethod `execute`(operations, sqltext, execution_options=None)

    This method is proxied on the `Operations` class, via the `Operations.execute()` method.

class `alembic.operations.ops.` `MigrateOperation`

  base class for migration command and organization objects.

  This system is part of the operation extensibility API.

  New in version 0.8.0.

  See also

  Built-in Operation Objects

  Operation Plugins

  Customizing Revision Generation

  `info`

    A dictionary that may be used to store arbitrary information along with this `MigrateOperation` object.

class `alembic.operations.ops.` `MigrationScript`(rev_id, upgrade_ops, downgrade_ops, message=None, imports=set([]), head=None, splice=None, branch_label=None, version_path=None, depends_on=None)

  represents a migration script.

  E.g. when autogenerate encounters this object, this corresponds to the production of an actual script file.

  A normal `MigrationScript` object would contain a single `UpgradeOps` and a single `DowngradeOps` directive. These are accessible via the `.upgrade_ops` and `.downgrade_ops` attributes.

  In the case of an autogenerate operation that runs multiple times, such as the multiple database example in the “multidb” template, the `.upgrade_ops` and `.downgrade_ops` attributes are disabled, and instead these objects should be accessed via the `.upgrade_ops_list` and `.downgrade_ops_list` list-based attributes. These latter attributes are always available at the very least as single-element lists.

  Changed in version 0.8.1: the `.upgrade_ops` and `.downgrade_ops` attributes should be accessed via the `.upgrade_ops_list` and `.downgrade_ops_list` attributes if multiple autogenerate passes proceed on the same `MigrationScript` object.

  See also

  Customizing Revision Generation

  `downgrade_ops`

    An instance of `DowngradeOps`.

    See also

    `MigrationScript.downgrade_ops_list`

  `downgrade_ops_list`

    A list of `DowngradeOps` instances.

    This is used in place of the `MigrationScript.downgrade_ops` attribute when dealing with a revision operation that does multiple autogenerate passes.

    New in version 0.8.1.

  `upgrade_ops`

    An instance of `UpgradeOps`.

    See also

    `MigrationScript.upgrade_ops_list`

  `upgrade_ops_list`

    A list of `UpgradeOps` instances.

    This is used in place of the `MigrationScript.upgrade_ops` attribute when dealing with a revision operation that does multiple autogenerate passes.

    New in version 0.8.1.

class `alembic.operations.ops.` `ModifyTableOps`(table_name, ops, schema=None)

  Contains a sequence of operations that all apply to a single Table.

class `alembic.operations.ops.` `OpContainer`(ops=())

  Represent a sequence of operations operation.

class `alembic.operations.ops.` `RenameTableOp`(old_table_name, new_table_name, schema=None)

  Represent a rename table operation.

  classmethod `rename_table`(operations, old_table_name, new_table_name, schema=None)

    This method is proxied on the `Operations` class, via the `Operations.rename_table()` method.

class `alembic.operations.ops.` `UpgradeOps`(ops=(), upgrade_token='upgrades')

  contains a sequence of operations that would apply to the ‘upgrade’ stream of a script.

  See also

  Customizing Revision Generation