]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
- add alter col default for PG/base
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 30 Nov 2011 00:21:58 +0000 (19:21 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 30 Nov 2011 00:21:58 +0000 (19:21 -0500)
- i want the ; after BEGIN/COMMIT for static generation, makes it easier to parse

alembic/context.py
alembic/ddl/base.py
alembic/ddl/impl.py
alembic/ddl/mssql.py
tests/test_mssql.py
tests/test_op.py
tests/test_sql_script.py

index c1f489af0675c4554159a8774a6480e6e6635c67..60081ba8d125337b7b45017858a891da5a207aa4 100644 (file)
@@ -526,6 +526,17 @@ def execute(sql):
     """
     get_context().execute(sql)
 
+def static_output(text):
+    """Emit text directly to the "offline" SQL stream.
+    
+    Typically this is for emitting comments that 
+    start with --.  The statement is not treated
+    as a SQL execution, no ; or batch separator
+    is added, etc.
+    
+    """
+    get_context().impl.static_output(text)
+
 def begin_transaction():
     """Return a context manager that will 
     enclose an operation within a "transaction",
index 8c10fb3bf137d65f5cbfd9d588c0a45633f19255..867d92f4f5f6521a7e3a12a8b26c97ab2b74c872 100644 (file)
@@ -99,7 +99,7 @@ def visit_column_type(element, compiler, **kw):
     return "%s %s %s" % (
         alter_table(compiler, element.table_name, element.schema),
         alter_column(compiler, element.column_name),
-        "TYPE %s" % compiler.dialect.type_compiler.process(element.type_)
+        "TYPE %s" % format_type(compiler, element.type_)
     )
 
 @compiles(ColumnName)
@@ -112,9 +112,14 @@ def visit_column_name(element, compiler, **kw):
 
 @compiles(ColumnDefault)
 def visit_column_default(element, compiler, **kw):
-    raise NotImplementedError(
-            "Default compilation not implemented "
-            "for column default change")
+    return "%s %s %s" % (
+        alter_table(compiler, element.table_name, element.schema),
+        alter_column(compiler, element.column_name),
+        "SET DEFAULT %s" % 
+            format_server_default(compiler, element.default)
+        if element.default is not None
+        else "DROP DEFAULT"
+    )
 
 def quote_dotted(name, quote):
     """quote the elements of a dotted name"""
@@ -133,9 +138,12 @@ def format_column_name(compiler, name):
     return compiler.preparer.quote(name, None)
 
 def format_server_default(compiler, default):
-#    if isinstance(default, basestring):
-#        default = DefaultClause(default)
-    return compiler.get_column_default_string(Column("x", Integer, server_default=default))
+    return compiler.get_column_default_string(
+                Column("x", Integer, server_default=default)
+            )
+
+def format_type(compiler, type_):
+    return compiler.dialect.type_compiler.process(type_)
 
 def alter_table(compiler, name, schema):
     return "ALTER TABLE %s" % format_table_name(compiler, name, schema)
index ef6ace714dc363a5ee76e09689bfd8c138420057..74cc94bb4cbd6d5fd110444d4929dc7de92d8f4d 100644 (file)
@@ -205,7 +205,7 @@ class DefaultImpl(object):
         via :func:`.context.begin_transaction`.
         
         """
-        self.static_output("BEGIN")
+        self.static_output("BEGIN;")
 
     def emit_commit(self):
         """Emit the string ``COMMIT``, or the backend-specific
@@ -215,7 +215,7 @@ class DefaultImpl(object):
         via :func:`.context.begin_transaction`.
         
         """
-        self.static_output("COMMIT")
+        self.static_output("COMMIT;")
 
 class _literal_bindparam(_BindParamClause):
     pass
index 6490bd550e64db88d363963129caa76fe043e7da..fc4c0f3f1afc5df5795484926504bd2f76e754e0 100644 (file)
@@ -1,7 +1,7 @@
 from alembic.ddl.impl import DefaultImpl
 from alembic.ddl.base import alter_table, AddColumn, ColumnName, \
     format_table_name, format_column_name, ColumnNullable, alter_column,\
-    format_server_default,ColumnDefault
+    format_server_default,ColumnDefault, format_type
 from alembic import util
 from sqlalchemy.ext.compiler import compiles
 
@@ -29,7 +29,7 @@ class MSSQLImpl(DefaultImpl):
             self.static_output(self.batch_separator)
 
     def emit_begin(self):
-        self.static_output("BEGIN TRANSACTION")
+        self.static_output("BEGIN TRANSACTION;")
 
     def alter_column(self, table_name, column_name, 
                         nullable=None,
@@ -147,7 +147,7 @@ def visit_column_nullable(element, compiler, **kw):
     return "%s %s %s %s" % (
         alter_table(compiler, element.table_name, element.schema),
         alter_column(compiler, element.column_name),
-        compiler.dialect.type_compiler.process(element.existing_type),
+        format_type(compiler, element.existing_type),
         "NULL" if element.nullable else "NOT NULL"
     )
 
index 883590fdf48481ca4e7caa1266bee01dba044278..bdf8272d4917a1330150272bba409536b24bd782 100644 (file)
@@ -26,8 +26,8 @@ class FullEnvironmentTests(TestCase):
     def test_begin_comit(self):
         with capture_context_buffer(transactional_ddl=True) as buf:
             command.upgrade(self.cfg, self.a, sql=True)
-        assert "BEGIN TRANSACTION" in buf.getvalue()
-        assert "COMMIT" in buf.getvalue()
+        assert "BEGIN TRANSACTION;" in buf.getvalue()
+        assert "COMMIT;" in buf.getvalue()
 
     def test_batch_separator_default(self):
         with capture_context_buffer() as buf:
index d69ec443b3ee587d341f8e96e24d8e14abfefcd9..bc20d80e6baaf1f9e6288e078a22ed393b6d4718 100644 (file)
@@ -98,6 +98,28 @@ def test_alter_column_type():
         'ALTER TABLE t ALTER COLUMN c TYPE VARCHAR(50)'
     )
 
+def test_alter_column_set_default():
+    context = op_fixture()
+    op.alter_column("t", "c", server_default="q")
+    context.assert_(
+        "ALTER TABLE t ALTER COLUMN c SET DEFAULT 'q'"
+    )
+
+def test_alter_column_set_compiled_default():
+    context = op_fixture()
+    op.alter_column("t", "c", server_default=func.utc_thing(func.current_timestamp()))
+    context.assert_(
+        "ALTER TABLE t ALTER COLUMN c SET DEFAULT utc_thing(CURRENT_TIMESTAMP)"
+    )
+
+def test_alter_column_drop_default():
+    context = op_fixture()
+    op.alter_column("t", "c", server_default=None)
+    context.assert_(
+        'ALTER TABLE t ALTER COLUMN c DROP DEFAULT'
+    )
+
+
 def test_alter_column_schema_type_unnamed():
     context = op_fixture('mssql')
     op.alter_column("t", "c", type_=Boolean())
index a615bdf1364320f4456a9b56fb563520e310d3b7..e127cb7334f8416c17ec11853afb9ca990ff621f 100644 (file)
@@ -17,13 +17,13 @@ def teardown():
 def test_begin_comit():
     with capture_context_buffer(transactional_ddl=True) as buf:
         command.upgrade(cfg, a, sql=True)
-    assert "BEGIN" in buf.getvalue()
-    assert "COMMIT" in buf.getvalue()
+    assert "BEGIN;" in buf.getvalue()
+    assert "COMMIT;" in buf.getvalue()
 
     with capture_context_buffer(transactional_ddl=False) as buf:
         command.upgrade(cfg, a, sql=True)
-    assert "BEGIN" not in buf.getvalue()
-    assert "COMMIT" not in buf.getvalue()
+    assert "BEGIN;" not in buf.getvalue()
+    assert "COMMIT;" not in buf.getvalue()
 
 def test_version_from_none_insert():
     with capture_context_buffer() as buf: