From c85c89a11528d7f342fca9e04a56663521c774a2 Mon Sep 17 00:00:00 2001 From: Matthew Sills Date: Thu, 19 Dec 2019 10:03:12 -0500 Subject: [PATCH] render table name with schema 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 | 7 +++++-- docs/build/unreleased/637.rst | 7 +++++++ tests/test_postgresql.py | 25 +++++++++++++++++++++---- 3 files changed, 33 insertions(+), 6 deletions(-) create mode 100644 docs/build/unreleased/637.rst diff --git a/alembic/ddl/postgresql.py b/alembic/ddl/postgresql.py index d5bcd177..9cb3c11e 100644 --- a/alembic/ddl/postgresql.py +++ b/alembic/ddl/postgresql.py @@ -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 index 00000000..a30afb7e --- /dev/null +++ b/docs/build/unreleased/637.rst @@ -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. diff --git a/tests/test_postgresql.py b/tests/test_postgresql.py index 59c12001..fe2635bd 100644 --- a/tests/test_postgresql.py +++ b/tests/test_postgresql.py @@ -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): -- 2.47.2