From dd1ea70779adff294140eddb0229c382e12f32f3 Mon Sep 17 00:00:00 2001 From: Tony Zhu Date: Fri, 24 Oct 2014 15:29:59 -0400 Subject: [PATCH] Fixed #23699 -- Prevented flush from loading initial data for apps with migrations. --- django/core/management/commands/flush.py | 10 ++++-- docs/releases/1.7.2.txt | 3 ++ tests/fixtures_migration/__init__.py | 0 .../fixtures/initial_data.json | 9 ++++++ .../migrations/0001_initial.py | 16 ++++++++++ .../fixtures_migration/migrations/__init__.py | 0 tests/fixtures_migration/models.py | 5 +++ tests/fixtures_migration/tests.py | 31 +++++++++++++++++++ 8 files changed, 71 insertions(+), 3 deletions(-) create mode 100644 tests/fixtures_migration/__init__.py create mode 100644 tests/fixtures_migration/fixtures/initial_data.json create mode 100644 tests/fixtures_migration/migrations/0001_initial.py create mode 100644 tests/fixtures_migration/migrations/__init__.py create mode 100644 tests/fixtures_migration/models.py create mode 100644 tests/fixtures_migration/tests.py diff --git a/django/core/management/commands/flush.py b/django/core/management/commands/flush.py index 02f558b31d..53e5e389f0 100644 --- a/django/core/management/commands/flush.py +++ b/django/core/management/commands/flush.py @@ -85,9 +85,13 @@ Are you sure you want to do this? # Reinstall the initial_data fixture. if options.get('load_initial_data'): - # Reinstall the initial_data fixture. - call_command('loaddata', 'initial_data', **options) - + # Reinstall the initial_data fixture for apps without migrations. + from django.db.migrations.executor import MigrationExecutor + executor = MigrationExecutor(connection) + app_options = options.copy() + for app_label in executor.loader.unmigrated_apps: + app_options['app_label'] = app_label + call_command('loaddata', 'initial_data', **app_options) else: self.stdout.write("Flush cancelled.\n") diff --git a/docs/releases/1.7.2.txt b/docs/releases/1.7.2.txt index 32500045f0..d9cf2e4be2 100644 --- a/docs/releases/1.7.2.txt +++ b/docs/releases/1.7.2.txt @@ -14,3 +14,6 @@ Bugfixes * Fixed a migration crash when adding an explicit ``id`` field to a model on SQLite (:ticket:`23702`). + +* Prevented :djadmin:`flush` from loading initial data for migrated apps + (:ticket:`23699`). diff --git a/tests/fixtures_migration/__init__.py b/tests/fixtures_migration/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/fixtures_migration/fixtures/initial_data.json b/tests/fixtures_migration/fixtures/initial_data.json new file mode 100644 index 0000000000..2e979fc842 --- /dev/null +++ b/tests/fixtures_migration/fixtures/initial_data.json @@ -0,0 +1,9 @@ +[ + { + "pk": "10", + "model": "fixtures_migration.book", + "fields": { + "name": "Achieving self-awareness of Python programs" + } + } +] diff --git a/tests/fixtures_migration/migrations/0001_initial.py b/tests/fixtures_migration/migrations/0001_initial.py new file mode 100644 index 0000000000..f1db74a874 --- /dev/null +++ b/tests/fixtures_migration/migrations/0001_initial.py @@ -0,0 +1,16 @@ +# -*- coding: utf-8 -*- +from __future__ import unicode_literals + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + operations = [ + migrations.CreateModel( + "Book", + [ + ("name", models.CharField(max_length=100)), + ], + ), + ] diff --git a/tests/fixtures_migration/migrations/__init__.py b/tests/fixtures_migration/migrations/__init__.py new file mode 100644 index 0000000000..e69de29bb2 diff --git a/tests/fixtures_migration/models.py b/tests/fixtures_migration/models.py new file mode 100644 index 0000000000..3c1d9b63fd --- /dev/null +++ b/tests/fixtures_migration/models.py @@ -0,0 +1,5 @@ +from django.db import models + + +class Book(models.Model): + name = models.CharField(max_length=100) diff --git a/tests/fixtures_migration/tests.py b/tests/fixtures_migration/tests.py new file mode 100644 index 0000000000..9a363385e6 --- /dev/null +++ b/tests/fixtures_migration/tests.py @@ -0,0 +1,31 @@ +from django.test import TestCase +from django.core import management + +from .models import Book + + +class TestNoInitialDataLoading(TestCase): + """ + Apps with migrations should ignore initial data. This test can be removed + in Django 1.9 when migrations become required and initial data is no longer + supported. + """ + available_apps = ['fixtures_migration'] + + def test_migrate(self): + self.assertQuerysetEqual(Book.objects.all(), []) + management.call_command( + 'migrate', + verbosity=0, + ) + self.assertQuerysetEqual(Book.objects.all(), []) + + def test_flush(self): + self.assertQuerysetEqual(Book.objects.all(), []) + management.call_command( + 'flush', + verbosity=0, + interactive=False, + load_initial_data=False + ) + self.assertQuerysetEqual(Book.objects.all(), [])