8000 Refs #23947 -- Improved migrations tests table cleanup. · alex-python/django@c17d821 · GitHub
[go: up one dir, main page]

Skip to content

Commit c17d821

Browse files
diegoguimaraestimgraham
authored andcommitted
Refs #23947 -- Improved migrations tests table cleanup.
Copied technique from schema tests.
1 parent ac5f2a4 commit c17d821

File tree

1 file changed

+19
-24
lines changed

1 file changed

+19
-24
lines changed

tests/migrations/test_operations.py

Lines changed: 19 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,12 @@
88
sqlparse = None
99

1010
from django import test
11-
from django.db import connection, migrations, models
11+
from django.db import connection, migrations, models, transaction
1212
from django.db.migrations.migration import Migration
1313
from django.db.migrations.state import ProjectState
1414
from django.db.models.fields import NOT_PROVIDED
1515
from django.db.transaction import atomic
16-
from django.db.utils import IntegrityError, DatabaseError
16+
from django.db.utils import IntegrityError
1717
from django.test import override_settings
1818
from django.utils import six
1919

@@ -55,30 +55,25 @@ def set_up_test_model(self, app_label, second_model=False, third_model=False,
5555
Creates a test model state and database table.
5656
"""
5757
# Delete the tables if they already exist
58-
with connection.cursor() as cursor:
58+
table_names = [
5959
# Start with ManyToMany tables
60-
try:
61-
cursor.execute("DROP TABLE %s_pony_stables" % app_label)
62-
except DatabaseError:
63-
pass
64-
try:
65-
cursor.execute("DROP TABLE %s_pony_vans" % app_label)
66-
except DatabaseError:
67-
pass
68-
60+
'_pony_stables', '_pony_vans',
6961
# Then standard model tables
70-
try:
71-
cursor.execute("DROP TABLE %s_pony" % app_label)
72-
except DatabaseError:
73-
pass
74-
try:
75-
cursor.execute("DROP TABLE %s_stable" % app_label)
76-
except DatabaseError:
77-
pass
78-
try:
79-
cursor.execute("DROP TABLE %s_van" % app_label)
80-
except DatabaseError:
81-
pass
62+
'_pony', '_stable', '_van',
63+
]
64+
tables = [(app_label + table_name) for table_name in table_names]
65+
with connection.cursor() as cursor:
66+
table_names = connection.introspection.table_names(cursor)
67+
connection.disable_constraint_checking()
68+
sql_delete_table = connection.schema_editor().sql_delete_table
69+
with transaction.atomic():
70+
for table in tables:
71+
if table in table_names:
72+
cursor.execute(sql_delete_table % {
73+
"table": connection.ops.quote_name(table),
74+
})
75+
connection.enable_constraint_checking()
76+
8277
# Make the "current" state
8378
model_options = {
8479
"swappable": "TEST_SWAP_MODEL",

0 commit comments

Comments
 (0)
0