From: Moriyoshi Koizumi Date: Tue, 24 Jul 2012 16:42:15 +0000 (+0900) Subject: Support autoincrement and existing_autoincrement in alter_column for MySQL dialect. X-Git-Tag: rel_0_4_0~13^2 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=b71bb01e7c4b0a10db039d43803a34fe90ddc7d8;p=thirdparty%2Fsqlalchemy%2Falembic.git Support autoincrement and existing_autoincrement in alter_column for MySQL dialect. It'd be impossible to alter an autoincremented column without this extension. --- diff --git a/alembic/ddl/impl.py b/alembic/ddl/impl.py index 95ead4c5..32e15e66 100644 --- a/alembic/ddl/impl.py +++ b/alembic/ddl/impl.py @@ -83,11 +83,14 @@ class DefaultImpl(object): name=None, type_=None, schema=None, + autoincrement=None, existing_type=None, existing_server_default=None, - existing_nullable=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, diff --git a/alembic/ddl/mssql.py b/alembic/ddl/mssql.py index 42e84478..d7eaba67 100644 --- a/alembic/ddl/mssql.py +++ b/alembic/ddl/mssql.py @@ -30,9 +30,11 @@ class MSSQLImpl(DefaultImpl): name=None, type_=None, schema=None, + autoincrement=None, existing_type=None, existing_server_default=None, - existing_nullable=None + existing_nullable=None, + existing_autoincrement=None ): if nullable is not None and existing_type is None: @@ -52,8 +54,10 @@ class MSSQLImpl(DefaultImpl): nullable=nullable, type_=type_, schema=schema, + autoincrement=autoincrement, existing_type=existing_type, - existing_nullable=existing_nullable + existing_nullable=existing_nullable, + existing_autoincrement=existing_autoincrement ) if server_default is not False: diff --git a/alembic/ddl/mysql.py b/alembic/ddl/mysql.py index 17920b4f..4d23c17b 100644 --- a/alembic/ddl/mysql.py +++ b/alembic/ddl/mysql.py @@ -15,9 +15,11 @@ class MySQLImpl(DefaultImpl): name=None, type_=None, schema=None, + autoincrement=None, existing_type=None, existing_server_default=None, - existing_nullable=None + existing_nullable=None, + existing_autoincrement=None ): self._exec( MySQLAlterColumn( @@ -29,6 +31,7 @@ class MySQLImpl(DefaultImpl): 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, + autoincrement=autoincrement if autoincrement is not None else existing_autoincrement ) ) @@ -37,12 +40,14 @@ class MySQLAlterColumn(AlterColumn): newname=None, type_=None, nullable=None, - default=False): + default=False, + autoincrement=None): super(AlterColumn, self).__init__(name, schema=schema) self.column_name = column_name self.nullable = nullable self.newname = newname self.default = default + self.autoincrement = autoincrement if type_ is None: raise util.CommandError( "All MySQL ALTER COLUMN operations " @@ -71,7 +76,8 @@ def _mysql_alter_column(element, compiler, **kw): name=element.newname, nullable=element.nullable, server_default=element.default, - type_=element.type_ + type_=element.type_, + autoincrement=element.autoincrement ), ) @@ -81,12 +87,14 @@ def render_value(compiler, expr): else: return compiler.sql_compiler.process(expr) -def _mysql_colspec(compiler, name, nullable, server_default, type_): +def _mysql_colspec(compiler, name, nullable, server_default, type_, autoincrement): spec = "%s %s %s" % ( name, compiler.dialect.type_compiler.process(type_), "NULL" if nullable else "NOT NULL" ) + if autoincrement is not None: + spec += " AUTO_INCREMENT" if server_default != False: spec += " DEFAULT %s" % render_value(compiler, server_default) diff --git a/alembic/operations.py b/alembic/operations.py index 2eef7cba..beb0f9fb 100644 --- a/alembic/operations.py +++ b/alembic/operations.py @@ -151,9 +151,11 @@ class Operations(object): server_default=False, name=None, type_=None, + autoincrement=None, existing_type=None, existing_server_default=False, existing_nullable=None, + existing_autoincrement=None ): """Issue an "alter column" instruction using the current migration context. @@ -218,6 +220,7 @@ class Operations(object): :param existing_nullable: Optional; the existing nullability of the column. Required on MySQL if the existing nullability is not being changed; else MySQL sets this to NULL. + :param existing_autoincrement: Optional; the """ if existing_type: @@ -233,9 +236,11 @@ class Operations(object): server_default=server_default, name=name, type_=type_, + autoincrement=autoincrement, existing_type=existing_type, existing_server_default=existing_server_default, existing_nullable=existing_nullable, + existing_autoincrement=existing_autoincrement ) if type_: