Refs #23947 -- Improved migrations tests table cleanup.

Copied technique from schema tests.
This commit is contained in:
Diego Guimarães 2014-12-15 19:30:02 -02:00 committed by Tim Graham
parent ac5f2a4ef7
commit c17d821fa7
1 changed files with 19 additions and 24 deletions

View File

@ -8,12 +8,12 @@ except ImportError:
sqlparse = None sqlparse = None
from django import test from django import test
from django.db import connection, migrations, models from django.db import connection, migrations, models, transaction
from django.db.migrations.migration import Migration from django.db.migrations.migration import Migration
from django.db.migrations.state import ProjectState from django.db.migrations.state import ProjectState
from django.db.models.fields import NOT_PROVIDED from django.db.models.fields import NOT_PROVIDED
from django.db.transaction import atomic from django.db.transaction import atomic
from django.db.utils import IntegrityError, DatabaseError from django.db.utils import IntegrityError
from django.test import override_settings from django.test import override_settings
from django.utils import six from django.utils import six
@ -55,30 +55,25 @@ class OperationTestBase(MigrationTestBase):
Creates a test model state and database table. Creates a test model state and database table.
""" """
# Delete the tables if they already exist # Delete the tables if they already exist
with connection.cursor() as cursor: table_names = [
# Start with ManyToMany tables # Start with ManyToMany tables
try: '_pony_stables', '_pony_vans',
cursor.execute("DROP TABLE %s_pony_stables" % app_label)
except DatabaseError:
pass
try:
cursor.execute("DROP TABLE %s_pony_vans" % app_label)
except DatabaseError:
pass
# Then standard model tables # Then standard model tables
try: '_pony', '_stable', '_van',
cursor.execute("DROP TABLE %s_pony" % app_label) ]
except DatabaseError: tables = [(app_label + table_name) for table_name in table_names]
pass with connection.cursor() as cursor:
try: table_names = connection.introspection.table_names(cursor)
cursor.execute("DROP TABLE %s_stable" % app_label) connection.disable_constraint_checking()
except DatabaseError: sql_delete_table = connection.schema_editor().sql_delete_table
pass with transaction.atomic():
try: for table in tables:
cursor.execute("DROP TABLE %s_van" % app_label) if table in table_names:
except DatabaseError: cursor.execute(sql_delete_table % {
pass "table": connection.ops.quote_name(table),
})
connection.enable_constraint_checking()
# Make the "current" state # Make the "current" state
model_options = { model_options = {
"swappable": "TEST_SWAP_MODEL", "swappable": "TEST_SWAP_MODEL",