]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
fix stringify for CreateSchema
authorMike Bayer <mike_mp@zzzcomputing.com>
Tue, 24 Jan 2023 20:34:27 +0000 (15:34 -0500)
committerMike Bayer <mike_mp@zzzcomputing.com>
Tue, 24 Jan 2023 20:35:43 +0000 (15:35 -0500)
Fixed stringify for a the :class:`.CreateSchema` DDL construct, which would
fail with an ``AttributeError`` when stringified without a dialect.

Fixes: #7664
Change-Id: Ifc1769604bc5219c060f5112f7bdea0f780f1a1c
(cherry picked from commit 90f4b5d84f248d95f3df38e74be92b23fd880e42)

doc/build/changelog/unreleased_14/7664.rst [new file with mode: 0644]
lib/sqlalchemy/sql/ddl.py
lib/sqlalchemy/testing/assertions.py
test/sql/test_metadata.py

diff --git a/doc/build/changelog/unreleased_14/7664.rst b/doc/build/changelog/unreleased_14/7664.rst
new file mode 100644 (file)
index 0000000..2188ba3
--- /dev/null
@@ -0,0 +1,7 @@
+.. change::
+    :tags: bug, sql
+    :tickets: 7664
+
+    Fixed stringify for a the :class:`.CreateSchema` DDL construct, which would
+    fail with an ``AttributeError`` when stringified without a dialect.
+
index bed64a56701beb54bfdb208511d31740d4cd0614..d47f1b2400733880b72ba1ec41fab75dcd1b9f06 100644 (file)
@@ -422,6 +422,8 @@ class CreateSchema(_CreateDropBase):
 
     __visit_name__ = "create_schema"
 
+    stringify_dialect = "default"
+
     def __init__(self, name, quote=None, **kw):
         """Create a new :class:`.CreateSchema` construct."""
 
index 5c646790bf0a6ae0f6650c2184ff75ca7a454878..754f535f5642dc6e291bb2b487e402850054b359 100644 (file)
@@ -334,8 +334,10 @@ def startswith_(a, fragment, msg=None):
 def eq_ignore_whitespace(a, b, msg=None):
     a = re.sub(r"^\s+?|\n", "", a)
     a = re.sub(r" {2,}", " ", a)
+    a = re.sub(r"\t", "", a)
     b = re.sub(r"^\s+?|\n", "", b)
     b = re.sub(r" {2,}", " ", b)
+    b = re.sub(r"\t", "", b)
 
     assert a == b, msg or "%r != %r" % (a, b)
 
index 50cf253379f2ac026b8fe23010dc64eea4031974..be410abd33a6b38b3e5d18352eb198b0c6df6239 100644 (file)
@@ -48,6 +48,7 @@ from sqlalchemy.testing import AssertsCompiledSQL
 from sqlalchemy.testing import ComparesTables
 from sqlalchemy.testing import emits_warning
 from sqlalchemy.testing import eq_
+from sqlalchemy.testing import eq_ignore_whitespace
 from sqlalchemy.testing import expect_raises_message
 from sqlalchemy.testing import fixtures
 from sqlalchemy.testing import is_
@@ -2594,6 +2595,32 @@ class SchemaTest(fixtures.TestBase, AssertsCompiledSQL):
         t2 = Table("t2", m, Column("x", Integer, ForeignKey("bar.t1.x")))
         assert t2.c.x.references(t1.c.x)
 
+    @testing.combinations(
+        (schema.CreateSchema("sa_schema"), "CREATE SCHEMA sa_schema"),
+        # note we don't yet support lower-case table() or
+        # lower-case column() for this
+        # (
+        #    schema.CreateTable(table("t", column("q", Integer))),
+        #    "CREATE TABLE t (q INTEGER)",
+        # ),
+        (
+            schema.CreateTable(Table("t", MetaData(), Column("q", Integer))),
+            "CREATE TABLE t (q INTEGER)",
+        ),
+        (
+            schema.CreateIndex(
+                Index(
+                    "foo",
+                    "x",
+                    _table=Table("t", MetaData(), Column("x", Integer)),
+                )
+            ),
+            "CREATE INDEX foo ON t (x)",
+        ),
+    )
+    def test_stringify_schema_elements(self, element, expected):
+        eq_ignore_whitespace(str(element), expected)
+
     def test_create_drop_schema(self):
 
         self.assert_compile(