Skip to content

Commit

Permalink
Fixed #23699 -- Prevented flush from loading initial data for apps wi…
Browse files Browse the repository at this point in the history
…th migrations.
  • Loading branch information
tony-zhu authored and timgraham committed Oct 27, 2014
1 parent ed7c4df commit dd1ea70
Show file tree
Hide file tree
Showing 8 changed files with 71 additions and 3 deletions.
10 changes: 7 additions & 3 deletions django/core/management/commands/flush.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,9 +85,13 @@ def handle(self, **options):

# 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")

Expand Down
3 changes: 3 additions & 0 deletions docs/releases/1.7.2.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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`).
Empty file.
9 changes: 9 additions & 0 deletions tests/fixtures_migration/fixtures/initial_data.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[
{
"pk": "10",
"model": "fixtures_migration.book",
"fields": {
"name": "Achieving self-awareness of Python programs"
}
}
]
16 changes: 16 additions & 0 deletions tests/fixtures_migration/migrations/0001_initial.py
Original file line number Diff line number Diff line change
@@ -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)),
],
),
]
Empty file.
5 changes: 5 additions & 0 deletions tests/fixtures_migration/models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from django.db import models


class Book(models.Model):
name = models.CharField(max_length=100)
31 changes: 31 additions & 0 deletions tests/fixtures_migration/tests.py
Original file line number Diff line number Diff line change
@@ -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(), [])

0 comments on commit dd1ea70

Please sign in to comment.