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
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
)
)
),
)
-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_),
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
: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
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.
: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(
"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)