]> 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:24 +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

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 c89e441b9efa23b0fe8463877fdcd92e183a5470..11ddabfc33da3a30f34203f5b56f9e8a5b2e37b7 100644 (file)
@@ -1758,13 +1758,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 "