From: Mike Bayer Date: Mon, 28 Nov 2011 00:52:24 +0000 (-0500) Subject: pg uses DROP NOT NULL, keep this as the default and move X-Git-Tag: rel_0_1_0~35 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=198f7067bd44fa31b8d849a433b693187b22b08c;p=thirdparty%2Fsqlalchemy%2Falembic.git pg uses DROP NOT NULL, keep this as the default and move NULL to SQL Server, until we get more data on other DBs --- diff --git a/alembic/ddl/base.py b/alembic/ddl/base.py index 58e3006f..5fb5d9c1 100644 --- a/alembic/ddl/base.py +++ b/alembic/ddl/base.py @@ -80,7 +80,7 @@ def visit_column_nullable(element, compiler, **kw): return "%s %s %s" % ( alter_table(compiler, element.table_name, element.schema), alter_column(compiler, element.column_name), - "NULL" if element.nullable else "SET NOT NULL" + "DROP NOT NULL" if element.nullable else "SET NOT NULL" ) @compiles(ColumnType) diff --git a/alembic/ddl/mssql.py b/alembic/ddl/mssql.py index 42c2952a..3f2b45b9 100644 --- a/alembic/ddl/mssql.py +++ b/alembic/ddl/mssql.py @@ -1,5 +1,5 @@ from alembic.ddl.impl import DefaultImpl -from alembic.ddl.base import alter_table, AddColumn, ColumnName, format_table_name, format_column_name +from alembic.ddl.base import alter_table, AddColumn, ColumnName, format_table_name, format_column_name, ColumnNullable, alter_column from sqlalchemy.ext.compiler import compiles class MSSQLImpl(DefaultImpl): @@ -59,6 +59,14 @@ def visit_add_column(element, compiler, **kw): def mssql_add_column(compiler, column, **kw): return "ADD %s" % compiler.get_column_specification(column, **kw) +@compiles(ColumnNullable, 'mssql') +def visit_column_nullable(element, compiler, **kw): + return "%s %s %s" % ( + alter_table(compiler, element.table_name, element.schema), + alter_column(compiler, element.column_name), + "NULL" if element.nullable else "SET NOT NULL" + ) + @compiles(ColumnName, 'mssql') def visit_rename_column(element, compiler, **kw): diff --git a/tests/test_mssql.py b/tests/test_mssql.py index c84f8491..7c143823 100644 --- a/tests/test_mssql.py +++ b/tests/test_mssql.py @@ -36,6 +36,20 @@ def test_drop_column_w_check(): context.assert_contains("exec('alter table t1 drop constraint ' + @const_name)") context.assert_contains("ALTER TABLE t1 DROP COLUMN c1") +def test_alter_column_nullable(): + context = _op_fixture('mssql') + op.alter_column("t", "c", nullable=True) + context.assert_( + "ALTER TABLE t ALTER COLUMN c NULL" + ) + +def test_alter_column_not_nullable(): + context = _op_fixture('mssql') + op.alter_column("t", "c", nullable=False) + context.assert_( + "ALTER TABLE t ALTER COLUMN c SET NOT NULL" + ) + # TODO: when we add schema support #def test_alter_column_rename_mssql_schema(): # context = _op_fixture('mssql') diff --git a/tests/test_op.py b/tests/test_op.py index cf8e6fbd..54476e69 100644 --- a/tests/test_op.py +++ b/tests/test_op.py @@ -70,8 +70,9 @@ def test_alter_column_nullable(): context = _op_fixture() op.alter_column("t", "c", nullable=True) context.assert_( - # TODO: not sure if this is supposed to be SET NULL - "ALTER TABLE t ALTER COLUMN c NULL" + # TODO: not sure if this is PG only or standard + # SQL + "ALTER TABLE t ALTER COLUMN c DROP NOT NULL" ) def test_alter_column_not_nullable():