test_executor.py 32 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778
  1. from django.apps.registry import apps as global_apps
  2. from django.db import connection
  3. from django.db.migrations.exceptions import InvalidMigrationPlan
  4. from django.db.migrations.executor import MigrationExecutor
  5. from django.db.migrations.graph import MigrationGraph
  6. from django.db.migrations.recorder import MigrationRecorder
  7. from django.db.utils import DatabaseError
  8. from django.test import TestCase, modify_settings, override_settings
  9. from .test_base import MigrationTestBase
  10. @modify_settings(INSTALLED_APPS={'append': 'migrations2'})
  11. class ExecutorTests(MigrationTestBase):
  12. """
  13. Tests the migration executor (full end-to-end running).
  14. Bear in mind that if these are failing you should fix the other
  15. test failures first, as they may be propagating into here.
  16. """
  17. available_apps = ["migrations", "migrations2", "django.contrib.auth", "django.contrib.contenttypes"]
  18. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  19. def test_run(self):
  20. """
  21. Tests running a simple set of migrations.
  22. """
  23. executor = MigrationExecutor(connection)
  24. # Let's look at the plan first and make sure it's up to scratch
  25. plan = executor.migration_plan([("migrations", "0002_second")])
  26. self.assertEqual(
  27. plan,
  28. [
  29. (executor.loader.graph.nodes["migrations", "0001_initial"], False),
  30. (executor.loader.graph.nodes["migrations", "0002_second"], False),
  31. ],
  32. )
  33. # Were the tables there before?
  34. self.assertTableNotExists("migrations_author")
  35. self.assertTableNotExists("migrations_book")
  36. # Alright, let's try running it
  37. executor.migrate([("migrations", "0002_second")])
  38. # Are the tables there now?
  39. self.assertTableExists("migrations_author")
  40. self.assertTableExists("migrations_book")
  41. # Rebuild the graph to reflect the new DB state
  42. executor.loader.build_graph()
  43. # Alright, let's undo what we did
  44. plan = executor.migration_plan([("migrations", None)])
  45. self.assertEqual(
  46. plan,
  47. [
  48. (executor.loader.graph.nodes["migrations", "0002_second"], True),
  49. (executor.loader.graph.nodes["migrations", "0001_initial"], True),
  50. ],
  51. )
  52. executor.migrate([("migrations", None)])
  53. # Are the tables gone?
  54. self.assertTableNotExists("migrations_author")
  55. self.assertTableNotExists("migrations_book")
  56. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"})
  57. def test_run_with_squashed(self):
  58. """
  59. Tests running a squashed migration from zero (should ignore what it replaces)
  60. """
  61. executor = MigrationExecutor(connection)
  62. # Check our leaf node is the squashed one
  63. leaves = [key for key in executor.loader.graph.leaf_nodes() if key[0] == "migrations"]
  64. self.assertEqual(leaves, [("migrations", "0001_squashed_0002")])
  65. # Check the plan
  66. plan = executor.migration_plan([("migrations", "0001_squashed_0002")])
  67. self.assertEqual(
  68. plan,
  69. [
  70. (executor.loader.graph.nodes["migrations", "0001_squashed_0002"], False),
  71. ],
  72. )
  73. # Were the tables there before?
  74. self.assertTableNotExists("migrations_author")
  75. self.assertTableNotExists("migrations_book")
  76. # Alright, let's try running it
  77. executor.migrate([("migrations", "0001_squashed_0002")])
  78. # Are the tables there now?
  79. self.assertTableExists("migrations_author")
  80. self.assertTableExists("migrations_book")
  81. # Rebuild the graph to reflect the new DB state
  82. executor.loader.build_graph()
  83. # Alright, let's undo what we did. Should also just use squashed.
  84. plan = executor.migration_plan([("migrations", None)])
  85. self.assertEqual(
  86. plan,
  87. [
  88. (executor.loader.graph.nodes["migrations", "0001_squashed_0002"], True),
  89. ],
  90. )
  91. executor.migrate([("migrations", None)])
  92. # Are the tables gone?
  93. self.assertTableNotExists("migrations_author")
  94. self.assertTableNotExists("migrations_book")
  95. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_non_atomic"})
  96. def test_non_atomic_migration(self):
  97. """
  98. Applying a non-atomic migration works as expected.
  99. """
  100. executor = MigrationExecutor(connection)
  101. with self.assertRaisesMessage(RuntimeError, "Abort migration"):
  102. executor.migrate([("migrations", "0001_initial")])
  103. self.assertTableExists("migrations_publisher")
  104. migrations_apps = executor.loader.project_state(("migrations", "0001_initial")).apps
  105. Publisher = migrations_apps.get_model("migrations", "Publisher")
  106. self.assertTrue(Publisher.objects.exists())
  107. self.assertTableNotExists("migrations_book")
  108. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_atomic_operation"})
  109. def test_atomic_operation_in_non_atomic_migration(self):
  110. """
  111. An atomic operation is properly rolled back inside a non-atomic
  112. migration.
  113. """
  114. executor = MigrationExecutor(connection)
  115. with self.assertRaisesMessage(RuntimeError, "Abort migration"):
  116. executor.migrate([("migrations", "0001_initial")])
  117. migrations_apps = executor.loader.project_state(("migrations", "0001_initial")).apps
  118. Editor = migrations_apps.get_model("migrations", "Editor")
  119. self.assertFalse(Editor.objects.exists())
  120. # Record previous migration as successful.
  121. executor.migrate([("migrations", "0001_initial")], fake=True)
  122. # Rebuild the graph to reflect the new DB state.
  123. executor.loader.build_graph()
  124. # Migrating backwards is also atomic.
  125. with self.assertRaisesMessage(RuntimeError, "Abort migration"):
  126. executor.migrate([("migrations", None)])
  127. self.assertFalse(Editor.objects.exists())
  128. @override_settings(MIGRATION_MODULES={
  129. "migrations": "migrations.test_migrations",
  130. "migrations2": "migrations2.test_migrations_2",
  131. })
  132. def test_empty_plan(self):
  133. """
  134. Re-planning a full migration of a fully-migrated set doesn't
  135. perform spurious unmigrations and remigrations.
  136. There was previously a bug where the executor just always performed the
  137. backwards plan for applied migrations - which even for the most recent
  138. migration in an app, might include other, dependent apps, and these
  139. were being unmigrated.
  140. """
  141. # Make the initial plan, check it
  142. executor = MigrationExecutor(connection)
  143. plan = executor.migration_plan([
  144. ("migrations", "0002_second"),
  145. ("migrations2", "0001_initial"),
  146. ])
  147. self.assertEqual(
  148. plan,
  149. [
  150. (executor.loader.graph.nodes["migrations", "0001_initial"], False),
  151. (executor.loader.graph.nodes["migrations", "0002_second"], False),
  152. (executor.loader.graph.nodes["migrations2", "0001_initial"], False),
  153. ],
  154. )
  155. # Fake-apply all migrations
  156. executor.migrate([
  157. ("migrations", "0002_second"),
  158. ("migrations2", "0001_initial")
  159. ], fake=True)
  160. # Rebuild the graph to reflect the new DB state
  161. executor.loader.build_graph()
  162. # Now plan a second time and make sure it's empty
  163. plan = executor.migration_plan([
  164. ("migrations", "0002_second"),
  165. ("migrations2", "0001_initial"),
  166. ])
  167. self.assertEqual(plan, [])
  168. # The resulting state should include applied migrations.
  169. state = executor.migrate([
  170. ("migrations", "0002_second"),
  171. ("migrations2", "0001_initial"),
  172. ])
  173. self.assertIn(('migrations', 'book'), state.models)
  174. self.assertIn(('migrations', 'author'), state.models)
  175. self.assertIn(('migrations2', 'otherauthor'), state.models)
  176. # Erase all the fake records
  177. executor.recorder.record_unapplied("migrations2", "0001_initial")
  178. executor.recorder.record_unapplied("migrations", "0002_second")
  179. executor.recorder.record_unapplied("migrations", "0001_initial")
  180. @override_settings(MIGRATION_MODULES={
  181. "migrations": "migrations.test_migrations",
  182. "migrations2": "migrations2.test_migrations_2_no_deps",
  183. })
  184. def test_mixed_plan_not_supported(self):
  185. """
  186. Although the MigrationExecutor interfaces allows for mixed migration
  187. plans (combined forwards and backwards migrations) this is not
  188. supported.
  189. """
  190. # Prepare for mixed plan
  191. executor = MigrationExecutor(connection)
  192. plan = executor.migration_plan([("migrations", "0002_second")])
  193. self.assertEqual(
  194. plan,
  195. [
  196. (executor.loader.graph.nodes["migrations", "0001_initial"], False),
  197. (executor.loader.graph.nodes["migrations", "0002_second"], False),
  198. ],
  199. )
  200. executor.migrate(None, plan)
  201. # Rebuild the graph to reflect the new DB state
  202. executor.loader.build_graph()
  203. self.assertIn(('migrations', '0001_initial'), executor.loader.applied_migrations)
  204. self.assertIn(('migrations', '0002_second'), executor.loader.applied_migrations)
  205. self.assertNotIn(('migrations2', '0001_initial'), executor.loader.applied_migrations)
  206. # Generate mixed plan
  207. plan = executor.migration_plan([
  208. ("migrations", None),
  209. ("migrations2", "0001_initial"),
  210. ])
  211. msg = (
  212. 'Migration plans with both forwards and backwards migrations are '
  213. 'not supported. Please split your migration process into separate '
  214. 'plans of only forwards OR backwards migrations.'
  215. )
  216. with self.assertRaisesMessage(InvalidMigrationPlan, msg) as cm:
  217. executor.migrate(None, plan)
  218. self.assertEqual(
  219. cm.exception.args[1],
  220. [
  221. (executor.loader.graph.nodes["migrations", "0002_second"], True),
  222. (executor.loader.graph.nodes["migrations", "0001_initial"], True),
  223. (executor.loader.graph.nodes["migrations2", "0001_initial"], False),
  224. ],
  225. )
  226. # Rebuild the graph to reflect the new DB state
  227. executor.loader.build_graph()
  228. executor.migrate([
  229. ("migrations", None),
  230. ("migrations2", None),
  231. ])
  232. # Are the tables gone?
  233. self.assertTableNotExists("migrations_author")
  234. self.assertTableNotExists("migrations_book")
  235. self.assertTableNotExists("migrations2_otherauthor")
  236. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  237. def test_soft_apply(self):
  238. """
  239. Tests detection of initial migrations already having been applied.
  240. """
  241. state = {"faked": None}
  242. def fake_storer(phase, migration=None, fake=None):
  243. state["faked"] = fake
  244. executor = MigrationExecutor(connection, progress_callback=fake_storer)
  245. # Were the tables there before?
  246. self.assertTableNotExists("migrations_author")
  247. self.assertTableNotExists("migrations_tribble")
  248. # Run it normally
  249. self.assertEqual(
  250. executor.migration_plan([("migrations", "0001_initial")]),
  251. [
  252. (executor.loader.graph.nodes["migrations", "0001_initial"], False),
  253. ],
  254. )
  255. executor.migrate([("migrations", "0001_initial")])
  256. # Are the tables there now?
  257. self.assertTableExists("migrations_author")
  258. self.assertTableExists("migrations_tribble")
  259. # We shouldn't have faked that one
  260. self.assertIs(state["faked"], False)
  261. # Rebuild the graph to reflect the new DB state
  262. executor.loader.build_graph()
  263. # Fake-reverse that
  264. executor.migrate([("migrations", None)], fake=True)
  265. # Are the tables still there?
  266. self.assertTableExists("migrations_author")
  267. self.assertTableExists("migrations_tribble")
  268. # Make sure that was faked
  269. self.assertIs(state["faked"], True)
  270. # Finally, migrate forwards; this should fake-apply our initial migration
  271. executor.loader.build_graph()
  272. self.assertEqual(
  273. executor.migration_plan([("migrations", "0001_initial")]),
  274. [
  275. (executor.loader.graph.nodes["migrations", "0001_initial"], False),
  276. ],
  277. )
  278. # Applying the migration should raise a database level error
  279. # because we haven't given the --fake-initial option
  280. with self.assertRaises(DatabaseError):
  281. executor.migrate([("migrations", "0001_initial")])
  282. # Reset the faked state
  283. state = {"faked": None}
  284. # Allow faking of initial CreateModel operations
  285. executor.migrate([("migrations", "0001_initial")], fake_initial=True)
  286. self.assertIs(state["faked"], True)
  287. # And migrate back to clean up the database
  288. executor.loader.build_graph()
  289. executor.migrate([("migrations", None)])
  290. self.assertTableNotExists("migrations_author")
  291. self.assertTableNotExists("migrations_tribble")
  292. @override_settings(
  293. MIGRATION_MODULES={
  294. "migrations": "migrations.test_migrations_custom_user",
  295. "django.contrib.auth": "django.contrib.auth.migrations",
  296. },
  297. AUTH_USER_MODEL="migrations.Author",
  298. )
  299. def test_custom_user(self):
  300. """
  301. Regression test for #22325 - references to a custom user model defined in the
  302. same app are not resolved correctly.
  303. """
  304. executor = MigrationExecutor(connection)
  305. self.assertTableNotExists("migrations_author")
  306. self.assertTableNotExists("migrations_tribble")
  307. # Migrate forwards
  308. executor.migrate([("migrations", "0001_initial")])
  309. self.assertTableExists("migrations_author")
  310. self.assertTableExists("migrations_tribble")
  311. # Make sure the soft-application detection works (#23093)
  312. # Change table_names to not return auth_user during this as
  313. # it wouldn't be there in a normal run, and ensure migrations.Author
  314. # exists in the global app registry temporarily.
  315. old_table_names = connection.introspection.table_names
  316. connection.introspection.table_names = lambda c: [x for x in old_table_names(c) if x != "auth_user"]
  317. migrations_apps = executor.loader.project_state(("migrations", "0001_initial")).apps
  318. global_apps.get_app_config("migrations").models["author"] = migrations_apps.get_model("migrations", "author")
  319. try:
  320. migration = executor.loader.get_migration("auth", "0001_initial")
  321. self.assertIs(executor.detect_soft_applied(None, migration)[0], True)
  322. finally:
  323. connection.introspection.table_names = old_table_names
  324. del global_apps.get_app_config("migrations").models["author"]
  325. # And migrate back to clean up the database
  326. executor.loader.build_graph()
  327. executor.migrate([("migrations", None)])
  328. self.assertTableNotExists("migrations_author")
  329. self.assertTableNotExists("migrations_tribble")
  330. @override_settings(
  331. MIGRATION_MODULES={
  332. "migrations": "migrations.test_add_many_to_many_field_initial",
  333. },
  334. )
  335. def test_detect_soft_applied_add_field_manytomanyfield(self):
  336. """
  337. executor.detect_soft_applied() detects ManyToManyField tables from an
  338. AddField operation. This checks the case of AddField in a migration
  339. with other operations (0001) and the case of AddField in its own
  340. migration (0002).
  341. """
  342. tables = [
  343. # from 0001
  344. "migrations_project",
  345. "migrations_task",
  346. "migrations_project_tasks",
  347. # from 0002
  348. "migrations_task_projects",
  349. ]
  350. executor = MigrationExecutor(connection)
  351. # Create the tables for 0001 but make it look like the migration hasn't
  352. # been applied.
  353. executor.migrate([("migrations", "0001_initial")])
  354. executor.migrate([("migrations", None)], fake=True)
  355. for table in tables[:3]:
  356. self.assertTableExists(table)
  357. # Table detection sees 0001 is applied but not 0002.
  358. migration = executor.loader.get_migration("migrations", "0001_initial")
  359. self.assertIs(executor.detect_soft_applied(None, migration)[0], True)
  360. migration = executor.loader.get_migration("migrations", "0002_initial")
  361. self.assertIs(executor.detect_soft_applied(None, migration)[0], False)
  362. # Create the tables for both migrations but make it look like neither
  363. # has been applied.
  364. executor.loader.build_graph()
  365. executor.migrate([("migrations", "0001_initial")], fake=True)
  366. executor.migrate([("migrations", "0002_initial")])
  367. executor.loader.build_graph()
  368. executor.migrate([("migrations", None)], fake=True)
  369. # Table detection sees 0002 is applied.
  370. migration = executor.loader.get_migration("migrations", "0002_initial")
  371. self.assertIs(executor.detect_soft_applied(None, migration)[0], True)
  372. # Leave the tables for 0001 except the many-to-many table. That missing
  373. # table should cause detect_soft_applied() to return False.
  374. with connection.schema_editor() as editor:
  375. for table in tables[2:]:
  376. editor.execute(editor.sql_delete_table % {"table": table})
  377. migration = executor.loader.get_migration("migrations", "0001_initial")
  378. self.assertIs(executor.detect_soft_applied(None, migration)[0], False)
  379. # Cleanup by removing the remaining tables.
  380. with connection.schema_editor() as editor:
  381. for table in tables[:2]:
  382. editor.execute(editor.sql_delete_table % {"table": table})
  383. for table in tables:
  384. self.assertTableNotExists(table)
  385. @override_settings(
  386. INSTALLED_APPS=[
  387. "migrations.migrations_test_apps.lookuperror_a",
  388. "migrations.migrations_test_apps.lookuperror_b",
  389. "migrations.migrations_test_apps.lookuperror_c"
  390. ]
  391. )
  392. def test_unrelated_model_lookups_forwards(self):
  393. """
  394. #24123 - All models of apps already applied which are
  395. unrelated to the first app being applied are part of the initial model
  396. state.
  397. """
  398. try:
  399. executor = MigrationExecutor(connection)
  400. self.assertTableNotExists("lookuperror_a_a1")
  401. self.assertTableNotExists("lookuperror_b_b1")
  402. self.assertTableNotExists("lookuperror_c_c1")
  403. executor.migrate([("lookuperror_b", "0003_b3")])
  404. self.assertTableExists("lookuperror_b_b3")
  405. # Rebuild the graph to reflect the new DB state
  406. executor.loader.build_graph()
  407. # Migrate forwards -- This led to a lookup LookupErrors because
  408. # lookuperror_b.B2 is already applied
  409. executor.migrate([
  410. ("lookuperror_a", "0004_a4"),
  411. ("lookuperror_c", "0003_c3"),
  412. ])
  413. self.assertTableExists("lookuperror_a_a4")
  414. self.assertTableExists("lookuperror_c_c3")
  415. # Rebuild the graph to reflect the new DB state
  416. executor.loader.build_graph()
  417. finally:
  418. # Cleanup
  419. executor.migrate([
  420. ("lookuperror_a", None),
  421. ("lookuperror_b", None),
  422. ("lookuperror_c", None),
  423. ])
  424. self.assertTableNotExists("lookuperror_a_a1")
  425. self.assertTableNotExists("lookuperror_b_b1")
  426. self.assertTableNotExists("lookuperror_c_c1")
  427. @override_settings(
  428. INSTALLED_APPS=[
  429. "migrations.migrations_test_apps.lookuperror_a",
  430. "migrations.migrations_test_apps.lookuperror_b",
  431. "migrations.migrations_test_apps.lookuperror_c"
  432. ]
  433. )
  434. def test_unrelated_model_lookups_backwards(self):
  435. """
  436. #24123 - All models of apps being unapplied which are
  437. unrelated to the first app being unapplied are part of the initial
  438. model state.
  439. """
  440. try:
  441. executor = MigrationExecutor(connection)
  442. self.assertTableNotExists("lookuperror_a_a1")
  443. self.assertTableNotExists("lookuperror_b_b1")
  444. self.assertTableNotExists("lookuperror_c_c1")
  445. executor.migrate([
  446. ("lookuperror_a", "0004_a4"),
  447. ("lookuperror_b", "0003_b3"),
  448. ("lookuperror_c", "0003_c3"),
  449. ])
  450. self.assertTableExists("lookuperror_b_b3")
  451. self.assertTableExists("lookuperror_a_a4")
  452. self.assertTableExists("lookuperror_c_c3")
  453. # Rebuild the graph to reflect the new DB state
  454. executor.loader.build_graph()
  455. # Migrate backwards -- This led to a lookup LookupErrors because
  456. # lookuperror_b.B2 is not in the initial state (unrelated to app c)
  457. executor.migrate([("lookuperror_a", None)])
  458. # Rebuild the graph to reflect the new DB state
  459. executor.loader.build_graph()
  460. finally:
  461. # Cleanup
  462. executor.migrate([
  463. ("lookuperror_b", None),
  464. ("lookuperror_c", None)
  465. ])
  466. self.assertTableNotExists("lookuperror_a_a1")
  467. self.assertTableNotExists("lookuperror_b_b1")
  468. self.assertTableNotExists("lookuperror_c_c1")
  469. @override_settings(
  470. INSTALLED_APPS=[
  471. 'migrations.migrations_test_apps.mutate_state_a',
  472. 'migrations.migrations_test_apps.mutate_state_b',
  473. ]
  474. )
  475. def test_unrelated_applied_migrations_mutate_state(self):
  476. """
  477. #26647 - Unrelated applied migrations should be part of the final
  478. state in both directions.
  479. """
  480. executor = MigrationExecutor(connection)
  481. executor.migrate([
  482. ('mutate_state_b', '0002_add_field'),
  483. ])
  484. # Migrate forward.
  485. executor.loader.build_graph()
  486. state = executor.migrate([
  487. ('mutate_state_a', '0001_initial'),
  488. ])
  489. self.assertIn('added', dict(state.models['mutate_state_b', 'b'].fields))
  490. executor.loader.build_graph()
  491. # Migrate backward.
  492. state = executor.migrate([
  493. ('mutate_state_a', None),
  494. ])
  495. self.assertIn('added', dict(state.models['mutate_state_b', 'b'].fields))
  496. executor.migrate([
  497. ('mutate_state_b', None),
  498. ])
  499. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  500. def test_process_callback(self):
  501. """
  502. #24129 - Tests callback process
  503. """
  504. call_args_list = []
  505. def callback(*args):
  506. call_args_list.append(args)
  507. executor = MigrationExecutor(connection, progress_callback=callback)
  508. # Were the tables there before?
  509. self.assertTableNotExists("migrations_author")
  510. self.assertTableNotExists("migrations_tribble")
  511. executor.migrate([
  512. ("migrations", "0001_initial"),
  513. ("migrations", "0002_second"),
  514. ])
  515. # Rebuild the graph to reflect the new DB state
  516. executor.loader.build_graph()
  517. executor.migrate([
  518. ("migrations", None),
  519. ("migrations", None),
  520. ])
  521. self.assertTableNotExists("migrations_author")
  522. self.assertTableNotExists("migrations_tribble")
  523. migrations = executor.loader.graph.nodes
  524. expected = [
  525. ("render_start",),
  526. ("render_success",),
  527. ("apply_start", migrations['migrations', '0001_initial'], False),
  528. ("apply_success", migrations['migrations', '0001_initial'], False),
  529. ("apply_start", migrations['migrations', '0002_second'], False),
  530. ("apply_success", migrations['migrations', '0002_second'], False),
  531. ("render_start",),
  532. ("render_success",),
  533. ("unapply_start", migrations['migrations', '0002_second'], False),
  534. ("unapply_success", migrations['migrations', '0002_second'], False),
  535. ("unapply_start", migrations['migrations', '0001_initial'], False),
  536. ("unapply_success", migrations['migrations', '0001_initial'], False),
  537. ]
  538. self.assertEqual(call_args_list, expected)
  539. @override_settings(
  540. INSTALLED_APPS=[
  541. "migrations.migrations_test_apps.alter_fk.author_app",
  542. "migrations.migrations_test_apps.alter_fk.book_app",
  543. ]
  544. )
  545. def test_alter_id_type_with_fk(self):
  546. try:
  547. executor = MigrationExecutor(connection)
  548. self.assertTableNotExists("author_app_author")
  549. self.assertTableNotExists("book_app_book")
  550. # Apply initial migrations
  551. executor.migrate([
  552. ("author_app", "0001_initial"),
  553. ("book_app", "0001_initial"),
  554. ])
  555. self.assertTableExists("author_app_author")
  556. self.assertTableExists("book_app_book")
  557. # Rebuild the graph to reflect the new DB state
  558. executor.loader.build_graph()
  559. # Apply PK type alteration
  560. executor.migrate([("author_app", "0002_alter_id")])
  561. # Rebuild the graph to reflect the new DB state
  562. executor.loader.build_graph()
  563. finally:
  564. # We can't simply unapply the migrations here because there is no
  565. # implicit cast from VARCHAR to INT on the database level.
  566. with connection.schema_editor() as editor:
  567. editor.execute(editor.sql_delete_table % {"table": "book_app_book"})
  568. editor.execute(editor.sql_delete_table % {"table": "author_app_author"})
  569. self.assertTableNotExists("author_app_author")
  570. self.assertTableNotExists("book_app_book")
  571. executor.migrate([("author_app", None)], fake=True)
  572. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"})
  573. def test_apply_all_replaced_marks_replacement_as_applied(self):
  574. """
  575. Applying all replaced migrations marks replacement as applied (#24628).
  576. """
  577. recorder = MigrationRecorder(connection)
  578. # Place the database in a state where the replaced migrations are
  579. # partially applied: 0001 is applied, 0002 is not.
  580. recorder.record_applied("migrations", "0001_initial")
  581. executor = MigrationExecutor(connection)
  582. # Use fake because we don't actually have the first migration
  583. # applied, so the second will fail. And there's no need to actually
  584. # create/modify tables here, we're just testing the
  585. # MigrationRecord, which works the same with or without fake.
  586. executor.migrate([("migrations", "0002_second")], fake=True)
  587. # Because we've now applied 0001 and 0002 both, their squashed
  588. # replacement should be marked as applied.
  589. self.assertIn(
  590. ("migrations", "0001_squashed_0002"),
  591. recorder.applied_migrations(),
  592. )
  593. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"})
  594. def test_migrate_marks_replacement_applied_even_if_it_did_nothing(self):
  595. """
  596. A new squash migration will be marked as applied even if all its
  597. replaced migrations were previously already applied (#24628).
  598. """
  599. recorder = MigrationRecorder(connection)
  600. # Record all replaced migrations as applied
  601. recorder.record_applied("migrations", "0001_initial")
  602. recorder.record_applied("migrations", "0002_second")
  603. executor = MigrationExecutor(connection)
  604. executor.migrate([("migrations", "0001_squashed_0002")])
  605. # Because 0001 and 0002 are both applied, even though this migrate run
  606. # didn't apply anything new, their squashed replacement should be
  607. # marked as applied.
  608. self.assertIn(
  609. ("migrations", "0001_squashed_0002"),
  610. recorder.applied_migrations(),
  611. )
  612. class FakeLoader:
  613. def __init__(self, graph, applied):
  614. self.graph = graph
  615. self.applied_migrations = applied
  616. class FakeMigration:
  617. """Really all we need is any object with a debug-useful repr."""
  618. def __init__(self, name):
  619. self.name = name
  620. def __repr__(self):
  621. return 'M<%s>' % self.name
  622. class ExecutorUnitTests(TestCase):
  623. """(More) isolated unit tests for executor methods."""
  624. def test_minimize_rollbacks(self):
  625. """
  626. Minimize unnecessary rollbacks in connected apps.
  627. When you say "./manage.py migrate appA 0001", rather than migrating to
  628. just after appA-0001 in the linearized migration plan (which could roll
  629. back migrations in other apps that depend on appA 0001, but don't need
  630. to be rolled back since we're not rolling back appA 0001), we migrate
  631. to just before appA-0002.
  632. """
  633. a1_impl = FakeMigration('a1')
  634. a1 = ('a', '1')
  635. a2_impl = FakeMigration('a2')
  636. a2 = ('a', '2')
  637. b1_impl = FakeMigration('b1')
  638. b1 = ('b', '1')
  639. graph = MigrationGraph()
  640. graph.add_node(a1, a1_impl)
  641. graph.add_node(a2, a2_impl)
  642. graph.add_node(b1, b1_impl)
  643. graph.add_dependency(None, b1, a1)
  644. graph.add_dependency(None, a2, a1)
  645. executor = MigrationExecutor(None)
  646. executor.loader = FakeLoader(graph, {a1, b1, a2})
  647. plan = executor.migration_plan({a1})
  648. self.assertEqual(plan, [(a2_impl, True)])
  649. def test_minimize_rollbacks_branchy(self):
  650. r"""
  651. Minimize rollbacks when target has multiple in-app children.
  652. a: 1 <---- 3 <--\
  653. \ \- 2 <--- 4
  654. \ \
  655. b: \- 1 <--- 2
  656. """
  657. a1_impl = FakeMigration('a1')
  658. a1 = ('a', '1')
  659. a2_impl = FakeMigration('a2')
  660. a2 = ('a', '2')
  661. a3_impl = FakeMigration('a3')
  662. a3 = ('a', '3')
  663. a4_impl = FakeMigration('a4')
  664. a4 = ('a', '4')
  665. b1_impl = FakeMigration('b1')
  666. b1 = ('b', '1')
  667. b2_impl = FakeMigration('b2')
  668. b2 = ('b', '2')
  669. graph = MigrationGraph()
  670. graph.add_node(a1, a1_impl)
  671. graph.add_node(a2, a2_impl)
  672. graph.add_node(a3, a3_impl)
  673. graph.add_node(a4, a4_impl)
  674. graph.add_node(b1, b1_impl)
  675. graph.add_node(b2, b2_impl)
  676. graph.add_dependency(None, a2, a1)
  677. graph.add_dependency(None, a3, a1)
  678. graph.add_dependency(None, a4, a2)
  679. graph.add_dependency(None, a4, a3)
  680. graph.add_dependency(None, b2, b1)
  681. graph.add_dependency(None, b1, a1)
  682. graph.add_dependency(None, b2, a2)
  683. executor = MigrationExecutor(None)
  684. executor.loader = FakeLoader(graph, {a1, b1, a2, b2, a3, a4})
  685. plan = executor.migration_plan({a1})
  686. should_be_rolled_back = [b2_impl, a4_impl, a2_impl, a3_impl]
  687. exp = [(m, True) for m in should_be_rolled_back]
  688. self.assertEqual(plan, exp)
  689. def test_backwards_nothing_to_do(self):
  690. r"""
  691. If the current state satisfies the given target, do nothing.
  692. a: 1 <--- 2
  693. b: \- 1
  694. c: \- 1
  695. If a1 is applied already and a2 is not, and we're asked to migrate to
  696. a1, don't apply or unapply b1 or c1, regardless of their current state.
  697. """
  698. a1_impl = FakeMigration('a1')
  699. a1 = ('a', '1')
  700. a2_impl = FakeMigration('a2')
  701. a2 = ('a', '2')
  702. b1_impl = FakeMigration('b1')
  703. b1 = ('b', '1')
  704. c1_impl = FakeMigration('c1')
  705. c1 = ('c', '1')
  706. graph = MigrationGraph()
  707. graph.add_node(a1, a1_impl)
  708. graph.add_node(a2, a2_impl)
  709. graph.add_node(b1, b1_impl)
  710. graph.add_node(c1, c1_impl)
  711. graph.add_dependency(None, a2, a1)
  712. graph.add_dependency(None, b1, a1)
  713. graph.add_dependency(None, c1, a1)
  714. executor = MigrationExecutor(None)
  715. executor.loader = FakeLoader(graph, {a1, b1})
  716. plan = executor.migration_plan({a1})
  717. self.assertEqual(plan, [])