Anonymous avatar Anonymous committed 4740193

added functionality to support unique multi-column indexes.
startmigration automatically adds these for any models that specify the unique_together Meta option

Comments (0)

Files changed (2)

             connection.ops.deferrable_sql() # Django knows this
         )
         
-    def create_index_sql(self, table_name, column_names, db_tablespace=''):
+    def create_index_name(self, table_name, column_names):
+        """
+        Generate a unique name for the index
+        """
+        index_unique_name = ''
+        if len(column_names) > 1:
+            index_unique_name = '_%x' % abs(hash((table_name, ','.join(column_names))))
+
+        return '%s_%s%s' % (table_name, column_names[0], index_unique_name)
+
+    def create_index_sql(self, table_name, column_names, unique=False, db_tablespace=''):
         """
         Generates a create index statement on 'table_name' for a list of 'column_names'
         """
         else:
             tablespace_sql = ''
         
-        index_unique_name = ''
-        if len(column_names) > 1:
-            index_unique_name = '_%x' % abs(hash((table_name, ','.join(column_names))))
-            
-        index_name = '%s_%s%s' % (table_name, column_names[0], index_unique_name)
+        index_name = self.create_index_name(table_name, column_names)
         qn = connection.ops.quote_name
-        return 'CREATE INDEX %s ON %s (%s)%s;' % (
+        return 'CREATE %sINDEX %s ON %s (%s)%s;' % (
+            unique and 'UNIQUE ' or '',
             index_name,
             table_name,
             ','.join([qn(field) for field in column_names]),
             tablespace_sql
             )
         
-
+    def create_index(self, table_name, column_names, unique=False, db_tablespace=''):
+        """ Executes a create index statement """
+        sql = self.create_index_sql(table_name, column_names, unique, db_tablespace)
+        self.execute(sql)
+        
 
     def delete_column(self, table_name, name):
         """

management/commands/startmigration.py

                     backwards += '''
         db.delete_table('%s')''' % m.m2m_db_table()
                 
+                if model._meta.unique_together:
+                    ut = model._meta.unique_together
+                    if not isinstance(ut[0], (list, tuple)):
+                        ut = (ut,)
+                        
+                    for unique in ut:
+                        columns = ["'%s'" % model._meta.get_field(f).column for f in unique]
+                        
+                        forwards += '''
+        db.create_index('%s', [%s], True, db_tablespace='%s')
+        ''' %   (
+                        table_name,
+                        ','.join(columns),
+                        model._meta.db_tablespace
+                )
+                
+                
             forwards += '''
         
         db.send_create_signal('%s', ['%s'])''' % (
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.