]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- tests for pull request #21
authorMike Bayer <mike_mp@zzzcomputing.com>
Thu, 23 Aug 2012 01:33:10 +0000 (21:33 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 23 Aug 2012 01:33:10 +0000 (21:33 -0400)
alembic/ddl/mysql.py
alembic/operations.py
tests/test_mysql.py

index 444b247d12c29fb63666bc8f14ca2b6866949da6..012aae34e065a8ee700481bb1a3a287897b90354 100644 (file)
@@ -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
 
index f69976896c67846520037650ee4b0a81451dfa9e..d2f986190069c6cb6f158705dd8aef722cc326df 100644 (file)
@@ -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(
index 02f0419249f95859da9cc59caf24f456d0183e6c..28c1fd6aaf44d0944894ab9b37a3cbb91861d596 100644 (file)
@@ -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)