]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Ensure compiler uses quote_schema hook for translates renders
authorMike Bayer <mike_mp@zzzcomputing.com>
Wed, 30 Jun 2021 14:52:09 +0000 (10:52 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Wed, 30 Jun 2021 14:52:09 +0000 (10:52 -0400)
Fixed regression where the special dotted-schema name handling for the SQL
Server dialect would not function correctly if the dotted schema name were
used within the ``schema_translate_map`` feature.

Fixes: #6697
Change-Id: Idb610755cbf8122e71223d5dd0a17fcb61b1b98d

doc/build/changelog/unreleased_14/6697.rst [new file with mode: 0644]
lib/sqlalchemy/sql/compiler.py
test/dialect/mssql/test_compiler.py

diff --git a/doc/build/changelog/unreleased_14/6697.rst b/doc/build/changelog/unreleased_14/6697.rst
new file mode 100644 (file)
index 0000000..d77bfc0
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: bug, regression, mssql
+    :tickets: 6697
+
+    Fixed regression where the special dotted-schema name handling for the SQL
+    Server dialect would not function correctly if the dotted schema name were
+    used within the ``schema_translate_map`` feature.
index 8ae56fd5440418a824708053cc5ebc24a9481fd4..67da036830406095462ec3efd7b195eacc26516d 100644 (file)
@@ -4751,7 +4751,7 @@ class IdentifierPreparer(object):
                         "Dialect has no default schema name; can't "
                         "use None as dynamic schema target."
                     )
-            return self.quote(effective_schema)
+            return self.quote_schema(effective_schema)
 
         return re.sub(r"(\[SCHEMA_([^\]]+)\])", replace, statement)
 
index a0127fa57b998458cde01ecc6ff6e938e1e4c7da..2e4a0871310529339c0a2b43bfa373c99191f4e7 100644 (file)
@@ -614,52 +614,84 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
             select(tbl), "SELECT [foo.dbo].test.id FROM [foo.dbo].test"
         )
 
-    def test_force_schema_quoted_name_w_dot_case_sensitive(self):
+    @testing.combinations((True,), (False,), argnames="use_schema_translate")
+    def test_force_schema_quoted_name_w_dot_case_sensitive(
+        self, use_schema_translate
+    ):
         metadata = MetaData()
         tbl = Table(
             "test",
             metadata,
             Column("id", Integer, primary_key=True),
-            schema=quoted_name("Foo.dbo", True),
+            schema=quoted_name("Foo.dbo", True)
+            if not use_schema_translate
+            else None,
         )
         self.assert_compile(
-            select(tbl), "SELECT [Foo.dbo].test.id FROM [Foo.dbo].test"
+            select(tbl),
+            "SELECT [Foo.dbo].test.id FROM [Foo.dbo].test",
+            schema_translate_map={None: quoted_name("Foo.dbo", True)}
+            if use_schema_translate
+            else None,
+            render_schema_translate=True if use_schema_translate else False,
         )
 
-    def test_force_schema_quoted_w_dot_case_sensitive(self):
+    @testing.combinations((True,), (False,), argnames="use_schema_translate")
+    def test_force_schema_quoted_w_dot_case_sensitive(
+        self, use_schema_translate
+    ):
         metadata = MetaData()
         tbl = Table(
             "test",
             metadata,
             Column("id", Integer, primary_key=True),
-            schema="[Foo.dbo]",
+            schema="[Foo.dbo]" if not use_schema_translate else None,
         )
         self.assert_compile(
-            select(tbl), "SELECT [Foo.dbo].test.id FROM [Foo.dbo].test"
+            select(tbl),
+            "SELECT [Foo.dbo].test.id FROM [Foo.dbo].test",
+            schema_translate_map={None: "[Foo.dbo]"}
+            if use_schema_translate
+            else None,
+            render_schema_translate=True if use_schema_translate else False,
         )
 
-    def test_schema_autosplit_w_dot_case_insensitive(self):
+    @testing.combinations((True,), (False,), argnames="use_schema_translate")
+    def test_schema_autosplit_w_dot_case_insensitive(
+        self, use_schema_translate
+    ):
         metadata = MetaData()
         tbl = Table(
             "test",
             metadata,
             Column("id", Integer, primary_key=True),
-            schema="foo.dbo",
+            schema="foo.dbo" if not use_schema_translate else None,
         )
         self.assert_compile(
-            select(tbl), "SELECT foo.dbo.test.id FROM foo.dbo.test"
+            select(tbl),
+            "SELECT foo.dbo.test.id FROM foo.dbo.test",
+            schema_translate_map={None: "foo.dbo"}
+            if use_schema_translate
+            else None,
+            render_schema_translate=True if use_schema_translate else False,
         )
 
-    def test_schema_autosplit_w_dot_case_sensitive(self):
+    @testing.combinations((True,), (False,), argnames="use_schema_translate")
+    def test_schema_autosplit_w_dot_case_sensitive(self, use_schema_translate):
         metadata = MetaData()
         tbl = Table(
             "test",
             metadata,
             Column("id", Integer, primary_key=True),
-            schema="Foo.dbo",
+            schema="Foo.dbo" if not use_schema_translate else None,
         )
         self.assert_compile(
-            select(tbl), "SELECT [Foo].dbo.test.id FROM [Foo].dbo.test"
+            select(tbl),
+            "SELECT [Foo].dbo.test.id FROM [Foo].dbo.test",
+            schema_translate_map={None: "Foo.dbo"}
+            if use_schema_translate
+            else None,
+            render_schema_translate=True if use_schema_translate else False,
         )
 
     def test_delete_schema(self):