From: Mike Bayer Date: Fri, 13 Mar 2020 03:11:18 +0000 (-0400) Subject: Include schema in all_tab_comments query X-Git-Tag: rel_1_3_16~27 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=19b248c14c37c88280018423b3ac929ea101cc46;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git Include schema in all_tab_comments query Fixed regression / incorrect fix caused by fix for :ticket:`5146` where the Oracle dialect reads from the "all_tab_comments" view to get table comments but fails to accommodate for the current owner of the table being requested, causing it to read the wrong comment if multiple tables of the same name exist in multiple schemas. Fixes: #5146 Change-Id: Id79fbaa81b0e36cd4af60c48e4ab35c593ace057 (cherry picked from commit f1429823d31c56b589017e60328f826f5e721f0e) --- diff --git a/doc/build/changelog/unreleased_13/5146.rst b/doc/build/changelog/unreleased_13/5146.rst new file mode 100644 index 0000000000..56e555424a --- /dev/null +++ b/doc/build/changelog/unreleased_13/5146.rst @@ -0,0 +1,10 @@ +.. change:: + :tags: bug, oracle, reflection + :tickets: 5146 + + Fixed regression / incorrect fix caused by fix for :ticket:`5146` where the + Oracle dialect reads from the "all_tab_comments" view to get table comments + but fails to accommodate for the current owner of the table being + requested, causing it to read the wrong comment if multiple tables of the + same name exist in multiple schemas. + diff --git a/lib/sqlalchemy/dialects/oracle/base.py b/lib/sqlalchemy/dialects/oracle/base.py index c04b8a4beb..7830a2b609 100644 --- a/lib/sqlalchemy/dialects/oracle/base.py +++ b/lib/sqlalchemy/dialects/oracle/base.py @@ -1712,13 +1712,18 @@ class OracleDialect(default.DefaultDialect): info_cache=info_cache, ) + if not schema: + schema = self.default_schema_name + COMMENT_SQL = """ SELECT comments FROM all_tab_comments - WHERE table_name = :table_name + WHERE table_name = :table_name AND owner = :schema_name """ - c = connection.execute(sql.text(COMMENT_SQL), table_name=table_name) + c = connection.execute( + sql.text(COMMENT_SQL), table_name=table_name, schema_name=schema + ) return {"text": c.scalar()} @reflection.cache diff --git a/test/dialect/oracle/test_reflection.py b/test/dialect/oracle/test_reflection.py index 411ba335c6..6359c5c175 100644 --- a/test/dialect/oracle/test_reflection.py +++ b/test/dialect/oracle/test_reflection.py @@ -194,6 +194,32 @@ drop synonym %(test_schema)s.local_table; # check table comment (#5146) eq_(parent.comment, "my table comment") + @testing.provide_metadata + def test_reflect_table_comment(self): + local_parent = Table( + "parent", + self.metadata, + Column("q", Integer), + comment="my local comment", + ) + + local_parent.create(testing.db) + + insp = inspect(testing.db) + eq_( + insp.get_table_comment( + "parent", schema=testing.config.test_schema + ), + {"text": "my table comment"}, + ) + eq_(insp.get_table_comment("parent",), {"text": "my local comment"}) + eq_( + insp.get_table_comment( + "parent", schema=testing.db.dialect.default_schema_name + ), + {"text": "my local comment"}, + ) + def test_reflect_local_to_remote(self): testing.db.execute( "CREATE TABLE localtable (id INTEGER "