Commits

Andrew Godwin committed be0c295

Fix #376 (db_index changes)

  • Participants
  • Parent commits fb70898

Comments (0)

Files changed (2)

File south/creator/changes.py

                 
                 # For the ones that exist in both models, see if they were changed
                 for fieldname in set(old_fields).intersection(set(new_fields)):
+                    # Non-index changes
                     if self.different_attributes(
-                     remove_useless_attributes(old_fields[fieldname], True),
-                     remove_useless_attributes(new_fields[fieldname], True)):
+                     remove_useless_attributes(old_fields[fieldname], True, True),
+                     remove_useless_attributes(new_fields[fieldname], True, True)):
                         yield ("ChangeField", {
                             "model": self.current_model_from_key(key),
                             "old_field": self.old_orm[key + ":" + fieldname],
                             "old_def": old_fields[fieldname],
                             "new_def": new_fields[fieldname],
                         })
-                    # See if their uniques have changed
+                    # Index changes
                     old_field = self.old_orm[key + ":" + fieldname]
                     new_field = self.current_field_from_key(key, fieldname)
+                    if not old_field.db_index and new_field.db_index:
+                        # They've added an index.
+                        yield ("AddIndex", {
+                            "model": self.current_model_from_key(key),
+                            "fields": [new_field],
+                        })
+                    if old_field.db_index and not new_field.db_index:
+                        # They've removed an index.
+                        yield ("DeleteIndex", {
+                            "model": self.old_orm[key],
+                            "fields": [old_field],
+                        })
+                    # See if their uniques have changed
                     if old_field.unique != new_field.unique:
                         # Make sure we look at the one explicitly given to see what happened
                         if new_field.unique:
                             yield ("AddUnique", {
                                 "model": self.current_model_from_key(key),
-                                "fields": [self.current_field_from_key(key, fieldname)],
+                                "fields": [new_field],
                             })
                         else:
                             yield ("DeleteUnique", {
                                 "model": self.old_orm[key],
-                                "fields": [self.old_orm[key + ":" + fieldname]],
+                                "fields": [old_field],
                             })
                 
                 # See if there's any M2Ms that have changed.

File south/creator/freezer.py

 
 USELESS_KEYWORDS = ["choices", "help_text", "upload_to", "verbose_name", "storage"]
 USELESS_DB_KEYWORDS = ["related_name", "default"] # Important for ORM, not for DB.
+INDEX_KEYWORDS = ["db_index"]
 
-def remove_useless_attributes(field, db=False):
+def remove_useless_attributes(field, db=False, indexes=False):
     "Removes useless (for database) attributes from the field's defn."
     # Work out what to remove, and remove it.
     keywords = USELESS_KEYWORDS[:]
     if db:
         keywords += USELESS_DB_KEYWORDS[:]
+    if indexes:
+        keywords += INDEX_KEYWORDS[:]
     if field:
         for name in keywords:
             if name in field[2]: