]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Include schema in all_tab_comments query
authorMike Bayer <mike_mp@zzzcomputing.com>
Fri, 13 Mar 2020 03:11:18 +0000 (23:11 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Fri, 13 Mar 2020 03:13:35 +0000 (23:13 -0400)
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)

doc/build/changelog/unreleased_13/5146.rst [new file with mode: 0644]
lib/sqlalchemy/dialects/oracle/base.py
test/dialect/oracle/test_reflection.py

diff --git a/doc/build/changelog/unreleased_13/5146.rst b/doc/build/changelog/unreleased_13/5146.rst
new file mode 100644 (file)
index 0000000..56e5554
--- /dev/null
@@ -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.
+
index c04b8a4beb69f09386a022bb57d8f14a2c97c8fb..7830a2b609b04faddd6698be8bcc7723a899b468 100644 (file)
@@ -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
index 411ba335c687348de28f88b0189a7b4982359a79..6359c5c1756e043aa1321e85dfef1859de89919b 100644 (file)
@@ -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 "