DDL Internals¶
These are some of the constructs used to generate migration
instructions. The APIs here build off of the sqlalchemy.schema.DDLElement
and sqlalchemy.ext.compiler_toplevel systems.
For programmatic usage of Alembic’s migration directives, the easiest route is to use the higher level functions given by Operation Directives.
- class alembic.ddl.base.AddColumn(name, column, schema=None)¶
- class alembic.ddl.base.AlterColumn(name, column_name, schema=None, existing_type=None, existing_nullable=None, existing_server_default=None, existing_comment=None)¶
- class alembic.ddl.base.AlterTable(table_name, schema=None)¶
Represent an ALTER TABLE statement.
Only the string name and optional schema name of the table is required, not a full Table object.
- class alembic.ddl.base.ColumnComment(name, column_name, comment, **kw)¶
- class alembic.ddl.base.ColumnDefault(name, column_name, default, **kw)¶
- class alembic.ddl.base.ColumnName(name, column_name, newname, **kw)¶
- class alembic.ddl.base.ColumnNullable(name, column_name, nullable, **kw)¶
- class alembic.ddl.base.ColumnType(name, column_name, type_, **kw)¶
- class alembic.ddl.base.ComputedColumnDefault(name, column_name, default, **kw)¶
- class alembic.ddl.base.DropColumn(name, column, schema=None)¶
- class alembic.ddl.base.IdentityColumnDefault(name, column_name, default, impl, **kw)¶
- class alembic.ddl.base.RenameTable(old_table_name, new_table_name, schema=None)¶
- alembic.ddl.base.add_column(compiler, column, **kw)¶
- alembic.ddl.base.alter_column(compiler, name)¶
- alembic.ddl.base.alter_table(compiler, name, schema)¶
- alembic.ddl.base.drop_column(compiler, name)¶
- alembic.ddl.base.format_column_name(compiler, name)¶
- alembic.ddl.base.format_server_default(compiler, default)¶
- alembic.ddl.base.format_table_name(compiler, name, schema)¶
- alembic.ddl.base.format_type(compiler, type_)¶
- alembic.ddl.base.quote_dotted(name, quote)¶
quote the elements of a dotted name
- alembic.ddl.base.visit_add_column(element, compiler, **kw)¶
- alembic.ddl.base.visit_column_default(element, compiler, **kw)¶
- alembic.ddl.base.visit_column_name(element, compiler, **kw)¶
- alembic.ddl.base.visit_column_nullable(element, compiler, **kw)¶
- alembic.ddl.base.visit_column_type(element, compiler, **kw)¶
- alembic.ddl.base.visit_computed_column(element, compiler, **kw)¶
- alembic.ddl.base.visit_drop_column(element, compiler, **kw)¶
- alembic.ddl.base.visit_identity_column(element, compiler, **kw)¶
- alembic.ddl.base.visit_rename_table(element, compiler, **kw)¶
- class alembic.ddl.impl.DefaultImpl(dialect, connection, as_sql, transactional_ddl, output_buffer, context_opts)¶
Provide the entrypoint for major migration operations, including database-specific behavioral variances.
While individual SQL/DDL constructs already provide for database-specific implementations, variances here allow for entirely different sequences of operations to take place for a particular migration, such as SQL Server’s special ‘IDENTITY INSERT’ step for bulk inserts.
- add_column(table_name, column, schema=None)¶
- add_constraint(const)¶
- alter_column(table_name, column_name, nullable=None, server_default=False, name=None, type_=None, schema=None, autoincrement=None, comment=False, existing_comment=None, existing_type=None, existing_server_default=None, existing_nullable=None, existing_autoincrement=None)¶
- autogen_column_reflect(inspector, table, column_info)¶
A hook that is attached to the ‘column_reflect’ event for when a Table is reflected from the database during the autogenerate process.
Dialects can elect to modify the information gathered here.
- property bind¶
- bulk_insert(table, rows, multiinsert=True)¶
- cast_for_batch_migrate(existing, existing_transfer, new_type)¶
- command_terminator = ';'¶
- compare_server_default(inspector_column, metadata_column, rendered_metadata_default, rendered_inspector_default)¶
- compare_type(inspector_column, metadata_column)¶
Returns True if there ARE differences between the types of the two columns. Takes impl.type_synonyms into account between retrospected and metadata types
- correct_for_autogen_constraints(conn_uniques, conn_indexes, metadata_unique_constraints, metadata_indexes)¶
- correct_for_autogen_foreignkeys(conn_fks, metadata_fks)¶
- create_column_comment(column)¶
- create_index(index)¶
- create_table(table)¶
- create_table_comment(table)¶
- drop_column(table_name, column, schema=None, **kw)¶
- drop_constraint(const)¶
- drop_index(index)¶
- drop_table(table)¶
- drop_table_comment(table)¶
- emit_begin()¶
Emit the string
BEGIN
, or the backend-specific equivalent, on the current connection context.This is used in offline mode and typically via
EnvironmentContext.begin_transaction()
.
- emit_commit()¶
Emit the string
COMMIT
, or the backend-specific equivalent, on the current connection context.This is used in offline mode and typically via
EnvironmentContext.begin_transaction()
.
- execute(sql, execution_options=None)¶
- classmethod get_by_dialect(dialect)¶
- identity_attrs_ignore = ('on_null',)¶
- prep_table_for_batch(batch_impl, table)¶
perform any operations needed on a table before a new one is created to replace it in batch mode.
the PG dialect uses this to drop constraints on the table before the new one uses those same names.
- rename_table(old_table_name, new_table_name, schema=None)¶
- render_ddl_sql_expr(expr, is_server_default=False, **kw)¶
Render a SQL expression that is typically a server default, index expression, etc.
New in version 1.0.11.
- render_type(type_obj, autogen_context)¶
- requires_recreate_in_batch(batch_op)¶
Return True if the given
BatchOperationsImpl
would need the table to be recreated and copied in order to proceed.Normally, only returns True on SQLite when operations other than add_column are present.
- start_migrations()¶
A hook called when
EnvironmentContext.run_migrations()
is called.Implementations can set up per-migration-run state here.
- static_output(text)¶
- transactional_ddl = False¶
- type_arg_extract = ()¶
- type_synonyms = ({'NUMERIC', 'DECIMAL'},)¶
- class alembic.ddl.impl.ImplMeta(classname, bases, dict_)¶
- class alembic.ddl.impl.Params(token0, tokens, args, kwargs)¶
Create new instance of Params(token0, tokens, args, kwargs)
- args¶
Alias for field number 2
- kwargs¶
Alias for field number 3
- token0¶
Alias for field number 0
- tokens¶
Alias for field number 1
MySQL¶
- class alembic.ddl.mysql.MariaDBImpl(dialect, connection, as_sql, transactional_ddl, output_buffer, context_opts)¶
Bases:
alembic.ddl.mysql.MySQLImpl
- class alembic.ddl.mysql.MySQLAlterDefault(name, column_name, default, schema=None)¶
Bases:
alembic.ddl.base.AlterColumn
- class alembic.ddl.mysql.MySQLChangeColumn(name, column_name, schema=None, newname=None, type_=None, nullable=None, default=False, autoincrement=None, comment=False)¶
Bases:
alembic.ddl.base.AlterColumn
- class alembic.ddl.mysql.MySQLImpl(dialect, connection, as_sql, transactional_ddl, output_buffer, context_opts)¶
Bases:
alembic.ddl.impl.DefaultImpl
- alter_column(table_name, column_name, nullable=None, server_default=False, name=None, type_=None, schema=None, existing_type=None, existing_server_default=None, existing_nullable=None, autoincrement=None, existing_autoincrement=None, comment=False, existing_comment=None, **kw)¶
- compare_server_default(inspector_column, metadata_column, rendered_metadata_default, rendered_inspector_default)¶
- correct_for_autogen_constraints(conn_unique_constraints, conn_indexes, metadata_unique_constraints, metadata_indexes)¶
- correct_for_autogen_foreignkeys(conn_fks, metadata_fks)¶
- drop_constraint(const)¶
- transactional_ddl = False¶
- type_arg_extract = ['character set ([\\w\\-_]+)', 'collate ([\\w\\-_]+)']¶
- type_synonyms = ({'NUMERIC', 'DECIMAL'}, {'TINYINT', 'BOOL'})¶
- class alembic.ddl.mysql.MySQLModifyColumn(name, column_name, schema=None, newname=None, type_=None, nullable=None, default=False, autoincrement=None, comment=False)¶
MS-SQL¶
- class alembic.ddl.mssql.MSSQLImpl(*arg, **kw)¶
Bases:
alembic.ddl.impl.DefaultImpl
- alter_column(table_name, column_name, nullable=None, server_default=False, name=None, type_=None, schema=None, existing_type=None, existing_server_default=None, existing_nullable=None, **kw)¶
- batch_separator = 'GO'¶
- bulk_insert(table, rows, **kw)¶
- compare_server_default(inspector_column, metadata_column, rendered_metadata_default, rendered_inspector_default)¶
- create_index(index)¶
- drop_column(table_name, column, schema=None, **kw)¶
- emit_begin()¶
Emit the string
BEGIN
, or the backend-specific equivalent, on the current connection context.This is used in offline mode and typically via
EnvironmentContext.begin_transaction()
.
- emit_commit()¶
Emit the string
COMMIT
, or the backend-specific equivalent, on the current connection context.This is used in offline mode and typically via
EnvironmentContext.begin_transaction()
.
- identity_attrs_ignore = ('minvalue', 'maxvalue', 'nominvalue', 'nomaxvalue', 'cycle', 'cache', 'order', 'on_null', 'order')¶
- transactional_ddl = True¶
- type_synonyms = ({'NUMERIC', 'DECIMAL'}, {'VARCHAR', 'NVARCHAR'})¶
- alembic.ddl.mssql.mssql_add_column(compiler, column, **kw)¶
- alembic.ddl.mssql.visit_add_column(element, compiler, **kw)¶
- alembic.ddl.mssql.visit_column_default(element, compiler, **kw)¶
- alembic.ddl.mssql.visit_column_nullable(element, compiler, **kw)¶
- alembic.ddl.mssql.visit_column_type(element, compiler, **kw)¶
- alembic.ddl.mssql.visit_rename_column(element, compiler, **kw)¶
- alembic.ddl.mssql.visit_rename_table(element, compiler, **kw)¶
Postgresql¶
- class alembic.ddl.postgresql.CreateExcludeConstraintOp(constraint_name, table_name, elements, where=None, schema=None, _orig_constraint=None, **kw)¶
Bases:
alembic.operations.ops.AddConstraintOp
Represent a create exclude constraint operation.
- classmethod batch_create_exclude_constraint(operations, constraint_name, *elements, **kw)¶
This method is proxied on the
BatchOperations
class, via theBatchOperations.create_exclude_constraint()
method.
- constraint_type = 'exclude'¶
- classmethod create_exclude_constraint(operations, constraint_name, table_name, *elements, **kw)¶
This method is proxied on the
Operations
class, via theOperations.create_exclude_constraint()
method.
- classmethod from_constraint(constraint)¶
- to_constraint(migration_context=None)¶
- class alembic.ddl.postgresql.PostgresqlColumnType(name, column_name, type_, **kw)¶
Bases:
alembic.ddl.base.AlterColumn
- class alembic.ddl.postgresql.PostgresqlImpl(dialect, connection, as_sql, transactional_ddl, output_buffer, context_opts)¶
Bases:
alembic.ddl.impl.DefaultImpl
- alter_column(table_name, column_name, nullable=None, server_default=False, name=None, type_=None, schema=None, autoincrement=None, existing_type=None, existing_server_default=None, existing_nullable=None, existing_autoincrement=None, **kw)¶
- autogen_column_reflect(inspector, table, column_info)¶
A hook that is attached to the ‘column_reflect’ event for when a Table is reflected from the database during the autogenerate process.
Dialects can elect to modify the information gathered here.
- compare_server_default(inspector_column, metadata_column, rendered_metadata_default, rendered_inspector_default)¶
- correct_for_autogen_constraints(conn_unique_constraints, conn_indexes, metadata_unique_constraints, metadata_indexes)¶
- identity_attrs_ignore = ('on_null', 'order')¶
- prep_table_for_batch(batch_impl, table)¶
perform any operations needed on a table before a new one is created to replace it in batch mode.
the PG dialect uses this to drop constraints on the table before the new one uses those same names.
- render_type(type_, autogen_context)¶
- transactional_ddl = True¶
- type_synonyms = ({'NUMERIC', 'DECIMAL'}, {'FLOAT', 'DOUBLE PRECISION'})¶
- alembic.ddl.postgresql.visit_column_comment(element, compiler, **kw)¶
- alembic.ddl.postgresql.visit_column_type(element, compiler, **kw)¶
- alembic.ddl.postgresql.visit_identity_column(element, compiler, **kw)¶
- alembic.ddl.postgresql.visit_rename_table(element, compiler, **kw)¶
SQLite¶
- class alembic.ddl.sqlite.SQLiteImpl(dialect, connection, as_sql, transactional_ddl, output_buffer, context_opts)¶
Bases:
alembic.ddl.impl.DefaultImpl
- add_constraint(const)¶
- autogen_column_reflect(inspector, table, column_info)¶
A hook that is attached to the ‘column_reflect’ event for when a Table is reflected from the database during the autogenerate process.
Dialects can elect to modify the information gathered here.
- cast_for_batch_migrate(existing, existing_transfer, new_type)¶
- compare_server_default(inspector_column, metadata_column, rendered_metadata_default, rendered_inspector_default)¶
- drop_constraint(const)¶
- render_ddl_sql_expr(expr, is_server_default=False, **kw)¶
Render a SQL expression that is typically a server default, index expression, etc.
New in version 1.0.11.
- requires_recreate_in_batch(batch_op)¶
Return True if the given
BatchOperationsImpl
would need the table to be recreated and copied in order to proceed.Normally, only returns True on SQLite when operations other than add_column are present.
- transactional_ddl = False¶
SQLite supports transactional DDL, but pysqlite does not: see: http://bugs.python.org/issue10740