From 90f4b5d84f248d95f3df38e74be92b23fd880e42 Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Tue, 24 Jan 2023 15:34:27 -0500 Subject: [PATCH] fix stringify for CreateSchema 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 | 7 ++++++ lib/sqlalchemy/sql/ddl.py | 2 ++ lib/sqlalchemy/testing/assertions.py | 2 ++ test/sql/test_metadata.py | 28 ++++++++++++++++++++++ 4 files changed, 39 insertions(+) create mode 100644 doc/build/changelog/unreleased_14/7664.rst diff --git a/doc/build/changelog/unreleased_14/7664.rst b/doc/build/changelog/unreleased_14/7664.rst new file mode 100644 index 0000000000..2188ba3bc0 --- /dev/null +++ b/doc/build/changelog/unreleased_14/7664.rst @@ -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. + diff --git a/lib/sqlalchemy/sql/ddl.py b/lib/sqlalchemy/sql/ddl.py index 5ea500a32b..715e364272 100644 --- a/lib/sqlalchemy/sql/ddl.py +++ b/lib/sqlalchemy/sql/ddl.py @@ -476,6 +476,8 @@ class CreateSchema(_CreateBase): __visit_name__ = "create_schema" + stringify_dialect = "default" # type: ignore + def __init__( self, name, diff --git a/lib/sqlalchemy/testing/assertions.py b/lib/sqlalchemy/testing/assertions.py index 0effcbd06a..5dd32e8cc4 100644 --- a/lib/sqlalchemy/testing/assertions.py +++ b/lib/sqlalchemy/testing/assertions.py @@ -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) diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index 7f6bee72bf..0cd73922e9 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -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" ) -- 2.47.2