]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- [bug] Fixed the DropIndex construct to support
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 24 Sep 2012 15:17:16 +0000 (11:17 -0400)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 24 Sep 2012 15:17:16 +0000 (11:17 -0400)
    an Index associated with a Table in a remote
    schema. [ticket:2571]

CHANGES
lib/sqlalchemy/sql/compiler.py
test/sql/test_constraints.py

diff --git a/CHANGES b/CHANGES
index a919d394b13f40789dfde73dcbc48ded7ad2bfc2..76f1a3547ae16b2f3fc7473f53301e04c5346137 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -779,6 +779,10 @@ are also present in 0.8.
     adding new content each time. [ticket:2566]
 
 - sql
+  - [bug] Fixed the DropIndex construct to support
+    an Index associated with a Table in a remote
+    schema. [ticket:2571]
+
   - [bug] Fixed CTE bug whereby positional
     bound parameters present in the CTEs themselves
     would corrupt the overall ordering of
index 8a7f05eb33fb1dea5af60f0d2dd5d2f60685b551..1acd37f62f0d0c0467b573d16400bfa207d6f581 100644 (file)
@@ -1859,13 +1859,12 @@ class DDLCompiler(engine.Compiled):
             max = self.dialect.max_index_name_length or \
                         self.dialect.max_identifier_length
             if len(ident) > max:
-                return ident[0:max - 8] + \
+                ident = ident[0:max - 8] + \
                                 "_" + util.md5_hex(ident)[-4:]
-            else:
-                return ident
         else:
             self.dialect.validate_identifier(ident)
-            return ident
+
+        return ident
 
     def visit_create_index(self, create):
         index = create.element
@@ -1883,9 +1882,20 @@ class DDLCompiler(engine.Compiled):
 
     def visit_drop_index(self, drop):
         index = drop.element
-        return "\nDROP INDEX " + \
-                    self.preparer.quote(
-                            self._index_identifier(index.name), index.quote)
+        if index.table is not None and index.table.schema:
+            schema = index.table.schema
+            schema_name = self.preparer.quote_schema(schema,
+                                index.table.quote_schema)
+        else:
+            schema_name = None
+
+        index_name = self.preparer.quote(
+                            self._index_identifier(index.name),
+                                    index.quote)
+
+        if schema_name:
+            index_name = schema_name + "." + index_name
+        return "\nDROP INDEX " + index_name
 
     def visit_add_constraint(self, create):
         preparer = self.preparer
index 2869839dcdcad13fa71f9925d7e842803cc73882..75a7cb2e153acbb56d7d74016fad505d2e6cf015 100644 (file)
@@ -9,6 +9,7 @@ from test.lib.assertsql import AllOf, RegexSQL, ExactSQL, CompiledSQL
 from sqlalchemy.dialects.postgresql import base as postgresql
 
 class ConstraintTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL):
+    __dialect__ = 'default'
 
     def setup(self):
         global metadata
@@ -285,6 +286,44 @@ class ConstraintTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiled
             Index, "foo", SomeClass()
         )
 
+    def test_create_plain(self):
+        t = Table('t', MetaData(), Column('x', Integer))
+        i = Index("xyz", t.c.x)
+        self.assert_compile(
+            schema.CreateIndex(i),
+            "CREATE INDEX xyz ON t (x)"
+        )
+
+    def test_drop_plain_unattached(self):
+        self.assert_compile(
+            schema.DropIndex(Index(name="xyz")),
+            "DROP INDEX xyz"
+        )
+
+    def test_drop_plain(self):
+        t = Table('t', MetaData(), Column('x', Integer))
+        i = Index("xyz", t.c.x)
+        self.assert_compile(
+            schema.DropIndex(Index(name="xyz")),
+            "DROP INDEX xyz"
+        )
+
+    def test_create_schema(self):
+        t = Table('t', MetaData(), Column('x', Integer), schema="foo")
+        i = Index("xyz", t.c.x)
+        self.assert_compile(
+            schema.CreateIndex(i),
+            "CREATE INDEX xyz ON foo.t (x)"
+        )
+
+    def test_drop_schema(self):
+        t = Table('t', MetaData(), Column('x', Integer), schema="foo")
+        i = Index("xyz", t.c.x)
+        self.assert_compile(
+            schema.DropIndex(i),
+            "DROP INDEX foo.xyz"
+        )
+
 class ConstraintCompilationTest(fixtures.TestBase, AssertsCompiledSQL):
     __dialect__ = 'default'