Andrew Godwin avatar Andrew Godwin committed 509b3b7

Add --rebase option to schemamigration and make --list rebase-aware.

Comments (0)

Files changed (2)

south/management/commands/migrate.py

         # Get the migrations object
         for migration in app:
             if migration.app_label() + "." + migration.name() in applied_migrations:
-                print format_migration_list_item(migration.name())
+                print format_migration_list_item(migration)
             else:
-                print format_migration_list_item(migration.name(), applied=False)
+                print format_migration_list_item(migration, applied=False)
         print
 
 
-def format_migration_list_item(name, applied=True):
+def format_migration_list_item(migration, applied=True):
     if applied:
-        return '  (*) %s' % name
-    return '  ( ) %s' % name
+        return '  (*) %s' % migration.name()
+    elif migration.is_rebase:
+        return '  (R) %s' % migration.name()
+    return '  ( ) %s' % migration.name()

south/management/commands/schemamigration.py

             help='Generate an Add Index migration for the specified modelname.fieldname - you can use this multiple times to add more than one column.'),
         make_option('--initial', action='store_true', dest='initial', default=False,
             help='Generate the initial schema for the app.'),
+        make_option('--rebase', action='store_true', dest='rebase', default=False,
+            help='Create a "rebase" migration (implies --initial).'),
         make_option('--auto', action='store_true', dest='auto', default=False,
             help='Attempt to automatically detect differences from the last migration.'),
         make_option('--empty', action='store_true', dest='empty', default=False,
     help = "Creates a new template schema migration for the given app"
     usage_str = "Usage: ./manage.py schemamigration appname migrationname [--empty] [--initial] [--auto] [--add-model ModelName] [--add-field ModelName.field_name] [--stdout]"
     
-    def handle(self, app=None, name="", added_model_list=None, added_field_list=None, freeze_list=None, initial=False, auto=False, stdout=False, added_index_list=None, verbosity=1, empty=False, **options):
+    def handle(self, app=None, name="", added_model_list=None, added_field_list=None, freeze_list=None, initial=False, auto=False, stdout=False, added_index_list=None, verbosity=1, empty=False, rebase=False, **options):
         
         # Any supposed lists that are None become empty lists
         added_model_list = added_model_list or []
         # --stdout means name = -
         if stdout:
             name = "-"
+        
+        # Rebase implies initial
+        if rebase:
+            initial = True
 	
         # Only allow valid names
         if re.search('[^_\w]', name) and name != "-":
             elif empty:
                 change_source = None
             else:
-                print >>sys.stderr, "You have not passed any of --initial, --auto, --empty, --add-model, --add-field or --add-index."
+                print >>sys.stderr, "You have not passed any of --initial, --rebase, --auto, --empty, --add-model, --add-field or --add-index."
                 sys.exit(1)
         
         # if not name, there's an error
             "forwards": "\n".join(forwards_actions or ["pass"]), 
             "backwards": "\n".join(backwards_actions or ["pass"]), 
             "frozen_models":  freezer.freeze_apps_to_string(apps_to_freeze),
-            "complete_apps": apps_to_freeze and "complete_apps = [%s]" % (", ".join(map(repr, apps_to_freeze))) or ""
+            "complete_apps": apps_to_freeze and "complete_apps = [%s]" % (", ".join(map(repr, apps_to_freeze))) or "",
+            "rebase": rebase and "\n\n    rebase = True" or "",
         }
         
         # - is a special name which means 'print to stdout'
             fp.close()
             if empty:
                 print >>sys.stderr, "Created %s. You must now edit this migration and add the code for each direction." % new_filename
+            elif rebase:
+                print >>sys.stderr, "Created rebase migration %s. There is no need to apply this migration." % new_filename
             else:
                 print >>sys.stderr, "Created %s. You can now apply this migration with: ./manage.py migrate %s" % (new_filename, app)
 
 from south.v2 import SchemaMigration
 from django.db import models
 
-class Migration(SchemaMigration):
+class Migration(SchemaMigration):%(rebase)s
 
     def forwards(self, orm):
         %(forwards)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.