# HG changeset patch -- Bitbucket.org # Project south # URL https://bitbucket.org/andrewgodwin/south/overview # User John Paulett # Date 1300138995 18000 # Node ID 21a63523132712eead38df6f3e43f7e616c114be # Parent 5bc35f14338fa86e051702943365942715a6a2ab Avoid flushing the database during create_test_db to preserve data migrations. Django 1.3-rc-1 introduced a backwards incompatible change (#14661) that runs flush after syncdb during testing, thus clearing out all South data migrations. This fix no-ops the flush command during create_test_db, but allows flush in test teardowns. --- a/south/management/commands/__init__.py +++ b/south/management/commands/__init__.py @@ -9,6 +9,7 @@ from django.conf import settings # Make sure the template loader cache is fixed _now_ (#448) import django.template.loaders.app_directories +from south.hacks import hacks from south.management.commands.syncdb import Command as SyncCommand class MigrateAndSyncCommand(SyncCommand): @@ -31,3 +32,9 @@ def patch_for_test_db_setup(): management._commands['syncdb'] = 'django.core' else: management._commands['syncdb'] = MigrateAndSyncCommand() + # Avoid flushing data migrations. + # http://code.djangoproject.com/ticket/14661 introduced change that flushed custom + # sql during the test database creation (thus flushing the data migrations). + # we patch flush to be no-op during create_test_db, but still allow flushing + # after each test for non-transactional backends. + hacks.patch_flush_during_test_db_creation() --- a/south/hacks/django_1_0.py +++ b/south/hacks/django_1_0.py @@ -4,9 +4,17 @@ Hacks for the Django 1.0/1.0.2 releases. from django.conf import settings from django.db import models +from django.db.backends.creation import BaseDatabaseCreation from django.db.models.loading import AppCache, cache +from django.core import management +from django.core.management.commands.flush import Command as FlushCommand from django.utils.datastructures import SortedDict +class SkipFlushCommand(FlushCommand): + def handle_noargs(self, **options): + # no-op to avoid calling flush + return + class Hacks: def set_installed_apps(self, apps): @@ -72,4 +80,24 @@ class Hacks: Rebuilds AppCache with the real model definitions. """ cache._populate() - + + def patch_flush_during_test_db_creation(self): + """ + Patches BaseDatabaseCreation.create_test_db to not flush database + """ + + def patch(f): + def wrapper(*args, **kwargs): + # hold onto the original and replace flush command with a no-op + original_flush_command = management._commands['flush'] + try: + management._commands['flush'] = SkipFlushCommand() + # run create_test_db + f(*args, **kwargs) + finally: + # unpatch flush back to the original + management._commands['flush'] = original_flush_command + return wrapper + + BaseDatabaseCreation.create_test_db = patch(BaseDatabaseCreation.create_test_db) +