From: Mike Bayer Date: Tue, 24 Jan 2023 20:34:27 +0000 (-0500) Subject: fix stringify for CreateSchema X-Git-Tag: rel_1_4_47~15 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=2c3372f6c03c7d15325cad08c9fd6d08c68fa2fd;p=thirdparty%2Fsqlalchemy%2Fsqlalchemy.git 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 (cherry picked from commit 90f4b5d84f248d95f3df38e74be92b23fd880e42) --- 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 bed64a5670..d47f1b2400 100644 --- a/lib/sqlalchemy/sql/ddl.py +++ b/lib/sqlalchemy/sql/ddl.py @@ -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.""" diff --git a/lib/sqlalchemy/testing/assertions.py b/lib/sqlalchemy/testing/assertions.py index 5c646790bf..754f535f56 100644 --- a/lib/sqlalchemy/testing/assertions.py +++ b/lib/sqlalchemy/testing/assertions.py @@ -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) diff --git a/test/sql/test_metadata.py b/test/sql/test_metadata.py index 50cf253379..be410abd33 100644 --- a/test/sql/test_metadata.py +++ b/test/sql/test_metadata.py @@ -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(