From 43453dfa31b4b399c38101eabf9f9b6dc9b0b7cd Mon Sep 17 00:00:00 2001 From: Mike Bayer Date: Mon, 24 Sep 2012 11:17:16 -0400 Subject: [PATCH] - [bug] Fixed the DropIndex construct to support an Index associated with a Table in a remote schema. [ticket:2571] --- CHANGES | 4 ++++ lib/sqlalchemy/sql/compiler.py | 24 +++++++++++++++------ test/sql/test_constraints.py | 39 ++++++++++++++++++++++++++++++++++ 3 files changed, 60 insertions(+), 7 deletions(-) diff --git a/CHANGES b/CHANGES index 0d421003de..2dab1c0189 100644 --- a/CHANGES +++ b/CHANGES @@ -46,6 +46,10 @@ CHANGES 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 diff --git a/lib/sqlalchemy/sql/compiler.py b/lib/sqlalchemy/sql/compiler.py index bd115314cf..f45c9c8967 100644 --- a/lib/sqlalchemy/sql/compiler.py +++ b/lib/sqlalchemy/sql/compiler.py @@ -1647,13 +1647,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 @@ -1671,9 +1670,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 diff --git a/test/sql/test_constraints.py b/test/sql/test_constraints.py index 8a82381399..ed86dfe7d4 100644 --- a/test/sql/test_constraints.py +++ b/test/sql/test_constraints.py @@ -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' -- 2.47.2