0
0
mirror of https://github.com/django/django.git synced 2024-11-29 22:56:46 +01:00

Ensure sequences are reset correctly in the presence of swapped models.

This commit is contained in:
Russell Keith-Magee 2012-09-24 07:48:05 +08:00
parent e2b6e22f29
commit 8a527dda13
2 changed files with 18 additions and 2 deletions

View File

@ -319,6 +319,7 @@ class BaseDatabaseWrapper(object):
def make_debug_cursor(self, cursor): def make_debug_cursor(self, cursor):
return util.CursorDebugWrapper(cursor, self) return util.CursorDebugWrapper(cursor, self)
class BaseDatabaseFeatures(object): class BaseDatabaseFeatures(object):
allows_group_by_pk = False allows_group_by_pk = False
# True if django.db.backend.utils.typecast_timestamp is used on values # True if django.db.backend.utils.typecast_timestamp is used on values
@ -776,7 +777,7 @@ class BaseDatabaseOperations(object):
The `style` argument is a Style object as returned by either The `style` argument is a Style object as returned by either
color_style() or no_style() in django.core.management.color. color_style() or no_style() in django.core.management.color.
""" """
return [] # No sequence reset required by default. return [] # No sequence reset required by default.
def start_transaction_sql(self): def start_transaction_sql(self):
""" """
@ -915,6 +916,7 @@ class BaseDatabaseOperations(object):
conn = ' %s ' % connector conn = ' %s ' % connector
return conn.join(sub_expressions) return conn.join(sub_expressions)
class BaseDatabaseIntrospection(object): class BaseDatabaseIntrospection(object):
""" """
This class encapsulates all backend-specific introspection utilities This class encapsulates all backend-specific introspection utilities
@ -1010,12 +1012,14 @@ class BaseDatabaseIntrospection(object):
for model in models.get_models(app): for model in models.get_models(app):
if not model._meta.managed: if not model._meta.managed:
continue continue
if model._meta.swapped:
continue
if not router.allow_syncdb(self.connection.alias, model): if not router.allow_syncdb(self.connection.alias, model):
continue continue
for f in model._meta.local_fields: for f in model._meta.local_fields:
if isinstance(f, models.AutoField): if isinstance(f, models.AutoField):
sequence_list.append({'table': model._meta.db_table, 'column': f.column}) sequence_list.append({'table': model._meta.db_table, 'column': f.column})
break # Only one AutoField is allowed per model, so don't bother continuing. break # Only one AutoField is allowed per model, so don't bother continuing.
for f in model._meta.local_many_to_many: for f in model._meta.local_many_to_many:
# If this is an m2m using an intermediate table, # If this is an m2m using an intermediate table,
@ -1052,6 +1056,7 @@ class BaseDatabaseIntrospection(object):
""" """
raise NotImplementedError raise NotImplementedError
class BaseDatabaseClient(object): class BaseDatabaseClient(object):
""" """
This class encapsulates all backend-specific methods for opening a This class encapsulates all backend-specific methods for opening a
@ -1068,6 +1073,7 @@ class BaseDatabaseClient(object):
def runshell(self): def runshell(self):
raise NotImplementedError() raise NotImplementedError()
class BaseDatabaseValidation(object): class BaseDatabaseValidation(object):
""" """
This class encapsualtes all backend-specific model validation. This class encapsualtes all backend-specific model validation.

View File

@ -1,4 +1,5 @@
from __future__ import absolute_import, unicode_literals from __future__ import absolute_import, unicode_literals
import copy
from django.conf import settings from django.conf import settings
from django.contrib.contenttypes.models import ContentType from django.contrib.contenttypes.models import ContentType
@ -6,6 +7,7 @@ from django.core import management
from django.core.exceptions import FieldError from django.core.exceptions import FieldError
from django.db import models, DEFAULT_DB_ALIAS from django.db import models, DEFAULT_DB_ALIAS
from django.db.models import signals from django.db.models import signals
from django.db.models.loading import cache
from django.test import TestCase from django.test import TestCase
@ -147,6 +149,12 @@ class ProxyModelTests(TestCase):
def test_swappable(self): def test_swappable(self):
try: try:
# This test adds dummy applications to the app cache. These
# need to be removed in order to prevent bad interactions
# with the flush operation in other tests.
old_app_models = copy.deepcopy(cache.app_models)
old_app_store = copy.deepcopy(cache.app_store)
settings.TEST_SWAPPABLE_MODEL = 'proxy_models.AlternateModel' settings.TEST_SWAPPABLE_MODEL = 'proxy_models.AlternateModel'
class SwappableModel(models.Model): class SwappableModel(models.Model):
@ -165,6 +173,8 @@ class ProxyModelTests(TestCase):
proxy = True proxy = True
finally: finally:
del settings.TEST_SWAPPABLE_MODEL del settings.TEST_SWAPPABLE_MODEL
cache.app_models = old_app_models
cache.app_store = old_app_store
def test_myperson_manager(self): def test_myperson_manager(self):
Person.objects.create(name="fred") Person.objects.create(name="fred")