Andrew Godwin committed d0b7ce9 Merge

Merged in accek/south (pull request #146)

Re-create non-unique indexes in _remake_table in sqlite3

Comments (0)

Files changed (1)


         cursor = self._get_connection().cursor()
         # Get the index descriptions
         indexes = self._get_connection().introspection.get_indexes(cursor, table_name)
-        multi_indexes = self._get_multi_indexes(table_name)
+        standalone_indexes = self._get_standalone_indexes(table_name)
         # Work out new column defs.
         for column_info in self._get_full_table_description(self._get_connection(), cursor, table_name):
             name = column_info['name']
         # Recreate multi-valued indexes
         # We can't do that before since it's impossible to rename indexes
         # and index name scope is global
-        self._make_multi_indexes(table_name, multi_indexes, renames=renames, deleted=deleted, uniques_deleted=uniques_deleted)
+        self._make_standalone_indexes(table_name, standalone_indexes, renames=renames, deleted=deleted, uniques_deleted=uniques_deleted)
     def _copy_data(self, src, dst, field_renames={}, added={}):
         "Used to copy data into a new table"
         # Make a list of all the fields to select
     def _create_unique(self, table_name, columns):
-        self.execute("CREATE UNIQUE INDEX %s ON %s(%s);" % (
-            self.quote_name('%s_%s' % (table_name, '__'.join(columns))),
+        self._create_index(table_name, columns, True)
+    def _create_index(self, table_name, columns, unique=False, index_name=None):
+        if index_name is None:
+            index_name = '%s_%s' % (table_name, '__'.join(columns))
+        self.execute("CREATE %sINDEX %s ON %s(%s);" % (
+            unique and "UNIQUE " or "",
+            self.quote_name(index_name),
             ', '.join(self.quote_name(c) for c in columns),
-    def _get_multi_indexes(self, table_name):
+    def _get_standalone_indexes(self, table_name):
         indexes = []
         cursor = self._get_connection().cursor()
         cursor.execute('PRAGMA index_list(%s)' % self.quote_name(table_name))
         # seq, name, unique
         for index, unique in [(field[1], field[2]) for field in cursor.fetchall()]:
-            if not unique:
-                continue
             cursor.execute('PRAGMA index_info(%s)' % self.quote_name(index))
             info = cursor.fetchall()
-            if len(info) == 1:
+            if len(info) == 1 and unique:
+                # This index is already specified in the CREATE TABLE columns
+                # specification
             columns = []
             for field in info:
-            indexes.append(columns)
+            indexes.append((index, columns, unique))
         return indexes
-    def _make_multi_indexes(self, table_name, indexes, deleted=[], renames={}, uniques_deleted=[]):
-        for index in indexes:
+    def _make_standalone_indexes(self, table_name, indexes, deleted=[], renames={}, uniques_deleted=[]):
+        for index_name, index, unique in indexes:
             columns = []
             for name in index:
                     name = renames[name]
-            if columns and set(columns) != set(uniques_deleted):
-                self._create_unique(table_name, columns)
+            if columns and (set(columns) != set(uniques_deleted) or not unique):
+                self._create_index(table_name, columns, unique, index_name)
     def _column_sql_for_create(self, table_name, name, field, explicit_name=True):
         "Given a field and its name, returns the full type for the CREATE TABLE (without unique/pk)"
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
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.