summaryrefslogtreecommitdiff
path: root/django
diff options
context:
space:
mode:
authorDakota Hawkins <dakotahawkins@users.noreply.github.com>2018-12-19 06:41:31 -0500
committerCarlton Gibson <carlton.gibson@noumenal.es>2018-12-19 12:41:31 +0100
commit8d3147e130c4e5638fceb1e6125c040362ce12e8 (patch)
tree3e17b6ed50be3466ced3ec73a2745f37f994cebb /django
parentb514dc14f4e1c364341f5931b354e83ef15ee12d (diff)
Fixed #30031 -- Added --no-header option to makemigrations/squashmigrations.
Diffstat (limited to 'django')
-rw-r--r--django/core/management/commands/makemigrations.py9
-rw-r--r--django/core/management/commands/squashmigrations.py7
-rw-r--r--django/db/migrations/writer.py22
3 files changed, 28 insertions, 10 deletions
diff --git a/django/core/management/commands/makemigrations.py b/django/core/management/commands/makemigrations.py
index cdccb8e9b3..5782b58398 100644
--- a/django/core/management/commands/makemigrations.py
+++ b/django/core/management/commands/makemigrations.py
@@ -49,6 +49,10 @@ class Command(BaseCommand):
help="Use this name for migration file(s).",
)
parser.add_argument(
+ '--no-header', action='store_false', dest='include_header',
+ help='Do not add header comments to new migration file(s).',
+ )
+ parser.add_argument(
'--check', action='store_true', dest='check_changes',
help='Exit with a non-zero status if model changes are missing migrations.',
)
@@ -63,6 +67,7 @@ class Command(BaseCommand):
self.migration_name = options['name']
if self.migration_name and not self.migration_name.isidentifier():
raise CommandError('The migration name must be a valid Python identifier.')
+ self.include_header = options['include_header']
check_changes = options['check_changes']
# Make sure the app they asked for exists
@@ -188,7 +193,7 @@ class Command(BaseCommand):
self.stdout.write(self.style.MIGRATE_HEADING("Migrations for '%s':" % app_label) + "\n")
for migration in app_migrations:
# Describe the migration
- writer = MigrationWriter(migration)
+ writer = MigrationWriter(migration, self.include_header)
if self.verbosity >= 1:
# Display a relative path if it's below the current working
# directory, or an absolute path otherwise.
@@ -288,7 +293,7 @@ class Command(BaseCommand):
self.migration_name or ("merge_%s" % get_migration_name_timestamp())
)
new_migration = subclass(migration_name, app_label)
- writer = MigrationWriter(new_migration)
+ writer = MigrationWriter(new_migration, self.include_header)
if not self.dry_run:
# Write the merge migrations file to the disk
diff --git a/django/core/management/commands/squashmigrations.py b/django/core/management/commands/squashmigrations.py
index ee91241369..4a0ab6af83 100644
--- a/django/core/management/commands/squashmigrations.py
+++ b/django/core/management/commands/squashmigrations.py
@@ -37,6 +37,10 @@ class Command(BaseCommand):
'--squashed-name',
help='Sets the name of the new squashed migration.',
)
+ parser.add_argument(
+ '--no-header', action='store_false', dest='include_header',
+ help='Do not add a header comment to the new squashed migration.',
+ )
def handle(self, **options):
@@ -47,6 +51,7 @@ class Command(BaseCommand):
migration_name = options['migration_name']
no_optimize = options['no_optimize']
squashed_name = options['squashed_name']
+ include_header = options['include_header']
# Validate app_label.
try:
apps.get_app_config(app_label)
@@ -178,7 +183,7 @@ class Command(BaseCommand):
new_migration.initial = True
# Write out the new migration file
- writer = MigrationWriter(new_migration)
+ writer = MigrationWriter(new_migration, include_header)
with open(writer.path, "w", encoding='utf-8') as fh:
fh.write(writer.as_string())
diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py
index d056e00646..6a62b4cbf4 100644
--- a/django/db/migrations/writer.py
+++ b/django/db/migrations/writer.py
@@ -132,8 +132,9 @@ class MigrationWriter:
of the migration file from it.
"""
- def __init__(self, migration):
+ def __init__(self, migration, include_header=True):
self.migration = migration
+ self.include_header = include_header
self.needs_manual_porting = False
def as_string(self):
@@ -195,10 +196,13 @@ class MigrationWriter:
if self.migration.replaces:
items['replaces_str'] = "\n replaces = %s\n" % self.serialize(self.migration.replaces)[0]
# Hinting that goes into comment
- items.update(
- version=get_version(),
- timestamp=now().strftime("%Y-%m-%d %H:%M"),
- )
+ if self.include_header:
+ items['migration_header'] = MIGRATION_HEADER_TEMPLATE % {
+ 'version': get_version(),
+ 'timestamp': now().strftime("%Y-%m-%d %H:%M"),
+ }
+ else:
+ items['migration_header'] = ""
if self.migration.initial:
items['initial_str'] = "\n initial = True\n"
@@ -279,10 +283,14 @@ class MigrationWriter:
return serializer_factory(value).serialize()
-MIGRATION_TEMPLATE = """\
+MIGRATION_HEADER_TEMPLATE = """\
# Generated by Django %(version)s on %(timestamp)s
-%(imports)s
+"""
+
+
+MIGRATION_TEMPLATE = """\
+%(migration_header)s%(imports)s
class Migration(migrations.Migration):
%(replaces_str)s%(initial_str)s