2
0

test_commands.py 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380
  1. # -*- coding: utf-8 -*-
  2. from __future__ import unicode_literals
  3. import datetime
  4. import importlib
  5. import io
  6. import os
  7. import sys
  8. from django.apps import apps
  9. from django.core.management import CommandError, call_command
  10. from django.db import (
  11. ConnectionHandler, DatabaseError, connection, connections, models,
  12. )
  13. from django.db.backends.base.schema import BaseDatabaseSchemaEditor
  14. from django.db.migrations.exceptions import (
  15. InconsistentMigrationHistory, MigrationSchemaMissing,
  16. )
  17. from django.db.migrations.recorder import MigrationRecorder
  18. from django.test import mock, override_settings
  19. from django.utils import six
  20. from django.utils.encoding import force_text
  21. from .models import UnicodeModel, UnserializableModel
  22. from .routers import TestRouter
  23. from .test_base import MigrationTestBase
  24. class MigrateTests(MigrationTestBase):
  25. """
  26. Tests running the migrate command.
  27. """
  28. multi_db = True
  29. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  30. def test_migrate(self):
  31. """
  32. Tests basic usage of the migrate command.
  33. """
  34. # No tables are created
  35. self.assertTableNotExists("migrations_author")
  36. self.assertTableNotExists("migrations_tribble")
  37. self.assertTableNotExists("migrations_book")
  38. # Run the migrations to 0001 only
  39. call_command("migrate", "migrations", "0001", verbosity=0)
  40. # The correct tables exist
  41. self.assertTableExists("migrations_author")
  42. self.assertTableExists("migrations_tribble")
  43. self.assertTableNotExists("migrations_book")
  44. # Run migrations all the way
  45. call_command("migrate", verbosity=0)
  46. # The correct tables exist
  47. self.assertTableExists("migrations_author")
  48. self.assertTableNotExists("migrations_tribble")
  49. self.assertTableExists("migrations_book")
  50. # Unmigrate everything
  51. call_command("migrate", "migrations", "zero", verbosity=0)
  52. # Tables are gone
  53. self.assertTableNotExists("migrations_author")
  54. self.assertTableNotExists("migrations_tribble")
  55. self.assertTableNotExists("migrations_book")
  56. @override_settings(INSTALLED_APPS=[
  57. 'django.contrib.auth',
  58. 'django.contrib.contenttypes',
  59. 'migrations.migrations_test_apps.migrated_app',
  60. ])
  61. def test_migrate_with_system_checks(self):
  62. out = six.StringIO()
  63. call_command('migrate', skip_checks=False, no_color=True, stdout=out)
  64. self.assertIn('Apply all migrations: migrated_app', out.getvalue())
  65. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_initial_false"})
  66. def test_migrate_initial_false(self):
  67. """
  68. `Migration.initial = False` skips fake-initial detection.
  69. """
  70. # Make sure no tables are created
  71. self.assertTableNotExists("migrations_author")
  72. self.assertTableNotExists("migrations_tribble")
  73. # Run the migrations to 0001 only
  74. call_command("migrate", "migrations", "0001", verbosity=0)
  75. # Fake rollback
  76. call_command("migrate", "migrations", "zero", fake=True, verbosity=0)
  77. # Make sure fake-initial detection does not run
  78. with self.assertRaises(DatabaseError):
  79. call_command("migrate", "migrations", "0001", fake_initial=True, verbosity=0)
  80. call_command("migrate", "migrations", "0001", fake=True, verbosity=0)
  81. # Real rollback
  82. call_command("migrate", "migrations", "zero", verbosity=0)
  83. # Make sure it's all gone
  84. self.assertTableNotExists("migrations_author")
  85. self.assertTableNotExists("migrations_tribble")
  86. self.assertTableNotExists("migrations_book")
  87. @override_settings(
  88. MIGRATION_MODULES={"migrations": "migrations.test_migrations"},
  89. DATABASE_ROUTERS=['migrations.routers.TestRouter'],
  90. )
  91. def test_migrate_fake_initial(self):
  92. """
  93. --fake-initial only works if all tables created in the initial
  94. migration of an app exists. Database routers must be obeyed when doing
  95. that check.
  96. """
  97. # Make sure no tables are created
  98. for db in connections:
  99. self.assertTableNotExists("migrations_author", using=db)
  100. self.assertTableNotExists("migrations_tribble", using=db)
  101. # Run the migrations to 0001 only
  102. call_command("migrate", "migrations", "0001", verbosity=0)
  103. call_command("migrate", "migrations", "0001", verbosity=0, database="other")
  104. # Make sure the right tables exist
  105. self.assertTableExists("migrations_author")
  106. self.assertTableNotExists("migrations_tribble")
  107. # Also check the "other" database
  108. self.assertTableNotExists("migrations_author", using="other")
  109. self.assertTableExists("migrations_tribble", using="other")
  110. # Fake a roll-back
  111. call_command("migrate", "migrations", "zero", fake=True, verbosity=0)
  112. call_command("migrate", "migrations", "zero", fake=True, verbosity=0, database="other")
  113. # Make sure the tables still exist
  114. self.assertTableExists("migrations_author")
  115. self.assertTableExists("migrations_tribble", using="other")
  116. # Try to run initial migration
  117. with self.assertRaises(DatabaseError):
  118. call_command("migrate", "migrations", "0001", verbosity=0)
  119. # Run initial migration with an explicit --fake-initial
  120. out = six.StringIO()
  121. with mock.patch('django.core.management.color.supports_color', lambda *args: False):
  122. call_command("migrate", "migrations", "0001", fake_initial=True, stdout=out, verbosity=1)
  123. call_command("migrate", "migrations", "0001", fake_initial=True, verbosity=0, database="other")
  124. self.assertIn(
  125. "migrations.0001_initial... faked",
  126. out.getvalue().lower()
  127. )
  128. # Run migrations all the way
  129. call_command("migrate", verbosity=0)
  130. call_command("migrate", verbosity=0, database="other")
  131. # Make sure the right tables exist
  132. self.assertTableExists("migrations_author")
  133. self.assertTableNotExists("migrations_tribble")
  134. self.assertTableExists("migrations_book")
  135. self.assertTableNotExists("migrations_author", using="other")
  136. self.assertTableNotExists("migrations_tribble", using="other")
  137. self.assertTableNotExists("migrations_book", using="other")
  138. # Fake a roll-back
  139. call_command("migrate", "migrations", "zero", fake=True, verbosity=0)
  140. call_command("migrate", "migrations", "zero", fake=True, verbosity=0, database="other")
  141. # Make sure the tables still exist
  142. self.assertTableExists("migrations_author")
  143. self.assertTableNotExists("migrations_tribble")
  144. self.assertTableExists("migrations_book")
  145. # Try to run initial migration
  146. with self.assertRaises(DatabaseError):
  147. call_command("migrate", "migrations", verbosity=0)
  148. # Run initial migration with an explicit --fake-initial
  149. with self.assertRaises(DatabaseError):
  150. # Fails because "migrations_tribble" does not exist but needs to in
  151. # order to make --fake-initial work.
  152. call_command("migrate", "migrations", fake_initial=True, verbosity=0)
  153. # Fake a apply
  154. call_command("migrate", "migrations", fake=True, verbosity=0)
  155. call_command("migrate", "migrations", fake=True, verbosity=0, database="other")
  156. # Unmigrate everything
  157. call_command("migrate", "migrations", "zero", verbosity=0)
  158. call_command("migrate", "migrations", "zero", verbosity=0, database="other")
  159. # Make sure it's all gone
  160. for db in connections:
  161. self.assertTableNotExists("migrations_author", using=db)
  162. self.assertTableNotExists("migrations_tribble", using=db)
  163. self.assertTableNotExists("migrations_book", using=db)
  164. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_fake_split_initial"})
  165. def test_migrate_fake_split_initial(self):
  166. """
  167. Split initial migrations can be faked with --fake-initial.
  168. """
  169. call_command("migrate", "migrations", "0002", verbosity=0)
  170. call_command("migrate", "migrations", "zero", fake=True, verbosity=0)
  171. out = six.StringIO()
  172. with mock.patch('django.core.management.color.supports_color', lambda *args: False):
  173. call_command("migrate", "migrations", "0002", fake_initial=True, stdout=out, verbosity=1)
  174. value = out.getvalue().lower()
  175. self.assertIn("migrations.0001_initial... faked", value)
  176. self.assertIn("migrations.0002_second... faked", value)
  177. # Fake an apply
  178. call_command("migrate", "migrations", fake=True, verbosity=0)
  179. # Unmigrate everything
  180. call_command("migrate", "migrations", "zero", verbosity=0)
  181. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_conflict"})
  182. def test_migrate_conflict_exit(self):
  183. """
  184. migrate exits if it detects a conflict.
  185. """
  186. with self.assertRaisesMessage(CommandError, "Conflicting migrations detected"):
  187. call_command("migrate", "migrations")
  188. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  189. def test_showmigrations_list(self):
  190. """
  191. showmigrations --list displays migrations and whether or not they're
  192. applied.
  193. """
  194. out = six.StringIO()
  195. with mock.patch('django.core.management.color.supports_color', lambda *args: True):
  196. call_command("showmigrations", format='list', stdout=out, verbosity=0, no_color=False)
  197. self.assertEqual(
  198. '\x1b[1mmigrations\n\x1b[0m'
  199. ' [ ] 0001_initial\n'
  200. ' [ ] 0002_second\n',
  201. out.getvalue().lower()
  202. )
  203. call_command("migrate", "migrations", "0001", verbosity=0)
  204. out = six.StringIO()
  205. # Giving the explicit app_label tests for selective `show_list` in the command
  206. call_command("showmigrations", "migrations", format='list', stdout=out, verbosity=0, no_color=True)
  207. self.assertEqual(
  208. 'migrations\n'
  209. ' [x] 0001_initial\n'
  210. ' [ ] 0002_second\n',
  211. out.getvalue().lower()
  212. )
  213. # Cleanup by unmigrating everything
  214. call_command("migrate", "migrations", "zero", verbosity=0)
  215. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_run_before"})
  216. def test_showmigrations_plan(self):
  217. """
  218. Tests --plan output of showmigrations command
  219. """
  220. out = six.StringIO()
  221. call_command("showmigrations", format='plan', stdout=out)
  222. self.assertEqual(
  223. "[ ] migrations.0001_initial\n"
  224. "[ ] migrations.0003_third\n"
  225. "[ ] migrations.0002_second\n",
  226. out.getvalue().lower()
  227. )
  228. out = six.StringIO()
  229. call_command("showmigrations", format='plan', stdout=out, verbosity=2)
  230. self.assertEqual(
  231. "[ ] migrations.0001_initial\n"
  232. "[ ] migrations.0003_third ... (migrations.0001_initial)\n"
  233. "[ ] migrations.0002_second ... (migrations.0001_initial, migrations.0003_third)\n",
  234. out.getvalue().lower()
  235. )
  236. call_command("migrate", "migrations", "0003", verbosity=0)
  237. out = six.StringIO()
  238. call_command("showmigrations", format='plan', stdout=out)
  239. self.assertEqual(
  240. "[x] migrations.0001_initial\n"
  241. "[x] migrations.0003_third\n"
  242. "[ ] migrations.0002_second\n",
  243. out.getvalue().lower()
  244. )
  245. out = six.StringIO()
  246. call_command("showmigrations", format='plan', stdout=out, verbosity=2)
  247. self.assertEqual(
  248. "[x] migrations.0001_initial\n"
  249. "[x] migrations.0003_third ... (migrations.0001_initial)\n"
  250. "[ ] migrations.0002_second ... (migrations.0001_initial, migrations.0003_third)\n",
  251. out.getvalue().lower()
  252. )
  253. # Cleanup by unmigrating everything
  254. call_command("migrate", "migrations", "zero", verbosity=0)
  255. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_empty"})
  256. def test_showmigrations_plan_no_migrations(self):
  257. """
  258. Tests --plan output of showmigrations command without migrations
  259. """
  260. out = six.StringIO()
  261. call_command("showmigrations", format='plan', stdout=out)
  262. self.assertEqual("", out.getvalue().lower())
  263. out = six.StringIO()
  264. call_command("showmigrations", format='plan', stdout=out, verbosity=2)
  265. self.assertEqual("", out.getvalue().lower())
  266. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed_complex"})
  267. def test_showmigrations_plan_squashed(self):
  268. """
  269. Tests --plan output of showmigrations command with squashed migrations.
  270. """
  271. out = six.StringIO()
  272. call_command("showmigrations", format='plan', stdout=out)
  273. self.assertEqual(
  274. "[ ] migrations.1_auto\n"
  275. "[ ] migrations.2_auto\n"
  276. "[ ] migrations.3_squashed_5\n"
  277. "[ ] migrations.6_auto\n"
  278. "[ ] migrations.7_auto\n",
  279. out.getvalue().lower()
  280. )
  281. out = six.StringIO()
  282. call_command("showmigrations", format='plan', stdout=out, verbosity=2)
  283. self.assertEqual(
  284. "[ ] migrations.1_auto\n"
  285. "[ ] migrations.2_auto ... (migrations.1_auto)\n"
  286. "[ ] migrations.3_squashed_5 ... (migrations.2_auto)\n"
  287. "[ ] migrations.6_auto ... (migrations.3_squashed_5)\n"
  288. "[ ] migrations.7_auto ... (migrations.6_auto)\n",
  289. out.getvalue().lower()
  290. )
  291. call_command("migrate", "migrations", "3_squashed_5", verbosity=0)
  292. out = six.StringIO()
  293. call_command("showmigrations", format='plan', stdout=out)
  294. self.assertEqual(
  295. "[x] migrations.1_auto\n"
  296. "[x] migrations.2_auto\n"
  297. "[x] migrations.3_squashed_5\n"
  298. "[ ] migrations.6_auto\n"
  299. "[ ] migrations.7_auto\n",
  300. out.getvalue().lower()
  301. )
  302. out = six.StringIO()
  303. call_command("showmigrations", format='plan', stdout=out, verbosity=2)
  304. self.assertEqual(
  305. "[x] migrations.1_auto\n"
  306. "[x] migrations.2_auto ... (migrations.1_auto)\n"
  307. "[x] migrations.3_squashed_5 ... (migrations.2_auto)\n"
  308. "[ ] migrations.6_auto ... (migrations.3_squashed_5)\n"
  309. "[ ] migrations.7_auto ... (migrations.6_auto)\n",
  310. out.getvalue().lower()
  311. )
  312. @override_settings(INSTALLED_APPS=[
  313. 'migrations.migrations_test_apps.mutate_state_b',
  314. 'migrations.migrations_test_apps.alter_fk.author_app',
  315. 'migrations.migrations_test_apps.alter_fk.book_app',
  316. ])
  317. def test_showmigrations_plan_single_app_label(self):
  318. """
  319. `showmigrations --plan app_label` output with a single app_label.
  320. """
  321. # Single app with no dependencies on other apps.
  322. out = six.StringIO()
  323. call_command('showmigrations', 'mutate_state_b', format='plan', stdout=out)
  324. self.assertEqual(
  325. '[ ] mutate_state_b.0001_initial\n'
  326. '[ ] mutate_state_b.0002_add_field\n',
  327. out.getvalue()
  328. )
  329. # Single app with dependencies.
  330. out = six.StringIO()
  331. call_command('showmigrations', 'author_app', format='plan', stdout=out)
  332. self.assertEqual(
  333. '[ ] author_app.0001_initial\n'
  334. '[ ] book_app.0001_initial\n'
  335. '[ ] author_app.0002_alter_id\n',
  336. out.getvalue()
  337. )
  338. # Some migrations already applied.
  339. call_command('migrate', 'author_app', '0001', verbosity=0)
  340. out = six.StringIO()
  341. call_command('showmigrations', 'author_app', format='plan', stdout=out)
  342. self.assertEqual(
  343. '[X] author_app.0001_initial\n'
  344. '[ ] book_app.0001_initial\n'
  345. '[ ] author_app.0002_alter_id\n',
  346. out.getvalue()
  347. )
  348. # Cleanup by unmigrating author_app.
  349. call_command('migrate', 'author_app', 'zero', verbosity=0)
  350. @override_settings(INSTALLED_APPS=[
  351. 'migrations.migrations_test_apps.mutate_state_b',
  352. 'migrations.migrations_test_apps.alter_fk.author_app',
  353. 'migrations.migrations_test_apps.alter_fk.book_app',
  354. ])
  355. def test_showmigrations_plan_multiple_app_labels(self):
  356. """
  357. `showmigrations --plan app_label` output with multiple app_labels.
  358. """
  359. # Multiple apps: author_app depends on book_app; mutate_state_b doesn't
  360. # depend on other apps.
  361. out = six.StringIO()
  362. call_command('showmigrations', 'mutate_state_b', 'author_app', format='plan', stdout=out)
  363. self.assertEqual(
  364. '[ ] author_app.0001_initial\n'
  365. '[ ] book_app.0001_initial\n'
  366. '[ ] author_app.0002_alter_id\n'
  367. '[ ] mutate_state_b.0001_initial\n'
  368. '[ ] mutate_state_b.0002_add_field\n',
  369. out.getvalue()
  370. )
  371. # Multiple apps: args order shouldn't matter (the same result is
  372. # expected as above).
  373. out = six.StringIO()
  374. call_command('showmigrations', 'author_app', 'mutate_state_b', format='plan', stdout=out)
  375. self.assertEqual(
  376. '[ ] author_app.0001_initial\n'
  377. '[ ] book_app.0001_initial\n'
  378. '[ ] author_app.0002_alter_id\n'
  379. '[ ] mutate_state_b.0001_initial\n'
  380. '[ ] mutate_state_b.0002_add_field\n',
  381. out.getvalue()
  382. )
  383. @override_settings(INSTALLED_APPS=['migrations.migrations_test_apps.unmigrated_app'])
  384. def test_showmigrations_plan_app_label_error(self):
  385. """
  386. `showmigrations --plan app_label` raises an error when no app or
  387. no migrations are present in provided app labels.
  388. """
  389. # App with no migrations.
  390. with self.assertRaisesMessage(CommandError, 'No migrations present for: unmigrated_app'):
  391. call_command('showmigrations', 'unmigrated_app', format='plan')
  392. # Nonexistent app (wrong app label).
  393. with self.assertRaisesMessage(CommandError, 'No migrations present for: nonexistent_app'):
  394. call_command('showmigrations', 'nonexistent_app', format='plan')
  395. # Multiple nonexistent apps; input order shouldn't matter.
  396. with self.assertRaisesMessage(CommandError, 'No migrations present for: nonexistent_app1, nonexistent_app2'):
  397. call_command('showmigrations', 'nonexistent_app1', 'nonexistent_app2', format='plan')
  398. with self.assertRaisesMessage(CommandError, 'No migrations present for: nonexistent_app1, nonexistent_app2'):
  399. call_command('showmigrations', 'nonexistent_app2', 'nonexistent_app1', format='plan')
  400. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  401. def test_sqlmigrate_forwards(self):
  402. """
  403. sqlmigrate outputs forward looking SQL.
  404. """
  405. out = six.StringIO()
  406. call_command("sqlmigrate", "migrations", "0001", stdout=out)
  407. output = out.getvalue().lower()
  408. index_tx_start = output.find(connection.ops.start_transaction_sql().lower())
  409. index_op_desc_author = output.find('-- create model author')
  410. index_create_table = output.find('create table')
  411. index_op_desc_tribble = output.find('-- create model tribble')
  412. index_op_desc_unique_together = output.find('-- alter unique_together')
  413. index_tx_end = output.find(connection.ops.end_transaction_sql().lower())
  414. self.assertGreater(index_tx_start, -1, "Transaction start not found")
  415. self.assertGreater(
  416. index_op_desc_author, index_tx_start,
  417. "Operation description (author) not found or found before transaction start"
  418. )
  419. self.assertGreater(
  420. index_create_table, index_op_desc_author,
  421. "CREATE TABLE not found or found before operation description (author)"
  422. )
  423. self.assertGreater(
  424. index_op_desc_tribble, index_create_table,
  425. "Operation description (tribble) not found or found before CREATE TABLE (author)"
  426. )
  427. self.assertGreater(
  428. index_op_desc_unique_together, index_op_desc_tribble,
  429. "Operation description (unique_together) not found or found before operation description (tribble)"
  430. )
  431. self.assertGreater(
  432. index_tx_end, index_op_desc_unique_together,
  433. "Transaction end not found or found before operation description (unique_together)"
  434. )
  435. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  436. def test_sqlmigrate_backwards(self):
  437. """
  438. sqlmigrate outputs reverse looking SQL.
  439. """
  440. # Cannot generate the reverse SQL unless we've applied the migration.
  441. call_command("migrate", "migrations", verbosity=0)
  442. out = six.StringIO()
  443. call_command("sqlmigrate", "migrations", "0001", stdout=out, backwards=True)
  444. output = out.getvalue().lower()
  445. index_tx_start = output.find(connection.ops.start_transaction_sql().lower())
  446. index_op_desc_unique_together = output.find('-- alter unique_together')
  447. index_op_desc_tribble = output.find('-- create model tribble')
  448. index_op_desc_author = output.find('-- create model author')
  449. index_drop_table = output.rfind('drop table')
  450. index_tx_end = output.find(connection.ops.end_transaction_sql().lower())
  451. self.assertGreater(index_tx_start, -1, "Transaction start not found")
  452. self.assertGreater(
  453. index_op_desc_unique_together, index_tx_start,
  454. "Operation description (unique_together) not found or found before transaction start"
  455. )
  456. self.assertGreater(
  457. index_op_desc_tribble, index_op_desc_unique_together,
  458. "Operation description (tribble) not found or found before operation description (unique_together)"
  459. )
  460. self.assertGreater(
  461. index_op_desc_author, index_op_desc_tribble,
  462. "Operation description (author) not found or found before operation description (tribble)"
  463. )
  464. self.assertGreater(
  465. index_drop_table, index_op_desc_author,
  466. "DROP TABLE not found or found before operation description (author)"
  467. )
  468. self.assertGreater(
  469. index_tx_end, index_op_desc_unique_together,
  470. "Transaction end not found or found before DROP TABLE"
  471. )
  472. # Cleanup by unmigrating everything
  473. call_command("migrate", "migrations", "zero", verbosity=0)
  474. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_non_atomic"})
  475. def test_sqlmigrate_for_non_atomic_migration(self):
  476. """
  477. Transaction wrappers aren't shown for non-atomic migrations.
  478. """
  479. out = six.StringIO()
  480. call_command("sqlmigrate", "migrations", "0001", stdout=out)
  481. output = out.getvalue().lower()
  482. queries = [q.strip() for q in output.splitlines()]
  483. if connection.ops.start_transaction_sql():
  484. self.assertNotIn(connection.ops.start_transaction_sql().lower(), queries)
  485. self.assertNotIn(connection.ops.end_transaction_sql().lower(), queries)
  486. @override_settings(
  487. INSTALLED_APPS=[
  488. "migrations.migrations_test_apps.migrated_app",
  489. "migrations.migrations_test_apps.migrated_unapplied_app",
  490. "migrations.migrations_test_apps.unmigrated_app",
  491. ],
  492. )
  493. def test_regression_22823_unmigrated_fk_to_migrated_model(self):
  494. """
  495. Assuming you have 3 apps, `A`, `B`, and `C`, such that:
  496. * `A` has migrations
  497. * `B` has a migration we want to apply
  498. * `C` has no migrations, but has an FK to `A`
  499. When we try to migrate "B", an exception occurs because the
  500. "B" was not included in the ProjectState that is used to detect
  501. soft-applied migrations (#22823).
  502. """
  503. call_command("migrate", "migrated_unapplied_app", stdout=six.StringIO())
  504. # unmigrated_app.SillyModel has a foreign key to 'migrations.Tribble',
  505. # but that model is only defined in a migration, so the global app
  506. # registry never sees it and the reference is left dangling. Remove it
  507. # to avoid problems in subsequent tests.
  508. del apps._pending_operations[('migrations', 'tribble')]
  509. @override_settings(INSTALLED_APPS=['migrations.migrations_test_apps.unmigrated_app_syncdb'])
  510. def test_migrate_syncdb_deferred_sql_executed_with_schemaeditor(self):
  511. """
  512. For an app without migrations, editor.execute() is used for executing
  513. the syncdb deferred SQL.
  514. """
  515. with mock.patch.object(BaseDatabaseSchemaEditor, 'execute') as execute:
  516. call_command('migrate', run_syncdb=True, verbosity=0)
  517. create_table_count = len([call for call in execute.mock_calls if 'CREATE TABLE' in str(call)])
  518. self.assertEqual(create_table_count, 2)
  519. # There's at least one deferred SQL for creating the foreign key
  520. # index.
  521. self.assertGreater(len(execute.mock_calls), 2)
  522. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"})
  523. def test_migrate_record_replaced(self):
  524. """
  525. Running a single squashed migration should record all of the original
  526. replaced migrations as run.
  527. """
  528. recorder = MigrationRecorder(connection)
  529. out = six.StringIO()
  530. call_command("migrate", "migrations", verbosity=0)
  531. call_command("showmigrations", "migrations", stdout=out, no_color=True)
  532. self.assertEqual(
  533. 'migrations\n'
  534. ' [x] 0001_squashed_0002 (2 squashed migrations)\n',
  535. out.getvalue().lower()
  536. )
  537. applied_migrations = recorder.applied_migrations()
  538. self.assertIn(("migrations", "0001_initial"), applied_migrations)
  539. self.assertIn(("migrations", "0002_second"), applied_migrations)
  540. self.assertIn(("migrations", "0001_squashed_0002"), applied_migrations)
  541. # Rollback changes
  542. call_command("migrate", "migrations", "zero", verbosity=0)
  543. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"})
  544. def test_migrate_record_squashed(self):
  545. """
  546. Running migrate for a squashed migration should record as run
  547. if all of the replaced migrations have been run (#25231).
  548. """
  549. recorder = MigrationRecorder(connection)
  550. recorder.record_applied("migrations", "0001_initial")
  551. recorder.record_applied("migrations", "0002_second")
  552. out = six.StringIO()
  553. call_command("migrate", "migrations", verbosity=0)
  554. call_command("showmigrations", "migrations", stdout=out, no_color=True)
  555. self.assertEqual(
  556. 'migrations\n'
  557. ' [x] 0001_squashed_0002 (2 squashed migrations)\n',
  558. out.getvalue().lower()
  559. )
  560. self.assertIn(
  561. ("migrations", "0001_squashed_0002"),
  562. recorder.applied_migrations()
  563. )
  564. # No changes were actually applied so there is nothing to rollback
  565. @override_settings(MIGRATION_MODULES={'migrations': 'migrations.test_migrations'})
  566. def test_migrate_inconsistent_history(self):
  567. """
  568. Running migrate with some migrations applied before their dependencies
  569. should not be allowed.
  570. """
  571. recorder = MigrationRecorder(connection)
  572. recorder.record_applied("migrations", "0002_second")
  573. msg = "Migration migrations.0002_second is applied before its dependency migrations.0001_initial"
  574. with self.assertRaisesMessage(InconsistentMigrationHistory, msg):
  575. call_command("migrate")
  576. applied_migrations = recorder.applied_migrations()
  577. self.assertNotIn(("migrations", "0001_initial"), applied_migrations)
  578. class MakeMigrationsTests(MigrationTestBase):
  579. """
  580. Tests running the makemigrations command.
  581. """
  582. def setUp(self):
  583. super(MakeMigrationsTests, self).setUp()
  584. self._old_models = apps.app_configs['migrations'].models.copy()
  585. def tearDown(self):
  586. apps.app_configs['migrations'].models = self._old_models
  587. apps.all_models['migrations'] = self._old_models
  588. apps.clear_cache()
  589. super(MakeMigrationsTests, self).tearDown()
  590. def test_files_content(self):
  591. self.assertTableNotExists("migrations_unicodemodel")
  592. apps.register_model('migrations', UnicodeModel)
  593. with self.temporary_migration_module() as migration_dir:
  594. call_command("makemigrations", "migrations", verbosity=0)
  595. # Check for empty __init__.py file in migrations folder
  596. init_file = os.path.join(migration_dir, "__init__.py")
  597. self.assertTrue(os.path.exists(init_file))
  598. with open(init_file, 'r') as fp:
  599. content = force_text(fp.read())
  600. self.assertEqual(content, '')
  601. # Check for existing 0001_initial.py file in migration folder
  602. initial_file = os.path.join(migration_dir, "0001_initial.py")
  603. self.assertTrue(os.path.exists(initial_file))
  604. with io.open(initial_file, 'r', encoding='utf-8') as fp:
  605. content = fp.read()
  606. self.assertIn('# -*- coding: utf-8 -*-', content)
  607. self.assertIn('migrations.CreateModel', content)
  608. self.assertIn('initial = True', content)
  609. if six.PY3:
  610. self.assertIn('úñí©óðé µóðéø', content) # Meta.verbose_name
  611. self.assertIn('úñí©óðé µóðéøß', content) # Meta.verbose_name_plural
  612. self.assertIn('ÚÑÍ¢ÓÐÉ', content) # title.verbose_name
  613. self.assertIn('“Ðjáñgó”', content) # title.default
  614. else:
  615. # Meta.verbose_name
  616. self.assertIn('\\xfa\\xf1\\xed\\xa9\\xf3\\xf0\\xe9 \\xb5\\xf3\\xf0\\xe9\\xf8', content)
  617. # Meta.verbose_name_plural
  618. self.assertIn('\\xfa\\xf1\\xed\\xa9\\xf3\\xf0\\xe9 \\xb5\\xf3\\xf0\\xe9\\xf8\\xdf', content)
  619. self.assertIn('\\xda\\xd1\\xcd\\xa2\\xd3\\xd0\\xc9', content) # title.verbose_name
  620. self.assertIn('\\u201c\\xd0j\\xe1\\xf1g\\xf3\\u201d', content) # title.default
  621. def test_makemigrations_order(self):
  622. """
  623. makemigrations should recognize number-only migrations (0001.py).
  624. """
  625. module = 'migrations.test_migrations_order'
  626. with self.temporary_migration_module(module=module) as migration_dir:
  627. if hasattr(importlib, 'invalidate_caches'):
  628. # Python 3 importlib caches os.listdir() on some platforms like
  629. # Mac OS X (#23850).
  630. importlib.invalidate_caches()
  631. call_command('makemigrations', 'migrations', '--empty', '-n', 'a', '-v', '0')
  632. self.assertTrue(os.path.exists(os.path.join(migration_dir, '0002_a.py')))
  633. def test_makemigrations_empty_connections(self):
  634. empty_connections = ConnectionHandler({'default': {}})
  635. with mock.patch('django.core.management.commands.makemigrations.connections', new=empty_connections):
  636. # with no apps
  637. out = six.StringIO()
  638. call_command('makemigrations', stdout=out)
  639. self.assertIn('No changes detected', out.getvalue())
  640. # with an app
  641. with self.temporary_migration_module() as migration_dir:
  642. call_command('makemigrations', 'migrations', verbosity=0)
  643. init_file = os.path.join(migration_dir, '__init__.py')
  644. self.assertTrue(os.path.exists(init_file))
  645. @override_settings(INSTALLED_APPS=['migrations', 'migrations2'])
  646. def test_makemigrations_consistency_checks_respect_routers(self):
  647. """
  648. The history consistency checks in makemigrations respect
  649. settings.DATABASE_ROUTERS.
  650. """
  651. def patched_ensure_schema(migration_recorder):
  652. if migration_recorder.connection is connections['other']:
  653. raise MigrationSchemaMissing('Patched')
  654. else:
  655. return mock.DEFAULT
  656. self.assertTableNotExists('migrations_unicodemodel')
  657. apps.register_model('migrations', UnicodeModel)
  658. with mock.patch.object(
  659. MigrationRecorder, 'ensure_schema',
  660. autospec=True, side_effect=patched_ensure_schema) as ensure_schema:
  661. with self.temporary_migration_module() as migration_dir:
  662. call_command("makemigrations", "migrations", verbosity=0)
  663. initial_file = os.path.join(migration_dir, "0001_initial.py")
  664. self.assertTrue(os.path.exists(initial_file))
  665. self.assertEqual(ensure_schema.call_count, 1) # 'default' is checked
  666. # Router says not to migrate 'other' so consistency shouldn't
  667. # be checked.
  668. with self.settings(DATABASE_ROUTERS=['migrations.routers.TestRouter']):
  669. call_command('makemigrations', 'migrations', verbosity=0)
  670. self.assertEqual(ensure_schema.call_count, 2) # 'default' again
  671. # With a router that doesn't prohibit migrating 'other',
  672. # consistency is checked.
  673. with self.settings(DATABASE_ROUTERS=['migrations.routers.EmptyRouter']):
  674. with self.assertRaisesMessage(MigrationSchemaMissing, 'Patched'):
  675. call_command('makemigrations', 'migrations', verbosity=0)
  676. self.assertEqual(ensure_schema.call_count, 4) # 'default' and 'other'
  677. # With a router that doesn't allow migrating on any database,
  678. # no consistency checks are made.
  679. with self.settings(DATABASE_ROUTERS=['migrations.routers.TestRouter']):
  680. with mock.patch.object(TestRouter, 'allow_migrate', return_value=False) as allow_migrate:
  681. call_command('makemigrations', 'migrations', verbosity=0)
  682. allow_migrate.assert_any_call('other', 'migrations', model_name='UnicodeModel')
  683. # allow_migrate() is called with the correct arguments.
  684. self.assertGreater(len(allow_migrate.mock_calls), 0)
  685. for mock_call in allow_migrate.mock_calls:
  686. _, call_args, call_kwargs = mock_call
  687. connection_alias, app_name = call_args
  688. self.assertIn(connection_alias, ['default', 'other'])
  689. # Raises an error if invalid app_name/model_name occurs.
  690. apps.get_app_config(app_name).get_model(call_kwargs['model_name'])
  691. self.assertEqual(ensure_schema.call_count, 4)
  692. def test_failing_migration(self):
  693. # If a migration fails to serialize, it shouldn't generate an empty file. #21280
  694. apps.register_model('migrations', UnserializableModel)
  695. with self.temporary_migration_module() as migration_dir:
  696. with self.assertRaisesMessage(ValueError, 'Cannot serialize'):
  697. call_command("makemigrations", "migrations", verbosity=0)
  698. initial_file = os.path.join(migration_dir, "0001_initial.py")
  699. self.assertFalse(os.path.exists(initial_file))
  700. def test_makemigrations_conflict_exit(self):
  701. """
  702. makemigrations exits if it detects a conflict.
  703. """
  704. with self.temporary_migration_module(module="migrations.test_migrations_conflict"):
  705. with self.assertRaises(CommandError):
  706. call_command("makemigrations")
  707. def test_makemigrations_merge_no_conflict(self):
  708. """
  709. makemigrations exits if in merge mode with no conflicts.
  710. """
  711. out = six.StringIO()
  712. with self.temporary_migration_module(module="migrations.test_migrations"):
  713. call_command("makemigrations", merge=True, stdout=out)
  714. self.assertIn("No conflicts detected to merge.", out.getvalue())
  715. def test_makemigrations_no_app_sys_exit(self):
  716. """
  717. makemigrations exits if a non-existent app is specified.
  718. """
  719. err = six.StringIO()
  720. with self.assertRaises(SystemExit):
  721. call_command("makemigrations", "this_app_does_not_exist", stderr=err)
  722. self.assertIn("'this_app_does_not_exist' could not be found.", err.getvalue())
  723. def test_makemigrations_empty_no_app_specified(self):
  724. """
  725. makemigrations exits if no app is specified with 'empty' mode.
  726. """
  727. with self.assertRaises(CommandError):
  728. call_command("makemigrations", empty=True)
  729. def test_makemigrations_empty_migration(self):
  730. """
  731. makemigrations properly constructs an empty migration.
  732. """
  733. with self.temporary_migration_module() as migration_dir:
  734. call_command("makemigrations", "migrations", empty=True, verbosity=0)
  735. # Check for existing 0001_initial.py file in migration folder
  736. initial_file = os.path.join(migration_dir, "0001_initial.py")
  737. self.assertTrue(os.path.exists(initial_file))
  738. with io.open(initial_file, 'r', encoding='utf-8') as fp:
  739. content = fp.read()
  740. self.assertIn('# -*- coding: utf-8 -*-', content)
  741. # Remove all whitespace to check for empty dependencies and operations
  742. content = content.replace(' ', '')
  743. self.assertIn('dependencies=[\n]', content)
  744. self.assertIn('operations=[\n]', content)
  745. @override_settings(MIGRATION_MODULES={"migrations": None})
  746. def test_makemigrations_disabled_migrations_for_app(self):
  747. """
  748. makemigrations raises a nice error when migrations are disabled for an
  749. app.
  750. """
  751. msg = (
  752. "Django can't create migrations for app 'migrations' because migrations "
  753. "have been disabled via the MIGRATION_MODULES setting."
  754. )
  755. with self.assertRaisesMessage(ValueError, msg):
  756. call_command("makemigrations", "migrations", empty=True, verbosity=0)
  757. def test_makemigrations_no_changes_no_apps(self):
  758. """
  759. makemigrations exits when there are no changes and no apps are specified.
  760. """
  761. out = six.StringIO()
  762. call_command("makemigrations", stdout=out)
  763. self.assertIn("No changes detected", out.getvalue())
  764. def test_makemigrations_no_changes(self):
  765. """
  766. makemigrations exits when there are no changes to an app.
  767. """
  768. out = six.StringIO()
  769. with self.temporary_migration_module(module="migrations.test_migrations_no_changes"):
  770. call_command("makemigrations", "migrations", stdout=out)
  771. self.assertIn("No changes detected in app 'migrations'", out.getvalue())
  772. def test_makemigrations_no_apps_initial(self):
  773. """
  774. makemigrations should detect initial is needed on empty migration
  775. modules if no app provided.
  776. """
  777. out = six.StringIO()
  778. with self.temporary_migration_module(module="migrations.test_migrations_empty"):
  779. call_command("makemigrations", stdout=out)
  780. self.assertIn("0001_initial.py", out.getvalue())
  781. def test_makemigrations_migrations_announce(self):
  782. """
  783. makemigrations announces the migration at the default verbosity level.
  784. """
  785. out = six.StringIO()
  786. with self.temporary_migration_module():
  787. call_command("makemigrations", "migrations", stdout=out)
  788. self.assertIn("Migrations for 'migrations'", out.getvalue())
  789. def test_makemigrations_no_common_ancestor(self):
  790. """
  791. makemigrations fails to merge migrations with no common ancestor.
  792. """
  793. with self.assertRaises(ValueError) as context:
  794. with self.temporary_migration_module(module="migrations.test_migrations_no_ancestor"):
  795. call_command("makemigrations", "migrations", merge=True)
  796. exception_message = str(context.exception)
  797. self.assertIn("Could not find common ancestor of", exception_message)
  798. self.assertIn("0002_second", exception_message)
  799. self.assertIn("0002_conflicting_second", exception_message)
  800. def test_makemigrations_interactive_reject(self):
  801. """
  802. makemigrations enters and exits interactive mode properly.
  803. """
  804. # Monkeypatch interactive questioner to auto reject
  805. with mock.patch('django.db.migrations.questioner.input', mock.Mock(return_value='N')):
  806. with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
  807. call_command("makemigrations", "migrations", name="merge", merge=True, interactive=True, verbosity=0)
  808. merge_file = os.path.join(migration_dir, '0003_merge.py')
  809. self.assertFalse(os.path.exists(merge_file))
  810. def test_makemigrations_interactive_accept(self):
  811. """
  812. makemigrations enters interactive mode and merges properly.
  813. """
  814. # Monkeypatch interactive questioner to auto accept
  815. with mock.patch('django.db.migrations.questioner.input', mock.Mock(return_value='y')):
  816. out = six.StringIO()
  817. with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
  818. call_command("makemigrations", "migrations", name="merge", merge=True, interactive=True, stdout=out)
  819. merge_file = os.path.join(migration_dir, '0003_merge.py')
  820. self.assertTrue(os.path.exists(merge_file))
  821. self.assertIn("Created new merge migration", out.getvalue())
  822. @mock.patch('django.db.migrations.utils.datetime')
  823. def test_makemigrations_default_merge_name(self, mock_datetime):
  824. mock_datetime.datetime.now.return_value = datetime.datetime(2016, 1, 2, 3, 4)
  825. with mock.patch('django.db.migrations.questioner.input', mock.Mock(return_value='y')):
  826. out = six.StringIO()
  827. with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
  828. call_command("makemigrations", "migrations", merge=True, interactive=True, stdout=out)
  829. merge_file = os.path.join(migration_dir, '0003_merge_20160102_0304.py')
  830. self.assertTrue(os.path.exists(merge_file))
  831. self.assertIn("Created new merge migration", out.getvalue())
  832. def test_makemigrations_non_interactive_not_null_addition(self):
  833. """
  834. Non-interactive makemigrations fails when a default is missing on a
  835. new not-null field.
  836. """
  837. class SillyModel(models.Model):
  838. silly_field = models.BooleanField(default=False)
  839. silly_int = models.IntegerField()
  840. class Meta:
  841. app_label = "migrations"
  842. out = six.StringIO()
  843. with self.assertRaises(SystemExit):
  844. with self.temporary_migration_module(module="migrations.test_migrations_no_default"):
  845. call_command("makemigrations", "migrations", interactive=False, stdout=out)
  846. def test_makemigrations_non_interactive_not_null_alteration(self):
  847. """
  848. Non-interactive makemigrations fails when a default is missing on a
  849. field changed to not-null.
  850. """
  851. class Author(models.Model):
  852. name = models.CharField(max_length=255)
  853. slug = models.SlugField()
  854. age = models.IntegerField(default=0)
  855. class Meta:
  856. app_label = "migrations"
  857. out = six.StringIO()
  858. with self.temporary_migration_module(module="migrations.test_migrations"):
  859. call_command("makemigrations", "migrations", interactive=False, stdout=out)
  860. self.assertIn("Alter field slug on author", out.getvalue())
  861. def test_makemigrations_non_interactive_no_model_rename(self):
  862. """
  863. makemigrations adds and removes a possible model rename in
  864. non-interactive mode.
  865. """
  866. class RenamedModel(models.Model):
  867. silly_field = models.BooleanField(default=False)
  868. class Meta:
  869. app_label = "migrations"
  870. out = six.StringIO()
  871. with self.temporary_migration_module(module="migrations.test_migrations_no_default"):
  872. call_command("makemigrations", "migrations", interactive=False, stdout=out)
  873. self.assertIn("Delete model SillyModel", out.getvalue())
  874. self.assertIn("Create model RenamedModel", out.getvalue())
  875. def test_makemigrations_non_interactive_no_field_rename(self):
  876. """
  877. makemigrations adds and removes a possible field rename in
  878. non-interactive mode.
  879. """
  880. class SillyModel(models.Model):
  881. silly_rename = models.BooleanField(default=False)
  882. class Meta:
  883. app_label = "migrations"
  884. out = six.StringIO()
  885. with self.temporary_migration_module(module="migrations.test_migrations_no_default"):
  886. call_command("makemigrations", "migrations", interactive=False, stdout=out)
  887. self.assertIn("Remove field silly_field from sillymodel", out.getvalue())
  888. self.assertIn("Add field silly_rename to sillymodel", out.getvalue())
  889. def test_makemigrations_handle_merge(self):
  890. """
  891. makemigrations properly merges the conflicting migrations with --noinput.
  892. """
  893. out = six.StringIO()
  894. with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
  895. call_command("makemigrations", "migrations", name="merge", merge=True, interactive=False, stdout=out)
  896. merge_file = os.path.join(migration_dir, '0003_merge.py')
  897. self.assertTrue(os.path.exists(merge_file))
  898. output = out.getvalue()
  899. self.assertIn("Merging migrations", output)
  900. self.assertIn("Branch 0002_second", output)
  901. self.assertIn("Branch 0002_conflicting_second", output)
  902. self.assertIn("Created new merge migration", output)
  903. def test_makemigration_merge_dry_run(self):
  904. """
  905. makemigrations respects --dry-run option when fixing migration
  906. conflicts (#24427).
  907. """
  908. out = six.StringIO()
  909. with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
  910. call_command(
  911. "makemigrations", "migrations", name="merge", dry_run=True,
  912. merge=True, interactive=False, stdout=out,
  913. )
  914. merge_file = os.path.join(migration_dir, '0003_merge.py')
  915. self.assertFalse(os.path.exists(merge_file))
  916. output = out.getvalue()
  917. self.assertIn("Merging migrations", output)
  918. self.assertIn("Branch 0002_second", output)
  919. self.assertIn("Branch 0002_conflicting_second", output)
  920. self.assertNotIn("Created new merge migration", output)
  921. def test_makemigration_merge_dry_run_verbosity_3(self):
  922. """
  923. `makemigrations --merge --dry-run` writes the merge migration file to
  924. stdout with `verbosity == 3` (#24427).
  925. """
  926. out = six.StringIO()
  927. with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
  928. call_command(
  929. "makemigrations", "migrations", name="merge", dry_run=True,
  930. merge=True, interactive=False, stdout=out, verbosity=3,
  931. )
  932. merge_file = os.path.join(migration_dir, '0003_merge.py')
  933. self.assertFalse(os.path.exists(merge_file))
  934. output = out.getvalue()
  935. self.assertIn("Merging migrations", output)
  936. self.assertIn("Branch 0002_second", output)
  937. self.assertIn("Branch 0002_conflicting_second", output)
  938. self.assertNotIn("Created new merge migration", output)
  939. # Additional output caused by verbosity 3
  940. # The complete merge migration file that would be written
  941. # '\n#' is to verify no bytestring prefix before #
  942. self.assertIn("\n# -*- coding: utf-8 -*-", output)
  943. self.assertIn("class Migration(migrations.Migration):", output)
  944. self.assertIn("dependencies = [", output)
  945. self.assertIn("('migrations', '0002_second')", output)
  946. self.assertIn("('migrations', '0002_conflicting_second')", output)
  947. self.assertIn("operations = [", output)
  948. self.assertIn("]", output)
  949. def test_makemigrations_dry_run(self):
  950. """
  951. `makemigrations --dry-run` should not ask for defaults.
  952. """
  953. class SillyModel(models.Model):
  954. silly_field = models.BooleanField(default=False)
  955. silly_date = models.DateField() # Added field without a default
  956. class Meta:
  957. app_label = "migrations"
  958. out = six.StringIO()
  959. with self.temporary_migration_module(module="migrations.test_migrations_no_default"):
  960. call_command("makemigrations", "migrations", dry_run=True, stdout=out)
  961. # Output the expected changes directly, without asking for defaults
  962. self.assertIn("Add field silly_date to sillymodel", out.getvalue())
  963. def test_makemigrations_dry_run_verbosity_3(self):
  964. """
  965. Allow `makemigrations --dry-run` to output the migrations file to
  966. stdout (with verbosity == 3).
  967. """
  968. class SillyModel(models.Model):
  969. silly_field = models.BooleanField(default=False)
  970. silly_char = models.CharField(default="")
  971. class Meta:
  972. app_label = "migrations"
  973. out = six.StringIO()
  974. with self.temporary_migration_module(module="migrations.test_migrations_no_default"):
  975. call_command("makemigrations", "migrations", dry_run=True, stdout=out, verbosity=3)
  976. # Normal --dry-run output
  977. self.assertIn("- Add field silly_char to sillymodel", out.getvalue())
  978. # Additional output caused by verbosity 3
  979. # The complete migrations file that would be written
  980. self.assertIn("# -*- coding: utf-8 -*-", out.getvalue())
  981. self.assertIn("class Migration(migrations.Migration):", out.getvalue())
  982. self.assertIn("dependencies = [", out.getvalue())
  983. self.assertIn("('migrations', '0001_initial'),", out.getvalue())
  984. self.assertIn("migrations.AddField(", out.getvalue())
  985. self.assertIn("model_name='sillymodel',", out.getvalue())
  986. self.assertIn("name='silly_char',", out.getvalue())
  987. def test_makemigrations_migrations_modules_path_not_exist(self):
  988. """
  989. makemigrations creates migrations when specifying a custom location
  990. for migration files using MIGRATION_MODULES if the custom path
  991. doesn't already exist.
  992. """
  993. class SillyModel(models.Model):
  994. silly_field = models.BooleanField(default=False)
  995. class Meta:
  996. app_label = "migrations"
  997. out = six.StringIO()
  998. migration_module = "migrations.test_migrations_path_doesnt_exist.foo.bar"
  999. with self.temporary_migration_module(module=migration_module) as migration_dir:
  1000. call_command("makemigrations", "migrations", stdout=out)
  1001. # Migrations file is actually created in the expected path.
  1002. initial_file = os.path.join(migration_dir, "0001_initial.py")
  1003. self.assertTrue(os.path.exists(initial_file))
  1004. # Command output indicates the migration is created.
  1005. self.assertIn(" - Create model SillyModel", out.getvalue())
  1006. def test_makemigrations_interactive_by_default(self):
  1007. """
  1008. The user is prompted to merge by default if there are conflicts and
  1009. merge is True. Answer negative to differentiate it from behavior when
  1010. --noinput is specified.
  1011. """
  1012. # Monkeypatch interactive questioner to auto reject
  1013. out = six.StringIO()
  1014. with mock.patch('django.db.migrations.questioner.input', mock.Mock(return_value='N')):
  1015. with self.temporary_migration_module(module="migrations.test_migrations_conflict") as migration_dir:
  1016. call_command("makemigrations", "migrations", name="merge", merge=True, stdout=out)
  1017. merge_file = os.path.join(migration_dir, '0003_merge.py')
  1018. # This will fail if interactive is False by default
  1019. self.assertFalse(os.path.exists(merge_file))
  1020. self.assertNotIn("Created new merge migration", out.getvalue())
  1021. @override_settings(
  1022. INSTALLED_APPS=[
  1023. "migrations",
  1024. "migrations.migrations_test_apps.unspecified_app_with_conflict"])
  1025. def test_makemigrations_unspecified_app_with_conflict_no_merge(self):
  1026. """
  1027. makemigrations does not raise a CommandError when an unspecified app
  1028. has conflicting migrations.
  1029. """
  1030. with self.temporary_migration_module(module="migrations.test_migrations_no_changes"):
  1031. call_command("makemigrations", "migrations", merge=False, verbosity=0)
  1032. @override_settings(
  1033. INSTALLED_APPS=[
  1034. "migrations.migrations_test_apps.migrated_app",
  1035. "migrations.migrations_test_apps.unspecified_app_with_conflict"])
  1036. def test_makemigrations_unspecified_app_with_conflict_merge(self):
  1037. """
  1038. makemigrations does not create a merge for an unspecified app even if
  1039. it has conflicting migrations.
  1040. """
  1041. # Monkeypatch interactive questioner to auto accept
  1042. with mock.patch('django.db.migrations.questioner.input', mock.Mock(return_value='y')):
  1043. out = six.StringIO()
  1044. with self.temporary_migration_module(app_label="migrated_app") as migration_dir:
  1045. call_command("makemigrations", "migrated_app", name="merge", merge=True, interactive=True, stdout=out)
  1046. merge_file = os.path.join(migration_dir, '0003_merge.py')
  1047. self.assertFalse(os.path.exists(merge_file))
  1048. self.assertIn("No conflicts detected to merge.", out.getvalue())
  1049. @override_settings(
  1050. INSTALLED_APPS=[
  1051. "migrations.migrations_test_apps.migrated_app",
  1052. "migrations.migrations_test_apps.conflicting_app_with_dependencies"])
  1053. def test_makemigrations_merge_dont_output_dependency_operations(self):
  1054. """
  1055. makemigrations --merge does not output any operations from apps that
  1056. don't belong to a given app.
  1057. """
  1058. # Monkeypatch interactive questioner to auto accept
  1059. with mock.patch('django.db.migrations.questioner.input', mock.Mock(return_value='N')):
  1060. out = six.StringIO()
  1061. with mock.patch('django.core.management.color.supports_color', lambda *args: False):
  1062. call_command(
  1063. "makemigrations", "conflicting_app_with_dependencies",
  1064. merge=True, interactive=True, stdout=out
  1065. )
  1066. val = out.getvalue().lower()
  1067. self.assertIn('merging conflicting_app_with_dependencies\n', val)
  1068. self.assertIn(
  1069. ' branch 0002_conflicting_second\n'
  1070. ' - create model something\n',
  1071. val
  1072. )
  1073. self.assertIn(
  1074. ' branch 0002_second\n'
  1075. ' - delete model tribble\n'
  1076. ' - remove field silly_field from author\n'
  1077. ' - add field rating to author\n'
  1078. ' - create model book\n',
  1079. val
  1080. )
  1081. def test_makemigrations_with_custom_name(self):
  1082. """
  1083. makemigrations --name generate a custom migration name.
  1084. """
  1085. with self.temporary_migration_module() as migration_dir:
  1086. def cmd(migration_count, migration_name, *args):
  1087. call_command("makemigrations", "migrations", "--verbosity", "0", "--name", migration_name, *args)
  1088. migration_file = os.path.join(migration_dir, "%s_%s.py" % (migration_count, migration_name))
  1089. # Check for existing migration file in migration folder
  1090. self.assertTrue(os.path.exists(migration_file))
  1091. with io.open(migration_file, "r", encoding="utf-8") as fp:
  1092. content = fp.read()
  1093. self.assertIn("# -*- coding: utf-8 -*-", content)
  1094. content = content.replace(" ", "")
  1095. return content
  1096. # generate an initial migration
  1097. migration_name_0001 = "my_initial_migration"
  1098. content = cmd("0001", migration_name_0001)
  1099. self.assertIn("dependencies=[\n]", content)
  1100. # Python 3 importlib caches os.listdir() on some platforms like
  1101. # Mac OS X (#23850).
  1102. if hasattr(importlib, 'invalidate_caches'):
  1103. importlib.invalidate_caches()
  1104. # generate an empty migration
  1105. migration_name_0002 = "my_custom_migration"
  1106. content = cmd("0002", migration_name_0002, "--empty")
  1107. self.assertIn("dependencies=[\n('migrations','0001_%s'),\n]" % migration_name_0001, content)
  1108. self.assertIn("operations=[\n]", content)
  1109. def test_makemigrations_check(self):
  1110. """
  1111. makemigrations --check should exit with a non-zero status when
  1112. there are changes to an app requiring migrations.
  1113. """
  1114. with self.temporary_migration_module():
  1115. with self.assertRaises(SystemExit):
  1116. call_command("makemigrations", "--check", "migrations", verbosity=0)
  1117. with self.temporary_migration_module(module="migrations.test_migrations_no_changes"):
  1118. call_command("makemigrations", "--check", "migrations", verbosity=0)
  1119. def test_makemigrations_migration_path_output(self):
  1120. """
  1121. makemigrations should print the relative paths to the migrations unless
  1122. they are outside of the current tree, in which case the absolute path
  1123. should be shown.
  1124. """
  1125. out = six.StringIO()
  1126. apps.register_model('migrations', UnicodeModel)
  1127. with self.temporary_migration_module() as migration_dir:
  1128. call_command("makemigrations", "migrations", stdout=out)
  1129. self.assertIn(os.path.join(migration_dir, '0001_initial.py'), out.getvalue())
  1130. def test_makemigrations_migration_path_output_valueerror(self):
  1131. """
  1132. makemigrations prints the absolute path if os.path.relpath() raises a
  1133. ValueError when it's impossible to obtain a relative path, e.g. on
  1134. Windows if Django is installed on a different drive than where the
  1135. migration files are created.
  1136. """
  1137. out = six.StringIO()
  1138. with self.temporary_migration_module() as migration_dir:
  1139. with mock.patch('os.path.relpath', side_effect=ValueError):
  1140. call_command('makemigrations', 'migrations', stdout=out)
  1141. self.assertIn(os.path.join(migration_dir, '0001_initial.py'), out.getvalue())
  1142. def test_makemigrations_inconsistent_history(self):
  1143. """
  1144. makemigrations should raise InconsistentMigrationHistory exception if
  1145. there are some migrations applied before their dependencies.
  1146. """
  1147. recorder = MigrationRecorder(connection)
  1148. recorder.record_applied('migrations', '0002_second')
  1149. msg = "Migration migrations.0002_second is applied before its dependency migrations.0001_initial"
  1150. with self.temporary_migration_module(module="migrations.test_migrations"):
  1151. with self.assertRaisesMessage(InconsistentMigrationHistory, msg):
  1152. call_command("makemigrations")
  1153. @mock.patch('django.db.migrations.questioner.input', return_value='1')
  1154. @mock.patch('django.db.migrations.questioner.sys.stdin', mock.MagicMock(encoding=sys.getdefaultencoding()))
  1155. def test_makemigrations_auto_now_add_interactive(self, *args):
  1156. """
  1157. makemigrations prompts the user when adding auto_now_add to an existing
  1158. model.
  1159. """
  1160. class Entry(models.Model):
  1161. title = models.CharField(max_length=255)
  1162. creation_date = models.DateTimeField(auto_now_add=True)
  1163. class Meta:
  1164. app_label = 'migrations'
  1165. # Monkeypatch interactive questioner to auto accept
  1166. with mock.patch('django.db.migrations.questioner.sys.stdout', new_callable=six.StringIO) as prompt_stdout:
  1167. out = six.StringIO()
  1168. with self.temporary_migration_module(module='migrations.test_auto_now_add'):
  1169. call_command('makemigrations', 'migrations', interactive=True, stdout=out)
  1170. output = out.getvalue()
  1171. prompt_output = prompt_stdout.getvalue()
  1172. self.assertIn("You can accept the default 'timezone.now' by pressing 'Enter'", prompt_output)
  1173. self.assertIn("Add field creation_date to entry", output)
  1174. class SquashMigrationsTests(MigrationTestBase):
  1175. """
  1176. Tests running the squashmigrations command.
  1177. """
  1178. def test_squashmigrations_squashes(self):
  1179. """
  1180. squashmigrations squashes migrations.
  1181. """
  1182. with self.temporary_migration_module(module="migrations.test_migrations") as migration_dir:
  1183. call_command("squashmigrations", "migrations", "0002", interactive=False, verbosity=0)
  1184. squashed_migration_file = os.path.join(migration_dir, "0001_squashed_0002_second.py")
  1185. self.assertTrue(os.path.exists(squashed_migration_file))
  1186. def test_squashmigrations_initial_attribute(self):
  1187. with self.temporary_migration_module(module="migrations.test_migrations") as migration_dir:
  1188. call_command("squashmigrations", "migrations", "0002", interactive=False, verbosity=0)
  1189. squashed_migration_file = os.path.join(migration_dir, "0001_squashed_0002_second.py")
  1190. with io.open(squashed_migration_file, "r", encoding="utf-8") as fp:
  1191. content = fp.read()
  1192. self.assertIn("initial = True", content)
  1193. def test_squashmigrations_optimizes(self):
  1194. """
  1195. squashmigrations optimizes operations.
  1196. """
  1197. out = six.StringIO()
  1198. with self.temporary_migration_module(module="migrations.test_migrations"):
  1199. call_command("squashmigrations", "migrations", "0002", interactive=False, verbosity=1, stdout=out)
  1200. self.assertIn("Optimized from 8 operations to 3 operations.", out.getvalue())
  1201. def test_ticket_23799_squashmigrations_no_optimize(self):
  1202. """
  1203. squashmigrations --no-optimize doesn't optimize operations.
  1204. """
  1205. out = six.StringIO()
  1206. with self.temporary_migration_module(module="migrations.test_migrations"):
  1207. call_command("squashmigrations", "migrations", "0002",
  1208. interactive=False, verbosity=1, no_optimize=True, stdout=out)
  1209. self.assertIn("Skipping optimization", out.getvalue())
  1210. def test_squashmigrations_valid_start(self):
  1211. """
  1212. squashmigrations accepts a starting migration.
  1213. """
  1214. out = six.StringIO()
  1215. with self.temporary_migration_module(module="migrations.test_migrations_no_changes") as migration_dir:
  1216. call_command("squashmigrations", "migrations", "0002", "0003",
  1217. interactive=False, verbosity=1, stdout=out)
  1218. squashed_migration_file = os.path.join(migration_dir, "0002_second_squashed_0003_third.py")
  1219. with io.open(squashed_migration_file, "r", encoding="utf-8") as fp:
  1220. content = fp.read()
  1221. self.assertIn(" ('migrations', '0001_initial')", content)
  1222. self.assertNotIn("initial = True", content)
  1223. out = out.getvalue()
  1224. self.assertNotIn(" - 0001_initial", out)
  1225. self.assertIn(" - 0002_second", out)
  1226. self.assertIn(" - 0003_third", out)
  1227. def test_squashmigrations_invalid_start(self):
  1228. """
  1229. squashmigrations doesn't accept a starting migration after the ending migration.
  1230. """
  1231. with self.temporary_migration_module(module="migrations.test_migrations_no_changes"):
  1232. msg = (
  1233. "The migration 'migrations.0003_third' cannot be found. Maybe "
  1234. "it comes after the migration 'migrations.0002_second'"
  1235. )
  1236. with self.assertRaisesMessage(CommandError, msg):
  1237. call_command("squashmigrations", "migrations", "0003", "0002", interactive=False, verbosity=0)