Skip to content

Commit

Permalink
Fixed #15926 -- Added option --no-initial-data to syncdb and flush.
Browse files Browse the repository at this point in the history
Thanks msiedlarek, jpaugh64 and vlinhart!
  • Loading branch information
honzakral committed Jun 5, 2012
1 parent 4db34e7 commit fedac99
Show file tree
Hide file tree
Showing 4 changed files with 77 additions and 4 deletions.
7 changes: 6 additions & 1 deletion django/core/management/commands/flush.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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 '
Expand Down Expand Up @@ -79,7 +81,10 @@ def handle_noargs(self, **options):
# 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")
5 changes: 2 additions & 3 deletions django/core/management/commands/syncdb.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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.'),
Expand All @@ -25,9 +27,6 @@ def handle_noargs(self, **options):
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()
Expand Down
15 changes: 15 additions & 0 deletions docs/ref/django-admin.txt
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -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
--------- ---------


Expand Down Expand Up @@ -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>
----------------------------- -----------------------------


Expand Down
54 changes: 54 additions & 0 deletions tests/modeltests/fixtures_model_package/tests.py
Original file line number Original file line Diff line number Diff line change
Expand Up @@ -20,6 +20,60 @@ def testClassFixtures(self):
) )




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"
Expand Down

0 comments on commit fedac99

Please sign in to comment.