mirror of
https://github.com/django/django.git
synced 2025-10-24 14:16:09 +00:00
Fixed #26429 -- Added a timestamp to merge migration names.
This reduces the possibility of a naming conflict, especially after squashing migrations.
This commit is contained in:
committed by
Tim Graham
parent
535660b852
commit
8f6a1a1551
@@ -76,7 +76,7 @@ def get_commands():
|
||||
return commands
|
||||
|
||||
|
||||
def call_command(name, *args, **options):
|
||||
def call_command(command_name, *args, **options):
|
||||
"""
|
||||
Calls the given command, with the given options and args/kwargs.
|
||||
|
||||
@@ -95,25 +95,25 @@ def call_command(name, *args, **options):
|
||||
call_command(cmd, verbosity=0, interactive=False)
|
||||
# Do something with cmd ...
|
||||
"""
|
||||
if isinstance(name, BaseCommand):
|
||||
if isinstance(command_name, BaseCommand):
|
||||
# Command object passed in.
|
||||
command = name
|
||||
name = command.__class__.__module__.split('.')[-1]
|
||||
command = command_name
|
||||
command_name = command.__class__.__module__.split('.')[-1]
|
||||
else:
|
||||
# Load the command object by name.
|
||||
try:
|
||||
app_name = get_commands()[name]
|
||||
app_name = get_commands()[command_name]
|
||||
except KeyError:
|
||||
raise CommandError("Unknown command: %r" % name)
|
||||
raise CommandError("Unknown command: %r" % command_name)
|
||||
|
||||
if isinstance(app_name, BaseCommand):
|
||||
# If the command is already loaded, use it directly.
|
||||
command = app_name
|
||||
else:
|
||||
command = load_command_class(app_name, name)
|
||||
command = load_command_class(app_name, command_name)
|
||||
|
||||
# Simulate argument parsing to get the option defaults (see #10080 for details).
|
||||
parser = command.create_parser('', name)
|
||||
parser = command.create_parser('', command_name)
|
||||
# Use the `dest` option name from the parser option
|
||||
opt_mapping = {
|
||||
sorted(s_opt.option_strings)[0].lstrip('-').replace('-', '_'): s_opt.dest
|
||||
|
||||
@@ -14,6 +14,7 @@ from django.db.migrations.questioner import (
|
||||
NonInteractiveMigrationQuestioner,
|
||||
)
|
||||
from django.db.migrations.state import ProjectState
|
||||
from django.db.migrations.utils import get_migration_name_timestamp
|
||||
from django.db.migrations.writer import MigrationWriter
|
||||
from django.utils.deprecation import RemovedInDjango20Warning
|
||||
from django.utils.six import iteritems
|
||||
@@ -283,7 +284,11 @@ class Command(BaseCommand):
|
||||
subclass = type("Migration", (Migration, ), {
|
||||
"dependencies": [(app_label, migration.name) for migration in merge_migrations],
|
||||
})
|
||||
new_migration = subclass("%04i_merge" % (biggest_number + 1), app_label)
|
||||
migration_name = "%04i_%s" % (
|
||||
biggest_number + 1,
|
||||
self.migration_name or ("merge_%s" % get_migration_name_timestamp())
|
||||
)
|
||||
new_migration = subclass(migration_name, app_label)
|
||||
writer = MigrationWriter(new_migration)
|
||||
|
||||
if not self.dry_run:
|
||||
|
||||
@@ -1,6 +1,5 @@
|
||||
from __future__ import unicode_literals
|
||||
|
||||
import datetime
|
||||
import functools
|
||||
import re
|
||||
from itertools import chain
|
||||
@@ -12,7 +11,9 @@ from django.db.migrations.migration import Migration
|
||||
from django.db.migrations.operations.models import AlterModelOptions
|
||||
from django.db.migrations.optimizer import MigrationOptimizer
|
||||
from django.db.migrations.questioner import MigrationQuestioner
|
||||
from django.db.migrations.utils import COMPILED_REGEX_TYPE, RegexObject
|
||||
from django.db.migrations.utils import (
|
||||
COMPILED_REGEX_TYPE, RegexObject, get_migration_name_timestamp,
|
||||
)
|
||||
from django.utils import six
|
||||
|
||||
from .topological_sort import stable_topological_sort
|
||||
@@ -1154,7 +1155,7 @@ class MigrationAutodetector(object):
|
||||
elif len(ops) > 1:
|
||||
if all(isinstance(o, operations.CreateModel) for o in ops):
|
||||
return "_".join(sorted(o.name_lower for o in ops))
|
||||
return "auto_%s" % datetime.datetime.now().strftime("%Y%m%d_%H%M")
|
||||
return "auto_%s" % get_migration_name_timestamp()
|
||||
|
||||
@classmethod
|
||||
def parse_number(cls, name):
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
import datetime
|
||||
import re
|
||||
|
||||
COMPILED_REGEX_TYPE = type(re.compile(''))
|
||||
@@ -10,3 +11,7 @@ class RegexObject(object):
|
||||
|
||||
def __eq__(self, other):
|
||||
return self.pattern == other.pattern and self.flags == other.flags
|
||||
|
||||
|
||||
def get_migration_name_timestamp():
|
||||
return datetime.datetime.now().strftime("%Y%m%d_%H%M")
|
||||
|
||||
Reference in New Issue
Block a user