]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- [bug] The CreateIndex construct in Oracle
authorMike Bayer <mike_mp@zzzcomputing.com>
Sun, 30 Sep 2012 20:00:46 +0000 (20:00 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Sun, 30 Sep 2012 20:00:46 +0000 (20:00 +0000)
    will now schema-qualify the name of the index
    to be that of the parent table.  Previously this
    name was omitted which apparently creates the
    index in the default schema, rather than that
    of the table.

CHANGES
lib/sqlalchemy/dialects/oracle/base.py
lib/sqlalchemy/sql/compiler.py
test/dialect/test_oracle.py

diff --git a/CHANGES b/CHANGES
index e3a893f34b2580a9870af6d6d48bf8ccb01ec39f..7f871d7d48ae8f0f3a39bfa3f1930930b77af325 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -738,6 +738,13 @@ underneath "0.7.xx".
     now defaults to STRING, UNICODE, removing
     CLOB, NCLOB from the list.  [ticket:2469]
 
+  - [bug] The CreateIndex construct in Oracle
+    will now schema-qualify the name of the index
+    to be that of the parent table.  Previously this
+    name was omitted which apparently creates the
+    index in the default schema, rather than that
+    of the table.
+
 - extensions
   - [removed] The SQLSoup extension is removed from
     SQLAlchemy, and is now an external project.
index 399f9003547bd6bd6068a3d055de1e35b570e372..c7cc6a7f932c93241e9186346e612c62ffe5f4b6 100644 (file)
@@ -631,6 +631,10 @@ class OracleDDLCompiler(compiler.DDLCompiler):
 
         return text
 
+    def visit_create_index(self, create, **kw):
+        return super(OracleDDLCompiler, self).\
+                    visit_create_index(create, include_schema=True)
+
 class OracleIdentifierPreparer(compiler.IdentifierPreparer):
 
     reserved_words = set([x.lower() for x in RESERVED_WORDS])
index 1acd37f62f0d0c0467b573d16400bfa207d6f581..3b17c040c5f17692fcbd46103a6e6015b010acab 100644 (file)
@@ -1866,15 +1866,16 @@ class DDLCompiler(engine.Compiled):
 
         return ident
 
-    def visit_create_index(self, create):
+    def visit_create_index(self, create, include_schema=False):
         index = create.element
         preparer = self.preparer
         text = "CREATE "
         if index.unique:
             text += "UNIQUE "
         text += "INDEX %s ON %s (%s)" \
-                    % (preparer.quote(self._index_identifier(index.name),
-                        index.quote),
+                    % (
+                        self._prepared_index_name(index,
+                                include_schema=include_schema),
                        preparer.format_table(index.table),
                        ', '.join(preparer.quote(c.name, c.quote)
                                  for c in index.columns))
@@ -1882,7 +1883,11 @@ class DDLCompiler(engine.Compiled):
 
     def visit_drop_index(self, drop):
         index = drop.element
-        if index.table is not None and index.table.schema:
+        return "\nDROP INDEX " + self._prepared_index_name(index,
+                                        include_schema=True)
+
+    def _prepared_index_name(self, index, include_schema=False):
+        if include_schema and index.table is not None and index.table.schema:
             schema = index.table.schema
             schema_name = self.preparer.quote_schema(schema,
                                 index.table.quote_schema)
@@ -1895,7 +1900,8 @@ class DDLCompiler(engine.Compiled):
 
         if schema_name:
             index_name = schema_name + "." + index_name
-        return "\nDROP INDEX " + index_name
+        return index_name
+
 
     def visit_add_constraint(self, create):
         preparer = self.preparer
index 66b27a12ae03e8ca7b4b79f7d00156873e292320..edb7be8402727b140ecbab47f4c3e9ba8ecd17ad 100644 (file)
@@ -2,7 +2,7 @@
 
 from sqlalchemy.testing import eq_
 from sqlalchemy import *
-from sqlalchemy import types as sqltypes, exc
+from sqlalchemy import types as sqltypes, exc, schema
 from sqlalchemy.sql import table, column
 from sqlalchemy.testing import fixtures, AssertsExecutionResults, AssertsCompiledSQL
 from sqlalchemy import testing
@@ -523,6 +523,16 @@ class CompileTest(fixtures.TestBase, AssertsCompiledSQL):
         ]:
             self.assert_compile(fn, expected)
 
+    def test_create_index_alt_schema(self):
+        m = MetaData()
+        t1 = Table('foo', m,
+                Column('x', Integer),
+                schema="alt_schema"
+            )
+        self.assert_compile(
+            schema.CreateIndex(Index("bar", t1.c.x)),
+            "CREATE INDEX alt_schema.bar ON alt_schema.foo (x)"
+        )
 class CompatFlagsTest(fixtures.TestBase, AssertsCompiledSQL):
     __only_on__ = 'oracle'