Skip to content

Commit a5452b6

Browse files
committed
Use Django's --keepdb with Django 1.8+
Django 1.8 supports `--keepdb` [1], and it makes sense to make use of it. This should probably handle migrations in the context of `transactional_db` better, i.e. by re-applying migrations with a flushed DB. 1: https://docs.djangoproject.com/en/1.8/ref/django-admin/#django-admin-option---keepdb
1 parent d3e03b9 commit a5452b6

File tree

3 files changed

+11
-6
lines changed

3 files changed

+11
-6
lines changed

pytest_django/db_reuse.py

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,8 @@ def create_test_db_with_reuse(self, verbosity=1, autoclobber=False,
9595
This method is a monkey patched version of create_test_db that
9696
will not actually create a new database, but just reuse the
9797
existing.
98+
99+
This is only used with Django < 1.8.
98100
"""
99101
test_database_name = self._get_test_db_name()
100102
self.connection.settings_dict['NAME'] = test_database_name

pytest_django/fixtures.py

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -43,15 +43,18 @@ def _django_db_setup(request,
4343
if request.config.getvalue('nomigrations'):
4444
_disable_native_migrations()
4545

46+
db_args = {}
4647
with _django_cursor_wrapper:
47-
# Monkey patch Django's setup code to support database re-use
48-
if request.config.getvalue('reuse_db'):
49-
if not request.config.getvalue('create_db'):
48+
if (request.config.getvalue('reuse_db') and
49+
not request.config.getvalue('create_db')):
50+
if get_django_version() >= (1, 8):
51+
db_args['keepdb'] = True
52+
else:
5053
monkey_patch_creation_for_db_reuse()
5154

5255
# Create the database
5356
db_cfg = setup_databases(verbosity=pytest.config.option.verbose,
54-
interactive=False)
57+
interactive=False, **db_args)
5558

5659
def teardown_database():
5760
with _django_cursor_wrapper:

tests/test_environment.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -188,8 +188,8 @@ def test_more_verbose_with_vv(self, testdir):
188188
"*PASSED*Destroying test database for alias 'default' ('*')...*"])
189189

190190
def test_more_verbose_with_vv_and_reusedb(self, testdir):
191-
"""More verbose output with '-v -v', and --reuse-db."""
192-
result = testdir.runpytest_subprocess('-s', '-v', '-v', '--reuse-db')
191+
"""More verbose output with '-v -v', and --create-db."""
192+
result = testdir.runpytest_subprocess('-s', '-v', '-v', '--create-db')
193193
result.stdout.fnmatch_lines([
194194
"tpkg/test_the_test.py:*Creating test database for alias*",
195195
"*PASSED*"])

0 commit comments

Comments
 (0)