mirror of
https://github.com/django/django.git
synced 2025-08-19 02:01:29 +00:00
[1.9.x] Fixed #25922 -- Fixed migrate --fake-initial detection of many-to-many tables.
Backport of fa9ce4e9a6
from master
This commit is contained in:
parent
571f7b759e
commit
a80fb8ae24
6 changed files with 130 additions and 4 deletions
|
@ -301,6 +301,65 @@ class ExecutorTests(MigrationTestBase):
|
|||
self.assertTableNotExists("migrations_author")
|
||||
self.assertTableNotExists("migrations_tribble")
|
||||
|
||||
@override_settings(
|
||||
MIGRATION_MODULES={
|
||||
"migrations": "migrations.test_add_many_to_many_field_initial",
|
||||
},
|
||||
)
|
||||
def test_detect_soft_applied_add_field_manytomanyfield(self):
|
||||
"""
|
||||
executor.detect_soft_applied() detects ManyToManyField tables from an
|
||||
AddField operation. This checks the case of AddField in a migration
|
||||
with other operations (0001) and the case of AddField in its own
|
||||
migration (0002).
|
||||
"""
|
||||
tables = [
|
||||
# from 0001
|
||||
"migrations_project",
|
||||
"migrations_task",
|
||||
"migrations_project_tasks",
|
||||
# from 0002
|
||||
"migrations_task_projects",
|
||||
]
|
||||
executor = MigrationExecutor(connection)
|
||||
# Create the tables for 0001 but make it look like the migration hasn't
|
||||
# been applied.
|
||||
executor.migrate([("migrations", "0001_initial")])
|
||||
executor.migrate([("migrations", None)], fake=True)
|
||||
for table in tables[:3]:
|
||||
self.assertTableExists(table)
|
||||
# Table detection sees 0001 is applied but not 0002.
|
||||
migration = executor.loader.get_migration("migrations", "0001_initial")
|
||||
self.assertEqual(executor.detect_soft_applied(None, migration)[0], True)
|
||||
migration = executor.loader.get_migration("migrations", "0002_initial")
|
||||
self.assertEqual(executor.detect_soft_applied(None, migration)[0], False)
|
||||
|
||||
# Create the tables for both migrations but make it look like neither
|
||||
# has been applied.
|
||||
executor.loader.build_graph()
|
||||
executor.migrate([("migrations", "0001_initial")], fake=True)
|
||||
executor.migrate([("migrations", "0002_initial")])
|
||||
executor.loader.build_graph()
|
||||
executor.migrate([("migrations", None)], fake=True)
|
||||
# Table detection sees 0002 is applied.
|
||||
migration = executor.loader.get_migration("migrations", "0002_initial")
|
||||
self.assertEqual(executor.detect_soft_applied(None, migration)[0], True)
|
||||
|
||||
# Leave the tables for 0001 except the many-to-many table. That missing
|
||||
# table should cause detect_soft_applied() to return False.
|
||||
with connection.schema_editor() as editor:
|
||||
for table in tables[2:]:
|
||||
editor.execute(editor.sql_delete_table % {"table": table})
|
||||
migration = executor.loader.get_migration("migrations", "0001_initial")
|
||||
self.assertEqual(executor.detect_soft_applied(None, migration)[0], False)
|
||||
|
||||
# Cleanup by removing the remaining tables.
|
||||
with connection.schema_editor() as editor:
|
||||
for table in tables[:2]:
|
||||
editor.execute(editor.sql_delete_table % {"table": table})
|
||||
for table in tables:
|
||||
self.assertTableNotExists(table)
|
||||
|
||||
@override_settings(
|
||||
INSTALLED_APPS=[
|
||||
"migrations.migrations_test_apps.lookuperror_a",
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue