from .impl import DefaultImpl
from .. import util
from ..autogenerate import compare
-from ..util.compat import string_types
from ..util.sqla_compat import _is_mariadb
from ..util.sqla_compat import _is_type_bound
)
-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, comment
):
if autoincrement:
spec += " AUTO_INCREMENT"
if server_default is not False and server_default is not None:
- spec += " DEFAULT %s" % _render_value(compiler, server_default)
+ spec += " DEFAULT %s" % format_server_default(compiler, server_default)
if comment:
spec += " COMMENT %s" % compiler.sql_compiler.render_literal_value(
comment, sqltypes.String()
from sqlalchemy import Boolean
from sqlalchemy import Column
from sqlalchemy import DATETIME
+from sqlalchemy import Float
from sqlalchemy import func
from sqlalchemy import inspect
from sqlalchemy import Integer
from alembic import op
from alembic import util
+from alembic.autogenerate import api
+from alembic.autogenerate import compare
from alembic.migration import MigrationContext
+from alembic.operations import ops
from alembic.testing import assert_raises_message
from alembic.testing import config
from alembic.testing.env import clear_staging_env
"ALTER TABLE t CHANGE c c DATETIME NULL DEFAULT CURRENT_TIMESTAMP"
)
+ def test_alter_column_modify_programmatic_default(self):
+ # test issue #736
+ # when autogenerate.compare creates the operation object
+ # programmatically, the server_default of the op has the full
+ # DefaultClause present. make sure the usual renderer works.
+ context = op_fixture("mysql")
+
+ m1 = MetaData()
+
+ autogen_context = api.AutogenContext(context, m1)
+
+ operation = ops.AlterColumnOp("t", "c")
+ for fn in (
+ compare._compare_nullable,
+ compare._compare_type,
+ compare._compare_server_default,
+ ):
+ fn(
+ autogen_context,
+ operation,
+ None,
+ "t",
+ "c",
+ Column("c", Float(), nullable=False, server_default=text("0")),
+ Column("c", Float(), nullable=True, default=0),
+ )
+ op.invoke(operation)
+ context.assert_("ALTER TABLE t MODIFY c FLOAT NULL DEFAULT 0")
+
def test_col_not_nullable(self):
context = op_fixture("mysql")
op.alter_column("t1", "c1", nullable=False, existing_type=Integer)