From: Mike Bayer Date: Thu, 23 Aug 2012 01:33:10 +0000 (-0400) Subject: - tests for pull request #21 X-Git-Tag: rel_0_4_0~12 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=385b88ff05645991fc668a1e69b04ab54acff461;p=thirdparty%2Fsqlalchemy%2Falembic.git - tests for pull request #21 --- diff --git a/alembic/ddl/mysql.py b/alembic/ddl/mysql.py index 444b247d..012aae34 100644 --- a/alembic/ddl/mysql.py +++ b/alembic/ddl/mysql.py @@ -1,5 +1,6 @@ from alembic.ddl.impl import DefaultImpl -from alembic.ddl.base import ColumnNullable, ColumnName, ColumnDefault, ColumnType, AlterColumn +from alembic.ddl.base import ColumnNullable, ColumnName, ColumnDefault, \ + ColumnType, AlterColumn from sqlalchemy.ext.compiler import compiles from alembic.ddl.base import alter_table from alembic import util @@ -26,12 +27,15 @@ class MySQLImpl(DefaultImpl): 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 + nullable=nullable if nullable is not None else + 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, - autoincrement=autoincrement if autoincrement is not None else existing_autoincrement + default=server_default if server_default is not False + else existing_server_default, + autoincrement=autoincrement if autoincrement is not None + else existing_autoincrement ) ) @@ -81,13 +85,14 @@ def _mysql_alter_column(element, compiler, **kw): ), ) -def render_value(compiler, expr): +def _render_value(compiler, expr): if isinstance(expr, basestring): return "'%s'" % expr else: return compiler.sql_compiler.process(expr) -def _mysql_colspec(compiler, name, nullable, server_default, type_, autoincrement): +def _mysql_colspec(compiler, name, nullable, server_default, type_, + autoincrement): spec = "%s %s %s" % ( name, compiler.dialect.type_compiler.process(type_), @@ -96,7 +101,7 @@ def _mysql_colspec(compiler, name, nullable, server_default, type_, autoincremen if autoincrement is not None: spec += " AUTO_INCREMENT" if server_default != False: - spec += " DEFAULT %s" % render_value(compiler, server_default) + spec += " DEFAULT %s" % _render_value(compiler, server_default) return spec diff --git a/alembic/operations.py b/alembic/operations.py index f6997689..d2f98619 100644 --- a/alembic/operations.py +++ b/alembic/operations.py @@ -203,9 +203,10 @@ class Operations(object): :param type_: Optional; a :class:`~sqlalchemy.types.TypeEngine` type object to specify a change to the column's type. For SQLAlchemy types that also indicate a constraint (i.e. - :class:`~sqlalchemy.types.Boolean`, - :class:`~sqlalchemy.types.Enum`), + :class:`~sqlalchemy.types.Boolean`, :class:`~sqlalchemy.types.Enum`), the constraint is also generated. + :param autoincrement: set the ``AUTO_INCREMENT`` flag of the column; + currently understood by the MySQL dialect. :param existing_type: Optional; a :class:`~sqlalchemy.types.TypeEngine` type object to specify the previous type. This @@ -213,8 +214,7 @@ class Operations(object): don't otherwise specify a new type, as well as for when nullability is being changed on a SQL Server column. It is also used if the type is a so-called - SQLlchemy "schema" type which - may define a constraint (i.e. + SQLlchemy "schema" type which may define a constraint (i.e. :class:`~sqlalchemy.types.Boolean`, :class:`~sqlalchemy.types.Enum`), so that the constraint can be dropped. @@ -225,7 +225,9 @@ 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 + :param existing_autoincrement: Optional; the existing autoincrement + of the column. Used for MySQL's system of altering a column + that specifies ``AUTO_INCREMENT``. """ compiler = self.impl.dialect.statement_compiler( diff --git a/tests/test_mysql.py b/tests/test_mysql.py index 02f04192..28c1fd6a 100644 --- a/tests/test_mysql.py +++ b/tests/test_mysql.py @@ -29,6 +29,22 @@ def test_rename_column_serv_compiled_default(): "ALTER TABLE t1 CHANGE c1 c2 INTEGER NULL DEFAULT utc_thing(CURRENT_TIMESTAMP)" ) +def test_rename_column_autoincrement(): + context = op_fixture('mysql') + op.alter_column('t1', 'c1', name="c2", existing_type=Integer, + existing_autoincrement=True) + context.assert_( + 'ALTER TABLE t1 CHANGE c1 c2 INTEGER NULL AUTO_INCREMENT' + ) + +def test_col_add_autoincrement(): + context = op_fixture('mysql') + op.alter_column('t1', 'c1', name="c2", existing_type=Integer, + autoincrement=True) + context.assert_( + 'ALTER TABLE t1 CHANGE c1 c2 INTEGER NULL AUTO_INCREMENT' + ) + def test_col_nullable(): context = op_fixture('mysql') op.alter_column('t1', 'c1', nullable=False, existing_type=Integer)