Ticket #23302: django-1.7-makemigrations-patch.diff

File django-1.7-makemigrations-patch.diff, 4.2 KB (added by Raffaele Salmaso, 10 years ago)

add --name/-n option to makemigrations

  • django/core/management/commands/makemigrations.py

    diff -rNu django-1.7/django/core/management/commands/makemigrations.py django-1.7-makemigrations-patch/django/core/management/commands/makemigrations.py
    old new  
    2525            help="Create an empty migration."),
    2626        make_option('--noinput', action='store_false', dest='interactive', default=True,
    2727            help='Tells Django to NOT prompt the user for input of any kind.'),
     28        make_option('--name', '-n', action='store', dest='name', default=None,
     29            help="Use this name for migration file(s)."),
    2830    )
    2931
    3032    help = "Creates new migration(s) for apps."
     
    3840        self.dry_run = options.get('dry_run', False)
    3941        self.merge = options.get('merge', False)
    4042        self.empty = options.get('empty', False)
     43        self.migration_name = options.get('name')
    4144
    4245        # Make sure the app they asked for exists
    4346        app_labels = set(app_labels)
     
    100103                (app, [Migration("custom", app)])
    101104                for app in app_labels
    102105            )
    103             changes = autodetector.arrange_for_graph(changes, loader.graph)
     106            # here is where the --empty migrations are processed
     107            changes = autodetector.arrange_for_graph(
     108                changes=changes,
     109                graph=loader.graph,
     110                migration_name=self.migration_name,
     111            )
    104112            self.write_migration_files(changes)
    105113            return
    106114
     
    109117            graph=loader.graph,
    110118            trim_to_apps=app_labels or None,
    111119            convert_apps=app_labels or None,
     120            migration_name=self.migration_name,
    112121        )
    113122
    114123        # No changes? Tell them.
  • django/db/migrations/autodetector.py

    diff -rNu django-1.7/django/db/migrations/autodetector.py django-1.7-makemigrations-patch/django/db/migrations/autodetector.py
    old new  
    3131        self.to_state = to_state
    3232        self.questioner = questioner or MigrationQuestioner()
    3333
    34     def changes(self, graph, trim_to_apps=None, convert_apps=None):
     34    def changes(self, graph, trim_to_apps=None, convert_apps=None, migration_name=None):
    3535        """
    3636        Main entry point to produce a list of appliable changes.
    3737        Takes a graph to base names on and an optional set of apps
    3838        to try and restrict to (restriction is not guaranteed)
    3939        """
    4040        changes = self._detect_changes(convert_apps, graph)
    41         changes = self.arrange_for_graph(changes, graph)
     41        changes = self.arrange_for_graph(changes, graph, migration_name)
    4242        if trim_to_apps:
    4343            changes = self._trim_to_apps(changes, trim_to_apps)
    4444        return changes
     
    937937                    dependencies=dependencies,
    938938                )
    939939
    940     def arrange_for_graph(self, changes, graph):
     940    def arrange_for_graph(self, changes, graph, migration_name=None):
    941941        """
    942942        Takes in a result from changes() and a MigrationGraph,
    943943        and fixes the names and dependencies of the changes so they
     
    971971                if i == 0 and app_leaf:
    972972                    migration.dependencies.append(app_leaf)
    973973                if i == 0 and not app_leaf:
    974                     new_name = "0001_initial"
     974                    if migration_name is None:
     975                        new_name = "0001_initial"
     976                    else:
     977                        new_name = "0001_%s" % migration_name
    975978                else:
     979                    if migration_name is None:
     980                        migration_name = self.suggest_name(migration.operations)[:100]
    976981                    new_name = "%04i_%s" % (
    977982                        next_number,
    978                         self.suggest_name(migration.operations)[:100],
     983                        migration_name,
    979984                    )
    980985                name_map[(app_label, migration.name)] = (app_label, new_name)
    981986                next_number += 1
Back to Top