Mike Bayer avatar Mike Bayer committed 1329ad3

- [bug] Fixed the DropIndex construct to support
an Index associated with a Table in a remote
schema. [ticket:2571]

Comments (0)

Files changed (3)

     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

lib/sqlalchemy/sql/compiler.py

             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
 
     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

test/sql/test_constraints.py

 from sqlalchemy.dialects.postgresql import base as postgresql
 
 class ConstraintTest(fixtures.TestBase, AssertsExecutionResults, AssertsCompiledSQL):
+    __dialect__ = 'default'
 
     def setup(self):
         global metadata
             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'
 
Tip: Filter by directory path e.g. /media app.js to search for public/media/app.js.
Tip: Use camelCasing e.g. ProjME to search for ProjectModifiedEvent.java.
Tip: Filter by extension type e.g. /repo .js to search for all .js files in the /repo directory.
Tip: Separate your search with spaces e.g. /ssh pom.xml to search for src/ssh/pom.xml.
Tip: Use ↑ and ↓ arrow keys to navigate and return to view the file.
Tip: You can also navigate files with Ctrl+j (next) and Ctrl+k (previous) and view the file with Ctrl+o.
Tip: You can also navigate files with Alt+j (next) and Alt+k (previous) and view the file with Alt+o.