]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
Add _alembic_quote method to format_constraint()
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 14 Oct 2019 03:02:22 +0000 (23:02 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 14 Oct 2019 03:02:22 +0000 (23:02 -0400)
Alembic needs a portable way of getting at the name of an
index without quoting being applied.   As we would like the
indexes created by the Column index=True flag to support
deferred index names, supply a function that delivers this
for Alembic without it having to dig too deeply into the
internals.    the _alembic_quote flag may be made public
at a later time, however as we've been through many quoting
flags that are difficult to get rid of, try to be conservative
to start.

Change-Id: I184adaeae26c2e75093aaea5ebe01a3815cadb08

lib/sqlalchemy/sql/compiler.py
test/sql/test_quote.py

index 453ff56d2eaeed4e9aede886df4e0f3a0765fc5d..c6c30629d804287085e8c64536b4dacc95d4cfff 100644 (file)
@@ -3698,7 +3698,7 @@ class IdentifierPreparer(object):
         return ident
 
     @util.dependencies("sqlalchemy.sql.naming")
-    def format_constraint(self, naming, constraint):
+    def format_constraint(self, naming, constraint, _alembic_quote=True):
         if isinstance(constraint.name, elements._defer_name):
             name = naming._constraint_name_for_table(
                 constraint, constraint.table
@@ -3725,7 +3725,10 @@ class IdentifierPreparer(object):
         else:
             self.dialect.validate_identifier(name)
 
-        return self.quote(name)
+        if not _alembic_quote:
+            return name
+        else:
+            return self.quote(name)
 
     def format_index(self, index):
         return self.format_constraint(index)
index b54da97ba24618514493a97dc5e6795b0e4dd559..d7219b12d30b5d8b8c7c1597b13464ff8f571f05 100644 (file)
@@ -868,6 +868,34 @@ class PreparerTest(fixtures.TestBase):
         a_eq(unformat("`foo`.bar"), ["foo", "bar"])
         a_eq(unformat("`foo`.`b``a``r`.`baz`"), ["foo", "b`a`r", "baz"])
 
+    def test_alembic_quote(self):
+        t1 = Table(
+            "TableOne", MetaData(), Column("MyCol", Integer, index=True)
+        )
+        t2 = Table(
+            "some_table", MetaData(), Column("some_col", Integer, index=True)
+        )
+        t3 = Table(
+            "some_table", MetaData(), Column("some_col", Integer, index=True)
+        )
+        ix3 = Index("my_index", t3.c.some_col)
+        ix4 = Index("MyIndex", t3.c.some_col)
+        ix5 = Index(None, t3.c.some_col)
+
+        for idx, expected in [
+            (list(t1.indexes)[0], "ix_TableOne_MyCol"),
+            (list(t2.indexes)[0], "ix_some_table_some_col"),
+            (ix3, "my_index"),
+            (ix4, "MyIndex"),
+            (ix5, "ix_some_table_some_col"),
+        ]:
+            eq_(
+                testing.db.dialect.identifier_preparer.format_constraint(
+                    idx, _alembic_quote=False
+                ),
+                expected,
+            )
+
 
 class QuotedIdentTest(fixtures.TestBase):
     def test_concat_quotetrue(self):