]> git.ipfire.org Git - thirdparty/sqlalchemy/sqlalchemy.git/commitdiff
- sqlite dialect properly generates CREATE INDEX for a table
authorMike Bayer <mike_mp@zzzcomputing.com>
Mon, 7 Dec 2009 23:08:42 +0000 (23:08 +0000)
committerMike Bayer <mike_mp@zzzcomputing.com>
Mon, 7 Dec 2009 23:08:42 +0000 (23:08 +0000)
that is in an alternate schema.  [ticket:1439]

CHANGES
lib/sqlalchemy/dialects/sqlite/base.py
test/dialect/test_sqlite.py

diff --git a/CHANGES b/CHANGES
index 1d652a29bbb6c255dd7256f3d7982c72e6f9230a..d7c30b55a30fb15498a0085356fa281eccd07584 100644 (file)
--- a/CHANGES
+++ b/CHANGES
@@ -792,6 +792,10 @@ CHANGES
     - The cursor associated with connection pool connections
       (i.e. _CursorFairy) now proxies `__iter__()` to the 
       underlying cursor correctly. [ticket:1632]
+
+- sqlite
+    - sqlite dialect properly generates CREATE INDEX for a table
+      that is in an alternate schema.  [ticket:1439]
       
 - postgresql
     - Added support for reflecting the DOUBLE PRECISION type,
index d83eb4b8649ac547e5a5ea940e1442f7631a7421..235a17a66fd2bbc5c03f78b6b4e5f98d93799e4e 100644 (file)
@@ -239,11 +239,25 @@ class SQLiteDDLCompiler(compiler.DDLCompiler):
         if not column.nullable:
             colspec += " NOT NULL"
         return colspec
-    
+
+    def visit_create_index(self, create):
+        index = create.element
+        preparer = self.preparer
+        text = "CREATE "
+        if index.unique:
+            text += "UNIQUE "
+        text += "INDEX %s ON %s (%s)" \
+                    % (preparer.format_index(index,
+                       name=self._validate_identifier(index.name, True)),
+                       preparer.format_table(index.table, use_schema=False),
+                       ', '.join(preparer.quote(c.name, c.quote)
+                                 for c in index.columns))
+        return text
+
 class SQLiteTypeCompiler(compiler.GenericTypeCompiler):
     def visit_binary(self, type_):
         return self.visit_BLOB(type_)
-    
+
 class SQLiteIdentifierPreparer(compiler.IdentifierPreparer):
     reserved_words = set([
         'add', 'after', 'all', 'alter', 'analyze', 'and', 'as', 'asc',
@@ -265,6 +279,16 @@ class SQLiteIdentifierPreparer(compiler.IdentifierPreparer):
         'vacuum', 'values', 'view', 'virtual', 'when', 'where',
         ])
 
+    def format_index(self, index, use_schema=True, name=None):
+        """Prepare a quoted index and schema name."""
+
+        if name is None:
+            name = index.name
+        result = self.quote(name, index.quote)
+        if not self.omit_schema and use_schema and getattr(index.table, "schema", None):
+            result = self.quote_schema(index.table.schema, index.table.quote_schema) + "." + result
+        return result
+
 class SQLiteDialect(default.DefaultDialect):
     name = 'sqlite'
     supports_alter = False
index 2d5cb1805d7db2b663c0b41a357fb5523ae34dee..7fced60d94aa0f3d9e617a4cda187b7d464f3fdf 100644 (file)
@@ -69,7 +69,7 @@ class TestTypes(TestBase, AssertsExecutionResults):
 
             bindproc = t.dialect_impl(dialect).bind_processor(dialect)
             assert not bindproc or isinstance(bindproc(u"some string"), unicode)
-        
+
     def test_type_reflection(self):
         # (ask_for, roundtripped_as_if_different)
         specs = [( String(), String(), ),
@@ -324,6 +324,18 @@ class DialectTest(TestBase, AssertsExecutionResults):
             isolation_level="FOO")
 
 
+    def test_create_index_with_schema(self):
+        """Test creation of index with explicit schema"""
+
+        meta = MetaData(testing.db)
+        t = Table('foo', meta, Column('bar', String, index=True), schema='main')
+
+        try:
+            meta.create_all()
+        finally:
+            meta.drop_all()
+
+
 class SQLTest(TestBase, AssertsCompiledSQL):
     """Tests SQLite-dialect specific compilation."""