From e34226fc37dfa9eba89d913fd7ab8e95663b0d64 Mon Sep 17 00:00:00 2001 From: Tyson Clugg Date: Tue, 11 Aug 2015 17:51:01 +1000 Subject: [PATCH] Fixed #25259 -- Added comments to header of generated migration files --- AUTHORS | 1 + django/db/migrations/writer.py | 9 ++++++++- docs/howto/writing-migrations.txt | 2 ++ docs/topics/migrations.txt | 1 + tests/migrations/test_writer.py | 24 +++++++++++++++++++++++- 5 files changed, 35 insertions(+), 2 deletions(-) diff --git a/AUTHORS b/AUTHORS index 73d701920d..48322c4e19 100644 --- a/AUTHORS +++ b/AUTHORS @@ -713,6 +713,7 @@ answer newbie questions, and generally made Django that much better: tstromberg@google.com tt@gurgle.no Tyler Tarabula + Tyson Clugg Tyson Tate Unai Zalakain Valentina Mukhamedzhanova diff --git a/django/db/migrations/writer.py b/django/db/migrations/writer.py index fb6a162b38..5d24fc4628 100644 --- a/django/db/migrations/writer.py +++ b/django/db/migrations/writer.py @@ -10,6 +10,7 @@ import re import types from importlib import import_module +from django import get_version from django.apps import apps from django.db import migrations, models from django.db.migrations.loader import MigrationLoader @@ -21,7 +22,7 @@ from django.utils.encoding import force_text from django.utils.functional import Promise from django.utils.inspect import get_func_args from django.utils.module_loading import module_dir -from django.utils.timezone import utc +from django.utils.timezone import now, utc from django.utils.version import get_docs_version @@ -211,6 +212,11 @@ class MigrationWriter(object): # If there's a replaces, make a string for it 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.migration.initial: items['initial_str'] = "\n initial = True\n" @@ -526,6 +532,7 @@ class MigrationWriter(object): MIGRATION_TEMPLATE = """\ # -*- coding: utf-8 -*- +# Generated by Django %(version)s on %(timestamp)s from __future__ import unicode_literals %(imports)s diff --git a/docs/howto/writing-migrations.txt b/docs/howto/writing-migrations.txt index f1dfc92d1f..5527302fa8 100644 --- a/docs/howto/writing-migrations.txt +++ b/docs/howto/writing-migrations.txt @@ -103,6 +103,7 @@ the respective field according to your needs. :filename: 0006_remove_uuid_null.py # -*- coding: utf-8 -*- + # Generated by Django A.B on YYYY-MM-DD HH:MM from __future__ import unicode_literals from django.db import migrations, models @@ -156,6 +157,7 @@ the respective field according to your needs. :filename: 0005_populate_uuid_values.py # -*- coding: utf-8 -*- + # Generated by Django A.B on YYYY-MM-DD HH:MM from __future__ import unicode_literals from django.db import migrations, models diff --git a/docs/topics/migrations.txt b/docs/topics/migrations.txt index c656e56db5..d7aa7051d1 100644 --- a/docs/topics/migrations.txt +++ b/docs/topics/migrations.txt @@ -449,6 +449,7 @@ the file in the right place, suggest a name, and add dependencies for you):: Then, open up the file; it should look something like this:: # -*- coding: utf-8 -*- + # Generated by Django A.B on YYYY-MM-DD HH:MM from django.db import models, migrations class Migration(migrations.Migration): diff --git a/tests/migrations/test_writer.py b/tests/migrations/test_writer.py index fb18f1cdb4..2b43fcf837 100644 --- a/tests/migrations/test_writer.py +++ b/tests/migrations/test_writer.py @@ -12,13 +12,14 @@ import unittest import custom_migration_operations.more_operations import custom_migration_operations.operations +from django import get_version from django.conf import settings from django.core.validators import EmailValidator, RegexValidator from django.db import migrations, models from django.db.migrations.writer import ( MigrationWriter, OperationWriter, SettingsReference, ) -from django.test import SimpleTestCase, ignore_warnings +from django.test import SimpleTestCase, ignore_warnings, mock from django.utils import datetime_safe, six from django.utils._os import upath from django.utils.deconstruct import deconstructible @@ -525,6 +526,27 @@ class WriterTests(SimpleTestCase): output ) + def test_migration_file_header_comments(self): + """ + Test comments at top of file. + """ + migration = type(str("Migration"), (migrations.Migration,), { + "operations": [] + }) + dt = datetime.datetime(2015, 7, 31, 4, 40, 0, 0, tzinfo=utc) + with mock.patch('django.db.migrations.writer.now', lambda: dt): + writer = MigrationWriter(migration) + output = writer.as_string().decode('utf-8') + + self.assertTrue( + output.startswith( + "# -*- coding: utf-8 -*-\n" + "# Generated by Django %(version)s on 2015-07-31 04:40\n" % { + 'version': get_version(), + } + ) + ) + def test_models_import_omitted(self): """ django.db.models shouldn't be imported if unused.