test_commands.py 138 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829283028312832283328342835283628372838283928402841284228432844284528462847284828492850285128522853285428552856285728582859286028612862286328642865286628672868286928702871287228732874287528762877287828792880288128822883288428852886288728882889289028912892289328942895289628972898289929002901290229032904290529062907290829092910291129122913291429152916291729182919292029212922292329242925292629272928292929302931293229332934293529362937293829392940294129422943294429452946294729482949295029512952295329542955295629572958295929602961296229632964296529662967296829692970297129722973297429752976297729782979298029812982298329842985298629872988298929902991299229932994299529962997299829993000300130023003300430053006300730083009301030113012301330143015301630173018301930203021302230233024302530263027302830293030303130323033303430353036303730383039304030413042304330443045304630473048304930503051305230533054305530563057305830593060306130623063306430653066306730683069307030713072307330743075307630773078307930803081308230833084308530863087308830893090309130923093309430953096309730983099310031013102310331043105310631073108310931103111311231133114311531163117311831193120312131223123312431253126312731283129313031313132313331343135313631373138313931403141314231433144314531463147314831493150315131523153315431553156315731583159316031613162316331643165316631673168316931703171317231733174317531763177317831793180318131823183318431853186318731883189319031913192319331943195319631973198319932003201320232033204320532063207320832093210321132123213321432153216321732183219322032213222322332243225322632273228322932303231323232333234323532363237323832393240324132423243324432453246324732483249325032513252325332543255325632573258325932603261326232633264326532663267326832693270327132723273327432753276327732783279328032813282328332843285328632873288328932903291329232933294329532963297329832993300330133023303330433053306330733083309331033113312331333143315331633173318331933203321332233233324332533263327332833293330333133323333333433353336333733383339334033413342334333443345334633473348334933503351335233533354335533563357335833593360336133623363336433653366336733683369337033713372337333743375337633773378337933803381338233833384338533863387338833893390339133923393339433953396339733983399340034013402340334043405340634073408340934103411341234133414341534163417341834193420342134223423342434253426342734283429343034313432343334343435343634373438
  1. import datetime
  2. import importlib
  3. import io
  4. import os
  5. import shutil
  6. import sys
  7. from pathlib import Path
  8. from unittest import mock
  9. from django.apps import apps
  10. from django.core.checks import Error, Tags, register
  11. from django.core.checks.registry import registry
  12. from django.core.management import CommandError, call_command
  13. from django.core.management.base import SystemCheckError
  14. from django.core.management.commands.makemigrations import (
  15. Command as MakeMigrationsCommand,
  16. )
  17. from django.core.management.commands.migrate import Command as MigrateCommand
  18. from django.db import (
  19. ConnectionHandler,
  20. DatabaseError,
  21. OperationalError,
  22. connection,
  23. connections,
  24. models,
  25. )
  26. from django.db.backends.base.schema import BaseDatabaseSchemaEditor
  27. from django.db.backends.utils import truncate_name
  28. from django.db.migrations.autodetector import MigrationAutodetector
  29. from django.db.migrations.exceptions import InconsistentMigrationHistory
  30. from django.db.migrations.recorder import MigrationRecorder
  31. from django.test import TestCase, override_settings, skipUnlessDBFeature
  32. from django.test.utils import captured_stdout, extend_sys_path, isolate_apps
  33. from django.utils import timezone
  34. from django.utils.version import get_docs_version
  35. from .models import UnicodeModel, UnserializableModel
  36. from .routers import TestRouter
  37. from .test_base import MigrationTestBase
  38. HAS_BLACK = shutil.which("black")
  39. class MigrateTests(MigrationTestBase):
  40. """
  41. Tests running the migrate command.
  42. """
  43. databases = {"default", "other"}
  44. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  45. def test_migrate(self):
  46. """
  47. Tests basic usage of the migrate command.
  48. """
  49. # No tables are created
  50. self.assertTableNotExists("migrations_author")
  51. self.assertTableNotExists("migrations_tribble")
  52. self.assertTableNotExists("migrations_book")
  53. # Run the migrations to 0001 only
  54. stdout = io.StringIO()
  55. call_command(
  56. "migrate", "migrations", "0001", verbosity=2, stdout=stdout, no_color=True
  57. )
  58. stdout = stdout.getvalue()
  59. self.assertIn(
  60. "Target specific migration: 0001_initial, from migrations", stdout
  61. )
  62. self.assertIn("Applying migrations.0001_initial... OK", stdout)
  63. self.assertIn("Running pre-migrate handlers for application migrations", stdout)
  64. self.assertIn(
  65. "Running post-migrate handlers for application migrations", stdout
  66. )
  67. # The correct tables exist
  68. self.assertTableExists("migrations_author")
  69. self.assertTableExists("migrations_tribble")
  70. self.assertTableNotExists("migrations_book")
  71. # Run migrations all the way
  72. call_command("migrate", verbosity=0)
  73. # The correct tables exist
  74. self.assertTableExists("migrations_author")
  75. self.assertTableNotExists("migrations_tribble")
  76. self.assertTableExists("migrations_book")
  77. # Unmigrate everything
  78. stdout = io.StringIO()
  79. call_command(
  80. "migrate", "migrations", "zero", verbosity=2, stdout=stdout, no_color=True
  81. )
  82. stdout = stdout.getvalue()
  83. self.assertIn("Unapply all migrations: migrations", stdout)
  84. self.assertIn("Unapplying migrations.0002_second... OK", stdout)
  85. self.assertIn("Running pre-migrate handlers for application migrations", stdout)
  86. self.assertIn(
  87. "Running post-migrate handlers for application migrations", stdout
  88. )
  89. # Tables are gone
  90. self.assertTableNotExists("migrations_author")
  91. self.assertTableNotExists("migrations_tribble")
  92. self.assertTableNotExists("migrations_book")
  93. @mock.patch("django.core.management.base.BaseCommand.check")
  94. @override_settings(
  95. INSTALLED_APPS=[
  96. "django.contrib.auth",
  97. "django.contrib.contenttypes",
  98. "migrations.migrations_test_apps.migrated_app",
  99. ]
  100. )
  101. def test_migrate_with_system_checks(self, mocked_check):
  102. out = io.StringIO()
  103. call_command("migrate", skip_checks=False, no_color=True, stdout=out)
  104. self.assertIn("Apply all migrations: migrated_app", out.getvalue())
  105. mocked_check.assert_called_once()
  106. def test_migrate_with_custom_system_checks(self):
  107. original_checks = registry.registered_checks.copy()
  108. @register(Tags.signals)
  109. def my_check(app_configs, **kwargs):
  110. return [Error("my error")]
  111. self.addCleanup(setattr, registry, "registered_checks", original_checks)
  112. class CustomMigrateCommandWithSignalsChecks(MigrateCommand):
  113. requires_system_checks = [Tags.signals]
  114. command = CustomMigrateCommandWithSignalsChecks()
  115. with self.assertRaises(SystemCheckError):
  116. call_command(command, skip_checks=False, stderr=io.StringIO())
  117. class CustomMigrateCommandWithSecurityChecks(MigrateCommand):
  118. requires_system_checks = [Tags.security]
  119. command = CustomMigrateCommandWithSecurityChecks()
  120. call_command(command, skip_checks=False, stdout=io.StringIO())
  121. @override_settings(
  122. INSTALLED_APPS=[
  123. "migrations",
  124. "migrations.migrations_test_apps.unmigrated_app_syncdb",
  125. ]
  126. )
  127. def test_app_without_migrations(self):
  128. msg = "App 'unmigrated_app_syncdb' does not have migrations."
  129. with self.assertRaisesMessage(CommandError, msg):
  130. call_command("migrate", app_label="unmigrated_app_syncdb")
  131. @override_settings(
  132. MIGRATION_MODULES={"migrations": "migrations.test_migrations_clashing_prefix"}
  133. )
  134. def test_ambiguous_prefix(self):
  135. msg = (
  136. "More than one migration matches 'a' in app 'migrations'. Please "
  137. "be more specific."
  138. )
  139. with self.assertRaisesMessage(CommandError, msg):
  140. call_command("migrate", app_label="migrations", migration_name="a")
  141. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  142. def test_unknown_prefix(self):
  143. msg = "Cannot find a migration matching 'nonexistent' from app 'migrations'."
  144. with self.assertRaisesMessage(CommandError, msg):
  145. call_command(
  146. "migrate", app_label="migrations", migration_name="nonexistent"
  147. )
  148. @override_settings(
  149. MIGRATION_MODULES={"migrations": "migrations.test_migrations_initial_false"}
  150. )
  151. def test_migrate_initial_false(self):
  152. """
  153. `Migration.initial = False` skips fake-initial detection.
  154. """
  155. # Make sure no tables are created
  156. self.assertTableNotExists("migrations_author")
  157. self.assertTableNotExists("migrations_tribble")
  158. # Run the migrations to 0001 only
  159. call_command("migrate", "migrations", "0001", verbosity=0)
  160. # Fake rollback
  161. call_command("migrate", "migrations", "zero", fake=True, verbosity=0)
  162. # Make sure fake-initial detection does not run
  163. with self.assertRaises(DatabaseError):
  164. call_command(
  165. "migrate", "migrations", "0001", fake_initial=True, verbosity=0
  166. )
  167. call_command("migrate", "migrations", "0001", fake=True, verbosity=0)
  168. # Real rollback
  169. call_command("migrate", "migrations", "zero", verbosity=0)
  170. # Make sure it's all gone
  171. self.assertTableNotExists("migrations_author")
  172. self.assertTableNotExists("migrations_tribble")
  173. self.assertTableNotExists("migrations_book")
  174. @override_settings(
  175. MIGRATION_MODULES={"migrations": "migrations.test_migrations"},
  176. DATABASE_ROUTERS=["migrations.routers.TestRouter"],
  177. )
  178. def test_migrate_fake_initial(self):
  179. """
  180. --fake-initial only works if all tables created in the initial
  181. migration of an app exists. Database routers must be obeyed when doing
  182. that check.
  183. """
  184. # Make sure no tables are created
  185. for db in self.databases:
  186. self.assertTableNotExists("migrations_author", using=db)
  187. self.assertTableNotExists("migrations_tribble", using=db)
  188. try:
  189. # Run the migrations to 0001 only
  190. call_command("migrate", "migrations", "0001", verbosity=0)
  191. call_command("migrate", "migrations", "0001", verbosity=0, database="other")
  192. # Make sure the right tables exist
  193. self.assertTableExists("migrations_author")
  194. self.assertTableNotExists("migrations_tribble")
  195. # Also check the "other" database
  196. self.assertTableNotExists("migrations_author", using="other")
  197. self.assertTableExists("migrations_tribble", using="other")
  198. # Fake a roll-back
  199. call_command("migrate", "migrations", "zero", fake=True, verbosity=0)
  200. call_command(
  201. "migrate",
  202. "migrations",
  203. "zero",
  204. fake=True,
  205. verbosity=0,
  206. database="other",
  207. )
  208. # Make sure the tables still exist
  209. self.assertTableExists("migrations_author")
  210. self.assertTableExists("migrations_tribble", using="other")
  211. # Try to run initial migration
  212. with self.assertRaises(DatabaseError):
  213. call_command("migrate", "migrations", "0001", verbosity=0)
  214. # Run initial migration with an explicit --fake-initial
  215. out = io.StringIO()
  216. with mock.patch(
  217. "django.core.management.color.supports_color", lambda *args: False
  218. ):
  219. call_command(
  220. "migrate",
  221. "migrations",
  222. "0001",
  223. fake_initial=True,
  224. stdout=out,
  225. verbosity=1,
  226. )
  227. call_command(
  228. "migrate",
  229. "migrations",
  230. "0001",
  231. fake_initial=True,
  232. verbosity=0,
  233. database="other",
  234. )
  235. self.assertIn("migrations.0001_initial... faked", out.getvalue().lower())
  236. # Run migrations all the way.
  237. call_command("migrate", verbosity=0)
  238. call_command("migrate", verbosity=0, database="other")
  239. self.assertTableExists("migrations_author")
  240. self.assertTableNotExists("migrations_tribble")
  241. self.assertTableExists("migrations_book")
  242. self.assertTableNotExists("migrations_author", using="other")
  243. self.assertTableNotExists("migrations_tribble", using="other")
  244. self.assertTableNotExists("migrations_book", using="other")
  245. # Fake a roll-back.
  246. call_command("migrate", "migrations", "zero", fake=True, verbosity=0)
  247. call_command(
  248. "migrate",
  249. "migrations",
  250. "zero",
  251. fake=True,
  252. verbosity=0,
  253. database="other",
  254. )
  255. self.assertTableExists("migrations_author")
  256. self.assertTableNotExists("migrations_tribble")
  257. self.assertTableExists("migrations_book")
  258. # Run initial migration.
  259. with self.assertRaises(DatabaseError):
  260. call_command("migrate", "migrations", verbosity=0)
  261. # Run initial migration with an explicit --fake-initial.
  262. with self.assertRaises(DatabaseError):
  263. # Fails because "migrations_tribble" does not exist but needs
  264. # to in order to make --fake-initial work.
  265. call_command("migrate", "migrations", fake_initial=True, verbosity=0)
  266. # Fake an apply.
  267. call_command("migrate", "migrations", fake=True, verbosity=0)
  268. call_command(
  269. "migrate", "migrations", fake=True, verbosity=0, database="other"
  270. )
  271. finally:
  272. # Unmigrate everything.
  273. call_command("migrate", "migrations", "zero", verbosity=0)
  274. call_command("migrate", "migrations", "zero", verbosity=0, database="other")
  275. # Make sure it's all gone
  276. for db in self.databases:
  277. self.assertTableNotExists("migrations_author", using=db)
  278. self.assertTableNotExists("migrations_tribble", using=db)
  279. self.assertTableNotExists("migrations_book", using=db)
  280. @skipUnlessDBFeature("ignores_table_name_case")
  281. def test_migrate_fake_initial_case_insensitive(self):
  282. with override_settings(
  283. MIGRATION_MODULES={
  284. "migrations": "migrations.test_fake_initial_case_insensitive.initial",
  285. }
  286. ):
  287. call_command("migrate", "migrations", "0001", verbosity=0)
  288. call_command("migrate", "migrations", "zero", fake=True, verbosity=0)
  289. with override_settings(
  290. MIGRATION_MODULES={
  291. "migrations": (
  292. "migrations.test_fake_initial_case_insensitive.fake_initial"
  293. ),
  294. }
  295. ):
  296. out = io.StringIO()
  297. call_command(
  298. "migrate",
  299. "migrations",
  300. "0001",
  301. fake_initial=True,
  302. stdout=out,
  303. verbosity=1,
  304. no_color=True,
  305. )
  306. self.assertIn(
  307. "migrations.0001_initial... faked",
  308. out.getvalue().lower(),
  309. )
  310. @override_settings(
  311. MIGRATION_MODULES={
  312. "migrations": "migrations.test_migrations_fake_split_initial"
  313. }
  314. )
  315. def test_migrate_fake_split_initial(self):
  316. """
  317. Split initial migrations can be faked with --fake-initial.
  318. """
  319. try:
  320. call_command("migrate", "migrations", "0002", verbosity=0)
  321. call_command("migrate", "migrations", "zero", fake=True, verbosity=0)
  322. out = io.StringIO()
  323. with mock.patch(
  324. "django.core.management.color.supports_color", lambda *args: False
  325. ):
  326. call_command(
  327. "migrate",
  328. "migrations",
  329. "0002",
  330. fake_initial=True,
  331. stdout=out,
  332. verbosity=1,
  333. )
  334. value = out.getvalue().lower()
  335. self.assertIn("migrations.0001_initial... faked", value)
  336. self.assertIn("migrations.0002_second... faked", value)
  337. finally:
  338. # Fake an apply.
  339. call_command("migrate", "migrations", fake=True, verbosity=0)
  340. # Unmigrate everything.
  341. call_command("migrate", "migrations", "zero", verbosity=0)
  342. @override_settings(
  343. MIGRATION_MODULES={"migrations": "migrations.test_migrations_conflict"}
  344. )
  345. def test_migrate_conflict_exit(self):
  346. """
  347. migrate exits if it detects a conflict.
  348. """
  349. msg = (
  350. "Conflicting migrations detected; multiple leaf nodes in the "
  351. "migration graph: (0002_conflicting_second, 0002_second in "
  352. "migrations).\n"
  353. "To fix them run 'python manage.py makemigrations --merge'"
  354. )
  355. with self.assertRaisesMessage(CommandError, msg):
  356. call_command("migrate", "migrations")
  357. @override_settings(
  358. MIGRATION_MODULES={
  359. "migrations": "migrations.test_migrations",
  360. }
  361. )
  362. def test_migrate_check(self):
  363. with self.assertRaises(SystemExit):
  364. call_command("migrate", "migrations", "0001", check_unapplied=True)
  365. self.assertTableNotExists("migrations_author")
  366. self.assertTableNotExists("migrations_tribble")
  367. self.assertTableNotExists("migrations_book")
  368. @override_settings(
  369. INSTALLED_APPS=[
  370. "migrations.migrations_test_apps.migrated_app",
  371. ]
  372. )
  373. def test_migrate_check_migrated_app(self):
  374. out = io.StringIO()
  375. try:
  376. call_command("migrate", "migrated_app", verbosity=0)
  377. call_command(
  378. "migrate",
  379. "migrated_app",
  380. stdout=out,
  381. check_unapplied=True,
  382. )
  383. self.assertEqual(out.getvalue(), "")
  384. finally:
  385. # Unmigrate everything.
  386. call_command("migrate", "migrated_app", "zero", verbosity=0)
  387. @override_settings(
  388. MIGRATION_MODULES={
  389. "migrations": "migrations.test_migrations_plan",
  390. }
  391. )
  392. def test_migrate_check_plan(self):
  393. out = io.StringIO()
  394. with self.assertRaises(SystemExit):
  395. call_command(
  396. "migrate",
  397. "migrations",
  398. "0001",
  399. check_unapplied=True,
  400. plan=True,
  401. stdout=out,
  402. no_color=True,
  403. )
  404. self.assertEqual(
  405. "Planned operations:\n"
  406. "migrations.0001_initial\n"
  407. " Create model Salamander\n"
  408. " Raw Python operation -> Grow salamander tail.\n",
  409. out.getvalue(),
  410. )
  411. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  412. def test_showmigrations_list(self):
  413. """
  414. showmigrations --list displays migrations and whether or not they're
  415. applied.
  416. """
  417. out = io.StringIO()
  418. with mock.patch(
  419. "django.core.management.color.supports_color", lambda *args: True
  420. ):
  421. call_command(
  422. "showmigrations", format="list", stdout=out, verbosity=0, no_color=False
  423. )
  424. self.assertEqual(
  425. "\x1b[1mmigrations\n\x1b[0m [ ] 0001_initial\n [ ] 0002_second\n",
  426. out.getvalue().lower(),
  427. )
  428. call_command("migrate", "migrations", "0001", verbosity=0)
  429. out = io.StringIO()
  430. # Giving the explicit app_label tests for selective `show_list` in the command
  431. call_command(
  432. "showmigrations",
  433. "migrations",
  434. format="list",
  435. stdout=out,
  436. verbosity=0,
  437. no_color=True,
  438. )
  439. self.assertEqual(
  440. "migrations\n [x] 0001_initial\n [ ] 0002_second\n", out.getvalue().lower()
  441. )
  442. out = io.StringIO()
  443. # Applied datetimes are displayed at verbosity 2+.
  444. call_command(
  445. "showmigrations", "migrations", stdout=out, verbosity=2, no_color=True
  446. )
  447. migration1 = MigrationRecorder(connection).migration_qs.get(
  448. app="migrations", name="0001_initial"
  449. )
  450. self.assertEqual(
  451. "migrations\n"
  452. " [x] 0001_initial (applied at %s)\n"
  453. " [ ] 0002_second\n" % migration1.applied.strftime("%Y-%m-%d %H:%M:%S"),
  454. out.getvalue().lower(),
  455. )
  456. # Cleanup by unmigrating everything
  457. call_command("migrate", "migrations", "zero", verbosity=0)
  458. @override_settings(
  459. MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"}
  460. )
  461. def test_showmigrations_list_squashed(self):
  462. out = io.StringIO()
  463. call_command(
  464. "showmigrations", format="list", stdout=out, verbosity=2, no_color=True
  465. )
  466. self.assertEqual(
  467. "migrations\n [ ] 0001_squashed_0002 (2 squashed migrations)\n",
  468. out.getvalue().lower(),
  469. )
  470. out = io.StringIO()
  471. call_command(
  472. "migrate",
  473. "migrations",
  474. "0001_squashed_0002",
  475. stdout=out,
  476. verbosity=2,
  477. no_color=True,
  478. )
  479. try:
  480. self.assertIn(
  481. "operations to perform:\n"
  482. " target specific migration: 0001_squashed_0002, from migrations\n"
  483. "running pre-migrate handlers for application migrations\n"
  484. "running migrations:\n"
  485. " applying migrations.0001_squashed_0002... ok (",
  486. out.getvalue().lower(),
  487. )
  488. out = io.StringIO()
  489. call_command(
  490. "showmigrations", format="list", stdout=out, verbosity=2, no_color=True
  491. )
  492. self.assertEqual(
  493. "migrations\n [x] 0001_squashed_0002 (2 squashed migrations)\n",
  494. out.getvalue().lower(),
  495. )
  496. finally:
  497. # Unmigrate everything.
  498. call_command("migrate", "migrations", "zero", verbosity=0)
  499. @override_settings(
  500. MIGRATION_MODULES={"migrations": "migrations.test_migrations_run_before"}
  501. )
  502. def test_showmigrations_plan(self):
  503. """
  504. Tests --plan output of showmigrations command
  505. """
  506. out = io.StringIO()
  507. call_command("showmigrations", format="plan", stdout=out)
  508. self.assertEqual(
  509. "[ ] migrations.0001_initial\n"
  510. "[ ] migrations.0003_third\n"
  511. "[ ] migrations.0002_second\n",
  512. out.getvalue().lower(),
  513. )
  514. out = io.StringIO()
  515. call_command("showmigrations", format="plan", stdout=out, verbosity=2)
  516. self.assertEqual(
  517. "[ ] migrations.0001_initial\n"
  518. "[ ] migrations.0003_third ... (migrations.0001_initial)\n"
  519. "[ ] migrations.0002_second ... (migrations.0001_initial, "
  520. "migrations.0003_third)\n",
  521. out.getvalue().lower(),
  522. )
  523. call_command("migrate", "migrations", "0003", verbosity=0)
  524. out = io.StringIO()
  525. call_command("showmigrations", format="plan", stdout=out)
  526. self.assertEqual(
  527. "[x] migrations.0001_initial\n"
  528. "[x] migrations.0003_third\n"
  529. "[ ] migrations.0002_second\n",
  530. out.getvalue().lower(),
  531. )
  532. out = io.StringIO()
  533. call_command("showmigrations", format="plan", stdout=out, verbosity=2)
  534. self.assertEqual(
  535. "[x] migrations.0001_initial\n"
  536. "[x] migrations.0003_third ... (migrations.0001_initial)\n"
  537. "[ ] migrations.0002_second ... (migrations.0001_initial, "
  538. "migrations.0003_third)\n",
  539. out.getvalue().lower(),
  540. )
  541. # Cleanup by unmigrating everything
  542. call_command("migrate", "migrations", "zero", verbosity=0)
  543. @override_settings(
  544. MIGRATION_MODULES={"migrations": "migrations.test_migrations_plan"}
  545. )
  546. def test_migrate_plan(self):
  547. """Tests migrate --plan output."""
  548. out = io.StringIO()
  549. # Show the plan up to the third migration.
  550. call_command(
  551. "migrate", "migrations", "0003", plan=True, stdout=out, no_color=True
  552. )
  553. self.assertEqual(
  554. "Planned operations:\n"
  555. "migrations.0001_initial\n"
  556. " Create model Salamander\n"
  557. " Raw Python operation -> Grow salamander tail.\n"
  558. "migrations.0002_second\n"
  559. " Create model Book\n"
  560. " Raw SQL operation -> ['SELECT * FROM migrations_book']\n"
  561. "migrations.0003_third\n"
  562. " Create model Author\n"
  563. " Raw SQL operation -> ['SELECT * FROM migrations_author']\n",
  564. out.getvalue(),
  565. )
  566. try:
  567. # Migrate to the third migration.
  568. call_command("migrate", "migrations", "0003", verbosity=0)
  569. out = io.StringIO()
  570. # Show the plan for when there is nothing to apply.
  571. call_command(
  572. "migrate", "migrations", "0003", plan=True, stdout=out, no_color=True
  573. )
  574. self.assertEqual(
  575. "Planned operations:\n No planned migration operations.\n",
  576. out.getvalue(),
  577. )
  578. out = io.StringIO()
  579. # Show the plan for reverse migration back to 0001.
  580. call_command(
  581. "migrate", "migrations", "0001", plan=True, stdout=out, no_color=True
  582. )
  583. self.assertEqual(
  584. "Planned operations:\n"
  585. "migrations.0003_third\n"
  586. " Undo Create model Author\n"
  587. " Raw SQL operation -> ['SELECT * FROM migrations_book']\n"
  588. "migrations.0002_second\n"
  589. " Undo Create model Book\n"
  590. " Raw SQL operation -> ['SELECT * FROM migrations_salamand…\n",
  591. out.getvalue(),
  592. )
  593. out = io.StringIO()
  594. # Show the migration plan to fourth, with truncated details.
  595. call_command(
  596. "migrate", "migrations", "0004", plan=True, stdout=out, no_color=True
  597. )
  598. self.assertEqual(
  599. "Planned operations:\n"
  600. "migrations.0004_fourth\n"
  601. " Raw SQL operation -> SELECT * FROM migrations_author WHE…\n",
  602. out.getvalue(),
  603. )
  604. # Show the plan when an operation is irreversible.
  605. # Migrate to the fourth migration.
  606. call_command("migrate", "migrations", "0004", verbosity=0)
  607. out = io.StringIO()
  608. call_command(
  609. "migrate", "migrations", "0003", plan=True, stdout=out, no_color=True
  610. )
  611. self.assertEqual(
  612. "Planned operations:\n"
  613. "migrations.0004_fourth\n"
  614. " Raw SQL operation -> IRREVERSIBLE\n",
  615. out.getvalue(),
  616. )
  617. out = io.StringIO()
  618. call_command(
  619. "migrate", "migrations", "0005", plan=True, stdout=out, no_color=True
  620. )
  621. # Operation is marked as irreversible only in the revert plan.
  622. self.assertEqual(
  623. "Planned operations:\n"
  624. "migrations.0005_fifth\n"
  625. " Raw Python operation\n"
  626. " Raw Python operation\n"
  627. " Raw Python operation -> Feed salamander.\n",
  628. out.getvalue(),
  629. )
  630. call_command("migrate", "migrations", "0005", verbosity=0)
  631. out = io.StringIO()
  632. call_command(
  633. "migrate", "migrations", "0004", plan=True, stdout=out, no_color=True
  634. )
  635. self.assertEqual(
  636. "Planned operations:\n"
  637. "migrations.0005_fifth\n"
  638. " Raw Python operation -> IRREVERSIBLE\n"
  639. " Raw Python operation -> IRREVERSIBLE\n"
  640. " Raw Python operation\n",
  641. out.getvalue(),
  642. )
  643. finally:
  644. # Cleanup by unmigrating everything: fake the irreversible, then
  645. # migrate all to zero.
  646. call_command("migrate", "migrations", "0003", fake=True, verbosity=0)
  647. call_command("migrate", "migrations", "zero", verbosity=0)
  648. @override_settings(
  649. MIGRATION_MODULES={"migrations": "migrations.test_migrations_empty"}
  650. )
  651. def test_showmigrations_no_migrations(self):
  652. out = io.StringIO()
  653. call_command("showmigrations", stdout=out, no_color=True)
  654. self.assertEqual("migrations\n (no migrations)\n", out.getvalue().lower())
  655. @override_settings(
  656. INSTALLED_APPS=["migrations.migrations_test_apps.unmigrated_app"]
  657. )
  658. def test_showmigrations_unmigrated_app(self):
  659. out = io.StringIO()
  660. call_command("showmigrations", "unmigrated_app", stdout=out, no_color=True)
  661. try:
  662. self.assertEqual(
  663. "unmigrated_app\n (no migrations)\n", out.getvalue().lower()
  664. )
  665. finally:
  666. # unmigrated_app.SillyModel has a foreign key to
  667. # 'migrations.Tribble', but that model is only defined in a
  668. # migration, so the global app registry never sees it and the
  669. # reference is left dangling. Remove it to avoid problems in
  670. # subsequent tests.
  671. apps._pending_operations.pop(("migrations", "tribble"), None)
  672. @override_settings(
  673. MIGRATION_MODULES={"migrations": "migrations.test_migrations_empty"}
  674. )
  675. def test_showmigrations_plan_no_migrations(self):
  676. """
  677. Tests --plan output of showmigrations command without migrations
  678. """
  679. out = io.StringIO()
  680. call_command("showmigrations", format="plan", stdout=out, no_color=True)
  681. self.assertEqual("(no migrations)\n", out.getvalue().lower())
  682. out = io.StringIO()
  683. call_command(
  684. "showmigrations", format="plan", stdout=out, verbosity=2, no_color=True
  685. )
  686. self.assertEqual("(no migrations)\n", out.getvalue().lower())
  687. @override_settings(
  688. MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed_complex"}
  689. )
  690. def test_showmigrations_plan_squashed(self):
  691. """
  692. Tests --plan output of showmigrations command with squashed migrations.
  693. """
  694. out = io.StringIO()
  695. call_command("showmigrations", format="plan", stdout=out)
  696. self.assertEqual(
  697. "[ ] migrations.1_auto\n"
  698. "[ ] migrations.2_auto\n"
  699. "[ ] migrations.3_squashed_5\n"
  700. "[ ] migrations.6_auto\n"
  701. "[ ] migrations.7_auto\n",
  702. out.getvalue().lower(),
  703. )
  704. out = io.StringIO()
  705. call_command("showmigrations", format="plan", stdout=out, verbosity=2)
  706. self.assertEqual(
  707. "[ ] migrations.1_auto\n"
  708. "[ ] migrations.2_auto ... (migrations.1_auto)\n"
  709. "[ ] migrations.3_squashed_5 ... (migrations.2_auto)\n"
  710. "[ ] migrations.6_auto ... (migrations.3_squashed_5)\n"
  711. "[ ] migrations.7_auto ... (migrations.6_auto)\n",
  712. out.getvalue().lower(),
  713. )
  714. call_command("migrate", "migrations", "3_squashed_5", verbosity=0)
  715. out = io.StringIO()
  716. call_command("showmigrations", format="plan", stdout=out)
  717. self.assertEqual(
  718. "[x] migrations.1_auto\n"
  719. "[x] migrations.2_auto\n"
  720. "[x] migrations.3_squashed_5\n"
  721. "[ ] migrations.6_auto\n"
  722. "[ ] migrations.7_auto\n",
  723. out.getvalue().lower(),
  724. )
  725. out = io.StringIO()
  726. call_command("showmigrations", format="plan", stdout=out, verbosity=2)
  727. self.assertEqual(
  728. "[x] migrations.1_auto\n"
  729. "[x] migrations.2_auto ... (migrations.1_auto)\n"
  730. "[x] migrations.3_squashed_5 ... (migrations.2_auto)\n"
  731. "[ ] migrations.6_auto ... (migrations.3_squashed_5)\n"
  732. "[ ] migrations.7_auto ... (migrations.6_auto)\n",
  733. out.getvalue().lower(),
  734. )
  735. @override_settings(
  736. INSTALLED_APPS=[
  737. "migrations.migrations_test_apps.mutate_state_b",
  738. "migrations.migrations_test_apps.alter_fk.author_app",
  739. "migrations.migrations_test_apps.alter_fk.book_app",
  740. ]
  741. )
  742. def test_showmigrations_plan_single_app_label(self):
  743. """
  744. `showmigrations --plan app_label` output with a single app_label.
  745. """
  746. # Single app with no dependencies on other apps.
  747. out = io.StringIO()
  748. call_command("showmigrations", "mutate_state_b", format="plan", stdout=out)
  749. self.assertEqual(
  750. "[ ] mutate_state_b.0001_initial\n[ ] mutate_state_b.0002_add_field\n",
  751. out.getvalue(),
  752. )
  753. # Single app with dependencies.
  754. out = io.StringIO()
  755. call_command("showmigrations", "author_app", format="plan", stdout=out)
  756. self.assertEqual(
  757. "[ ] author_app.0001_initial\n"
  758. "[ ] book_app.0001_initial\n"
  759. "[ ] author_app.0002_alter_id\n",
  760. out.getvalue(),
  761. )
  762. # Some migrations already applied.
  763. call_command("migrate", "author_app", "0001", verbosity=0)
  764. out = io.StringIO()
  765. call_command("showmigrations", "author_app", format="plan", stdout=out)
  766. self.assertEqual(
  767. "[X] author_app.0001_initial\n"
  768. "[ ] book_app.0001_initial\n"
  769. "[ ] author_app.0002_alter_id\n",
  770. out.getvalue(),
  771. )
  772. # Cleanup by unmigrating author_app.
  773. call_command("migrate", "author_app", "zero", verbosity=0)
  774. @override_settings(
  775. INSTALLED_APPS=[
  776. "migrations.migrations_test_apps.mutate_state_b",
  777. "migrations.migrations_test_apps.alter_fk.author_app",
  778. "migrations.migrations_test_apps.alter_fk.book_app",
  779. ]
  780. )
  781. def test_showmigrations_plan_multiple_app_labels(self):
  782. """
  783. `showmigrations --plan app_label` output with multiple app_labels.
  784. """
  785. # Multiple apps: author_app depends on book_app; mutate_state_b doesn't
  786. # depend on other apps.
  787. out = io.StringIO()
  788. call_command(
  789. "showmigrations", "mutate_state_b", "author_app", format="plan", stdout=out
  790. )
  791. self.assertEqual(
  792. "[ ] author_app.0001_initial\n"
  793. "[ ] book_app.0001_initial\n"
  794. "[ ] author_app.0002_alter_id\n"
  795. "[ ] mutate_state_b.0001_initial\n"
  796. "[ ] mutate_state_b.0002_add_field\n",
  797. out.getvalue(),
  798. )
  799. # Multiple apps: args order shouldn't matter (the same result is
  800. # expected as above).
  801. out = io.StringIO()
  802. call_command(
  803. "showmigrations", "author_app", "mutate_state_b", format="plan", stdout=out
  804. )
  805. self.assertEqual(
  806. "[ ] author_app.0001_initial\n"
  807. "[ ] book_app.0001_initial\n"
  808. "[ ] author_app.0002_alter_id\n"
  809. "[ ] mutate_state_b.0001_initial\n"
  810. "[ ] mutate_state_b.0002_add_field\n",
  811. out.getvalue(),
  812. )
  813. @override_settings(
  814. INSTALLED_APPS=["migrations.migrations_test_apps.unmigrated_app"]
  815. )
  816. def test_showmigrations_plan_app_label_no_migrations(self):
  817. out = io.StringIO()
  818. call_command(
  819. "showmigrations", "unmigrated_app", format="plan", stdout=out, no_color=True
  820. )
  821. try:
  822. self.assertEqual("(no migrations)\n", out.getvalue())
  823. finally:
  824. # unmigrated_app.SillyModel has a foreign key to
  825. # 'migrations.Tribble', but that model is only defined in a
  826. # migration, so the global app registry never sees it and the
  827. # reference is left dangling. Remove it to avoid problems in
  828. # subsequent tests.
  829. apps._pending_operations.pop(("migrations", "tribble"), None)
  830. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  831. def test_sqlmigrate_forwards(self):
  832. """
  833. sqlmigrate outputs forward looking SQL.
  834. """
  835. out = io.StringIO()
  836. call_command("sqlmigrate", "migrations", "0001", stdout=out, no_color=True)
  837. lines = out.getvalue().splitlines()
  838. if connection.features.can_rollback_ddl:
  839. self.assertEqual(lines[0], connection.ops.start_transaction_sql())
  840. self.assertEqual(lines[-1], connection.ops.end_transaction_sql())
  841. lines = lines[1:-1]
  842. self.assertEqual(
  843. lines[:3],
  844. [
  845. "--",
  846. "-- Create model Author",
  847. "--",
  848. ],
  849. )
  850. self.assertIn(
  851. "create table %s" % connection.ops.quote_name("migrations_author").lower(),
  852. lines[3].lower(),
  853. )
  854. pos = lines.index("--", 3)
  855. self.assertEqual(
  856. lines[pos : pos + 3],
  857. [
  858. "--",
  859. "-- Create model Tribble",
  860. "--",
  861. ],
  862. )
  863. self.assertIn(
  864. "create table %s" % connection.ops.quote_name("migrations_tribble").lower(),
  865. lines[pos + 3].lower(),
  866. )
  867. pos = lines.index("--", pos + 3)
  868. self.assertEqual(
  869. lines[pos : pos + 3],
  870. [
  871. "--",
  872. "-- Add field bool to tribble",
  873. "--",
  874. ],
  875. )
  876. pos = lines.index("--", pos + 3)
  877. self.assertEqual(
  878. lines[pos : pos + 3],
  879. [
  880. "--",
  881. "-- Alter unique_together for author (1 constraint(s))",
  882. "--",
  883. ],
  884. )
  885. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  886. def test_sqlmigrate_backwards(self):
  887. """
  888. sqlmigrate outputs reverse looking SQL.
  889. """
  890. # Cannot generate the reverse SQL unless we've applied the migration.
  891. call_command("migrate", "migrations", verbosity=0)
  892. out = io.StringIO()
  893. call_command(
  894. "sqlmigrate",
  895. "migrations",
  896. "0001",
  897. stdout=out,
  898. backwards=True,
  899. no_color=True,
  900. )
  901. lines = out.getvalue().splitlines()
  902. try:
  903. if connection.features.can_rollback_ddl:
  904. self.assertEqual(lines[0], connection.ops.start_transaction_sql())
  905. self.assertEqual(lines[-1], connection.ops.end_transaction_sql())
  906. lines = lines[1:-1]
  907. self.assertEqual(
  908. lines[:3],
  909. [
  910. "--",
  911. "-- Alter unique_together for author (1 constraint(s))",
  912. "--",
  913. ],
  914. )
  915. pos = lines.index("--", 3)
  916. self.assertEqual(
  917. lines[pos : pos + 3],
  918. [
  919. "--",
  920. "-- Add field bool to tribble",
  921. "--",
  922. ],
  923. )
  924. pos = lines.index("--", pos + 3)
  925. self.assertEqual(
  926. lines[pos : pos + 3],
  927. [
  928. "--",
  929. "-- Create model Tribble",
  930. "--",
  931. ],
  932. )
  933. next_pos = lines.index("--", pos + 3)
  934. drop_table_sql = (
  935. "drop table %s"
  936. % connection.ops.quote_name("migrations_tribble").lower()
  937. )
  938. for line in lines[pos + 3 : next_pos]:
  939. if drop_table_sql in line.lower():
  940. break
  941. else:
  942. self.fail("DROP TABLE (tribble) not found.")
  943. pos = next_pos
  944. self.assertEqual(
  945. lines[pos : pos + 3],
  946. [
  947. "--",
  948. "-- Create model Author",
  949. "--",
  950. ],
  951. )
  952. drop_table_sql = (
  953. "drop table %s" % connection.ops.quote_name("migrations_author").lower()
  954. )
  955. for line in lines[pos + 3 :]:
  956. if drop_table_sql in line.lower():
  957. break
  958. else:
  959. self.fail("DROP TABLE (author) not found.")
  960. finally:
  961. # Unmigrate everything.
  962. call_command("migrate", "migrations", "zero", verbosity=0)
  963. @override_settings(
  964. MIGRATION_MODULES={"migrations": "migrations.test_migrations_non_atomic"}
  965. )
  966. def test_sqlmigrate_for_non_atomic_migration(self):
  967. """
  968. Transaction wrappers aren't shown for non-atomic migrations.
  969. """
  970. out = io.StringIO()
  971. call_command("sqlmigrate", "migrations", "0001", stdout=out)
  972. output = out.getvalue().lower()
  973. queries = [q.strip() for q in output.splitlines()]
  974. if connection.ops.start_transaction_sql():
  975. self.assertNotIn(connection.ops.start_transaction_sql().lower(), queries)
  976. self.assertNotIn(connection.ops.end_transaction_sql().lower(), queries)
  977. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  978. def test_sqlmigrate_for_non_transactional_databases(self):
  979. """
  980. Transaction wrappers aren't shown for databases that don't support
  981. transactional DDL.
  982. """
  983. out = io.StringIO()
  984. with mock.patch.object(connection.features, "can_rollback_ddl", False):
  985. call_command("sqlmigrate", "migrations", "0001", stdout=out)
  986. output = out.getvalue().lower()
  987. queries = [q.strip() for q in output.splitlines()]
  988. start_transaction_sql = connection.ops.start_transaction_sql()
  989. if start_transaction_sql:
  990. self.assertNotIn(start_transaction_sql.lower(), queries)
  991. self.assertNotIn(connection.ops.end_transaction_sql().lower(), queries)
  992. @override_settings(
  993. MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"}
  994. )
  995. def test_sqlmigrate_ambiguous_prefix_squashed_migrations(self):
  996. msg = (
  997. "More than one migration matches '0001' in app 'migrations'. "
  998. "Please be more specific."
  999. )
  1000. with self.assertRaisesMessage(CommandError, msg):
  1001. call_command("sqlmigrate", "migrations", "0001")
  1002. @override_settings(
  1003. MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"}
  1004. )
  1005. def test_sqlmigrate_squashed_migration(self):
  1006. out = io.StringIO()
  1007. call_command("sqlmigrate", "migrations", "0001_squashed_0002", stdout=out)
  1008. output = out.getvalue().lower()
  1009. self.assertIn("-- create model author", output)
  1010. self.assertIn("-- create model book", output)
  1011. self.assertNotIn("-- create model tribble", output)
  1012. @override_settings(
  1013. MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"}
  1014. )
  1015. def test_sqlmigrate_replaced_migration(self):
  1016. out = io.StringIO()
  1017. call_command("sqlmigrate", "migrations", "0001_initial", stdout=out)
  1018. output = out.getvalue().lower()
  1019. self.assertIn("-- create model author", output)
  1020. self.assertIn("-- create model tribble", output)
  1021. @override_settings(
  1022. MIGRATION_MODULES={"migrations": "migrations.test_migrations_no_operations"}
  1023. )
  1024. def test_sqlmigrate_no_operations(self):
  1025. err = io.StringIO()
  1026. call_command("sqlmigrate", "migrations", "0001_initial", stderr=err)
  1027. self.assertEqual(err.getvalue(), "No operations found.\n")
  1028. @override_settings(
  1029. MIGRATION_MODULES={"migrations": "migrations.test_migrations_noop"}
  1030. )
  1031. def test_sqlmigrate_noop(self):
  1032. out = io.StringIO()
  1033. call_command("sqlmigrate", "migrations", "0001", stdout=out)
  1034. lines = out.getvalue().splitlines()
  1035. if connection.features.can_rollback_ddl:
  1036. lines = lines[1:-1]
  1037. self.assertEqual(
  1038. lines,
  1039. [
  1040. "--",
  1041. "-- Raw SQL operation",
  1042. "--",
  1043. "-- (no-op)",
  1044. ],
  1045. )
  1046. @override_settings(
  1047. MIGRATION_MODULES={"migrations": "migrations.test_migrations_manual_porting"}
  1048. )
  1049. def test_sqlmigrate_unrepresentable(self):
  1050. out = io.StringIO()
  1051. call_command("sqlmigrate", "migrations", "0002", stdout=out)
  1052. lines = out.getvalue().splitlines()
  1053. if connection.features.can_rollback_ddl:
  1054. lines = lines[1:-1]
  1055. self.assertEqual(
  1056. lines,
  1057. [
  1058. "--",
  1059. "-- Raw Python operation",
  1060. "--",
  1061. "-- THIS OPERATION CANNOT BE WRITTEN AS SQL",
  1062. ],
  1063. )
  1064. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  1065. def test_sqlmigrate_transaction_keywords_not_colorized(self):
  1066. out = io.StringIO()
  1067. with mock.patch(
  1068. "django.core.management.color.supports_color", lambda *args: True
  1069. ):
  1070. call_command("sqlmigrate", "migrations", "0001", stdout=out, no_color=False)
  1071. self.assertNotIn("\x1b", out.getvalue())
  1072. @override_settings(
  1073. MIGRATION_MODULES={"migrations": "migrations.test_migrations_no_operations"},
  1074. INSTALLED_APPS=["django.contrib.auth"],
  1075. )
  1076. def test_sqlmigrate_system_checks_colorized(self):
  1077. with (
  1078. mock.patch(
  1079. "django.core.management.color.supports_color", lambda *args: True
  1080. ),
  1081. self.assertRaisesMessage(SystemCheckError, "\x1b"),
  1082. ):
  1083. call_command(
  1084. "sqlmigrate", "migrations", "0001", skip_checks=False, no_color=False
  1085. )
  1086. @override_settings(
  1087. INSTALLED_APPS=[
  1088. "migrations.migrations_test_apps.migrated_app",
  1089. "migrations.migrations_test_apps.migrated_unapplied_app",
  1090. "migrations.migrations_test_apps.unmigrated_app",
  1091. ],
  1092. )
  1093. def test_regression_22823_unmigrated_fk_to_migrated_model(self):
  1094. """
  1095. Assuming you have 3 apps, `A`, `B`, and `C`, such that:
  1096. * `A` has migrations
  1097. * `B` has a migration we want to apply
  1098. * `C` has no migrations, but has an FK to `A`
  1099. When we try to migrate "B", an exception occurs because the
  1100. "B" was not included in the ProjectState that is used to detect
  1101. soft-applied migrations (#22823).
  1102. """
  1103. call_command("migrate", "migrated_unapplied_app", verbosity=0)
  1104. # unmigrated_app.SillyModel has a foreign key to 'migrations.Tribble',
  1105. # but that model is only defined in a migration, so the global app
  1106. # registry never sees it and the reference is left dangling. Remove it
  1107. # to avoid problems in subsequent tests.
  1108. apps._pending_operations.pop(("migrations", "tribble"), None)
  1109. @override_settings(
  1110. INSTALLED_APPS=["migrations.migrations_test_apps.unmigrated_app_syncdb"]
  1111. )
  1112. def test_migrate_syncdb_deferred_sql_executed_with_schemaeditor(self):
  1113. """
  1114. For an app without migrations, editor.execute() is used for executing
  1115. the syncdb deferred SQL.
  1116. """
  1117. stdout = io.StringIO()
  1118. with mock.patch.object(BaseDatabaseSchemaEditor, "execute") as execute:
  1119. call_command(
  1120. "migrate", run_syncdb=True, verbosity=1, stdout=stdout, no_color=True
  1121. )
  1122. create_table_count = len(
  1123. [call for call in execute.mock_calls if "CREATE TABLE" in str(call)]
  1124. )
  1125. self.assertEqual(create_table_count, 2)
  1126. # There's at least one deferred SQL for creating the foreign key
  1127. # index.
  1128. self.assertGreater(len(execute.mock_calls), 2)
  1129. stdout = stdout.getvalue()
  1130. self.assertIn("Synchronize unmigrated apps: unmigrated_app_syncdb", stdout)
  1131. self.assertIn("Creating tables...", stdout)
  1132. table_name = truncate_name(
  1133. "unmigrated_app_syncdb_classroom", connection.ops.max_name_length()
  1134. )
  1135. self.assertIn("Creating table %s" % table_name, stdout)
  1136. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  1137. def test_migrate_syncdb_app_with_migrations(self):
  1138. msg = "Can't use run_syncdb with app 'migrations' as it has migrations."
  1139. with self.assertRaisesMessage(CommandError, msg):
  1140. call_command("migrate", "migrations", run_syncdb=True, verbosity=0)
  1141. @override_settings(
  1142. INSTALLED_APPS=[
  1143. "migrations.migrations_test_apps.unmigrated_app_syncdb",
  1144. "migrations.migrations_test_apps.unmigrated_app_simple",
  1145. ]
  1146. )
  1147. def test_migrate_syncdb_app_label(self):
  1148. """
  1149. Running migrate --run-syncdb with an app_label only creates tables for
  1150. the specified app.
  1151. """
  1152. stdout = io.StringIO()
  1153. with mock.patch.object(BaseDatabaseSchemaEditor, "execute") as execute:
  1154. call_command(
  1155. "migrate", "unmigrated_app_syncdb", run_syncdb=True, stdout=stdout
  1156. )
  1157. create_table_count = len(
  1158. [call for call in execute.mock_calls if "CREATE TABLE" in str(call)]
  1159. )
  1160. self.assertEqual(create_table_count, 2)
  1161. self.assertGreater(len(execute.mock_calls), 2)
  1162. self.assertIn(
  1163. "Synchronize unmigrated app: unmigrated_app_syncdb", stdout.getvalue()
  1164. )
  1165. @override_settings(
  1166. MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"}
  1167. )
  1168. def test_migrate_record_replaced(self):
  1169. """
  1170. Running a single squashed migration should record all of the original
  1171. replaced migrations as run.
  1172. """
  1173. recorder = MigrationRecorder(connection)
  1174. out = io.StringIO()
  1175. call_command("migrate", "migrations", verbosity=0)
  1176. call_command("showmigrations", "migrations", stdout=out, no_color=True)
  1177. self.assertEqual(
  1178. "migrations\n [x] 0001_squashed_0002 (2 squashed migrations)\n",
  1179. out.getvalue().lower(),
  1180. )
  1181. applied_migrations = recorder.applied_migrations()
  1182. self.assertIn(("migrations", "0001_initial"), applied_migrations)
  1183. self.assertIn(("migrations", "0002_second"), applied_migrations)
  1184. self.assertIn(("migrations", "0001_squashed_0002"), applied_migrations)
  1185. # Rollback changes
  1186. call_command("migrate", "migrations", "zero", verbosity=0)
  1187. @override_settings(
  1188. MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"}
  1189. )
  1190. def test_migrate_record_squashed(self):
  1191. """
  1192. Running migrate for a squashed migration should record as run
  1193. if all of the replaced migrations have been run (#25231).
  1194. """
  1195. recorder = MigrationRecorder(connection)
  1196. recorder.record_applied("migrations", "0001_initial")
  1197. recorder.record_applied("migrations", "0002_second")
  1198. out = io.StringIO()
  1199. call_command("showmigrations", "migrations", stdout=out, no_color=True)
  1200. self.assertEqual(
  1201. "migrations\n"
  1202. " [-] 0001_squashed_0002 (2 squashed migrations) "
  1203. "run 'manage.py migrate' to finish recording.\n",
  1204. out.getvalue().lower(),
  1205. )
  1206. out = io.StringIO()
  1207. call_command("migrate", "migrations", verbosity=0)
  1208. call_command("showmigrations", "migrations", stdout=out, no_color=True)
  1209. self.assertEqual(
  1210. "migrations\n [x] 0001_squashed_0002 (2 squashed migrations)\n",
  1211. out.getvalue().lower(),
  1212. )
  1213. self.assertIn(
  1214. ("migrations", "0001_squashed_0002"), recorder.applied_migrations()
  1215. )
  1216. # No changes were actually applied so there is nothing to rollback
  1217. def test_migrate_partially_applied_squashed_migration(self):
  1218. """
  1219. Migrating to a squashed migration specified by name should succeed
  1220. even if it is partially applied.
  1221. """
  1222. with self.temporary_migration_module(module="migrations.test_migrations"):
  1223. recorder = MigrationRecorder(connection)
  1224. try:
  1225. call_command("migrate", "migrations", "0001_initial", verbosity=0)
  1226. call_command(
  1227. "squashmigrations",
  1228. "migrations",
  1229. "0002",
  1230. interactive=False,
  1231. verbosity=0,
  1232. )
  1233. call_command(
  1234. "migrate",
  1235. "migrations",
  1236. "0001_squashed_0002_second",
  1237. verbosity=0,
  1238. )
  1239. applied_migrations = recorder.applied_migrations()
  1240. self.assertIn(("migrations", "0002_second"), applied_migrations)
  1241. finally:
  1242. # Unmigrate everything.
  1243. call_command("migrate", "migrations", "zero", verbosity=0)
  1244. @override_settings(
  1245. MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"}
  1246. )
  1247. def test_migrate_backward_to_squashed_migration(self):
  1248. try:
  1249. call_command("migrate", "migrations", "0001_squashed_0002", verbosity=0)
  1250. self.assertTableExists("migrations_author")
  1251. self.assertTableExists("migrations_book")
  1252. call_command("migrate", "migrations", "0001_initial", verbosity=0)
  1253. self.assertTableExists("migrations_author")
  1254. self.assertTableNotExists("migrations_book")
  1255. finally:
  1256. # Unmigrate everything.
  1257. call_command("migrate", "migrations", "zero", verbosity=0)
  1258. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  1259. def test_migrate_inconsistent_history(self):
  1260. """
  1261. Running migrate with some migrations applied before their dependencies
  1262. should not be allowed.
  1263. """
  1264. recorder = MigrationRecorder(connection)
  1265. recorder.record_applied("migrations", "0002_second")
  1266. msg = (
  1267. "Migration migrations.0002_second is applied before its dependency "
  1268. "migrations.0001_initial"
  1269. )
  1270. with self.assertRaisesMessage(InconsistentMigrationHistory, msg):
  1271. call_command("migrate")
  1272. applied_migrations = recorder.applied_migrations()
  1273. self.assertNotIn(("migrations", "0001_initial"), applied_migrations)
  1274. @override_settings(
  1275. INSTALLED_APPS=[
  1276. "migrations.migrations_test_apps.migrated_unapplied_app",
  1277. "migrations.migrations_test_apps.migrated_app",
  1278. ]
  1279. )
  1280. def test_migrate_not_reflected_changes(self):
  1281. class NewModel1(models.Model):
  1282. class Meta:
  1283. app_label = "migrated_app"
  1284. class NewModel2(models.Model):
  1285. class Meta:
  1286. app_label = "migrated_unapplied_app"
  1287. out = io.StringIO()
  1288. try:
  1289. call_command("migrate", verbosity=0)
  1290. call_command("migrate", stdout=out, no_color=True)
  1291. self.assertEqual(
  1292. "operations to perform:\n"
  1293. " apply all migrations: migrated_app, migrated_unapplied_app\n"
  1294. "running migrations:\n"
  1295. " no migrations to apply.\n"
  1296. " your models in app(s): 'migrated_app', "
  1297. "'migrated_unapplied_app' have changes that are not yet "
  1298. "reflected in a migration, and so won't be applied.\n"
  1299. " run 'manage.py makemigrations' to make new migrations, and "
  1300. "then re-run 'manage.py migrate' to apply them.\n",
  1301. out.getvalue().lower(),
  1302. )
  1303. finally:
  1304. # Unmigrate everything.
  1305. call_command("migrate", "migrated_app", "zero", verbosity=0)
  1306. call_command("migrate", "migrated_unapplied_app", "zero", verbosity=0)
  1307. @override_settings(
  1308. MIGRATION_MODULES={
  1309. "migrations": "migrations.test_migrations_squashed_no_replaces",
  1310. }
  1311. )
  1312. def test_migrate_prune(self):
  1313. """
  1314. With prune=True, references to migration files deleted from the
  1315. migrations module (such as after being squashed) are removed from the
  1316. django_migrations table.
  1317. """
  1318. recorder = MigrationRecorder(connection)
  1319. recorder.record_applied("migrations", "0001_initial")
  1320. recorder.record_applied("migrations", "0002_second")
  1321. recorder.record_applied("migrations", "0001_squashed_0002")
  1322. out = io.StringIO()
  1323. try:
  1324. call_command("migrate", "migrations", prune=True, stdout=out, no_color=True)
  1325. self.assertEqual(
  1326. out.getvalue(),
  1327. "Pruning migrations:\n"
  1328. " Pruning migrations.0001_initial OK\n"
  1329. " Pruning migrations.0002_second OK\n",
  1330. )
  1331. applied_migrations = [
  1332. migration
  1333. for migration in recorder.applied_migrations()
  1334. if migration[0] == "migrations"
  1335. ]
  1336. self.assertEqual(applied_migrations, [("migrations", "0001_squashed_0002")])
  1337. finally:
  1338. recorder.record_unapplied("migrations", "0001_initial")
  1339. recorder.record_unapplied("migrations", "0001_second")
  1340. recorder.record_unapplied("migrations", "0001_squashed_0002")
  1341. @override_settings(
  1342. MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"}
  1343. )
  1344. def test_prune_deleted_squashed_migrations_in_replaces(self):
  1345. out = io.StringIO()
  1346. with self.temporary_migration_module(
  1347. module="migrations.test_migrations_squashed"
  1348. ) as migration_dir:
  1349. try:
  1350. call_command("migrate", "migrations", verbosity=0)
  1351. # Delete the replaced migrations.
  1352. os.remove(os.path.join(migration_dir, "0001_initial.py"))
  1353. os.remove(os.path.join(migration_dir, "0002_second.py"))
  1354. # --prune cannot be used before removing the "replaces"
  1355. # attribute.
  1356. call_command(
  1357. "migrate",
  1358. "migrations",
  1359. prune=True,
  1360. stdout=out,
  1361. no_color=True,
  1362. )
  1363. self.assertEqual(
  1364. out.getvalue(),
  1365. "Pruning migrations:\n"
  1366. " Cannot use --prune because the following squashed "
  1367. "migrations have their 'replaces' attributes and may not "
  1368. "be recorded as applied:\n"
  1369. " migrations.0001_squashed_0002\n"
  1370. " Re-run 'manage.py migrate' if they are not marked as "
  1371. "applied, and remove 'replaces' attributes in their "
  1372. "Migration classes.\n",
  1373. )
  1374. finally:
  1375. # Unmigrate everything.
  1376. call_command("migrate", "migrations", "zero", verbosity=0)
  1377. @override_settings(
  1378. MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"}
  1379. )
  1380. def test_prune_no_migrations_to_prune(self):
  1381. out = io.StringIO()
  1382. call_command("migrate", "migrations", prune=True, stdout=out, no_color=True)
  1383. self.assertEqual(
  1384. out.getvalue(),
  1385. "Pruning migrations:\n No migrations to prune.\n",
  1386. )
  1387. out = io.StringIO()
  1388. call_command(
  1389. "migrate",
  1390. "migrations",
  1391. prune=True,
  1392. stdout=out,
  1393. no_color=True,
  1394. verbosity=0,
  1395. )
  1396. self.assertEqual(out.getvalue(), "")
  1397. def test_prune_no_app_label(self):
  1398. msg = "Migrations can be pruned only when an app is specified."
  1399. with self.assertRaisesMessage(CommandError, msg):
  1400. call_command("migrate", prune=True)
  1401. @override_settings(
  1402. MIGRATION_MODULES={
  1403. "migrations": "migrations.test_migrations_squashed_no_replaces",
  1404. "migrations2": "migrations2.test_migrations_2_squashed_with_replaces",
  1405. },
  1406. INSTALLED_APPS=["migrations", "migrations2"],
  1407. )
  1408. def test_prune_respect_app_label(self):
  1409. recorder = MigrationRecorder(connection)
  1410. recorder.record_applied("migrations", "0001_initial")
  1411. recorder.record_applied("migrations", "0002_second")
  1412. recorder.record_applied("migrations", "0001_squashed_0002")
  1413. # Second app has squashed migrations with replaces.
  1414. recorder.record_applied("migrations2", "0001_initial")
  1415. recorder.record_applied("migrations2", "0002_second")
  1416. recorder.record_applied("migrations2", "0001_squashed_0002")
  1417. out = io.StringIO()
  1418. try:
  1419. call_command("migrate", "migrations", prune=True, stdout=out, no_color=True)
  1420. self.assertEqual(
  1421. out.getvalue(),
  1422. "Pruning migrations:\n"
  1423. " Pruning migrations.0001_initial OK\n"
  1424. " Pruning migrations.0002_second OK\n",
  1425. )
  1426. applied_migrations = [
  1427. migration
  1428. for migration in recorder.applied_migrations()
  1429. if migration[0] in ["migrations", "migrations2"]
  1430. ]
  1431. self.assertEqual(
  1432. applied_migrations,
  1433. [
  1434. ("migrations", "0001_squashed_0002"),
  1435. ("migrations2", "0001_initial"),
  1436. ("migrations2", "0002_second"),
  1437. ("migrations2", "0001_squashed_0002"),
  1438. ],
  1439. )
  1440. finally:
  1441. recorder.record_unapplied("migrations", "0001_initial")
  1442. recorder.record_unapplied("migrations", "0001_second")
  1443. recorder.record_unapplied("migrations", "0001_squashed_0002")
  1444. recorder.record_unapplied("migrations2", "0001_initial")
  1445. recorder.record_unapplied("migrations2", "0002_second")
  1446. recorder.record_unapplied("migrations2", "0001_squashed_0002")
  1447. class MakeMigrationsTests(MigrationTestBase):
  1448. """
  1449. Tests running the makemigrations command.
  1450. """
  1451. def setUp(self):
  1452. super().setUp()
  1453. self._old_models = apps.app_configs["migrations"].models.copy()
  1454. def tearDown(self):
  1455. apps.app_configs["migrations"].models = self._old_models
  1456. apps.all_models["migrations"] = self._old_models
  1457. apps.clear_cache()
  1458. super().tearDown()
  1459. def test_files_content(self):
  1460. self.assertTableNotExists("migrations_unicodemodel")
  1461. apps.register_model("migrations", UnicodeModel)
  1462. with self.temporary_migration_module() as migration_dir:
  1463. call_command("makemigrations", "migrations", verbosity=0)
  1464. # Check for empty __init__.py file in migrations folder
  1465. init_file = os.path.join(migration_dir, "__init__.py")
  1466. self.assertTrue(os.path.exists(init_file))
  1467. with open(init_file) as fp:
  1468. content = fp.read()
  1469. self.assertEqual(content, "")
  1470. # Check for existing 0001_initial.py file in migration folder
  1471. initial_file = os.path.join(migration_dir, "0001_initial.py")
  1472. self.assertTrue(os.path.exists(initial_file))
  1473. with open(initial_file, encoding="utf-8") as fp:
  1474. content = fp.read()
  1475. self.assertIn("migrations.CreateModel", content)
  1476. self.assertIn("initial = True", content)
  1477. self.assertIn("úñí©óðé µóðéø", content) # Meta.verbose_name
  1478. self.assertIn("úñí©óðé µóðéøß", content) # Meta.verbose_name_plural
  1479. self.assertIn("ÚÑÍ¢ÓÐÉ", content) # title.verbose_name
  1480. self.assertIn("“Ðjáñgó”", content) # title.default
  1481. def test_makemigrations_order(self):
  1482. """
  1483. makemigrations should recognize number-only migrations (0001.py).
  1484. """
  1485. module = "migrations.test_migrations_order"
  1486. with self.temporary_migration_module(module=module) as migration_dir:
  1487. if hasattr(importlib, "invalidate_caches"):
  1488. # importlib caches os.listdir() on some platforms like macOS
  1489. # (#23850).
  1490. importlib.invalidate_caches()
  1491. call_command(
  1492. "makemigrations", "migrations", "--empty", "-n", "a", "-v", "0"
  1493. )
  1494. self.assertTrue(os.path.exists(os.path.join(migration_dir, "0002_a.py")))
  1495. def test_makemigrations_empty_connections(self):
  1496. empty_connections = ConnectionHandler({"default": {}})
  1497. with mock.patch(
  1498. "django.core.management.commands.makemigrations.connections",
  1499. new=empty_connections,
  1500. ):
  1501. # with no apps
  1502. out = io.StringIO()
  1503. call_command("makemigrations", stdout=out)
  1504. self.assertIn("No changes detected", out.getvalue())
  1505. # with an app
  1506. with self.temporary_migration_module() as migration_dir:
  1507. call_command("makemigrations", "migrations", verbosity=0)
  1508. init_file = os.path.join(migration_dir, "__init__.py")
  1509. self.assertTrue(os.path.exists(init_file))
  1510. @override_settings(INSTALLED_APPS=["migrations", "migrations2"])
  1511. def test_makemigrations_consistency_checks_respect_routers(self):
  1512. """
  1513. The history consistency checks in makemigrations respect
  1514. settings.DATABASE_ROUTERS.
  1515. """
  1516. def patched_has_table(migration_recorder):
  1517. if migration_recorder.connection is connections["other"]:
  1518. raise Exception("Other connection")
  1519. else:
  1520. return mock.DEFAULT
  1521. self.assertTableNotExists("migrations_unicodemodel")
  1522. apps.register_model("migrations", UnicodeModel)
  1523. with mock.patch.object(
  1524. MigrationRecorder, "has_table", autospec=True, side_effect=patched_has_table
  1525. ) as has_table:
  1526. with self.temporary_migration_module() as migration_dir:
  1527. call_command("makemigrations", "migrations", verbosity=0)
  1528. initial_file = os.path.join(migration_dir, "0001_initial.py")
  1529. self.assertTrue(os.path.exists(initial_file))
  1530. self.assertEqual(has_table.call_count, 1) # 'default' is checked
  1531. # Router says not to migrate 'other' so consistency shouldn't
  1532. # be checked.
  1533. with self.settings(DATABASE_ROUTERS=["migrations.routers.TestRouter"]):
  1534. call_command("makemigrations", "migrations", verbosity=0)
  1535. self.assertEqual(has_table.call_count, 2) # 'default' again
  1536. # With a router that doesn't prohibit migrating 'other',
  1537. # consistency is checked.
  1538. with self.settings(
  1539. DATABASE_ROUTERS=["migrations.routers.DefaultOtherRouter"]
  1540. ):
  1541. with self.assertRaisesMessage(Exception, "Other connection"):
  1542. call_command("makemigrations", "migrations", verbosity=0)
  1543. self.assertEqual(has_table.call_count, 4) # 'default' and 'other'
  1544. # With a router that doesn't allow migrating on any database,
  1545. # no consistency checks are made.
  1546. with self.settings(DATABASE_ROUTERS=["migrations.routers.TestRouter"]):
  1547. with mock.patch.object(
  1548. TestRouter, "allow_migrate", return_value=False
  1549. ) as allow_migrate:
  1550. call_command("makemigrations", "migrations", verbosity=0)
  1551. allow_migrate.assert_any_call(
  1552. "other", "migrations", model_name="UnicodeModel"
  1553. )
  1554. # allow_migrate() is called with the correct arguments.
  1555. self.assertGreater(len(allow_migrate.mock_calls), 0)
  1556. called_aliases = set()
  1557. for mock_call in allow_migrate.mock_calls:
  1558. _, call_args, call_kwargs = mock_call
  1559. connection_alias, app_name = call_args
  1560. called_aliases.add(connection_alias)
  1561. # Raises an error if invalid app_name/model_name occurs.
  1562. apps.get_app_config(app_name).get_model(call_kwargs["model_name"])
  1563. self.assertEqual(called_aliases, set(connections))
  1564. self.assertEqual(has_table.call_count, 4)
  1565. def test_failing_migration(self):
  1566. # If a migration fails to serialize, it shouldn't generate an empty file. #21280
  1567. apps.register_model("migrations", UnserializableModel)
  1568. with self.temporary_migration_module() as migration_dir:
  1569. with self.assertRaisesMessage(ValueError, "Cannot serialize"):
  1570. call_command("makemigrations", "migrations", verbosity=0)
  1571. initial_file = os.path.join(migration_dir, "0001_initial.py")
  1572. self.assertFalse(os.path.exists(initial_file))
  1573. def test_makemigrations_conflict_exit(self):
  1574. """
  1575. makemigrations exits if it detects a conflict.
  1576. """
  1577. with self.temporary_migration_module(
  1578. module="migrations.test_migrations_conflict"
  1579. ):
  1580. with self.assertRaises(CommandError) as context:
  1581. call_command("makemigrations")
  1582. self.assertEqual(
  1583. str(context.exception),
  1584. "Conflicting migrations detected; multiple leaf nodes in the "
  1585. "migration graph: (0002_conflicting_second, 0002_second in "
  1586. "migrations).\n"
  1587. "To fix them run 'python manage.py makemigrations --merge'",
  1588. )
  1589. def test_makemigrations_merge_no_conflict(self):
  1590. """
  1591. makemigrations exits if in merge mode with no conflicts.
  1592. """
  1593. out = io.StringIO()
  1594. with self.temporary_migration_module(module="migrations.test_migrations"):
  1595. call_command("makemigrations", merge=True, stdout=out)
  1596. self.assertIn("No conflicts detected to merge.", out.getvalue())
  1597. def test_makemigrations_empty_no_app_specified(self):
  1598. """
  1599. makemigrations exits if no app is specified with 'empty' mode.
  1600. """
  1601. msg = "You must supply at least one app label when using --empty."
  1602. with self.assertRaisesMessage(CommandError, msg):
  1603. call_command("makemigrations", empty=True)
  1604. def test_makemigrations_empty_migration(self):
  1605. """
  1606. makemigrations properly constructs an empty migration.
  1607. """
  1608. with self.temporary_migration_module() as migration_dir:
  1609. call_command("makemigrations", "migrations", empty=True, verbosity=0)
  1610. # Check for existing 0001_initial.py file in migration folder
  1611. initial_file = os.path.join(migration_dir, "0001_initial.py")
  1612. self.assertTrue(os.path.exists(initial_file))
  1613. with open(initial_file, encoding="utf-8") as fp:
  1614. content = fp.read()
  1615. # Remove all whitespace to check for empty dependencies and operations
  1616. content = content.replace(" ", "")
  1617. self.assertIn(
  1618. "dependencies=[]" if HAS_BLACK else "dependencies=[\n]", content
  1619. )
  1620. self.assertIn(
  1621. "operations=[]" if HAS_BLACK else "operations=[\n]", content
  1622. )
  1623. @override_settings(MIGRATION_MODULES={"migrations": None})
  1624. def test_makemigrations_disabled_migrations_for_app(self):
  1625. """
  1626. makemigrations raises a nice error when migrations are disabled for an
  1627. app.
  1628. """
  1629. msg = (
  1630. "Django can't create migrations for app 'migrations' because migrations "
  1631. "have been disabled via the MIGRATION_MODULES setting."
  1632. )
  1633. with self.assertRaisesMessage(ValueError, msg):
  1634. call_command("makemigrations", "migrations", empty=True, verbosity=0)
  1635. def test_makemigrations_no_changes_no_apps(self):
  1636. """
  1637. makemigrations exits when there are no changes and no apps are specified.
  1638. """
  1639. out = io.StringIO()
  1640. call_command("makemigrations", stdout=out)
  1641. self.assertIn("No changes detected", out.getvalue())
  1642. def test_makemigrations_no_changes(self):
  1643. """
  1644. makemigrations exits when there are no changes to an app.
  1645. """
  1646. out = io.StringIO()
  1647. with self.temporary_migration_module(
  1648. module="migrations.test_migrations_no_changes"
  1649. ):
  1650. call_command("makemigrations", "migrations", stdout=out)
  1651. self.assertIn("No changes detected in app 'migrations'", out.getvalue())
  1652. def test_makemigrations_no_apps_initial(self):
  1653. """
  1654. makemigrations should detect initial is needed on empty migration
  1655. modules if no app provided.
  1656. """
  1657. out = io.StringIO()
  1658. with self.temporary_migration_module(module="migrations.test_migrations_empty"):
  1659. call_command("makemigrations", stdout=out)
  1660. self.assertIn("0001_initial.py", out.getvalue())
  1661. def test_makemigrations_no_init(self):
  1662. """Migration directories without an __init__.py file are allowed."""
  1663. out = io.StringIO()
  1664. with self.temporary_migration_module(
  1665. module="migrations.test_migrations_no_init"
  1666. ):
  1667. call_command("makemigrations", stdout=out)
  1668. self.assertIn("0001_initial.py", out.getvalue())
  1669. def test_makemigrations_no_init_ambiguous(self):
  1670. """
  1671. Migration directories without an __init__.py file are not allowed if
  1672. there are multiple namespace search paths that resolve to them.
  1673. """
  1674. out = io.StringIO()
  1675. with self.temporary_migration_module(
  1676. module="migrations.test_migrations_no_init"
  1677. ) as migration_dir:
  1678. # Copy the project directory into another place under sys.path.
  1679. app_dir = Path(migration_dir).parent
  1680. os.remove(app_dir / "__init__.py")
  1681. project_dir = app_dir.parent
  1682. dest = project_dir.parent / "other_dir_in_path"
  1683. shutil.copytree(project_dir, dest)
  1684. with extend_sys_path(str(dest)):
  1685. call_command("makemigrations", stdout=out)
  1686. self.assertEqual("No changes detected\n", out.getvalue())
  1687. def test_makemigrations_migrations_announce(self):
  1688. """
  1689. makemigrations announces the migration at the default verbosity level.
  1690. """
  1691. out = io.StringIO()
  1692. with self.temporary_migration_module():
  1693. call_command("makemigrations", "migrations", stdout=out)
  1694. self.assertIn("Migrations for 'migrations'", out.getvalue())
  1695. def test_makemigrations_no_common_ancestor(self):
  1696. """
  1697. makemigrations fails to merge migrations with no common ancestor.
  1698. """
  1699. with self.assertRaises(ValueError) as context:
  1700. with self.temporary_migration_module(
  1701. module="migrations.test_migrations_no_ancestor"
  1702. ):
  1703. call_command("makemigrations", "migrations", merge=True)
  1704. exception_message = str(context.exception)
  1705. self.assertIn("Could not find common ancestor of", exception_message)
  1706. self.assertIn("0002_second", exception_message)
  1707. self.assertIn("0002_conflicting_second", exception_message)
  1708. def test_makemigrations_interactive_reject(self):
  1709. """
  1710. makemigrations enters and exits interactive mode properly.
  1711. """
  1712. # Monkeypatch interactive questioner to auto reject
  1713. with mock.patch("builtins.input", mock.Mock(return_value="N")):
  1714. with self.temporary_migration_module(
  1715. module="migrations.test_migrations_conflict"
  1716. ) as migration_dir:
  1717. with captured_stdout():
  1718. call_command(
  1719. "makemigrations",
  1720. "migrations",
  1721. name="merge",
  1722. merge=True,
  1723. interactive=True,
  1724. verbosity=0,
  1725. )
  1726. merge_file = os.path.join(migration_dir, "0003_merge.py")
  1727. self.assertFalse(os.path.exists(merge_file))
  1728. def test_makemigrations_interactive_accept(self):
  1729. """
  1730. makemigrations enters interactive mode and merges properly.
  1731. """
  1732. # Monkeypatch interactive questioner to auto accept
  1733. with mock.patch("builtins.input", mock.Mock(return_value="y")):
  1734. out = io.StringIO()
  1735. with self.temporary_migration_module(
  1736. module="migrations.test_migrations_conflict"
  1737. ) as migration_dir:
  1738. call_command(
  1739. "makemigrations",
  1740. "migrations",
  1741. name="merge",
  1742. merge=True,
  1743. interactive=True,
  1744. stdout=out,
  1745. )
  1746. merge_file = os.path.join(migration_dir, "0003_merge.py")
  1747. self.assertTrue(os.path.exists(merge_file))
  1748. self.assertIn("Created new merge migration", out.getvalue())
  1749. def test_makemigrations_default_merge_name(self):
  1750. out = io.StringIO()
  1751. with self.temporary_migration_module(
  1752. module="migrations.test_migrations_conflict"
  1753. ) as migration_dir:
  1754. call_command(
  1755. "makemigrations",
  1756. "migrations",
  1757. merge=True,
  1758. interactive=False,
  1759. stdout=out,
  1760. )
  1761. merge_file = os.path.join(
  1762. migration_dir,
  1763. "0003_merge_0002_conflicting_second_0002_second.py",
  1764. )
  1765. self.assertIs(os.path.exists(merge_file), True)
  1766. with open(merge_file, encoding="utf-8") as fp:
  1767. content = fp.read()
  1768. if HAS_BLACK:
  1769. target_str = '("migrations", "0002_conflicting_second")'
  1770. else:
  1771. target_str = "('migrations', '0002_conflicting_second')"
  1772. self.assertIn(target_str, content)
  1773. self.assertIn("Created new merge migration %s" % merge_file, out.getvalue())
  1774. @mock.patch("django.db.migrations.utils.datetime")
  1775. def test_makemigrations_auto_merge_name(self, mock_datetime):
  1776. mock_datetime.datetime.now.return_value = datetime.datetime(2016, 1, 2, 3, 4)
  1777. with mock.patch("builtins.input", mock.Mock(return_value="y")):
  1778. out = io.StringIO()
  1779. with self.temporary_migration_module(
  1780. module="migrations.test_migrations_conflict_long_name"
  1781. ) as migration_dir:
  1782. call_command(
  1783. "makemigrations",
  1784. "migrations",
  1785. merge=True,
  1786. interactive=True,
  1787. stdout=out,
  1788. )
  1789. merge_file = os.path.join(migration_dir, "0003_merge_20160102_0304.py")
  1790. self.assertTrue(os.path.exists(merge_file))
  1791. self.assertIn("Created new merge migration", out.getvalue())
  1792. def test_makemigrations_non_interactive_not_null_addition(self):
  1793. """
  1794. Non-interactive makemigrations fails when a default is missing on a
  1795. new not-null field.
  1796. """
  1797. class SillyModel(models.Model):
  1798. silly_field = models.BooleanField(default=False)
  1799. silly_int = models.IntegerField()
  1800. class Meta:
  1801. app_label = "migrations"
  1802. with self.assertRaises(SystemExit):
  1803. with self.temporary_migration_module(
  1804. module="migrations.test_migrations_no_default"
  1805. ):
  1806. with captured_stdout() as out:
  1807. call_command("makemigrations", "migrations", interactive=False)
  1808. self.assertIn(
  1809. "Field 'silly_int' on model 'sillymodel' not migrated: it is "
  1810. "impossible to add a non-nullable field without specifying a "
  1811. "default.",
  1812. out.getvalue(),
  1813. )
  1814. def test_makemigrations_interactive_not_null_addition(self):
  1815. """
  1816. makemigrations messages when adding a NOT NULL field in interactive
  1817. mode.
  1818. """
  1819. class Author(models.Model):
  1820. silly_field = models.BooleanField(null=False)
  1821. class Meta:
  1822. app_label = "migrations"
  1823. input_msg = (
  1824. "It is impossible to add a non-nullable field 'silly_field' to "
  1825. "author without specifying a default. This is because the "
  1826. "database needs something to populate existing rows.\n"
  1827. "Please select a fix:\n"
  1828. " 1) Provide a one-off default now (will be set on all existing "
  1829. "rows with a null value for this column)\n"
  1830. " 2) Quit and manually define a default value in models.py."
  1831. )
  1832. with self.temporary_migration_module(module="migrations.test_migrations"):
  1833. # 2 - quit.
  1834. with mock.patch("builtins.input", return_value="2"):
  1835. with captured_stdout() as out, self.assertRaises(SystemExit):
  1836. call_command("makemigrations", "migrations", interactive=True)
  1837. self.assertIn(input_msg, out.getvalue())
  1838. # 1 - provide a default.
  1839. with mock.patch("builtins.input", return_value="1"):
  1840. with captured_stdout() as out:
  1841. call_command("makemigrations", "migrations", interactive=True)
  1842. output = out.getvalue()
  1843. self.assertIn(input_msg, output)
  1844. self.assertIn("Please enter the default value as valid Python.", output)
  1845. self.assertIn(
  1846. "The datetime and django.utils.timezone modules are "
  1847. "available, so it is possible to provide e.g. timezone.now as "
  1848. "a value",
  1849. output,
  1850. )
  1851. self.assertIn("Type 'exit' to exit this prompt", output)
  1852. def test_makemigrations_non_interactive_not_null_alteration(self):
  1853. """
  1854. Non-interactive makemigrations fails when a default is missing on a
  1855. field changed to not-null.
  1856. """
  1857. class Author(models.Model):
  1858. name = models.CharField(max_length=255)
  1859. slug = models.SlugField()
  1860. age = models.IntegerField(default=0)
  1861. class Meta:
  1862. app_label = "migrations"
  1863. with self.temporary_migration_module(module="migrations.test_migrations"):
  1864. with captured_stdout() as out:
  1865. call_command("makemigrations", "migrations", interactive=False)
  1866. self.assertIn("Alter field slug on author", out.getvalue())
  1867. self.assertIn(
  1868. "Field 'slug' on model 'author' given a default of NOT PROVIDED "
  1869. "and must be corrected.",
  1870. out.getvalue(),
  1871. )
  1872. def test_makemigrations_interactive_not_null_alteration(self):
  1873. """
  1874. makemigrations messages when changing a NULL field to NOT NULL in
  1875. interactive mode.
  1876. """
  1877. class Author(models.Model):
  1878. slug = models.SlugField(null=False)
  1879. class Meta:
  1880. app_label = "migrations"
  1881. input_msg = (
  1882. "It is impossible to change a nullable field 'slug' on author to "
  1883. "non-nullable without providing a default. This is because the "
  1884. "database needs something to populate existing rows.\n"
  1885. "Please select a fix:\n"
  1886. " 1) Provide a one-off default now (will be set on all existing "
  1887. "rows with a null value for this column)\n"
  1888. " 2) Ignore for now. Existing rows that contain NULL values will "
  1889. "have to be handled manually, for example with a RunPython or "
  1890. "RunSQL operation.\n"
  1891. " 3) Quit and manually define a default value in models.py."
  1892. )
  1893. with self.temporary_migration_module(module="migrations.test_migrations"):
  1894. # No message appears if --dry-run.
  1895. with captured_stdout() as out:
  1896. call_command(
  1897. "makemigrations",
  1898. "migrations",
  1899. interactive=True,
  1900. dry_run=True,
  1901. )
  1902. self.assertNotIn(input_msg, out.getvalue())
  1903. # 3 - quit.
  1904. with mock.patch("builtins.input", return_value="3"):
  1905. with captured_stdout() as out, self.assertRaises(SystemExit):
  1906. call_command("makemigrations", "migrations", interactive=True)
  1907. self.assertIn(input_msg, out.getvalue())
  1908. # 1 - provide a default.
  1909. with mock.patch("builtins.input", return_value="1"):
  1910. with captured_stdout() as out:
  1911. call_command("makemigrations", "migrations", interactive=True)
  1912. output = out.getvalue()
  1913. self.assertIn(input_msg, output)
  1914. self.assertIn("Please enter the default value as valid Python.", output)
  1915. self.assertIn(
  1916. "The datetime and django.utils.timezone modules are "
  1917. "available, so it is possible to provide e.g. timezone.now as "
  1918. "a value",
  1919. output,
  1920. )
  1921. self.assertIn("Type 'exit' to exit this prompt", output)
  1922. def test_makemigrations_non_interactive_no_model_rename(self):
  1923. """
  1924. makemigrations adds and removes a possible model rename in
  1925. non-interactive mode.
  1926. """
  1927. class RenamedModel(models.Model):
  1928. silly_field = models.BooleanField(default=False)
  1929. class Meta:
  1930. app_label = "migrations"
  1931. out = io.StringIO()
  1932. with self.temporary_migration_module(
  1933. module="migrations.test_migrations_no_default"
  1934. ):
  1935. call_command("makemigrations", "migrations", interactive=False, stdout=out)
  1936. self.assertIn("Delete model SillyModel", out.getvalue())
  1937. self.assertIn("Create model RenamedModel", out.getvalue())
  1938. def test_makemigrations_non_interactive_no_field_rename(self):
  1939. """
  1940. makemigrations adds and removes a possible field rename in
  1941. non-interactive mode.
  1942. """
  1943. class SillyModel(models.Model):
  1944. silly_rename = models.BooleanField(default=False)
  1945. class Meta:
  1946. app_label = "migrations"
  1947. out = io.StringIO()
  1948. with self.temporary_migration_module(
  1949. module="migrations.test_migrations_no_default"
  1950. ):
  1951. call_command("makemigrations", "migrations", interactive=False, stdout=out)
  1952. self.assertIn("Remove field silly_field from sillymodel", out.getvalue())
  1953. self.assertIn("Add field silly_rename to sillymodel", out.getvalue())
  1954. @mock.patch("builtins.input", return_value="Y")
  1955. def test_makemigrations_model_rename_interactive(self, mock_input):
  1956. class RenamedModel(models.Model):
  1957. silly_field = models.BooleanField(default=False)
  1958. class Meta:
  1959. app_label = "migrations"
  1960. with self.temporary_migration_module(
  1961. module="migrations.test_migrations_no_default",
  1962. ):
  1963. with captured_stdout() as out:
  1964. call_command("makemigrations", "migrations", interactive=True)
  1965. self.assertIn("Rename model SillyModel to RenamedModel", out.getvalue())
  1966. @mock.patch("builtins.input", return_value="Y")
  1967. def test_makemigrations_field_rename_interactive(self, mock_input):
  1968. class SillyModel(models.Model):
  1969. silly_rename = models.BooleanField(default=False)
  1970. class Meta:
  1971. app_label = "migrations"
  1972. with self.temporary_migration_module(
  1973. module="migrations.test_migrations_no_default",
  1974. ):
  1975. with captured_stdout() as out:
  1976. call_command("makemigrations", "migrations", interactive=True)
  1977. self.assertIn(
  1978. "Rename field silly_field on sillymodel to silly_rename",
  1979. out.getvalue(),
  1980. )
  1981. def test_makemigrations_handle_merge(self):
  1982. """
  1983. makemigrations properly merges the conflicting migrations with --noinput.
  1984. """
  1985. out = io.StringIO()
  1986. with self.temporary_migration_module(
  1987. module="migrations.test_migrations_conflict"
  1988. ) as migration_dir:
  1989. call_command(
  1990. "makemigrations",
  1991. "migrations",
  1992. name="merge",
  1993. merge=True,
  1994. interactive=False,
  1995. stdout=out,
  1996. )
  1997. merge_file = os.path.join(migration_dir, "0003_merge.py")
  1998. self.assertTrue(os.path.exists(merge_file))
  1999. output = out.getvalue()
  2000. self.assertIn("Merging migrations", output)
  2001. self.assertIn("Branch 0002_second", output)
  2002. self.assertIn("Branch 0002_conflicting_second", output)
  2003. self.assertIn("Created new merge migration", output)
  2004. def test_makemigration_merge_dry_run(self):
  2005. """
  2006. makemigrations respects --dry-run option when fixing migration
  2007. conflicts (#24427).
  2008. """
  2009. out = io.StringIO()
  2010. with self.temporary_migration_module(
  2011. module="migrations.test_migrations_conflict"
  2012. ) as migration_dir:
  2013. call_command(
  2014. "makemigrations",
  2015. "migrations",
  2016. name="merge",
  2017. dry_run=True,
  2018. merge=True,
  2019. interactive=False,
  2020. stdout=out,
  2021. )
  2022. merge_file = os.path.join(migration_dir, "0003_merge.py")
  2023. self.assertFalse(os.path.exists(merge_file))
  2024. output = out.getvalue()
  2025. self.assertIn("Merging migrations", output)
  2026. self.assertIn("Branch 0002_second", output)
  2027. self.assertIn("Branch 0002_conflicting_second", output)
  2028. self.assertNotIn("Created new merge migration", output)
  2029. def test_makemigration_merge_dry_run_verbosity_3(self):
  2030. """
  2031. `makemigrations --merge --dry-run` writes the merge migration file to
  2032. stdout with `verbosity == 3` (#24427).
  2033. """
  2034. out = io.StringIO()
  2035. with self.temporary_migration_module(
  2036. module="migrations.test_migrations_conflict"
  2037. ) as migration_dir:
  2038. call_command(
  2039. "makemigrations",
  2040. "migrations",
  2041. name="merge",
  2042. dry_run=True,
  2043. merge=True,
  2044. interactive=False,
  2045. stdout=out,
  2046. verbosity=3,
  2047. )
  2048. merge_file = os.path.join(migration_dir, "0003_merge.py")
  2049. self.assertFalse(os.path.exists(merge_file))
  2050. output = out.getvalue()
  2051. self.assertIn("Merging migrations", output)
  2052. self.assertIn("Branch 0002_second", output)
  2053. self.assertIn("Branch 0002_conflicting_second", output)
  2054. self.assertNotIn("Created new merge migration", output)
  2055. # Additional output caused by verbosity 3
  2056. # The complete merge migration file that would be written
  2057. self.assertIn("class Migration(migrations.Migration):", output)
  2058. self.assertIn("dependencies = [", output)
  2059. self.assertIn("('migrations', '0002_second')", output)
  2060. self.assertIn("('migrations', '0002_conflicting_second')", output)
  2061. self.assertIn("operations = [", output)
  2062. self.assertIn("]", output)
  2063. def test_makemigrations_dry_run(self):
  2064. """
  2065. `makemigrations --dry-run` should not ask for defaults.
  2066. """
  2067. class SillyModel(models.Model):
  2068. silly_field = models.BooleanField(default=False)
  2069. silly_date = models.DateField() # Added field without a default
  2070. silly_auto_now = models.DateTimeField(auto_now_add=True)
  2071. class Meta:
  2072. app_label = "migrations"
  2073. out = io.StringIO()
  2074. with self.temporary_migration_module(
  2075. module="migrations.test_migrations_no_default"
  2076. ):
  2077. call_command("makemigrations", "migrations", dry_run=True, stdout=out)
  2078. # Output the expected changes directly, without asking for defaults
  2079. self.assertIn("Add field silly_date to sillymodel", out.getvalue())
  2080. def test_makemigrations_dry_run_verbosity_3(self):
  2081. """
  2082. Allow `makemigrations --dry-run` to output the migrations file to
  2083. stdout (with verbosity == 3).
  2084. """
  2085. class SillyModel(models.Model):
  2086. silly_field = models.BooleanField(default=False)
  2087. silly_char = models.CharField(default="")
  2088. class Meta:
  2089. app_label = "migrations"
  2090. out = io.StringIO()
  2091. with self.temporary_migration_module(
  2092. module="migrations.test_migrations_no_default"
  2093. ):
  2094. call_command(
  2095. "makemigrations", "migrations", dry_run=True, stdout=out, verbosity=3
  2096. )
  2097. # Normal --dry-run output
  2098. self.assertIn("+ Add field silly_char to sillymodel", out.getvalue())
  2099. # Additional output caused by verbosity 3
  2100. # The complete migrations file that would be written
  2101. self.assertIn("class Migration(migrations.Migration):", out.getvalue())
  2102. self.assertIn("dependencies = [", out.getvalue())
  2103. self.assertIn("('migrations', '0001_initial'),", out.getvalue())
  2104. self.assertIn("migrations.AddField(", out.getvalue())
  2105. self.assertIn("model_name='sillymodel',", out.getvalue())
  2106. self.assertIn("name='silly_char',", out.getvalue())
  2107. def test_makemigrations_scriptable(self):
  2108. """
  2109. With scriptable=True, log output is diverted to stderr, and only the
  2110. paths of generated migration files are written to stdout.
  2111. """
  2112. out = io.StringIO()
  2113. err = io.StringIO()
  2114. with self.temporary_migration_module(
  2115. module="migrations.migrations.test_migrations",
  2116. ) as migration_dir:
  2117. call_command(
  2118. "makemigrations",
  2119. "migrations",
  2120. scriptable=True,
  2121. stdout=out,
  2122. stderr=err,
  2123. )
  2124. initial_file = os.path.join(migration_dir, "0001_initial.py")
  2125. self.assertEqual(out.getvalue(), f"{initial_file}\n")
  2126. self.assertIn(" + Create model ModelWithCustomBase\n", err.getvalue())
  2127. @mock.patch("builtins.input", return_value="Y")
  2128. def test_makemigrations_scriptable_merge(self, mock_input):
  2129. out = io.StringIO()
  2130. err = io.StringIO()
  2131. with self.temporary_migration_module(
  2132. module="migrations.test_migrations_conflict",
  2133. ) as migration_dir:
  2134. call_command(
  2135. "makemigrations",
  2136. "migrations",
  2137. merge=True,
  2138. name="merge",
  2139. scriptable=True,
  2140. stdout=out,
  2141. stderr=err,
  2142. )
  2143. merge_file = os.path.join(migration_dir, "0003_merge.py")
  2144. self.assertEqual(out.getvalue(), f"{merge_file}\n")
  2145. self.assertIn(f"Created new merge migration {merge_file}", err.getvalue())
  2146. def test_makemigrations_failure_to_format_code(self):
  2147. self.assertFormatterFailureCaught("makemigrations", "migrations")
  2148. def test_merge_makemigrations_failure_to_format_code(self):
  2149. self.assertFormatterFailureCaught("makemigrations", "migrations", empty=True)
  2150. self.assertFormatterFailureCaught(
  2151. "makemigrations",
  2152. "migrations",
  2153. merge=True,
  2154. interactive=False,
  2155. module="migrations.test_migrations_conflict",
  2156. )
  2157. def test_makemigrations_migrations_modules_path_not_exist(self):
  2158. """
  2159. makemigrations creates migrations when specifying a custom location
  2160. for migration files using MIGRATION_MODULES if the custom path
  2161. doesn't already exist.
  2162. """
  2163. class SillyModel(models.Model):
  2164. silly_field = models.BooleanField(default=False)
  2165. class Meta:
  2166. app_label = "migrations"
  2167. out = io.StringIO()
  2168. migration_module = "migrations.test_migrations_path_doesnt_exist.foo.bar"
  2169. with self.temporary_migration_module(module=migration_module) as migration_dir:
  2170. call_command("makemigrations", "migrations", stdout=out)
  2171. # Migrations file is actually created in the expected path.
  2172. initial_file = os.path.join(migration_dir, "0001_initial.py")
  2173. self.assertTrue(os.path.exists(initial_file))
  2174. # Command output indicates the migration is created.
  2175. self.assertIn(" + Create model SillyModel", out.getvalue())
  2176. @override_settings(MIGRATION_MODULES={"migrations": "some.nonexistent.path"})
  2177. def test_makemigrations_migrations_modules_nonexistent_toplevel_package(self):
  2178. msg = (
  2179. "Could not locate an appropriate location to create migrations "
  2180. "package some.nonexistent.path. Make sure the toplevel package "
  2181. "exists and can be imported."
  2182. )
  2183. with self.assertRaisesMessage(ValueError, msg):
  2184. call_command("makemigrations", "migrations", empty=True, verbosity=0)
  2185. def test_makemigrations_interactive_by_default(self):
  2186. """
  2187. The user is prompted to merge by default if there are conflicts and
  2188. merge is True. Answer negative to differentiate it from behavior when
  2189. --noinput is specified.
  2190. """
  2191. # Monkeypatch interactive questioner to auto reject
  2192. out = io.StringIO()
  2193. with mock.patch("builtins.input", mock.Mock(return_value="N")):
  2194. with self.temporary_migration_module(
  2195. module="migrations.test_migrations_conflict"
  2196. ) as migration_dir:
  2197. call_command(
  2198. "makemigrations", "migrations", name="merge", merge=True, stdout=out
  2199. )
  2200. merge_file = os.path.join(migration_dir, "0003_merge.py")
  2201. # This will fail if interactive is False by default
  2202. self.assertFalse(os.path.exists(merge_file))
  2203. self.assertNotIn("Created new merge migration", out.getvalue())
  2204. @override_settings(
  2205. INSTALLED_APPS=[
  2206. "migrations",
  2207. "migrations.migrations_test_apps.unspecified_app_with_conflict",
  2208. ]
  2209. )
  2210. def test_makemigrations_unspecified_app_with_conflict_no_merge(self):
  2211. """
  2212. makemigrations does not raise a CommandError when an unspecified app
  2213. has conflicting migrations.
  2214. """
  2215. with self.temporary_migration_module(
  2216. module="migrations.test_migrations_no_changes"
  2217. ):
  2218. call_command("makemigrations", "migrations", merge=False, verbosity=0)
  2219. @override_settings(
  2220. INSTALLED_APPS=[
  2221. "migrations.migrations_test_apps.migrated_app",
  2222. "migrations.migrations_test_apps.unspecified_app_with_conflict",
  2223. ]
  2224. )
  2225. def test_makemigrations_unspecified_app_with_conflict_merge(self):
  2226. """
  2227. makemigrations does not create a merge for an unspecified app even if
  2228. it has conflicting migrations.
  2229. """
  2230. # Monkeypatch interactive questioner to auto accept
  2231. with mock.patch("builtins.input", mock.Mock(return_value="y")):
  2232. out = io.StringIO()
  2233. with self.temporary_migration_module(
  2234. app_label="migrated_app"
  2235. ) as migration_dir:
  2236. call_command(
  2237. "makemigrations",
  2238. "migrated_app",
  2239. name="merge",
  2240. merge=True,
  2241. interactive=True,
  2242. stdout=out,
  2243. )
  2244. merge_file = os.path.join(migration_dir, "0003_merge.py")
  2245. self.assertFalse(os.path.exists(merge_file))
  2246. self.assertIn("No conflicts detected to merge.", out.getvalue())
  2247. @override_settings(
  2248. INSTALLED_APPS=[
  2249. "migrations.migrations_test_apps.migrated_app",
  2250. "migrations.migrations_test_apps.conflicting_app_with_dependencies",
  2251. ]
  2252. )
  2253. def test_makemigrations_merge_dont_output_dependency_operations(self):
  2254. """
  2255. makemigrations --merge does not output any operations from apps that
  2256. don't belong to a given app.
  2257. """
  2258. # Monkeypatch interactive questioner to auto accept
  2259. with mock.patch("builtins.input", mock.Mock(return_value="N")):
  2260. out = io.StringIO()
  2261. with mock.patch(
  2262. "django.core.management.color.supports_color", lambda *args: False
  2263. ):
  2264. call_command(
  2265. "makemigrations",
  2266. "conflicting_app_with_dependencies",
  2267. merge=True,
  2268. interactive=True,
  2269. stdout=out,
  2270. )
  2271. self.assertEqual(
  2272. out.getvalue().lower(),
  2273. "merging conflicting_app_with_dependencies\n"
  2274. " branch 0002_conflicting_second\n"
  2275. " + create model something\n"
  2276. " branch 0002_second\n"
  2277. " - delete model tribble\n"
  2278. " - remove field silly_field from author\n"
  2279. " + add field rating to author\n"
  2280. " + create model book\n"
  2281. "\n"
  2282. "merging will only work if the operations printed above do not "
  2283. "conflict\n"
  2284. "with each other (working on different fields or models)\n"
  2285. "should these migration branches be merged? [y/n] ",
  2286. )
  2287. def test_makemigrations_with_custom_name(self):
  2288. """
  2289. makemigrations --name generate a custom migration name.
  2290. """
  2291. with self.temporary_migration_module() as migration_dir:
  2292. def cmd(migration_count, migration_name, *args):
  2293. call_command(
  2294. "makemigrations",
  2295. "migrations",
  2296. "--verbosity",
  2297. "0",
  2298. "--name",
  2299. migration_name,
  2300. *args,
  2301. )
  2302. migration_file = os.path.join(
  2303. migration_dir, "%s_%s.py" % (migration_count, migration_name)
  2304. )
  2305. # Check for existing migration file in migration folder
  2306. self.assertTrue(os.path.exists(migration_file))
  2307. with open(migration_file, encoding="utf-8") as fp:
  2308. content = fp.read()
  2309. content = content.replace(" ", "")
  2310. return content
  2311. # generate an initial migration
  2312. migration_name_0001 = "my_initial_migration"
  2313. content = cmd("0001", migration_name_0001)
  2314. self.assertIn(
  2315. "dependencies=[]" if HAS_BLACK else "dependencies=[\n]", content
  2316. )
  2317. # importlib caches os.listdir() on some platforms like macOS
  2318. # (#23850).
  2319. if hasattr(importlib, "invalidate_caches"):
  2320. importlib.invalidate_caches()
  2321. # generate an empty migration
  2322. migration_name_0002 = "my_custom_migration"
  2323. content = cmd("0002", migration_name_0002, "--empty")
  2324. if HAS_BLACK:
  2325. template_str = 'dependencies=[\n("migrations","0001_%s"),\n]'
  2326. else:
  2327. template_str = "dependencies=[\n('migrations','0001_%s'),\n]"
  2328. self.assertIn(
  2329. template_str % migration_name_0001,
  2330. content,
  2331. )
  2332. self.assertIn("operations=[]" if HAS_BLACK else "operations=[\n]", content)
  2333. def test_makemigrations_with_invalid_custom_name(self):
  2334. msg = "The migration name must be a valid Python identifier."
  2335. with self.assertRaisesMessage(CommandError, msg):
  2336. call_command(
  2337. "makemigrations", "migrations", "--name", "invalid name", "--empty"
  2338. )
  2339. def test_makemigrations_check_with_changes(self):
  2340. """
  2341. makemigrations --check should exit with a non-zero status when
  2342. there are changes to an app requiring migrations.
  2343. """
  2344. out = io.StringIO()
  2345. with self.temporary_migration_module() as tmpdir:
  2346. with self.assertRaises(SystemExit) as cm:
  2347. call_command(
  2348. "makemigrations",
  2349. "--check",
  2350. "migrations",
  2351. stdout=out,
  2352. )
  2353. self.assertEqual(os.listdir(tmpdir), ["__init__.py"])
  2354. self.assertEqual(cm.exception.code, 1)
  2355. self.assertIn("Migrations for 'migrations':", out.getvalue())
  2356. def test_makemigrations_check_no_changes(self):
  2357. """
  2358. makemigrations --check should exit with a zero status when there are no
  2359. changes.
  2360. """
  2361. out = io.StringIO()
  2362. with self.temporary_migration_module(
  2363. module="migrations.test_migrations_no_changes"
  2364. ):
  2365. call_command("makemigrations", "--check", "migrations", stdout=out)
  2366. self.assertEqual("No changes detected in app 'migrations'\n", out.getvalue())
  2367. def test_makemigrations_migration_path_output(self):
  2368. """
  2369. makemigrations should print the relative paths to the migrations unless
  2370. they are outside of the current tree, in which case the absolute path
  2371. should be shown.
  2372. """
  2373. out = io.StringIO()
  2374. apps.register_model("migrations", UnicodeModel)
  2375. with self.temporary_migration_module() as migration_dir:
  2376. call_command("makemigrations", "migrations", stdout=out)
  2377. self.assertIn(
  2378. os.path.join(migration_dir, "0001_initial.py"), out.getvalue()
  2379. )
  2380. def test_makemigrations_migration_path_output_valueerror(self):
  2381. """
  2382. makemigrations prints the absolute path if os.path.relpath() raises a
  2383. ValueError when it's impossible to obtain a relative path, e.g. on
  2384. Windows if Django is installed on a different drive than where the
  2385. migration files are created.
  2386. """
  2387. out = io.StringIO()
  2388. with self.temporary_migration_module() as migration_dir:
  2389. with mock.patch("os.path.relpath", side_effect=ValueError):
  2390. call_command("makemigrations", "migrations", stdout=out)
  2391. self.assertIn(os.path.join(migration_dir, "0001_initial.py"), out.getvalue())
  2392. def test_makemigrations_inconsistent_history(self):
  2393. """
  2394. makemigrations should raise InconsistentMigrationHistory exception if
  2395. there are some migrations applied before their dependencies.
  2396. """
  2397. recorder = MigrationRecorder(connection)
  2398. recorder.record_applied("migrations", "0002_second")
  2399. msg = (
  2400. "Migration migrations.0002_second is applied before its dependency "
  2401. "migrations.0001_initial"
  2402. )
  2403. with self.temporary_migration_module(module="migrations.test_migrations"):
  2404. with self.assertRaisesMessage(InconsistentMigrationHistory, msg):
  2405. call_command("makemigrations")
  2406. def test_makemigrations_inconsistent_history_db_failure(self):
  2407. msg = (
  2408. "Got an error checking a consistent migration history performed "
  2409. "for database connection 'default': could not connect to server"
  2410. )
  2411. with mock.patch(
  2412. "django.db.migrations.loader.MigrationLoader.check_consistent_history",
  2413. side_effect=OperationalError("could not connect to server"),
  2414. ):
  2415. with self.temporary_migration_module():
  2416. with self.assertWarns(RuntimeWarning) as cm:
  2417. call_command("makemigrations", verbosity=0)
  2418. self.assertEqual(str(cm.warning), msg)
  2419. @mock.patch("builtins.input", return_value="1")
  2420. @mock.patch(
  2421. "django.db.migrations.questioner.sys.stdin",
  2422. mock.MagicMock(encoding=sys.getdefaultencoding()),
  2423. )
  2424. def test_makemigrations_auto_now_add_interactive(self, *args):
  2425. """
  2426. makemigrations prompts the user when adding auto_now_add to an existing
  2427. model.
  2428. """
  2429. class Entry(models.Model):
  2430. title = models.CharField(max_length=255)
  2431. creation_date = models.DateTimeField(auto_now_add=True)
  2432. class Meta:
  2433. app_label = "migrations"
  2434. input_msg = (
  2435. "It is impossible to add the field 'creation_date' with "
  2436. "'auto_now_add=True' to entry without providing a default. This "
  2437. "is because the database needs something to populate existing "
  2438. "rows.\n"
  2439. " 1) Provide a one-off default now which will be set on all "
  2440. "existing rows\n"
  2441. " 2) Quit and manually define a default value in models.py."
  2442. )
  2443. # Monkeypatch interactive questioner to auto accept
  2444. prompt_stdout = io.StringIO()
  2445. with self.temporary_migration_module(module="migrations.test_auto_now_add"):
  2446. call_command(
  2447. "makemigrations", "migrations", interactive=True, stdout=prompt_stdout
  2448. )
  2449. prompt_output = prompt_stdout.getvalue()
  2450. self.assertIn(input_msg, prompt_output)
  2451. self.assertIn("Please enter the default value as valid Python.", prompt_output)
  2452. self.assertIn(
  2453. "Accept the default 'timezone.now' by pressing 'Enter' or provide "
  2454. "another value.",
  2455. prompt_output,
  2456. )
  2457. self.assertIn("Type 'exit' to exit this prompt", prompt_output)
  2458. self.assertIn("Add field creation_date to entry", prompt_output)
  2459. @mock.patch("builtins.input", return_value="2")
  2460. def test_makemigrations_auto_now_add_interactive_quit(self, mock_input):
  2461. class Author(models.Model):
  2462. publishing_date = models.DateField(auto_now_add=True)
  2463. class Meta:
  2464. app_label = "migrations"
  2465. with self.temporary_migration_module(module="migrations.test_migrations"):
  2466. with captured_stdout():
  2467. with self.assertRaises(SystemExit):
  2468. call_command("makemigrations", "migrations", interactive=True)
  2469. def test_makemigrations_non_interactive_auto_now_add_addition(self):
  2470. """
  2471. Non-interactive makemigrations fails when a default is missing on a
  2472. new field when auto_now_add=True.
  2473. """
  2474. class Entry(models.Model):
  2475. creation_date = models.DateTimeField(auto_now_add=True)
  2476. class Meta:
  2477. app_label = "migrations"
  2478. with self.temporary_migration_module(module="migrations.test_auto_now_add"):
  2479. with self.assertRaises(SystemExit), captured_stdout() as out:
  2480. call_command("makemigrations", "migrations", interactive=False)
  2481. self.assertIn(
  2482. "Field 'creation_date' on model 'entry' not migrated: it is "
  2483. "impossible to add a field with 'auto_now_add=True' without "
  2484. "specifying a default.",
  2485. out.getvalue(),
  2486. )
  2487. def test_makemigrations_interactive_unique_callable_default_addition(self):
  2488. """
  2489. makemigrations prompts the user when adding a unique field with
  2490. a callable default.
  2491. """
  2492. class Book(models.Model):
  2493. created = models.DateTimeField(unique=True, default=timezone.now)
  2494. class Meta:
  2495. app_label = "migrations"
  2496. version = get_docs_version()
  2497. input_msg = (
  2498. f"Callable default on unique field book.created will not generate "
  2499. f"unique values upon migrating.\n"
  2500. f"Please choose how to proceed:\n"
  2501. f" 1) Continue making this migration as the first step in writing "
  2502. f"a manual migration to generate unique values described here: "
  2503. f"https://docs.djangoproject.com/en/{version}/howto/"
  2504. f"writing-migrations/#migrations-that-add-unique-fields.\n"
  2505. f" 2) Quit and edit field options in models.py.\n"
  2506. )
  2507. with self.temporary_migration_module(module="migrations.test_migrations"):
  2508. # 2 - quit.
  2509. with mock.patch("builtins.input", return_value="2"):
  2510. with captured_stdout() as out, self.assertRaises(SystemExit):
  2511. call_command("makemigrations", "migrations", interactive=True)
  2512. out_value = out.getvalue()
  2513. self.assertIn(input_msg, out_value)
  2514. self.assertNotIn("Add field created to book", out_value)
  2515. # 1 - continue.
  2516. with mock.patch("builtins.input", return_value="1"):
  2517. with captured_stdout() as out:
  2518. call_command("makemigrations", "migrations", interactive=True)
  2519. out_value = out.getvalue()
  2520. self.assertIn(input_msg, out_value)
  2521. self.assertIn("Add field created to book", out_value)
  2522. def test_makemigrations_non_interactive_unique_callable_default_addition(self):
  2523. class Book(models.Model):
  2524. created = models.DateTimeField(unique=True, default=timezone.now)
  2525. class Meta:
  2526. app_label = "migrations"
  2527. with self.temporary_migration_module(module="migrations.test_migrations"):
  2528. with captured_stdout() as out:
  2529. call_command("makemigrations", "migrations", interactive=False)
  2530. out_value = out.getvalue()
  2531. self.assertIn("Add field created to book", out_value)
  2532. @override_settings(
  2533. MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"},
  2534. )
  2535. def test_makemigrations_continues_number_sequence_after_squash(self):
  2536. with self.temporary_migration_module(
  2537. module="migrations.test_migrations_squashed"
  2538. ):
  2539. with captured_stdout() as out:
  2540. call_command(
  2541. "makemigrations",
  2542. "migrations",
  2543. interactive=False,
  2544. empty=True,
  2545. )
  2546. out_value = out.getvalue()
  2547. self.assertIn("0003_auto", out_value)
  2548. def test_makemigrations_update(self):
  2549. with self.temporary_migration_module(
  2550. module="migrations.test_migrations"
  2551. ) as migration_dir:
  2552. migration_file = os.path.join(migration_dir, "0002_second.py")
  2553. with open(migration_file) as fp:
  2554. initial_content = fp.read()
  2555. with captured_stdout() as out:
  2556. call_command("makemigrations", "migrations", update=True)
  2557. self.assertFalse(
  2558. any(
  2559. filename.startswith("0003")
  2560. for filename in os.listdir(migration_dir)
  2561. )
  2562. )
  2563. self.assertIs(os.path.exists(migration_file), False)
  2564. new_migration_file = os.path.join(
  2565. migration_dir,
  2566. "0002_delete_tribble_author_rating_modelwithcustombase_and_more.py",
  2567. )
  2568. with open(new_migration_file) as fp:
  2569. self.assertNotEqual(initial_content, fp.read())
  2570. self.assertIn(f"Deleted {migration_file}", out.getvalue())
  2571. def test_makemigrations_update_existing_name(self):
  2572. with self.temporary_migration_module(
  2573. module="migrations.test_auto_now_add"
  2574. ) as migration_dir:
  2575. migration_file = os.path.join(migration_dir, "0001_initial.py")
  2576. with open(migration_file) as fp:
  2577. initial_content = fp.read()
  2578. with captured_stdout() as out:
  2579. call_command("makemigrations", "migrations", update=True)
  2580. self.assertIs(os.path.exists(migration_file), False)
  2581. new_migration_file = os.path.join(
  2582. migration_dir,
  2583. "0001_initial_updated.py",
  2584. )
  2585. with open(new_migration_file) as fp:
  2586. self.assertNotEqual(initial_content, fp.read())
  2587. self.assertIn(f"Deleted {migration_file}", out.getvalue())
  2588. def test_makemigrations_update_custom_name(self):
  2589. custom_name = "delete_something"
  2590. with self.temporary_migration_module(
  2591. module="migrations.test_migrations"
  2592. ) as migration_dir:
  2593. old_migration_file = os.path.join(migration_dir, "0002_second.py")
  2594. with open(old_migration_file) as fp:
  2595. initial_content = fp.read()
  2596. with captured_stdout() as out:
  2597. call_command(
  2598. "makemigrations", "migrations", update=True, name=custom_name
  2599. )
  2600. self.assertFalse(
  2601. any(
  2602. filename.startswith("0003")
  2603. for filename in os.listdir(migration_dir)
  2604. )
  2605. )
  2606. self.assertIs(os.path.exists(old_migration_file), False)
  2607. new_migration_file = os.path.join(migration_dir, f"0002_{custom_name}.py")
  2608. self.assertIs(os.path.exists(new_migration_file), True)
  2609. with open(new_migration_file) as fp:
  2610. self.assertNotEqual(initial_content, fp.read())
  2611. self.assertIn(f"Deleted {old_migration_file}", out.getvalue())
  2612. def test_makemigrations_update_applied_migration(self):
  2613. recorder = MigrationRecorder(connection)
  2614. recorder.record_applied("migrations", "0001_initial")
  2615. recorder.record_applied("migrations", "0002_second")
  2616. with self.temporary_migration_module(module="migrations.test_migrations"):
  2617. msg = "Cannot update applied migration 'migrations.0002_second'."
  2618. with self.assertRaisesMessage(CommandError, msg):
  2619. call_command("makemigrations", "migrations", update=True)
  2620. def test_makemigrations_update_no_migration(self):
  2621. with self.temporary_migration_module(module="migrations.test_migrations_empty"):
  2622. msg = "App migrations has no migration, cannot update last migration."
  2623. with self.assertRaisesMessage(CommandError, msg):
  2624. call_command("makemigrations", "migrations", update=True)
  2625. def test_makemigrations_update_squash_migration(self):
  2626. with self.temporary_migration_module(
  2627. module="migrations.test_migrations_squashed"
  2628. ):
  2629. msg = "Cannot update squash migration 'migrations.0001_squashed_0002'."
  2630. with self.assertRaisesMessage(CommandError, msg):
  2631. call_command("makemigrations", "migrations", update=True)
  2632. def test_makemigrations_update_manual_porting(self):
  2633. with self.temporary_migration_module(
  2634. module="migrations.test_migrations_plan"
  2635. ) as migration_dir:
  2636. with captured_stdout() as out:
  2637. call_command("makemigrations", "migrations", update=True)
  2638. # Previous migration exists.
  2639. previous_migration_file = os.path.join(migration_dir, "0005_fifth.py")
  2640. self.assertIs(os.path.exists(previous_migration_file), True)
  2641. # New updated migration exists.
  2642. files = [f for f in os.listdir(migration_dir) if f.startswith("0005_auto")]
  2643. updated_migration_file = os.path.join(migration_dir, files[0])
  2644. self.assertIs(os.path.exists(updated_migration_file), True)
  2645. self.assertIn(
  2646. f"Updated migration {updated_migration_file} requires manual porting.\n"
  2647. f"Previous migration {previous_migration_file} was kept and must be "
  2648. f"deleted after porting functions manually.",
  2649. out.getvalue(),
  2650. )
  2651. @override_settings(
  2652. INSTALLED_APPS=[
  2653. "migrations.migrations_test_apps.alter_fk.author_app",
  2654. "migrations.migrations_test_apps.alter_fk.book_app",
  2655. ]
  2656. )
  2657. def test_makemigrations_update_dependency_migration(self):
  2658. with self.temporary_migration_module(app_label="book_app"):
  2659. msg = (
  2660. "Cannot update migration 'book_app.0001_initial' that migrations "
  2661. "'author_app.0002_alter_id' depend on."
  2662. )
  2663. with self.assertRaisesMessage(CommandError, msg):
  2664. call_command("makemigrations", "book_app", update=True)
  2665. class SquashMigrationsTests(MigrationTestBase):
  2666. """
  2667. Tests running the squashmigrations command.
  2668. """
  2669. def test_squashmigrations_squashes(self):
  2670. """
  2671. squashmigrations squashes migrations.
  2672. """
  2673. out = io.StringIO()
  2674. with self.temporary_migration_module(
  2675. module="migrations.test_migrations"
  2676. ) as migration_dir:
  2677. call_command(
  2678. "squashmigrations",
  2679. "migrations",
  2680. "0002",
  2681. interactive=False,
  2682. stdout=out,
  2683. no_color=True,
  2684. )
  2685. squashed_migration_file = os.path.join(
  2686. migration_dir, "0001_squashed_0002_second.py"
  2687. )
  2688. self.assertTrue(os.path.exists(squashed_migration_file))
  2689. self.assertEqual(
  2690. out.getvalue(),
  2691. "Will squash the following migrations:\n"
  2692. " - 0001_initial\n"
  2693. " - 0002_second\n"
  2694. "Optimizing...\n"
  2695. " Optimized from 8 operations to 2 operations.\n"
  2696. "Created new squashed migration %s\n"
  2697. " You should commit this migration but leave the old ones in place;\n"
  2698. " the new migration will be used for new installs. Once you are sure\n"
  2699. " all instances of the codebase have applied the migrations you "
  2700. "squashed,\n"
  2701. " you can delete them.\n" % squashed_migration_file,
  2702. )
  2703. def test_squashmigrations_initial_attribute(self):
  2704. with self.temporary_migration_module(
  2705. module="migrations.test_migrations"
  2706. ) as migration_dir:
  2707. call_command(
  2708. "squashmigrations", "migrations", "0002", interactive=False, verbosity=0
  2709. )
  2710. squashed_migration_file = os.path.join(
  2711. migration_dir, "0001_squashed_0002_second.py"
  2712. )
  2713. with open(squashed_migration_file, encoding="utf-8") as fp:
  2714. content = fp.read()
  2715. self.assertIn("initial = True", content)
  2716. def test_squashmigrations_optimizes(self):
  2717. """
  2718. squashmigrations optimizes operations.
  2719. """
  2720. out = io.StringIO()
  2721. with self.temporary_migration_module(module="migrations.test_migrations"):
  2722. call_command(
  2723. "squashmigrations",
  2724. "migrations",
  2725. "0002",
  2726. interactive=False,
  2727. verbosity=1,
  2728. stdout=out,
  2729. )
  2730. self.assertIn("Optimized from 8 operations to 2 operations.", out.getvalue())
  2731. def test_ticket_23799_squashmigrations_no_optimize(self):
  2732. """
  2733. squashmigrations --no-optimize doesn't optimize operations.
  2734. """
  2735. out = io.StringIO()
  2736. with self.temporary_migration_module(module="migrations.test_migrations"):
  2737. call_command(
  2738. "squashmigrations",
  2739. "migrations",
  2740. "0002",
  2741. interactive=False,
  2742. verbosity=1,
  2743. no_optimize=True,
  2744. stdout=out,
  2745. )
  2746. self.assertIn("Skipping optimization", out.getvalue())
  2747. def test_squashmigrations_valid_start(self):
  2748. """
  2749. squashmigrations accepts a starting migration.
  2750. """
  2751. out = io.StringIO()
  2752. with self.temporary_migration_module(
  2753. module="migrations.test_migrations_no_changes"
  2754. ) as migration_dir:
  2755. call_command(
  2756. "squashmigrations",
  2757. "migrations",
  2758. "0002",
  2759. "0003",
  2760. interactive=False,
  2761. verbosity=1,
  2762. stdout=out,
  2763. )
  2764. squashed_migration_file = os.path.join(
  2765. migration_dir, "0002_second_squashed_0003_third.py"
  2766. )
  2767. with open(squashed_migration_file, encoding="utf-8") as fp:
  2768. content = fp.read()
  2769. if HAS_BLACK:
  2770. test_str = ' ("migrations", "0001_initial")'
  2771. else:
  2772. test_str = " ('migrations', '0001_initial')"
  2773. self.assertIn(test_str, content)
  2774. self.assertNotIn("initial = True", content)
  2775. out = out.getvalue()
  2776. self.assertNotIn(" - 0001_initial", out)
  2777. self.assertIn(" - 0002_second", out)
  2778. self.assertIn(" - 0003_third", out)
  2779. def test_squashmigrations_invalid_start(self):
  2780. """
  2781. squashmigrations doesn't accept a starting migration after the ending migration.
  2782. """
  2783. with self.temporary_migration_module(
  2784. module="migrations.test_migrations_no_changes"
  2785. ):
  2786. msg = (
  2787. "The migration 'migrations.0003_third' cannot be found. Maybe "
  2788. "it comes after the migration 'migrations.0002_second'"
  2789. )
  2790. with self.assertRaisesMessage(CommandError, msg):
  2791. call_command(
  2792. "squashmigrations",
  2793. "migrations",
  2794. "0003",
  2795. "0002",
  2796. interactive=False,
  2797. verbosity=0,
  2798. )
  2799. def test_squashed_name_with_start_migration_name(self):
  2800. """--squashed-name specifies the new migration's name."""
  2801. squashed_name = "squashed_name"
  2802. with self.temporary_migration_module(
  2803. module="migrations.test_migrations"
  2804. ) as migration_dir:
  2805. call_command(
  2806. "squashmigrations",
  2807. "migrations",
  2808. "0001",
  2809. "0002",
  2810. squashed_name=squashed_name,
  2811. interactive=False,
  2812. verbosity=0,
  2813. )
  2814. squashed_migration_file = os.path.join(
  2815. migration_dir, "0001_%s.py" % squashed_name
  2816. )
  2817. self.assertTrue(os.path.exists(squashed_migration_file))
  2818. def test_squashed_name_without_start_migration_name(self):
  2819. """--squashed-name also works if a start migration is omitted."""
  2820. squashed_name = "squashed_name"
  2821. with self.temporary_migration_module(
  2822. module="migrations.test_migrations"
  2823. ) as migration_dir:
  2824. call_command(
  2825. "squashmigrations",
  2826. "migrations",
  2827. "0001",
  2828. squashed_name=squashed_name,
  2829. interactive=False,
  2830. verbosity=0,
  2831. )
  2832. squashed_migration_file = os.path.join(
  2833. migration_dir, "0001_%s.py" % squashed_name
  2834. )
  2835. self.assertTrue(os.path.exists(squashed_migration_file))
  2836. def test_squashed_name_exists(self):
  2837. msg = "Migration 0001_initial already exists. Use a different name."
  2838. with self.temporary_migration_module(module="migrations.test_migrations"):
  2839. with self.assertRaisesMessage(CommandError, msg):
  2840. call_command(
  2841. "squashmigrations",
  2842. "migrations",
  2843. "0001",
  2844. "0002",
  2845. squashed_name="initial",
  2846. interactive=False,
  2847. verbosity=0,
  2848. )
  2849. def test_squashmigrations_manual_porting(self):
  2850. out = io.StringIO()
  2851. with self.temporary_migration_module(
  2852. module="migrations.test_migrations_manual_porting",
  2853. ) as migration_dir:
  2854. call_command(
  2855. "squashmigrations",
  2856. "migrations",
  2857. "0002",
  2858. interactive=False,
  2859. stdout=out,
  2860. no_color=True,
  2861. )
  2862. squashed_migration_file = os.path.join(
  2863. migration_dir,
  2864. "0001_squashed_0002_second.py",
  2865. )
  2866. self.assertTrue(os.path.exists(squashed_migration_file))
  2867. black_warning = ""
  2868. if HAS_BLACK:
  2869. black_warning = (
  2870. "Squashed migration couldn't be formatted using the "
  2871. '"black" command. You can call it manually.\n'
  2872. )
  2873. self.assertEqual(
  2874. out.getvalue(),
  2875. f"Will squash the following migrations:\n"
  2876. f" - 0001_initial\n"
  2877. f" - 0002_second\n"
  2878. f"Optimizing...\n"
  2879. f" No optimizations possible.\n"
  2880. f"Created new squashed migration {squashed_migration_file}\n"
  2881. f" You should commit this migration but leave the old ones in place;\n"
  2882. f" the new migration will be used for new installs. Once you are sure\n"
  2883. f" all instances of the codebase have applied the migrations you "
  2884. f"squashed,\n"
  2885. f" you can delete them.\n"
  2886. f"Manual porting required\n"
  2887. f" Your migrations contained functions that must be manually copied "
  2888. f"over,\n"
  2889. f" as we could not safely copy their implementation.\n"
  2890. f" See the comment at the top of the squashed migration for details.\n"
  2891. + black_warning,
  2892. )
  2893. def test_failure_to_format_code(self):
  2894. self.assertFormatterFailureCaught(
  2895. "squashmigrations", "migrations", "0002", interactive=False
  2896. )
  2897. class AppLabelErrorTests(TestCase):
  2898. """
  2899. This class inherits TestCase because MigrationTestBase uses
  2900. `available_apps = ['migrations']` which means that it's the only installed
  2901. app. 'django.contrib.auth' must be in INSTALLED_APPS for some of these
  2902. tests.
  2903. """
  2904. nonexistent_app_error = "No installed app with label 'nonexistent_app'."
  2905. did_you_mean_auth_error = (
  2906. "No installed app with label 'django.contrib.auth'. Did you mean 'auth'?"
  2907. )
  2908. def test_makemigrations_nonexistent_app_label(self):
  2909. err = io.StringIO()
  2910. with self.assertRaises(SystemExit):
  2911. call_command("makemigrations", "nonexistent_app", stderr=err)
  2912. self.assertIn(self.nonexistent_app_error, err.getvalue())
  2913. def test_makemigrations_app_name_specified_as_label(self):
  2914. err = io.StringIO()
  2915. with self.assertRaises(SystemExit):
  2916. call_command("makemigrations", "django.contrib.auth", stderr=err)
  2917. self.assertIn(self.did_you_mean_auth_error, err.getvalue())
  2918. def test_migrate_nonexistent_app_label(self):
  2919. with self.assertRaisesMessage(CommandError, self.nonexistent_app_error):
  2920. call_command("migrate", "nonexistent_app")
  2921. def test_migrate_app_name_specified_as_label(self):
  2922. with self.assertRaisesMessage(CommandError, self.did_you_mean_auth_error):
  2923. call_command("migrate", "django.contrib.auth")
  2924. def test_showmigrations_nonexistent_app_label(self):
  2925. err = io.StringIO()
  2926. with self.assertRaises(SystemExit):
  2927. call_command("showmigrations", "nonexistent_app", stderr=err)
  2928. self.assertIn(self.nonexistent_app_error, err.getvalue())
  2929. def test_showmigrations_app_name_specified_as_label(self):
  2930. err = io.StringIO()
  2931. with self.assertRaises(SystemExit):
  2932. call_command("showmigrations", "django.contrib.auth", stderr=err)
  2933. self.assertIn(self.did_you_mean_auth_error, err.getvalue())
  2934. def test_sqlmigrate_nonexistent_app_label(self):
  2935. with self.assertRaisesMessage(CommandError, self.nonexistent_app_error):
  2936. call_command("sqlmigrate", "nonexistent_app", "0002")
  2937. def test_sqlmigrate_app_name_specified_as_label(self):
  2938. with self.assertRaisesMessage(CommandError, self.did_you_mean_auth_error):
  2939. call_command("sqlmigrate", "django.contrib.auth", "0002")
  2940. def test_squashmigrations_nonexistent_app_label(self):
  2941. with self.assertRaisesMessage(CommandError, self.nonexistent_app_error):
  2942. call_command("squashmigrations", "nonexistent_app", "0002")
  2943. def test_squashmigrations_app_name_specified_as_label(self):
  2944. with self.assertRaisesMessage(CommandError, self.did_you_mean_auth_error):
  2945. call_command("squashmigrations", "django.contrib.auth", "0002")
  2946. def test_optimizemigration_nonexistent_app_label(self):
  2947. with self.assertRaisesMessage(CommandError, self.nonexistent_app_error):
  2948. call_command("optimizemigration", "nonexistent_app", "0002")
  2949. def test_optimizemigration_app_name_specified_as_label(self):
  2950. with self.assertRaisesMessage(CommandError, self.did_you_mean_auth_error):
  2951. call_command("optimizemigration", "django.contrib.auth", "0002")
  2952. class OptimizeMigrationTests(MigrationTestBase):
  2953. def test_no_optimization_possible(self):
  2954. out = io.StringIO()
  2955. with self.temporary_migration_module(
  2956. module="migrations.test_migrations"
  2957. ) as migration_dir:
  2958. call_command(
  2959. "optimizemigration", "migrations", "0002", stdout=out, no_color=True
  2960. )
  2961. migration_file = os.path.join(migration_dir, "0002_second.py")
  2962. self.assertTrue(os.path.exists(migration_file))
  2963. call_command(
  2964. "optimizemigration",
  2965. "migrations",
  2966. "0002",
  2967. stdout=out,
  2968. no_color=True,
  2969. verbosity=0,
  2970. )
  2971. self.assertEqual(out.getvalue(), "No optimizations possible.\n")
  2972. def test_optimization(self):
  2973. out = io.StringIO()
  2974. with self.temporary_migration_module(
  2975. module="migrations.test_migrations"
  2976. ) as migration_dir:
  2977. call_command(
  2978. "optimizemigration", "migrations", "0001", stdout=out, no_color=True
  2979. )
  2980. initial_migration_file = os.path.join(migration_dir, "0001_initial.py")
  2981. self.assertTrue(os.path.exists(initial_migration_file))
  2982. with open(initial_migration_file) as fp:
  2983. content = fp.read()
  2984. self.assertIn(
  2985. (
  2986. '("bool", models.BooleanField'
  2987. if HAS_BLACK
  2988. else "('bool', models.BooleanField"
  2989. ),
  2990. content,
  2991. )
  2992. self.assertEqual(
  2993. out.getvalue(),
  2994. f"Optimizing from 4 operations to 2 operations.\n"
  2995. f"Optimized migration {initial_migration_file}\n",
  2996. )
  2997. def test_optimization_no_verbosity(self):
  2998. out = io.StringIO()
  2999. with self.temporary_migration_module(
  3000. module="migrations.test_migrations"
  3001. ) as migration_dir:
  3002. call_command(
  3003. "optimizemigration",
  3004. "migrations",
  3005. "0001",
  3006. stdout=out,
  3007. no_color=True,
  3008. verbosity=0,
  3009. )
  3010. initial_migration_file = os.path.join(migration_dir, "0001_initial.py")
  3011. self.assertTrue(os.path.exists(initial_migration_file))
  3012. with open(initial_migration_file) as fp:
  3013. content = fp.read()
  3014. self.assertIn(
  3015. (
  3016. '("bool", models.BooleanField'
  3017. if HAS_BLACK
  3018. else "('bool', models.BooleanField"
  3019. ),
  3020. content,
  3021. )
  3022. self.assertEqual(out.getvalue(), "")
  3023. def test_creates_replace_migration_manual_porting(self):
  3024. out = io.StringIO()
  3025. with self.temporary_migration_module(
  3026. module="migrations.test_migrations_manual_porting"
  3027. ) as migration_dir:
  3028. call_command(
  3029. "optimizemigration", "migrations", "0003", stdout=out, no_color=True
  3030. )
  3031. optimized_migration_file = os.path.join(
  3032. migration_dir, "0003_third_optimized.py"
  3033. )
  3034. self.assertTrue(os.path.exists(optimized_migration_file))
  3035. with open(optimized_migration_file) as fp:
  3036. content = fp.read()
  3037. self.assertIn("replaces = [", content)
  3038. black_warning = ""
  3039. if HAS_BLACK:
  3040. black_warning = (
  3041. "Optimized migration couldn't be formatted using the "
  3042. '"black" command. You can call it manually.\n'
  3043. )
  3044. self.assertEqual(
  3045. out.getvalue(),
  3046. "Optimizing from 3 operations to 2 operations.\n"
  3047. "Manual porting required\n"
  3048. " Your migrations contained functions that must be manually copied over,\n"
  3049. " as we could not safely copy their implementation.\n"
  3050. " See the comment at the top of the optimized migration for details.\n"
  3051. + black_warning
  3052. + f"Optimized migration {optimized_migration_file}\n",
  3053. )
  3054. def test_fails_squash_migration_manual_porting(self):
  3055. out = io.StringIO()
  3056. with self.temporary_migration_module(
  3057. module="migrations.test_migrations_manual_porting"
  3058. ) as migration_dir:
  3059. version = get_docs_version()
  3060. msg = (
  3061. f"Migration will require manual porting but is already a squashed "
  3062. f"migration.\nTransition to a normal migration first: "
  3063. f"https://docs.djangoproject.com/en/{version}/topics/migrations/"
  3064. f"#squashing-migrations"
  3065. )
  3066. with self.assertRaisesMessage(CommandError, msg):
  3067. call_command("optimizemigration", "migrations", "0004", stdout=out)
  3068. optimized_migration_file = os.path.join(
  3069. migration_dir, "0004_fourth_optimized.py"
  3070. )
  3071. self.assertFalse(os.path.exists(optimized_migration_file))
  3072. self.assertEqual(
  3073. out.getvalue(), "Optimizing from 3 operations to 2 operations.\n"
  3074. )
  3075. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  3076. def test_optimizemigration_check(self):
  3077. with self.assertRaises(SystemExit):
  3078. call_command(
  3079. "optimizemigration", "--check", "migrations", "0001", verbosity=0
  3080. )
  3081. call_command("optimizemigration", "--check", "migrations", "0002", verbosity=0)
  3082. @override_settings(
  3083. INSTALLED_APPS=["migrations.migrations_test_apps.unmigrated_app_simple"],
  3084. )
  3085. def test_app_without_migrations(self):
  3086. msg = "App 'unmigrated_app_simple' does not have migrations."
  3087. with self.assertRaisesMessage(CommandError, msg):
  3088. call_command("optimizemigration", "unmigrated_app_simple", "0001")
  3089. @override_settings(
  3090. MIGRATION_MODULES={"migrations": "migrations.test_migrations_clashing_prefix"},
  3091. )
  3092. def test_ambigious_prefix(self):
  3093. msg = (
  3094. "More than one migration matches 'a' in app 'migrations'. Please "
  3095. "be more specific."
  3096. )
  3097. with self.assertRaisesMessage(CommandError, msg):
  3098. call_command("optimizemigration", "migrations", "a")
  3099. @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
  3100. def test_unknown_prefix(self):
  3101. msg = "Cannot find a migration matching 'nonexistent' from app 'migrations'."
  3102. with self.assertRaisesMessage(CommandError, msg):
  3103. call_command("optimizemigration", "migrations", "nonexistent")
  3104. def test_failure_to_format_code(self):
  3105. self.assertFormatterFailureCaught("optimizemigration", "migrations", "0001")
  3106. class CustomMigrationCommandTests(MigrationTestBase):
  3107. @override_settings(
  3108. MIGRATION_MODULES={"migrations": "migrations.test_migrations"},
  3109. INSTALLED_APPS=["migrations.migrations_test_apps.migrated_app"],
  3110. )
  3111. @isolate_apps("migrations.migrations_test_apps.migrated_app")
  3112. def test_makemigrations_custom_autodetector(self):
  3113. class CustomAutodetector(MigrationAutodetector):
  3114. def changes(self, *args, **kwargs):
  3115. return []
  3116. class CustomMakeMigrationsCommand(MakeMigrationsCommand):
  3117. autodetector = CustomAutodetector
  3118. class NewModel(models.Model):
  3119. class Meta:
  3120. app_label = "migrated_app"
  3121. out = io.StringIO()
  3122. command = CustomMakeMigrationsCommand(stdout=out)
  3123. call_command(command, "migrated_app", stdout=out)
  3124. self.assertIn("No changes detected", out.getvalue())
  3125. @override_settings(INSTALLED_APPS=["migrations.migrations_test_apps.migrated_app"])
  3126. @isolate_apps("migrations.migrations_test_apps.migrated_app")
  3127. def test_migrate_custom_autodetector(self):
  3128. class CustomAutodetector(MigrationAutodetector):
  3129. def changes(self, *args, **kwargs):
  3130. return []
  3131. class CustomMigrateCommand(MigrateCommand):
  3132. autodetector = CustomAutodetector
  3133. class NewModel(models.Model):
  3134. class Meta:
  3135. app_label = "migrated_app"
  3136. out = io.StringIO()
  3137. command = CustomMigrateCommand(stdout=out)
  3138. out = io.StringIO()
  3139. try:
  3140. call_command(command, verbosity=0)
  3141. call_command(command, stdout=out, no_color=True)
  3142. command_stdout = out.getvalue().lower()
  3143. self.assertEqual(
  3144. "operations to perform:\n"
  3145. " apply all migrations: migrated_app\n"
  3146. "running migrations:\n"
  3147. " no migrations to apply.\n",
  3148. command_stdout,
  3149. )
  3150. finally:
  3151. call_command(command, "migrated_app", "zero", verbosity=0)