2
0

test_executor.py 26 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644
  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={
  96. "migrations": "migrations.test_migrations",
  97. "migrations2": "migrations2.test_migrations_2",
  98. })
  99. def test_empty_plan(self):
  100. """
  101. Tests that re-planning a full migration of a fully-migrated set doesn't
  102. perform spurious unmigrations and remigrations.
  103. There was previously a bug where the executor just always performed the
  104. backwards plan for applied migrations - which even for the most recent
  105. migration in an app, might include other, dependent apps, and these
  106. were being unmigrated.
  107. """
  108. # Make the initial plan, check it
  109. executor = MigrationExecutor(connection)
  110. plan = executor.migration_plan([
  111. ("migrations", "0002_second"),
  112. ("migrations2", "0001_initial"),
  113. ])
  114. self.assertEqual(
  115. plan,
  116. [
  117. (executor.loader.graph.nodes["migrations", "0001_initial"], False),
  118. (executor.loader.graph.nodes["migrations", "0002_second"], False),
  119. (executor.loader.graph.nodes["migrations2", "0001_initial"], False),
  120. ],
  121. )
  122. # Fake-apply all migrations
  123. executor.migrate([
  124. ("migrations", "0002_second"),
  125. ("migrations2", "0001_initial")
  126. ], fake=True)
  127. # Rebuild the graph to reflect the new DB state
  128. executor.loader.build_graph()
  129. # Now plan a second time and make sure it's empty
  130. plan = executor.migration_plan([
  131. ("migrations", "0002_second"),
  132. ("migrations2", "0001_initial"),
  133. ])
  134. self.assertEqual(plan, [])
  135. # Erase all the fake records
  136. executor.recorder.record_unapplied("migrations2", "0001_initial")
  137. executor.recorder.record_unapplied("migrations", "0002_second")
  138. executor.recorder.record_unapplied("migrations", "0001_initial")
  139. @override_settings(MIGRATION_MODULES={
  140. "migrations": "migrations.test_migrations",
  141. "migrations2": "migrations2.test_migrations_2_no_deps",
  142. })
  143. def test_mixed_plan_not_supported(self):
  144. """
  145. Although the MigrationExecutor interfaces allows for mixed migration
  146. plans (combined forwards and backwards migrations) this is not
  147. supported.
  148. """
  149. # Prepare for mixed plan
  150. executor = MigrationExecutor(connection)
  151. plan = executor.migration_plan([("migrations", "0002_second")])
  152. self.assertEqual(
  153. plan,
  154. [
  155. (executor.loader.graph.nodes["migrations", "0001_initial"], False),
  156. (executor.loader.graph.nodes["migrations", "0002_second"], False),
  157. ],
  158. )
  159. executor.migrate(None, plan)
  160. # Rebuild the graph to reflect the new DB state
  161. executor.loader.build_graph()
  162. self.assertIn(('migrations', '0001_initial'), executor.loader.applied_migrations)
  163. self.assertIn(('migrations', '0002_second'), executor.loader.applied_migrations)
  164. self.assertNotIn(('migrations2', '0001_initial'), executor.loader.applied_migrations)
  165. # Generate mixed plan
  166. plan = executor.migration_plan([
  167. ("migrations", None),
  168. ("migrations2", "0001_initial"),
  169. ])
  170. msg = (
  171. 'Migration plans with both forwards and backwards migrations are '
  172. 'not supported. Please split your migration process into separate '
  173. 'plans of only forwards OR backwards migrations.'
  174. )
  175. with self.assertRaisesMessage(InvalidMigrationPlan, msg) as cm:
  176. executor.migrate(None, plan)
  177. self.assertEqual(
  178. cm.exception.args[1],
  179. [
  180. (executor.loader.graph.nodes["migrations", "0002_second"], True),
  181. (executor.loader.graph.nodes["migrations", "0001_initial"], True),
  182. (executor.loader.graph.nodes["migrations2", "0001_initial"], False),
  183. ],
  184. )
  185. # Rebuild the graph to reflect the new DB state
  186. executor.loader.build_graph()
  187. executor.migrate([
  188. ("migrations", None),
  189. ("migrations2", None),
  190. ])
  191. # Are the tables gone?
  192. self.assertTableNotExists("migrations_author")
  193. self.assertTableNotExists("migrations_book")
  194. self.assertTableNotExists("migrations2_otherauthor")
  195. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  196. def test_soft_apply(self):
  197. """
  198. Tests detection of initial migrations already having been applied.
  199. """
  200. state = {"faked": None}
  201. def fake_storer(phase, migration=None, fake=None):
  202. state["faked"] = fake
  203. executor = MigrationExecutor(connection, progress_callback=fake_storer)
  204. # Were the tables there before?
  205. self.assertTableNotExists("migrations_author")
  206. self.assertTableNotExists("migrations_tribble")
  207. # Run it normally
  208. self.assertEqual(
  209. executor.migration_plan([("migrations", "0001_initial")]),
  210. [
  211. (executor.loader.graph.nodes["migrations", "0001_initial"], False),
  212. ],
  213. )
  214. executor.migrate([("migrations", "0001_initial")])
  215. # Are the tables there now?
  216. self.assertTableExists("migrations_author")
  217. self.assertTableExists("migrations_tribble")
  218. # We shouldn't have faked that one
  219. self.assertEqual(state["faked"], False)
  220. # Rebuild the graph to reflect the new DB state
  221. executor.loader.build_graph()
  222. # Fake-reverse that
  223. executor.migrate([("migrations", None)], fake=True)
  224. # Are the tables still there?
  225. self.assertTableExists("migrations_author")
  226. self.assertTableExists("migrations_tribble")
  227. # Make sure that was faked
  228. self.assertEqual(state["faked"], True)
  229. # Finally, migrate forwards; this should fake-apply our initial migration
  230. executor.loader.build_graph()
  231. self.assertEqual(
  232. executor.migration_plan([("migrations", "0001_initial")]),
  233. [
  234. (executor.loader.graph.nodes["migrations", "0001_initial"], False),
  235. ],
  236. )
  237. # Applying the migration should raise a database level error
  238. # because we haven't given the --fake-initial option
  239. with self.assertRaises(DatabaseError):
  240. executor.migrate([("migrations", "0001_initial")])
  241. # Reset the faked state
  242. state = {"faked": None}
  243. # Allow faking of initial CreateModel operations
  244. executor.migrate([("migrations", "0001_initial")], fake_initial=True)
  245. self.assertEqual(state["faked"], True)
  246. # And migrate back to clean up the database
  247. executor.loader.build_graph()
  248. executor.migrate([("migrations", None)])
  249. self.assertTableNotExists("migrations_author")
  250. self.assertTableNotExists("migrations_tribble")
  251. @override_settings(
  252. MIGRATION_MODULES={
  253. "migrations": "migrations.test_migrations_custom_user",
  254. "django.contrib.auth": "django.contrib.auth.migrations",
  255. },
  256. AUTH_USER_MODEL="migrations.Author",
  257. )
  258. def test_custom_user(self):
  259. """
  260. Regression test for #22325 - references to a custom user model defined in the
  261. same app are not resolved correctly.
  262. """
  263. executor = MigrationExecutor(connection)
  264. self.assertTableNotExists("migrations_author")
  265. self.assertTableNotExists("migrations_tribble")
  266. # Migrate forwards
  267. executor.migrate([("migrations", "0001_initial")])
  268. self.assertTableExists("migrations_author")
  269. self.assertTableExists("migrations_tribble")
  270. # Make sure the soft-application detection works (#23093)
  271. # Change table_names to not return auth_user during this as
  272. # it wouldn't be there in a normal run, and ensure migrations.Author
  273. # exists in the global app registry temporarily.
  274. old_table_names = connection.introspection.table_names
  275. connection.introspection.table_names = lambda c: [x for x in old_table_names(c) if x != "auth_user"]
  276. migrations_apps = executor.loader.project_state(("migrations", "0001_initial")).apps
  277. global_apps.get_app_config("migrations").models["author"] = migrations_apps.get_model("migrations", "author")
  278. try:
  279. migration = executor.loader.get_migration("auth", "0001_initial")
  280. self.assertEqual(executor.detect_soft_applied(None, migration)[0], True)
  281. finally:
  282. connection.introspection.table_names = old_table_names
  283. del global_apps.get_app_config("migrations").models["author"]
  284. # And migrate back to clean up the database
  285. executor.loader.build_graph()
  286. executor.migrate([("migrations", None)])
  287. self.assertTableNotExists("migrations_author")
  288. self.assertTableNotExists("migrations_tribble")
  289. @override_settings(
  290. INSTALLED_APPS=[
  291. "migrations.migrations_test_apps.lookuperror_a",
  292. "migrations.migrations_test_apps.lookuperror_b",
  293. "migrations.migrations_test_apps.lookuperror_c"
  294. ]
  295. )
  296. def test_unrelated_model_lookups_forwards(self):
  297. """
  298. #24123 - Tests that all models of apps already applied which are
  299. unrelated to the first app being applied are part of the initial model
  300. state.
  301. """
  302. try:
  303. executor = MigrationExecutor(connection)
  304. self.assertTableNotExists("lookuperror_a_a1")
  305. self.assertTableNotExists("lookuperror_b_b1")
  306. self.assertTableNotExists("lookuperror_c_c1")
  307. executor.migrate([("lookuperror_b", "0003_b3")])
  308. self.assertTableExists("lookuperror_b_b3")
  309. # Rebuild the graph to reflect the new DB state
  310. executor.loader.build_graph()
  311. # Migrate forwards -- This led to a lookup LookupErrors because
  312. # lookuperror_b.B2 is already applied
  313. executor.migrate([
  314. ("lookuperror_a", "0004_a4"),
  315. ("lookuperror_c", "0003_c3"),
  316. ])
  317. self.assertTableExists("lookuperror_a_a4")
  318. self.assertTableExists("lookuperror_c_c3")
  319. # Rebuild the graph to reflect the new DB state
  320. executor.loader.build_graph()
  321. finally:
  322. # Cleanup
  323. executor.migrate([
  324. ("lookuperror_a", None),
  325. ("lookuperror_b", None),
  326. ("lookuperror_c", None),
  327. ])
  328. self.assertTableNotExists("lookuperror_a_a1")
  329. self.assertTableNotExists("lookuperror_b_b1")
  330. self.assertTableNotExists("lookuperror_c_c1")
  331. @override_settings(
  332. INSTALLED_APPS=[
  333. "migrations.migrations_test_apps.lookuperror_a",
  334. "migrations.migrations_test_apps.lookuperror_b",
  335. "migrations.migrations_test_apps.lookuperror_c"
  336. ]
  337. )
  338. def test_unrelated_model_lookups_backwards(self):
  339. """
  340. #24123 - Tests that all models of apps being unapplied which are
  341. unrelated to the first app being unapplied are part of the initial
  342. model state.
  343. """
  344. try:
  345. executor = MigrationExecutor(connection)
  346. self.assertTableNotExists("lookuperror_a_a1")
  347. self.assertTableNotExists("lookuperror_b_b1")
  348. self.assertTableNotExists("lookuperror_c_c1")
  349. executor.migrate([
  350. ("lookuperror_a", "0004_a4"),
  351. ("lookuperror_b", "0003_b3"),
  352. ("lookuperror_c", "0003_c3"),
  353. ])
  354. self.assertTableExists("lookuperror_b_b3")
  355. self.assertTableExists("lookuperror_a_a4")
  356. self.assertTableExists("lookuperror_c_c3")
  357. # Rebuild the graph to reflect the new DB state
  358. executor.loader.build_graph()
  359. # Migrate backwards -- This led to a lookup LookupErrors because
  360. # lookuperror_b.B2 is not in the initial state (unrelated to app c)
  361. executor.migrate([("lookuperror_a", None)])
  362. # Rebuild the graph to reflect the new DB state
  363. executor.loader.build_graph()
  364. finally:
  365. # Cleanup
  366. executor.migrate([
  367. ("lookuperror_b", None),
  368. ("lookuperror_c", None)
  369. ])
  370. self.assertTableNotExists("lookuperror_a_a1")
  371. self.assertTableNotExists("lookuperror_b_b1")
  372. self.assertTableNotExists("lookuperror_c_c1")
  373. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  374. def test_process_callback(self):
  375. """
  376. #24129 - Tests callback process
  377. """
  378. call_args_list = []
  379. def callback(*args):
  380. call_args_list.append(args)
  381. executor = MigrationExecutor(connection, progress_callback=callback)
  382. # Were the tables there before?
  383. self.assertTableNotExists("migrations_author")
  384. self.assertTableNotExists("migrations_tribble")
  385. executor.migrate([
  386. ("migrations", "0001_initial"),
  387. ("migrations", "0002_second"),
  388. ])
  389. # Rebuild the graph to reflect the new DB state
  390. executor.loader.build_graph()
  391. executor.migrate([
  392. ("migrations", None),
  393. ("migrations", None),
  394. ])
  395. self.assertTableNotExists("migrations_author")
  396. self.assertTableNotExists("migrations_tribble")
  397. migrations = executor.loader.graph.nodes
  398. expected = [
  399. ("render_start", ),
  400. ("render_success", ),
  401. ("apply_start", migrations['migrations', '0001_initial'], False),
  402. ("apply_success", migrations['migrations', '0001_initial'], False),
  403. ("apply_start", migrations['migrations', '0002_second'], False),
  404. ("apply_success", migrations['migrations', '0002_second'], False),
  405. ("render_start", ),
  406. ("render_success", ),
  407. ("unapply_start", migrations['migrations', '0002_second'], False),
  408. ("unapply_success", migrations['migrations', '0002_second'], False),
  409. ("unapply_start", migrations['migrations', '0001_initial'], False),
  410. ("unapply_success", migrations['migrations', '0001_initial'], False),
  411. ]
  412. self.assertEqual(call_args_list, expected)
  413. @override_settings(
  414. INSTALLED_APPS=[
  415. "migrations.migrations_test_apps.alter_fk.author_app",
  416. "migrations.migrations_test_apps.alter_fk.book_app",
  417. ]
  418. )
  419. def test_alter_id_type_with_fk(self):
  420. try:
  421. executor = MigrationExecutor(connection)
  422. self.assertTableNotExists("author_app_author")
  423. self.assertTableNotExists("book_app_book")
  424. # Apply initial migrations
  425. executor.migrate([
  426. ("author_app", "0001_initial"),
  427. ("book_app", "0001_initial"),
  428. ])
  429. self.assertTableExists("author_app_author")
  430. self.assertTableExists("book_app_book")
  431. # Rebuild the graph to reflect the new DB state
  432. executor.loader.build_graph()
  433. # Apply PK type alteration
  434. executor.migrate([("author_app", "0002_alter_id")])
  435. # Rebuild the graph to reflect the new DB state
  436. executor.loader.build_graph()
  437. finally:
  438. # We can't simply unapply the migrations here because there is no
  439. # implicit cast from VARCHAR to INT on the database level.
  440. with connection.schema_editor() as editor:
  441. editor.execute(editor.sql_delete_table % {"table": "book_app_book"})
  442. editor.execute(editor.sql_delete_table % {"table": "author_app_author"})
  443. self.assertTableNotExists("author_app_author")
  444. self.assertTableNotExists("book_app_book")
  445. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"})
  446. def test_apply_all_replaced_marks_replacement_as_applied(self):
  447. """
  448. Applying all replaced migrations marks replacement as applied (#24628).
  449. """
  450. recorder = MigrationRecorder(connection)
  451. # Place the database in a state where the replaced migrations are
  452. # partially applied: 0001 is applied, 0002 is not.
  453. recorder.record_applied("migrations", "0001_initial")
  454. executor = MigrationExecutor(connection)
  455. # Use fake because we don't actually have the first migration
  456. # applied, so the second will fail. And there's no need to actually
  457. # create/modify tables here, we're just testing the
  458. # MigrationRecord, which works the same with or without fake.
  459. executor.migrate([("migrations", "0002_second")], fake=True)
  460. # Because we've now applied 0001 and 0002 both, their squashed
  461. # replacement should be marked as applied.
  462. self.assertIn(
  463. ("migrations", "0001_squashed_0002"),
  464. recorder.applied_migrations(),
  465. )
  466. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"})
  467. def test_migrate_marks_replacement_applied_even_if_it_did_nothing(self):
  468. """
  469. A new squash migration will be marked as applied even if all its
  470. replaced migrations were previously already applied (#24628).
  471. """
  472. recorder = MigrationRecorder(connection)
  473. # Record all replaced migrations as applied
  474. recorder.record_applied("migrations", "0001_initial")
  475. recorder.record_applied("migrations", "0002_second")
  476. executor = MigrationExecutor(connection)
  477. executor.migrate([("migrations", "0001_squashed_0002")])
  478. # Because 0001 and 0002 are both applied, even though this migrate run
  479. # didn't apply anything new, their squashed replacement should be
  480. # marked as applied.
  481. self.assertIn(
  482. ("migrations", "0001_squashed_0002"),
  483. recorder.applied_migrations(),
  484. )
  485. class FakeLoader(object):
  486. def __init__(self, graph, applied):
  487. self.graph = graph
  488. self.applied_migrations = applied
  489. class FakeMigration(object):
  490. """Really all we need is any object with a debug-useful repr."""
  491. def __init__(self, name):
  492. self.name = name
  493. def __repr__(self):
  494. return 'M<%s>' % self.name
  495. class ExecutorUnitTests(TestCase):
  496. """(More) isolated unit tests for executor methods."""
  497. def test_minimize_rollbacks(self):
  498. """
  499. Minimize unnecessary rollbacks in connected apps.
  500. When you say "./manage.py migrate appA 0001", rather than migrating to
  501. just after appA-0001 in the linearized migration plan (which could roll
  502. back migrations in other apps that depend on appA 0001, but don't need
  503. to be rolled back since we're not rolling back appA 0001), we migrate
  504. to just before appA-0002.
  505. """
  506. a1_impl = FakeMigration('a1')
  507. a1 = ('a', '1')
  508. a2_impl = FakeMigration('a2')
  509. a2 = ('a', '2')
  510. b1_impl = FakeMigration('b1')
  511. b1 = ('b', '1')
  512. graph = MigrationGraph()
  513. graph.add_node(a1, a1_impl)
  514. graph.add_node(a2, a2_impl)
  515. graph.add_node(b1, b1_impl)
  516. graph.add_dependency(None, b1, a1)
  517. graph.add_dependency(None, a2, a1)
  518. executor = MigrationExecutor(None)
  519. executor.loader = FakeLoader(graph, {a1, b1, a2})
  520. plan = executor.migration_plan({a1})
  521. self.assertEqual(plan, [(a2_impl, True)])
  522. def test_minimize_rollbacks_branchy(self):
  523. """
  524. Minimize rollbacks when target has multiple in-app children.
  525. a: 1 <---- 3 <--\
  526. \ \- 2 <--- 4
  527. \ \
  528. b: \- 1 <--- 2
  529. """
  530. a1_impl = FakeMigration('a1')
  531. a1 = ('a', '1')
  532. a2_impl = FakeMigration('a2')
  533. a2 = ('a', '2')
  534. a3_impl = FakeMigration('a3')
  535. a3 = ('a', '3')
  536. a4_impl = FakeMigration('a4')
  537. a4 = ('a', '4')
  538. b1_impl = FakeMigration('b1')
  539. b1 = ('b', '1')
  540. b2_impl = FakeMigration('b2')
  541. b2 = ('b', '2')
  542. graph = MigrationGraph()
  543. graph.add_node(a1, a1_impl)
  544. graph.add_node(a2, a2_impl)
  545. graph.add_node(a3, a3_impl)
  546. graph.add_node(a4, a4_impl)
  547. graph.add_node(b1, b1_impl)
  548. graph.add_node(b2, b2_impl)
  549. graph.add_dependency(None, a2, a1)
  550. graph.add_dependency(None, a3, a1)
  551. graph.add_dependency(None, a4, a2)
  552. graph.add_dependency(None, a4, a3)
  553. graph.add_dependency(None, b2, b1)
  554. graph.add_dependency(None, b1, a1)
  555. graph.add_dependency(None, b2, a2)
  556. executor = MigrationExecutor(None)
  557. executor.loader = FakeLoader(graph, {a1, b1, a2, b2, a3, a4})
  558. plan = executor.migration_plan({a1})
  559. should_be_rolled_back = [b2_impl, a4_impl, a2_impl, a3_impl]
  560. exp = [(m, True) for m in should_be_rolled_back]
  561. self.assertEqual(plan, exp)
  562. def test_backwards_nothing_to_do(self):
  563. """
  564. If the current state satisfies the given target, do nothing.
  565. a: 1 <--- 2
  566. b: \- 1
  567. c: \- 1
  568. If a1 is applied already and a2 is not, and we're asked to migrate to
  569. a1, don't apply or unapply b1 or c1, regardless of their current state.
  570. """
  571. a1_impl = FakeMigration('a1')
  572. a1 = ('a', '1')
  573. a2_impl = FakeMigration('a2')
  574. a2 = ('a', '2')
  575. b1_impl = FakeMigration('b1')
  576. b1 = ('b', '1')
  577. c1_impl = FakeMigration('c1')
  578. c1 = ('c', '1')
  579. graph = MigrationGraph()
  580. graph.add_node(a1, a1_impl)
  581. graph.add_node(a2, a2_impl)
  582. graph.add_node(b1, b1_impl)
  583. graph.add_node(c1, c1_impl)
  584. graph.add_dependency(None, a2, a1)
  585. graph.add_dependency(None, b1, a1)
  586. graph.add_dependency(None, c1, a1)
  587. executor = MigrationExecutor(None)
  588. executor.loader = FakeLoader(graph, {a1, b1})
  589. plan = executor.migration_plan({a1})
  590. self.assertEqual(plan, [])