Skip to content

multidb: let one connection be reused, and other not #74

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 5 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 25 additions & 3 deletions pytest_django/db_reuse.py
Original file line number Diff line number Diff line change
Expand Up @@ -93,13 +93,35 @@ def create_test_db_with_reuse(self, verbosity=1, autoclobber=False):
# See https://code.djangoproject.com/ticket/17760
if hasattr(self.connection.features, 'confirm'):
self.connection.features.confirm()

self.connection.settings_dict['TEST_REUSE']=True #to check if destroy or not

return test_database_name


def monkey_patch_creation_for_db_reuse():
def monkey_patch_creation_for_db_reuse(reuse_db,create_db):
from django.db import connections

for connection in connections.all():
if test_database_exists_from_previous_run(connection):
_monkeypatch(connection.creation, 'create_test_db', create_test_db_with_reuse)
if reuse_db and not create_db or connection.settings_dict.get('TEST_REUSE',False):
if test_database_exists_from_previous_run(connection):
_monkeypatch(connection.creation, 'create_test_db', create_test_db_with_reuse)

def monkey_patch_create_with_template():
"""
Monkeypatch create sql to do 'CREATE DATABASE test_xxx WITH TEMPLATE xxx' instead of just 'CREATE DATABASE test_xxx'
Useful when testing with PostgreSQL and barebone of existing database
"""
def _sql_table_creation_suffix(self):
try:
return " WITH TEMPLATE=%s"%self.connection.settings_dict['TEST_WITH_TEMPLATE']
except KeyError:
return ""


from django.db import connections

for connection in connections.all():
if "TEST_WITH_TEMPLATE" in connection.settings_dict:
_monkeypatch(connection.creation,'sql_table_creation_suffix',_sql_table_creation_suffix)

19 changes: 13 additions & 6 deletions pytest_django/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,8 @@

from . import live_server_helper
from .db_reuse import (monkey_patch_creation_for_db_reuse,
monkey_patch_creation_for_db_suffix)
monkey_patch_creation_for_db_suffix,
monkey_patch_create_with_template)
from .django_compat import is_django_unittest
from .lazy_django import skip_if_no_django

Expand All @@ -34,6 +35,7 @@ def _django_db_setup(request, _django_runner, _django_cursor_wrapper):
db_suffix = None

monkey_patch_creation_for_db_suffix(db_suffix)
monkey_patch_create_with_template()

# Disable south's syncdb command
commands = management.get_commands()
Expand All @@ -42,13 +44,18 @@ def _django_db_setup(request, _django_runner, _django_cursor_wrapper):

with _django_cursor_wrapper:
# Monkey patch Django's setup code to support database re-use
if request.config.getvalue('reuse_db'):
if not request.config.getvalue('create_db'):
monkey_patch_creation_for_db_reuse()
_django_runner.teardown_databases = lambda db_cfg: None

monkey_patch_creation_for_db_reuse(request.config.getvalue('reuse_db'),
request.config.getvalue('create_db'))
# Create the database
db_cfg = _django_runner.setup_databases()
#rewrite db_cfg - remove those with REUSE
_old_dbs,mirror = db_cfg
_new_dbs = []
for connection,old_name,destroy in _old_dbs:
if not connection.settings_dict.get('TEST_REUSE'):
_new_dbs.append((connection,old_name,destroy))
db_cfg = _new_dbs,mirror


def teardown_database():
with _django_cursor_wrapper:
Expand Down