Fixed #15926 -- Added option --no-initial-data to syncdb and flush.

Thanks msiedlarek, jpaugh64 and vlinhart!
This commit is contained in:
Honza Kral 2012-06-05 16:46:15 +02:00
parent 4db34e7b4f
commit fedac99c85
4 changed files with 77 additions and 4 deletions

View File

@ -16,6 +16,8 @@ class Command(NoArgsCommand):
make_option('--database', action='store', dest='database', make_option('--database', action='store', dest='database',
default=DEFAULT_DB_ALIAS, help='Nominates a database to flush. ' default=DEFAULT_DB_ALIAS, help='Nominates a database to flush. '
'Defaults to the "default" database.'), '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 ' help = ('Returns the database to the state it was in immediately after '
'syncdb was executed. This means that all data will be removed ' '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. # Reinstall the initial_data fixture.
kwargs = options.copy() kwargs = options.copy()
kwargs['database'] = db 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: else:
self.stdout.write("Flush cancelled.\n") self.stdout.write("Flush cancelled.\n")

View File

@ -14,6 +14,8 @@ class Command(NoArgsCommand):
option_list = NoArgsCommand.option_list + ( option_list = NoArgsCommand.option_list + (
make_option('--noinput', action='store_false', dest='interactive', default=True, make_option('--noinput', action='store_false', dest='interactive', default=True,
help='Tells Django to NOT prompt the user for input of any kind.'), 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', make_option('--database', action='store', dest='database',
default=DEFAULT_DB_ALIAS, help='Nominates a database to synchronize. ' default=DEFAULT_DB_ALIAS, help='Nominates a database to synchronize. '
'Defaults to the "default" database.'), 'Defaults to the "default" database.'),
@ -25,9 +27,6 @@ class Command(NoArgsCommand):
verbosity = int(options.get('verbosity')) verbosity = int(options.get('verbosity'))
interactive = options.get('interactive') interactive = options.get('interactive')
show_traceback = options.get('traceback') 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) load_initial_data = options.get('load_initial_data', True)
self.style = no_style() self.style = no_style()

View File

@ -245,6 +245,14 @@ prompts.
The :djadminopt:`--database` option may be used to specify the database The :djadminopt:`--database` option may be used to specify the database
to flush. to flush.
--no-initial-data
~~~~~~~~~~~~~~~~~
.. versionadded:: 1.5
Use ``--no-initial-data`` to avoid loading the initial_data fixture.
inspectdb inspectdb
--------- ---------
@ -1024,6 +1032,13 @@ prompts.
The :djadminopt:`--database` option can be used to specify the database to The :djadminopt:`--database` option can be used to specify the database to
synchronize. synchronize.
--no-initial-data
~~~~~~~~~~~~~~~~~
.. versionadded:: 1.5
Use ``--no-initial-data`` to avoid loading the initial_data fixture.
test <app or test identifier> test <app or test identifier>
----------------------------- -----------------------------

View File

@ -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): class FixtureTestCase(TestCase):
def test_initial_data(self): def test_initial_data(self):
"Fixtures can load initial data into models defined in packages" "Fixtures can load initial data into models defined in packages"