]> 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:34:27 +0000 (15:34 -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

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 5ea500a32be0a51513d0acbd5f6f7d82aaf9638d..715e364272578d40a36c8fd894fb42aecc979ad7 100644 (file)
@@ -476,6 +476,8 @@ class CreateSchema(_CreateBase):
 
     __visit_name__ = "create_schema"
 
+    stringify_dialect = "default"  # type: ignore
+
     def __init__(
         self,
         name,
index 0effcbd06ad919eecbb52a541872c76a5fd98bfd..5dd32e8cc4d0fe064f1b8b2e42d6b3fe73011e3f 100644 (file)
@@ -327,8 +327,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 7f6bee72bf209a8e0bb0e2ab1517e25235d0cf86..0cd73922e9f7d6c10670f56369f4234e0d5410af 100644 (file)
@@ -53,6 +53,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_
@@ -2620,7 +2621,34 @@ 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(
             schema.CreateSchema("sa_schema"), "CREATE SCHEMA sa_schema"
         )