diff options
author | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-09-09 12:24:43 -0400 |
---|---|---|
committer | Mike Bayer <mike_mp@zzzcomputing.com> | 2014-09-09 12:24:43 -0400 |
commit | 10d3556a5501563a4110733ecc9d9ba8eb172a99 (patch) | |
tree | 0646aa84d3f89c2f9cf59ba298e06d5c1f312ee1 /alembic/ddl | |
parent | effe3d89238923c5913449879adb1c685eb17c60 (diff) | |
download | alembic-10d3556a5501563a4110733ecc9d9ba8eb172a99.tar.gz |
- do an autopep8 pass for just about everything other
than line length
Diffstat (limited to 'alembic/ddl')
-rw-r--r-- | alembic/ddl/base.py | 60 | ||||
-rw-r--r-- | alembic/ddl/impl.py | 116 | ||||
-rw-r--r-- | alembic/ddl/mssql.py | 94 | ||||
-rw-r--r-- | alembic/ddl/mysql.py | 92 | ||||
-rw-r--r-- | alembic/ddl/oracle.py | 16 | ||||
-rw-r--r-- | alembic/ddl/postgresql.py | 11 | ||||
-rw-r--r-- | alembic/ddl/sqlite.py | 30 |
7 files changed, 241 insertions, 178 deletions
diff --git a/alembic/ddl/base.py b/alembic/ddl/base.py index 5d703a5..3a60926 100644 --- a/alembic/ddl/base.py +++ b/alembic/ddl/base.py @@ -5,62 +5,81 @@ from sqlalchemy.schema import DDLElement, Column from sqlalchemy import Integer from sqlalchemy import types as sqltypes + class AlterTable(DDLElement): + """Represent an ALTER TABLE statement. Only the string name and optional schema name of the table is required, not a full Table object. """ + def __init__(self, table_name, schema=None): self.table_name = table_name self.schema = schema + class RenameTable(AlterTable): + def __init__(self, old_table_name, new_table_name, schema=None): super(RenameTable, self).__init__(old_table_name, schema=schema) self.new_table_name = new_table_name + class AlterColumn(AlterTable): + def __init__(self, name, column_name, schema=None, - existing_type=None, - existing_nullable=None, - existing_server_default=None): + existing_type=None, + existing_nullable=None, + existing_server_default=None): super(AlterColumn, self).__init__(name, schema=schema) self.column_name = column_name - self.existing_type=sqltypes.to_instance(existing_type) \ - if existing_type is not None else None - self.existing_nullable=existing_nullable - self.existing_server_default=existing_server_default + self.existing_type = sqltypes.to_instance(existing_type) \ + if existing_type is not None else None + self.existing_nullable = existing_nullable + self.existing_server_default = existing_server_default + class ColumnNullable(AlterColumn): + def __init__(self, name, column_name, nullable, **kw): super(ColumnNullable, self).__init__(name, column_name, - **kw) + **kw) self.nullable = nullable + class ColumnType(AlterColumn): + def __init__(self, name, column_name, type_, **kw): super(ColumnType, self).__init__(name, column_name, - **kw) + **kw) self.type_ = sqltypes.to_instance(type_) + class ColumnName(AlterColumn): + def __init__(self, name, column_name, newname, **kw): super(ColumnName, self).__init__(name, column_name, **kw) self.newname = newname + class ColumnDefault(AlterColumn): + def __init__(self, name, column_name, default, **kw): super(ColumnDefault, self).__init__(name, column_name, **kw) self.default = default + class AddColumn(AlterTable): + def __init__(self, name, column, schema=None): super(AddColumn, self).__init__(name, schema=schema) self.column = column + class DropColumn(AlterTable): + def __init__(self, name, column, schema=None): super(DropColumn, self).__init__(name, schema=schema) self.column = column @@ -73,6 +92,7 @@ def visit_rename_table(element, compiler, **kw): format_table_name(compiler, element.new_table_name, element.schema) ) + @compiles(AddColumn) def visit_add_column(element, compiler, **kw): return "%s %s" % ( @@ -80,6 +100,7 @@ def visit_add_column(element, compiler, **kw): add_column(compiler, element.column, **kw) ) + @compiles(DropColumn) def visit_drop_column(element, compiler, **kw): return "%s %s" % ( @@ -87,6 +108,7 @@ def visit_drop_column(element, compiler, **kw): drop_column(compiler, element.column.name, **kw) ) + @compiles(ColumnNullable) def visit_column_nullable(element, compiler, **kw): return "%s %s %s" % ( @@ -95,6 +117,7 @@ def visit_column_nullable(element, compiler, **kw): "DROP NOT NULL" if element.nullable else "SET NOT NULL" ) + @compiles(ColumnType) def visit_column_type(element, compiler, **kw): return "%s %s %s" % ( @@ -103,6 +126,7 @@ def visit_column_type(element, compiler, **kw): "TYPE %s" % format_type(compiler, element.type_) ) + @compiles(ColumnName) def visit_column_name(element, compiler, **kw): return "%s RENAME %s TO %s" % ( @@ -111,23 +135,26 @@ def visit_column_name(element, compiler, **kw): format_column_name(compiler, element.newname) ) + @compiles(ColumnDefault) def visit_column_default(element, compiler, **kw): return "%s %s %s" % ( alter_table(compiler, element.table_name, element.schema), alter_column(compiler, element.column_name), "SET DEFAULT %s" % - format_server_default(compiler, element.default) + format_server_default(compiler, element.default) if element.default is not None else "DROP DEFAULT" ) + def quote_dotted(name, quote): """quote the elements of a dotted name""" result = '.'.join([quote(x) for x in name.split('.')]) return result + def format_table_name(compiler, name, schema): quote = functools.partial(compiler.preparer.quote, force=None) if schema: @@ -135,27 +162,32 @@ def format_table_name(compiler, name, schema): else: return quote(name) + def format_column_name(compiler, name): return compiler.preparer.quote(name, None) + def format_server_default(compiler, default): return compiler.get_column_default_string( - Column("x", Integer, server_default=default) - ) + Column("x", Integer, server_default=default) + ) + def format_type(compiler, type_): return compiler.dialect.type_compiler.process(type_) + def alter_table(compiler, name, schema): return "ALTER TABLE %s" % format_table_name(compiler, name, schema) + def drop_column(compiler, name): return 'DROP COLUMN %s' % format_column_name(compiler, name) + def alter_column(compiler, name): return 'ALTER COLUMN %s' % format_column_name(compiler, name) + def add_column(compiler, column, **kw): return "ADD COLUMN %s" % compiler.get_column_specification(column, **kw) - - diff --git a/alembic/ddl/impl.py b/alembic/ddl/impl.py index 664158f..a22a4fb 100644 --- a/alembic/ddl/impl.py +++ b/alembic/ddl/impl.py @@ -8,7 +8,9 @@ from ..compat import string_types, text_type, with_metaclass from .. import util from . import base + class ImplMeta(type): + def __init__(cls, classname, bases, dict_): newtype = type.__init__(cls, classname, bases, dict_) if '__dialect__' in dict_: @@ -17,7 +19,9 @@ class ImplMeta(type): _impls = {} + class DefaultImpl(with_metaclass(ImplMeta)): + """Provide the entrypoint for major migration operations, including database-specific behavioral variances. @@ -35,8 +39,8 @@ class DefaultImpl(with_metaclass(ImplMeta)): command_terminator = ";" def __init__(self, dialect, connection, as_sql, - transactional_ddl, output_buffer, - context_opts): + transactional_ddl, output_buffer, + context_opts): self.dialect = dialect self.connection = connection self.as_sql = as_sql @@ -59,8 +63,8 @@ class DefaultImpl(with_metaclass(ImplMeta)): return self.connection def _exec(self, construct, execution_options=None, - multiparams=(), - params=util.immutabledict()): + multiparams=(), + params=util.immutabledict()): if isinstance(construct, string_types): construct = text(construct) if self.as_sql: @@ -68,8 +72,8 @@ class DefaultImpl(with_metaclass(ImplMeta)): # TODO: coverage raise Exception("Execution arguments not allowed with as_sql") self.static_output(text_type( - construct.compile(dialect=self.dialect) - ).replace("\t", " ").strip() + self.command_terminator) + construct.compile(dialect=self.dialect) + ).replace("\t", " ").strip() + self.command_terminator) else: conn = self.connection if execution_options: @@ -80,49 +84,49 @@ class DefaultImpl(with_metaclass(ImplMeta)): self._exec(sql, execution_options) def alter_column(self, 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 - ): + 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 + ): if autoincrement is not None or existing_autoincrement is not None: util.warn("nautoincrement and existing_autoincrement only make sense for MySQL") if nullable is not None: self._exec(base.ColumnNullable(table_name, column_name, - nullable, schema=schema, - existing_type=existing_type, - existing_server_default=existing_server_default, - existing_nullable=existing_nullable, - )) + nullable, schema=schema, + existing_type=existing_type, + existing_server_default=existing_server_default, + existing_nullable=existing_nullable, + )) if server_default is not False: self._exec(base.ColumnDefault( - table_name, column_name, server_default, - schema=schema, - existing_type=existing_type, - existing_server_default=existing_server_default, - existing_nullable=existing_nullable, - )) + table_name, column_name, server_default, + schema=schema, + existing_type=existing_type, + existing_server_default=existing_server_default, + existing_nullable=existing_nullable, + )) if type_ is not None: self._exec(base.ColumnType( - table_name, column_name, type_, schema=schema, - existing_type=existing_type, - existing_server_default=existing_server_default, - existing_nullable=existing_nullable, - )) + table_name, column_name, type_, schema=schema, + existing_type=existing_type, + existing_server_default=existing_server_default, + existing_nullable=existing_nullable, + )) # do the new name last ;) if name is not None: self._exec(base.ColumnName( - table_name, column_name, name, schema=schema, - existing_type=existing_type, - existing_server_default=existing_server_default, - existing_nullable=existing_nullable, - )) + table_name, column_name, name, schema=schema, + existing_type=existing_type, + existing_server_default=existing_server_default, + existing_nullable=existing_nullable, + )) def add_column(self, table_name, column, schema=None): self._exec(base.AddColumn(table_name, column, schema=schema)) @@ -132,7 +136,7 @@ class DefaultImpl(with_metaclass(ImplMeta)): def add_constraint(self, const): if const._create_rule is None or \ - const._create_rule(self): + const._create_rule(self): self._exec(schema.AddConstraint(const)) def drop_constraint(self, const): @@ -140,18 +144,18 @@ class DefaultImpl(with_metaclass(ImplMeta)): def rename_table(self, old_table_name, new_table_name, schema=None): self._exec(base.RenameTable(old_table_name, - new_table_name, schema=schema)) + new_table_name, schema=schema)) def create_table(self, table): if util.sqla_07: table.dispatch.before_create(table, self.connection, - checkfirst=False, - _ddl_runner=self) + checkfirst=False, + _ddl_runner=self) self._exec(schema.CreateTable(table)) if util.sqla_07: table.dispatch.after_create(table, self.connection, checkfirst=False, - _ddl_runner=self) + _ddl_runner=self) for index in table.indexes: self._exec(schema.CreateIndex(index)) @@ -200,8 +204,8 @@ class DefaultImpl(with_metaclass(ImplMeta)): metadata_impl.__dict__.pop('_type_affinity', None) if conn_type._compare_type_affinity( - metadata_impl - ): + metadata_impl + ): comparator = _type_comparators.get(conn_type._type_affinity, None) return comparator and comparator(metadata_type, conn_type) @@ -209,9 +213,9 @@ class DefaultImpl(with_metaclass(ImplMeta)): return True def compare_server_default(self, inspector_column, - metadata_column, - rendered_metadata_default, - rendered_inspector_default): + metadata_column, + rendered_metadata_default, + rendered_inspector_default): return rendered_inspector_default != rendered_metadata_default def correct_for_autogen_constraints(self, conn_uniques, conn_indexes, @@ -247,9 +251,11 @@ class DefaultImpl(with_metaclass(ImplMeta)): """ self.static_output("COMMIT" + self.command_terminator) + class _literal_bindparam(_BindParamClause): pass + @compiles(_literal_bindparam) def _render_literal_bindparam(element, compiler, **kw): return compiler.render_literal_bindparam(element, **kw) @@ -268,6 +274,7 @@ def _textual_index_column(table, text_): class _textual_index_element(sql.ColumnElement): + """Wrap around a sqlalchemy text() construct in such a way that we appear like a column-oriented SQL expression to an Index construct. @@ -305,21 +312,18 @@ def _string_compare(t1, t2): t1.length is not None and \ t1.length != t2.length + def _numeric_compare(t1, t2): return \ ( - t1.precision is not None and \ + t1.precision is not None and t1.precision != t2.precision ) or \ ( - t1.scale is not None and \ + t1.scale is not None and t1.scale != t2.scale ) _type_comparators = { - sqltypes.String:_string_compare, - sqltypes.Numeric:_numeric_compare + sqltypes.String: _string_compare, + sqltypes.Numeric: _numeric_compare } - - - - diff --git a/alembic/ddl/mssql.py b/alembic/ddl/mssql.py index a3c67d6..d6c835c 100644 --- a/alembic/ddl/mssql.py +++ b/alembic/ddl/mssql.py @@ -4,9 +4,10 @@ from .. import util from .impl import DefaultImpl from .base import alter_table, AddColumn, ColumnName, RenameTable,\ format_table_name, format_column_name, ColumnNullable, alter_column,\ - format_server_default,ColumnDefault, format_type, ColumnType + format_server_default, ColumnDefault, format_type, ColumnType from sqlalchemy.sql.expression import ClauseElement, Executable + class MSSQLImpl(DefaultImpl): __dialect__ = 'mssql' transactional_ddl = True @@ -15,8 +16,8 @@ class MSSQLImpl(DefaultImpl): def __init__(self, *arg, **kw): super(MSSQLImpl, self).__init__(*arg, **kw) self.batch_separator = self.context_opts.get( - "mssql_batch_separator", - self.batch_separator) + "mssql_batch_separator", + self.batch_separator) def _exec(self, construct, *args, **kw): super(MSSQLImpl, self)._exec(construct, *args, **kw) @@ -32,17 +33,17 @@ class MSSQLImpl(DefaultImpl): self.static_output(self.batch_separator) def alter_column(self, 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 - ): + 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 + ): if nullable is not None and existing_type is None: if type_ is not None: @@ -52,70 +53,69 @@ class MSSQLImpl(DefaultImpl): type_ = None else: raise util.CommandError( - "MS-SQL ALTER COLUMN operations " - "with NULL or NOT NULL require the " - "existing_type or a new type_ be passed.") + "MS-SQL ALTER COLUMN operations " + "with NULL or NOT NULL require the " + "existing_type or a new type_ be passed.") super(MSSQLImpl, self).alter_column( - table_name, column_name, - nullable=nullable, - type_=type_, - schema=schema, - autoincrement=autoincrement, - existing_type=existing_type, - existing_nullable=existing_nullable, - existing_autoincrement=existing_autoincrement + table_name, column_name, + nullable=nullable, + type_=type_, + schema=schema, + autoincrement=autoincrement, + existing_type=existing_type, + existing_nullable=existing_nullable, + existing_autoincrement=existing_autoincrement ) if server_default is not False: if existing_server_default is not False or \ - server_default is None: + server_default is None: self._exec( _ExecDropConstraint( - table_name, column_name, - 'sys.default_constraints') + table_name, column_name, + 'sys.default_constraints') ) if server_default is not None: super(MSSQLImpl, self).alter_column( - table_name, column_name, - schema=schema, - server_default=server_default) + table_name, column_name, + schema=schema, + server_default=server_default) if name is not None: super(MSSQLImpl, self).alter_column( - table_name, column_name, - schema=schema, - name=name) + table_name, column_name, + schema=schema, + name=name) def bulk_insert(self, table, rows, **kw): if self.as_sql: self._exec( "SET IDENTITY_INSERT %s ON" % - self.dialect.identifier_preparer.format_table(table) + self.dialect.identifier_preparer.format_table(table) ) super(MSSQLImpl, self).bulk_insert(table, rows, **kw) self._exec( "SET IDENTITY_INSERT %s OFF" % - self.dialect.identifier_preparer.format_table(table) + self.dialect.identifier_preparer.format_table(table) ) else: super(MSSQLImpl, self).bulk_insert(table, rows, **kw) - def drop_column(self, table_name, column, **kw): drop_default = kw.pop('mssql_drop_default', False) if drop_default: self._exec( _ExecDropConstraint( - table_name, column, - 'sys.default_constraints') + table_name, column, + 'sys.default_constraints') ) drop_check = kw.pop('mssql_drop_check', False) if drop_check: self._exec( _ExecDropConstraint( - table_name, column, - 'sys.check_constraints') + table_name, column, + 'sys.check_constraints') ) drop_fks = kw.pop('mssql_drop_foreign_key', False) if drop_fks: @@ -124,13 +124,17 @@ class MSSQLImpl(DefaultImpl): ) super(MSSQLImpl, self).drop_column(table_name, column) + class _ExecDropConstraint(Executable, ClauseElement): + def __init__(self, tname, colname, type_): self.tname = tname self.colname = colname self.type_ = type_ + class _ExecDropFKConstraint(Executable, ClauseElement): + def __init__(self, tname, colname): self.tname = tname self.colname = colname @@ -152,6 +156,7 @@ exec('alter table %(tname_quoted)s drop constraint ' + @const_name)""" % { 'tname_quoted': format_table_name(compiler, tname, None), } + @compiles(_ExecDropFKConstraint, 'mssql') def _exec_drop_col_fk_constraint(element, compiler, **kw): tname, colname = element.tname, element.colname @@ -169,7 +174,6 @@ exec('alter table %(tname_quoted)s drop constraint ' + @const_name)""" % { } - @compiles(AddColumn, 'mssql') def visit_add_column(element, compiler, **kw): return "%s %s" % ( @@ -177,9 +181,11 @@ def visit_add_column(element, compiler, **kw): mssql_add_column(compiler, element.column, **kw) ) + def mssql_add_column(compiler, column, **kw): return "ADD %s" % compiler.get_column_specification(column, **kw) + @compiles(ColumnNullable, 'mssql') def visit_column_nullable(element, compiler, **kw): return "%s %s %s %s" % ( @@ -189,6 +195,7 @@ def visit_column_nullable(element, compiler, **kw): "NULL" if element.nullable else "NOT NULL" ) + @compiles(ColumnDefault, 'mssql') def visit_column_default(element, compiler, **kw): # TODO: there can also be a named constraint @@ -199,6 +206,7 @@ def visit_column_default(element, compiler, **kw): format_column_name(compiler, element.column_name) ) + @compiles(ColumnName, 'mssql') def visit_rename_column(element, compiler, **kw): return "EXEC sp_rename '%s.%s', %s, 'COLUMN'" % ( @@ -207,6 +215,7 @@ def visit_rename_column(element, compiler, **kw): format_column_name(compiler, element.newname) ) + @compiles(ColumnType, 'mssql') def visit_column_type(element, compiler, **kw): return "%s %s %s" % ( @@ -215,6 +224,7 @@ def visit_column_type(element, compiler, **kw): format_type(compiler, element.type_) ) + @compiles(RenameTable, 'mssql') def visit_rename_table(element, compiler, **kw): return "EXEC sp_rename '%s', %s" % ( diff --git a/alembic/ddl/mysql.py b/alembic/ddl/mysql.py index 58d5c70..7545df7 100644 --- a/alembic/ddl/mysql.py +++ b/alembic/ddl/mysql.py @@ -6,27 +6,28 @@ from ..compat import string_types from .. import util from .impl import DefaultImpl from .base import ColumnNullable, ColumnName, ColumnDefault, \ - ColumnType, AlterColumn, format_column_name, \ - format_server_default + ColumnType, AlterColumn, format_column_name, \ + format_server_default from .base import alter_table + class MySQLImpl(DefaultImpl): __dialect__ = 'mysql' transactional_ddl = False def alter_column(self, 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 - ): + 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 + ): if name is not None: self._exec( MySQLChangeColumn( @@ -34,33 +35,33 @@ class MySQLImpl(DefaultImpl): schema=schema, newname=name, nullable=nullable if nullable is not None else - existing_nullable - if existing_nullable is not None - else True, + existing_nullable + if existing_nullable is not None + else True, type_=type_ if type_ is not None else existing_type, default=server_default if server_default is not False - else existing_server_default, + else existing_server_default, autoincrement=autoincrement if autoincrement is not None - else existing_autoincrement + else existing_autoincrement ) ) elif nullable is not None or \ - type_ is not None or \ - autoincrement is not None: + type_ is not None or \ + autoincrement is not None: self._exec( MySQLModifyColumn( table_name, column_name, schema=schema, newname=name if name is not None else column_name, nullable=nullable if nullable is not None else - existing_nullable - if existing_nullable is not None - else True, + existing_nullable + if existing_nullable is not None + else True, type_=type_ if type_ is not None else existing_type, default=server_default if server_default is not False - else existing_server_default, + else existing_server_default, autoincrement=autoincrement if autoincrement is not None - else existing_autoincrement + else existing_autoincrement ) ) elif server_default is not False: @@ -99,7 +100,9 @@ class MySQLImpl(DefaultImpl): if idx.name in removed: metadata_indexes.remove(idx) + class MySQLAlterDefault(AlterColumn): + def __init__(self, name, column_name, default, schema=None): super(AlterColumn, self).__init__(name, schema=schema) self.column_name = column_name @@ -107,12 +110,13 @@ class MySQLAlterDefault(AlterColumn): class MySQLChangeColumn(AlterColumn): + def __init__(self, name, column_name, schema=None, - newname=None, - type_=None, - nullable=None, - default=False, - autoincrement=None): + newname=None, + type_=None, + nullable=None, + default=False, + autoincrement=None): super(AlterColumn, self).__init__(name, schema=schema) self.column_name = column_name self.nullable = nullable @@ -127,6 +131,7 @@ class MySQLChangeColumn(AlterColumn): self.type_ = sqltypes.to_instance(type_) + class MySQLModifyColumn(MySQLChangeColumn): pass @@ -137,8 +142,8 @@ class MySQLModifyColumn(MySQLChangeColumn): @compiles(ColumnType, 'mysql') def _mysql_doesnt_support_individual(element, compiler, **kw): raise NotImplementedError( - "Individual alter column constructs not supported by MySQL" - ) + "Individual alter column constructs not supported by MySQL" + ) @compiles(MySQLAlterDefault, "mysql") @@ -147,10 +152,11 @@ def _mysql_alter_default(element, compiler, **kw): alter_table(compiler, element.table_name, element.schema), format_column_name(compiler, element.column_name), "SET DEFAULT %s" % format_server_default(compiler, element.default) - if element.default is not None - else "DROP DEFAULT" + if element.default is not None + else "DROP DEFAULT" ) + @compiles(MySQLModifyColumn, "mysql") def _mysql_modify_column(element, compiler, **kw): return "%s MODIFY %s %s" % ( @@ -181,14 +187,16 @@ def _mysql_change_column(element, compiler, **kw): ), ) + def _render_value(compiler, expr): if isinstance(expr, string_types): return "'%s'" % expr else: return compiler.sql_compiler.process(expr) + def _mysql_colspec(compiler, nullable, server_default, type_, - autoincrement): + autoincrement): spec = "%s %s" % ( compiler.dialect.type_compiler.process(type_), "NULL" if nullable else "NOT NULL" @@ -200,6 +208,7 @@ def _mysql_colspec(compiler, nullable, server_default, type_, return spec + @compiles(schema.DropConstraint, "mysql") def _mysql_drop_constraint(element, compiler, **kw): """Redefine SQLAlchemy's drop constraint to @@ -207,15 +216,14 @@ def _mysql_drop_constraint(element, compiler, **kw): constraint = element.element if isinstance(constraint, (schema.ForeignKeyConstraint, - schema.PrimaryKeyConstraint, - schema.UniqueConstraint) - ): + schema.PrimaryKeyConstraint, + schema.UniqueConstraint) + ): return compiler.visit_drop_constraint(element, **kw) elif isinstance(constraint, schema.CheckConstraint): raise NotImplementedError( - "MySQL does not support CHECK constraints.") + "MySQL does not support CHECK constraints.") else: raise NotImplementedError( - "No generic 'DROP CONSTRAINT' in MySQL - " - "please specify constraint type") - + "No generic 'DROP CONSTRAINT' in MySQL - " + "please specify constraint type") diff --git a/alembic/ddl/oracle.py b/alembic/ddl/oracle.py index 28eb246..93e71e5 100644 --- a/alembic/ddl/oracle.py +++ b/alembic/ddl/oracle.py @@ -3,7 +3,8 @@ from sqlalchemy.ext.compiler import compiles from .impl import DefaultImpl from .base import alter_table, AddColumn, ColumnName, \ format_column_name, ColumnNullable, \ - format_server_default,ColumnDefault, format_type, ColumnType + format_server_default, ColumnDefault, format_type, ColumnType + class OracleImpl(DefaultImpl): __dialect__ = 'oracle' @@ -14,8 +15,8 @@ class OracleImpl(DefaultImpl): def __init__(self, *arg, **kw): super(OracleImpl, self).__init__(*arg, **kw) self.batch_separator = self.context_opts.get( - "oracle_batch_separator", - self.batch_separator) + "oracle_batch_separator", + self.batch_separator) def _exec(self, construct, *args, **kw): super(OracleImpl, self)._exec(construct, *args, **kw) @@ -28,6 +29,7 @@ class OracleImpl(DefaultImpl): def emit_commit(self): self._exec("COMMIT") + @compiles(AddColumn, 'oracle') def visit_add_column(element, compiler, **kw): return "%s %s" % ( @@ -35,6 +37,7 @@ def visit_add_column(element, compiler, **kw): add_column(compiler, element.column, **kw), ) + @compiles(ColumnNullable, 'oracle') def visit_column_nullable(element, compiler, **kw): return "%s %s %s" % ( @@ -43,6 +46,7 @@ def visit_column_nullable(element, compiler, **kw): "NULL" if element.nullable else "NOT NULL" ) + @compiles(ColumnType, 'oracle') def visit_column_type(element, compiler, **kw): return "%s %s %s" % ( @@ -51,6 +55,7 @@ def visit_column_type(element, compiler, **kw): "%s" % format_type(compiler, element.type_) ) + @compiles(ColumnName, 'oracle') def visit_column_name(element, compiler, **kw): return "%s RENAME COLUMN %s TO %s" % ( @@ -59,19 +64,22 @@ def visit_column_name(element, compiler, **kw): format_column_name(compiler, element.newname) ) + @compiles(ColumnDefault, 'oracle') def visit_column_default(element, compiler, **kw): return "%s %s %s" % ( alter_table(compiler, element.table_name, element.schema), alter_column(compiler, element.column_name), "DEFAULT %s" % - format_server_default(compiler, element.default) + format_server_default(compiler, element.default) if element.default is not None else "DEFAULT NULL" ) + def alter_column(compiler, name): return 'MODIFY %s' % format_column_name(compiler, name) + def add_column(compiler, column, **kw): return "ADD %s" % compiler.get_column_specification(column, **kw) diff --git a/alembic/ddl/postgresql.py b/alembic/ddl/postgresql.py index 27f31b0..eab1f4d 100644 --- a/alembic/ddl/postgresql.py +++ b/alembic/ddl/postgresql.py @@ -5,18 +5,19 @@ from .. import compat from .base import compiles, alter_table, format_table_name, RenameTable from .impl import DefaultImpl + class PostgresqlImpl(DefaultImpl): __dialect__ = 'postgresql' transactional_ddl = True def compare_server_default(self, inspector_column, - metadata_column, - rendered_metadata_default, - rendered_inspector_default): + metadata_column, + rendered_metadata_default, + rendered_inspector_default): # don't do defaults for SERIAL columns if metadata_column.primary_key and \ - metadata_column is metadata_column.table._autoincrement_column: + metadata_column is metadata_column.table._autoincrement_column: return False conn_col_default = rendered_inspector_default @@ -26,7 +27,7 @@ class PostgresqlImpl(DefaultImpl): if metadata_column.server_default is not None and \ isinstance(metadata_column.server_default.arg, - compat.string_types) and \ + compat.string_types) and \ not re.match(r"^'.+'$", rendered_metadata_default): rendered_metadata_default = "'%s'" % rendered_metadata_default diff --git a/alembic/ddl/sqlite.py b/alembic/ddl/sqlite.py index 85c829e..1a00be1 100644 --- a/alembic/ddl/sqlite.py +++ b/alembic/ddl/sqlite.py @@ -6,6 +6,7 @@ import re #from .base import AddColumn, alter_table #from sqlalchemy.schema import AddConstraint + class SQLiteImpl(DefaultImpl): __dialect__ = 'sqlite' @@ -19,21 +20,20 @@ class SQLiteImpl(DefaultImpl): # auto-gen constraint and an explicit one if const._create_rule is None: raise NotImplementedError( - "No support for ALTER of constraints in SQLite dialect") + "No support for ALTER of constraints in SQLite dialect") elif const._create_rule(self): util.warn("Skipping unsupported ALTER for " - "creation of implicit constraint") - + "creation of implicit constraint") def drop_constraint(self, const): if const._create_rule is None: raise NotImplementedError( - "No support for ALTER of constraints in SQLite dialect") + "No support for ALTER of constraints in SQLite dialect") def compare_server_default(self, inspector_column, - metadata_column, - rendered_metadata_default, - rendered_inspector_default): + metadata_column, + rendered_metadata_default, + rendered_inspector_default): rendered_metadata_default = re.sub(r"^'|'$", "", rendered_metadata_default) return rendered_inspector_default != repr(rendered_metadata_default) @@ -46,9 +46,9 @@ class SQLiteImpl(DefaultImpl): return tuple(sorted(uq.columns.keys())) conn_unique_sigs = set( - uq_sig(uq) - for uq in conn_unique_constraints - ) + uq_sig(uq) + for uq in conn_unique_constraints + ) for idx in list(metadata_unique_constraints): # SQLite backend can't report on unnamed UNIQUE constraints, @@ -65,18 +65,18 @@ class SQLiteImpl(DefaultImpl): conn_uniques.remove(idx) #@compiles(AddColumn, 'sqlite') -#def visit_add_column(element, compiler, **kw): +# def visit_add_column(element, compiler, **kw): # return "%s %s" % ( # alter_table(compiler, element.table_name, element.schema), # add_column(compiler, element.column, **kw) # ) -#def add_column(compiler, column, **kw): +# def add_column(compiler, column, **kw): # text = "ADD COLUMN %s" % compiler.get_column_specification(column, **kw) -# # need to modify SQLAlchemy so that the CHECK associated with a Boolean -# # or Enum gets placed as part of the column constraints, not the Table -# # see ticket 98 +# need to modify SQLAlchemy so that the CHECK associated with a Boolean +# or Enum gets placed as part of the column constraints, not the Table +# see ticket 98 # for const in column.constraints: # text += compiler.process(AddConstraint(const)) # return text |