From fedac99c859c626544d38fc0ea07207ef3fd101a Mon Sep 17 00:00:00 2001 From: Honza Kral Date: Tue, 5 Jun 2012 16:46:15 +0200 Subject: [PATCH] Fixed #15926 -- Added option --no-initial-data to syncdb and flush. Thanks msiedlarek, jpaugh64 and vlinhart! --- django/core/management/commands/flush.py | 7 ++- django/core/management/commands/syncdb.py | 5 +- docs/ref/django-admin.txt | 15 ++++++ .../fixtures_model_package/tests.py | 54 +++++++++++++++++++ 4 files changed, 77 insertions(+), 4 deletions(-) diff --git a/django/core/management/commands/flush.py b/django/core/management/commands/flush.py index ce3c6e856b..7eb71fd1bb 100644 --- a/django/core/management/commands/flush.py +++ b/django/core/management/commands/flush.py @@ -16,6 +16,8 @@ class Command(NoArgsCommand): make_option('--database', action='store', dest='database', default=DEFAULT_DB_ALIAS, help='Nominates a database to flush. ' 'Defaults to the "default" database.'), + make_option('--no-initial-data', action='store_false', dest='load_initial_data', default=True, + help='Tells Django not to load any initial data after database synchronization.'), ) help = ('Returns the database to the state it was in immediately after ' 'syncdb was executed. This means that all data will be removed ' @@ -79,7 +81,10 @@ The full error: %s""" % (connection.settings_dict['NAME'], e)) # Reinstall the initial_data fixture. kwargs = options.copy() kwargs['database'] = db - call_command('loaddata', 'initial_data', **kwargs) + if options.get('load_initial_data', True): + # Reinstall the initial_data fixture. + from django.core.management import call_command + call_command('loaddata', 'initial_data', **options) else: self.stdout.write("Flush cancelled.\n") diff --git a/django/core/management/commands/syncdb.py b/django/core/management/commands/syncdb.py index 88caea152c..f751a3768b 100644 --- a/django/core/management/commands/syncdb.py +++ b/django/core/management/commands/syncdb.py @@ -14,6 +14,8 @@ class Command(NoArgsCommand): option_list = NoArgsCommand.option_list + ( make_option('--noinput', action='store_false', dest='interactive', default=True, help='Tells Django to NOT prompt the user for input of any kind.'), + make_option('--no-initial-data', action='store_false', dest='load_initial_data', default=True, + help='Tells Django not to load any initial data after database synchronization.'), make_option('--database', action='store', dest='database', default=DEFAULT_DB_ALIAS, help='Nominates a database to synchronize. ' 'Defaults to the "default" database.'), @@ -25,9 +27,6 @@ class Command(NoArgsCommand): verbosity = int(options.get('verbosity')) interactive = options.get('interactive') show_traceback = options.get('traceback') - - # Stealth option -- 'load_initial_data' is used by the testing setup - # process to disable initial fixture loading. load_initial_data = options.get('load_initial_data', True) self.style = no_style() diff --git a/docs/ref/django-admin.txt b/docs/ref/django-admin.txt index 0ea8252c71..f04f9ee058 100644 --- a/docs/ref/django-admin.txt +++ b/docs/ref/django-admin.txt @@ -245,6 +245,14 @@ prompts. The :djadminopt:`--database` option may be used to specify the database to flush. +--no-initial-data +~~~~~~~~~~~~~~~~~ + +.. versionadded:: 1.5 + +Use ``--no-initial-data`` to avoid loading the initial_data fixture. + + inspectdb --------- @@ -1024,6 +1032,13 @@ prompts. The :djadminopt:`--database` option can be used to specify the database to synchronize. +--no-initial-data +~~~~~~~~~~~~~~~~~ + +.. versionadded:: 1.5 + +Use ``--no-initial-data`` to avoid loading the initial_data fixture. + test ----------------------------- diff --git a/tests/modeltests/fixtures_model_package/tests.py b/tests/modeltests/fixtures_model_package/tests.py index 4da20697fe..0cc90381b8 100644 --- a/tests/modeltests/fixtures_model_package/tests.py +++ b/tests/modeltests/fixtures_model_package/tests.py @@ -20,6 +20,60 @@ class SampleTestCase(TestCase): ) +class TestNoInitialDataLoading(TestCase): + def test_syncdb(self): + Book.objects.all().delete() + + management.call_command( + 'syncdb', + verbosity=0, + commit=False + ) + self.assertQuerysetEqual( + Book.objects.all(), [ + u'Achieving self-awareness of Python programs' + ], + lambda a: a.name + ) + + Book.objects.all().delete() + + management.call_command( + 'syncdb', + verbosity=0, + commit=False, + load_initial_data=False + ) + self.assertQuerysetEqual(Book.objects.all(), []) + + def test_flush(self): + Book.objects.all().delete() + + management.call_command( + 'flush', + verbosity=0, + interactive=False, + commit=False + ) + self.assertQuerysetEqual( + Book.objects.all(), [ + u'Achieving self-awareness of Python programs' + ], + lambda a: a.name + ) + + Book.objects.all().delete() + + management.call_command( + 'flush', + verbosity=0, + commit=False, + interactive=False, + load_initial_data=False + ) + self.assertQuerysetEqual(Book.objects.all(), []) + + class FixtureTestCase(TestCase): def test_initial_data(self): "Fixtures can load initial data into models defined in packages"