]> git.ipfire.org Git - thirdparty/sqlalchemy/alembic.git/commitdiff
render table name with schema
authorMatthew Sills <matthew@eightminuteswest.com>
Thu, 19 Dec 2019 15:03:12 +0000 (10:03 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Thu, 19 Dec 2019 23:55:15 +0000 (18:55 -0500)
Fixed issue where COMMENT directives for PostgreSQL failed to correctly
include an explicit schema name, as well as correct quoting rules for
schema, table, and column names.  Pull request courtesy Matthew Sills.

Fixes: #637
Closes: #636
Pull-request: https://github.com/sqlalchemy/alembic/pull/636
Pull-request-sha: 703e27bab5b2d0907367dd03a4babb00ea5ee8aa

Change-Id: I319171b0596faf0bbea1b965e094d13acad14b6c

alembic/ddl/postgresql.py
docs/build/unreleased/637.rst [new file with mode: 0644]
tests/test_postgresql.py

index d5bcd1772fc4ae1124b347d0ab80b6dfe62b2c54..9cb3c11ea87d89650033f6f0d3fa76a7ac615b8d 100644 (file)
@@ -17,6 +17,7 @@ from .base import alter_table
 from .base import AlterColumn
 from .base import ColumnComment
 from .base import compiles
+from .base import format_column_name
 from .base import format_table_name
 from .base import format_type
 from .base import RenameTable
@@ -280,8 +281,10 @@ def visit_column_comment(element, compiler, **kw):
     )
 
     return ddl.format(
-        table_name=element.table_name,
-        column_name=element.column_name,
+        table_name=format_table_name(
+            compiler, element.table_name, element.schema
+        ),
+        column_name=format_column_name(compiler, element.column_name),
         comment=comment,
     )
 
diff --git a/docs/build/unreleased/637.rst b/docs/build/unreleased/637.rst
new file mode 100644 (file)
index 0000000..a30afb7
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: bug, postgresql
+    :tickets: 637
+
+    Fixed issue where COMMENT directives for PostgreSQL failed to correctly
+    include an explicit schema name, as well as correct quoting rules for
+    schema, table, and column names.  Pull request courtesy Matthew Sills.
index 59c120010cabba0c020630289613fccfcc35e4ef..fe2635bda2ef76e33f7925a4ebca36a9dab87624 100644 (file)
@@ -177,7 +177,7 @@ class PostgresqlOpTest(TestBase):
 
         context.assert_(
             "ALTER TABLE foo.t ALTER COLUMN c SET NOT NULL",
-            "COMMENT ON COLUMN t.c IS 'This is a column comment'",
+            "COMMENT ON COLUMN foo.t.c IS 'This is a column comment'",
         )
 
     @config.requirements.comments_api
@@ -191,7 +191,24 @@ class PostgresqlOpTest(TestBase):
             comment="This is a column comment",
         )
 
-        context.assert_("COMMENT ON COLUMN t.c IS 'This is a column comment'")
+        context.assert_(
+            "COMMENT ON COLUMN foo.t.c IS 'This is a column comment'"
+        )
+
+    @config.requirements.comments_api
+    def test_alter_column_add_comment_table_and_column_quoting(self):
+        context = op_fixture("postgresql")
+        op.alter_column(
+            "T",
+            "C",
+            existing_type=Boolean(),
+            schema="foo",
+            comment="This is a column comment",
+        )
+
+        context.assert_(
+            'COMMENT ON COLUMN foo."T"."C" IS \'This is a column comment\''
+        )
 
     @config.requirements.comments_api
     def test_alter_column_add_comment_quoting(self):
@@ -205,7 +222,7 @@ class PostgresqlOpTest(TestBase):
         )
 
         context.assert_(
-            "COMMENT ON COLUMN t.c IS 'This is a column ''comment'''"
+            "COMMENT ON COLUMN foo.t.c IS 'This is a column ''comment'''"
         )
 
     @config.requirements.comments_api
@@ -220,7 +237,7 @@ class PostgresqlOpTest(TestBase):
             existing_comment="This is a column comment",
         )
 
-        context.assert_("COMMENT ON COLUMN t.c IS NULL")
+        context.assert_("COMMENT ON COLUMN foo.t.c IS NULL")
 
     @config.requirements.comments_api
     def test_create_table_with_comment(self):