test_operations.py 177 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755275627572758275927602761276227632764276527662767276827692770277127722773277427752776277727782779278027812782278327842785278627872788278927902791279227932794279527962797279827992800280128022803280428052806280728082809281028112812281328142815281628172818281928202821282228232824282528262827282828292830283128322833283428352836283728382839284028412842284328442845284628472848284928502851285228532854285528562857285828592860286128622863286428652866286728682869287028712872287328742875287628772878287928802881288228832884288528862887288828892890289128922893289428952896289728982899290029012902290329042905290629072908290929102911291229132914291529162917291829192920292129222923292429252926292729282929293029312932293329342935293629372938293929402941294229432944294529462947294829492950295129522953295429552956295729582959296029612962296329642965296629672968296929702971297229732974297529762977297829792980298129822983298429852986298729882989299029912992299329942995299629972998299930003001300230033004300530063007300830093010301130123013301430153016301730183019302030213022302330243025302630273028302930303031303230333034303530363037303830393040304130423043304430453046304730483049305030513052305330543055305630573058305930603061306230633064306530663067306830693070307130723073307430753076307730783079308030813082308330843085308630873088308930903091309230933094309530963097309830993100310131023103310431053106310731083109311031113112311331143115311631173118311931203121312231233124312531263127312831293130313131323133313431353136313731383139314031413142314331443145314631473148314931503151315231533154315531563157315831593160316131623163316431653166316731683169317031713172317331743175317631773178317931803181318231833184318531863187318831893190319131923193319431953196319731983199320032013202320332043205320632073208320932103211321232133214321532163217321832193220322132223223322432253226322732283229323032313232323332343235323632373238323932403241324232433244324532463247324832493250325132523253325432553256325732583259326032613262326332643265326632673268326932703271327232733274327532763277327832793280328132823283328432853286328732883289329032913292329332943295329632973298329933003301330233033304330533063307330833093310331133123313331433153316331733183319332033213322332333243325332633273328332933303331333233333334333533363337333833393340334133423343334433453346334733483349335033513352335333543355335633573358335933603361336233633364336533663367336833693370337133723373337433753376337733783379338033813382338333843385338633873388338933903391339233933394339533963397339833993400340134023403340434053406340734083409341034113412341334143415341634173418341934203421342234233424342534263427342834293430343134323433343434353436343734383439344034413442344334443445344634473448344934503451345234533454345534563457345834593460346134623463346434653466346734683469347034713472347334743475347634773478347934803481348234833484348534863487348834893490349134923493349434953496349734983499350035013502350335043505350635073508350935103511351235133514351535163517351835193520352135223523352435253526352735283529353035313532353335343535353635373538353935403541354235433544354535463547354835493550355135523553355435553556355735583559356035613562356335643565356635673568356935703571357235733574357535763577357835793580358135823583358435853586358735883589359035913592359335943595359635973598359936003601360236033604360536063607360836093610361136123613361436153616361736183619362036213622362336243625362636273628362936303631363236333634363536363637363836393640364136423643364436453646364736483649365036513652365336543655365636573658365936603661366236633664366536663667
  1. from django.core.exceptions import FieldDoesNotExist
  2. from django.db import (
  3. IntegrityError, connection, migrations, models, transaction,
  4. )
  5. from django.db.migrations.migration import Migration
  6. from django.db.migrations.operations.fields import FieldOperation
  7. from django.db.migrations.state import ModelState, ProjectState
  8. from django.db.models.functions import Abs
  9. from django.db.transaction import atomic
  10. from django.test import SimpleTestCase, override_settings, skipUnlessDBFeature
  11. from .models import FoodManager, FoodQuerySet, UnicodeModel
  12. from .test_base import OperationTestBase
  13. class Mixin:
  14. pass
  15. class OperationTests(OperationTestBase):
  16. """
  17. Tests running the operations and making sure they do what they say they do.
  18. Each test looks at their state changing, and then their database operation -
  19. both forwards and backwards.
  20. """
  21. def test_create_model(self):
  22. """
  23. Tests the CreateModel operation.
  24. Most other tests use this operation as part of setup, so check failures here first.
  25. """
  26. operation = migrations.CreateModel(
  27. "Pony",
  28. [
  29. ("id", models.AutoField(primary_key=True)),
  30. ("pink", models.IntegerField(default=1)),
  31. ],
  32. )
  33. self.assertEqual(operation.describe(), "Create model Pony")
  34. self.assertEqual(operation.migration_name_fragment, 'pony')
  35. # Test the state alteration
  36. project_state = ProjectState()
  37. new_state = project_state.clone()
  38. operation.state_forwards("test_crmo", new_state)
  39. self.assertEqual(new_state.models["test_crmo", "pony"].name, "Pony")
  40. self.assertEqual(len(new_state.models["test_crmo", "pony"].fields), 2)
  41. # Test the database alteration
  42. self.assertTableNotExists("test_crmo_pony")
  43. with connection.schema_editor() as editor:
  44. operation.database_forwards("test_crmo", editor, project_state, new_state)
  45. self.assertTableExists("test_crmo_pony")
  46. # And test reversal
  47. with connection.schema_editor() as editor:
  48. operation.database_backwards("test_crmo", editor, new_state, project_state)
  49. self.assertTableNotExists("test_crmo_pony")
  50. # And deconstruction
  51. definition = operation.deconstruct()
  52. self.assertEqual(definition[0], "CreateModel")
  53. self.assertEqual(definition[1], [])
  54. self.assertEqual(sorted(definition[2]), ["fields", "name"])
  55. # And default manager not in set
  56. operation = migrations.CreateModel("Foo", fields=[], managers=[("objects", models.Manager())])
  57. definition = operation.deconstruct()
  58. self.assertNotIn('managers', definition[2])
  59. def test_create_model_with_duplicate_field_name(self):
  60. with self.assertRaisesMessage(ValueError, 'Found duplicate value pink in CreateModel fields argument.'):
  61. migrations.CreateModel(
  62. "Pony",
  63. [
  64. ("id", models.AutoField(primary_key=True)),
  65. ("pink", models.TextField()),
  66. ("pink", models.IntegerField(default=1)),
  67. ],
  68. )
  69. def test_create_model_with_duplicate_base(self):
  70. message = 'Found duplicate value test_crmo.pony in CreateModel bases argument.'
  71. with self.assertRaisesMessage(ValueError, message):
  72. migrations.CreateModel(
  73. "Pony",
  74. fields=[],
  75. bases=("test_crmo.Pony", "test_crmo.Pony",),
  76. )
  77. with self.assertRaisesMessage(ValueError, message):
  78. migrations.CreateModel(
  79. "Pony",
  80. fields=[],
  81. bases=("test_crmo.Pony", "test_crmo.pony",),
  82. )
  83. message = 'Found duplicate value migrations.unicodemodel in CreateModel bases argument.'
  84. with self.assertRaisesMessage(ValueError, message):
  85. migrations.CreateModel(
  86. "Pony",
  87. fields=[],
  88. bases=(UnicodeModel, UnicodeModel,),
  89. )
  90. with self.assertRaisesMessage(ValueError, message):
  91. migrations.CreateModel(
  92. "Pony",
  93. fields=[],
  94. bases=(UnicodeModel, 'migrations.unicodemodel',),
  95. )
  96. with self.assertRaisesMessage(ValueError, message):
  97. migrations.CreateModel(
  98. "Pony",
  99. fields=[],
  100. bases=(UnicodeModel, 'migrations.UnicodeModel',),
  101. )
  102. message = "Found duplicate value <class 'django.db.models.base.Model'> in CreateModel bases argument."
  103. with self.assertRaisesMessage(ValueError, message):
  104. migrations.CreateModel(
  105. "Pony",
  106. fields=[],
  107. bases=(models.Model, models.Model,),
  108. )
  109. message = "Found duplicate value <class 'migrations.test_operations.Mixin'> in CreateModel bases argument."
  110. with self.assertRaisesMessage(ValueError, message):
  111. migrations.CreateModel(
  112. "Pony",
  113. fields=[],
  114. bases=(Mixin, Mixin,),
  115. )
  116. def test_create_model_with_duplicate_manager_name(self):
  117. with self.assertRaisesMessage(ValueError, 'Found duplicate value objects in CreateModel managers argument.'):
  118. migrations.CreateModel(
  119. "Pony",
  120. fields=[],
  121. managers=[
  122. ("objects", models.Manager()),
  123. ("objects", models.Manager()),
  124. ],
  125. )
  126. def test_create_model_with_unique_after(self):
  127. """
  128. Tests the CreateModel operation directly followed by an
  129. AlterUniqueTogether (bug #22844 - sqlite remake issues)
  130. """
  131. operation1 = migrations.CreateModel(
  132. "Pony",
  133. [
  134. ("id", models.AutoField(primary_key=True)),
  135. ("pink", models.IntegerField(default=1)),
  136. ],
  137. )
  138. operation2 = migrations.CreateModel(
  139. "Rider",
  140. [
  141. ("id", models.AutoField(primary_key=True)),
  142. ("number", models.IntegerField(default=1)),
  143. ("pony", models.ForeignKey("test_crmoua.Pony", models.CASCADE)),
  144. ],
  145. )
  146. operation3 = migrations.AlterUniqueTogether(
  147. "Rider",
  148. [
  149. ("number", "pony"),
  150. ],
  151. )
  152. # Test the database alteration
  153. project_state = ProjectState()
  154. self.assertTableNotExists("test_crmoua_pony")
  155. self.assertTableNotExists("test_crmoua_rider")
  156. with connection.schema_editor() as editor:
  157. new_state = project_state.clone()
  158. operation1.state_forwards("test_crmoua", new_state)
  159. operation1.database_forwards("test_crmoua", editor, project_state, new_state)
  160. project_state, new_state = new_state, new_state.clone()
  161. operation2.state_forwards("test_crmoua", new_state)
  162. operation2.database_forwards("test_crmoua", editor, project_state, new_state)
  163. project_state, new_state = new_state, new_state.clone()
  164. operation3.state_forwards("test_crmoua", new_state)
  165. operation3.database_forwards("test_crmoua", editor, project_state, new_state)
  166. self.assertTableExists("test_crmoua_pony")
  167. self.assertTableExists("test_crmoua_rider")
  168. def test_create_model_m2m(self):
  169. """
  170. Test the creation of a model with a ManyToMany field and the
  171. auto-created "through" model.
  172. """
  173. project_state = self.set_up_test_model("test_crmomm")
  174. operation = migrations.CreateModel(
  175. "Stable",
  176. [
  177. ("id", models.AutoField(primary_key=True)),
  178. ("ponies", models.ManyToManyField("Pony", related_name="stables"))
  179. ]
  180. )
  181. # Test the state alteration
  182. new_state = project_state.clone()
  183. operation.state_forwards("test_crmomm", new_state)
  184. # Test the database alteration
  185. self.assertTableNotExists("test_crmomm_stable_ponies")
  186. with connection.schema_editor() as editor:
  187. operation.database_forwards("test_crmomm", editor, project_state, new_state)
  188. self.assertTableExists("test_crmomm_stable")
  189. self.assertTableExists("test_crmomm_stable_ponies")
  190. self.assertColumnNotExists("test_crmomm_stable", "ponies")
  191. # Make sure the M2M field actually works
  192. with atomic():
  193. Pony = new_state.apps.get_model("test_crmomm", "Pony")
  194. Stable = new_state.apps.get_model("test_crmomm", "Stable")
  195. stable = Stable.objects.create()
  196. p1 = Pony.objects.create(pink=False, weight=4.55)
  197. p2 = Pony.objects.create(pink=True, weight=5.43)
  198. stable.ponies.add(p1, p2)
  199. self.assertEqual(stable.ponies.count(), 2)
  200. stable.ponies.all().delete()
  201. # And test reversal
  202. with connection.schema_editor() as editor:
  203. operation.database_backwards("test_crmomm", editor, new_state, project_state)
  204. self.assertTableNotExists("test_crmomm_stable")
  205. self.assertTableNotExists("test_crmomm_stable_ponies")
  206. def test_create_model_inheritance(self):
  207. """
  208. Tests the CreateModel operation on a multi-table inheritance setup.
  209. """
  210. project_state = self.set_up_test_model("test_crmoih")
  211. # Test the state alteration
  212. operation = migrations.CreateModel(
  213. "ShetlandPony",
  214. [
  215. ('pony_ptr', models.OneToOneField(
  216. 'test_crmoih.Pony',
  217. models.CASCADE,
  218. auto_created=True,
  219. primary_key=True,
  220. to_field='id',
  221. serialize=False,
  222. )),
  223. ("cuteness", models.IntegerField(default=1)),
  224. ],
  225. )
  226. new_state = project_state.clone()
  227. operation.state_forwards("test_crmoih", new_state)
  228. self.assertIn(("test_crmoih", "shetlandpony"), new_state.models)
  229. # Test the database alteration
  230. self.assertTableNotExists("test_crmoih_shetlandpony")
  231. with connection.schema_editor() as editor:
  232. operation.database_forwards("test_crmoih", editor, project_state, new_state)
  233. self.assertTableExists("test_crmoih_shetlandpony")
  234. # And test reversal
  235. with connection.schema_editor() as editor:
  236. operation.database_backwards("test_crmoih", editor, new_state, project_state)
  237. self.assertTableNotExists("test_crmoih_shetlandpony")
  238. def test_create_proxy_model(self):
  239. """
  240. CreateModel ignores proxy models.
  241. """
  242. project_state = self.set_up_test_model("test_crprmo")
  243. # Test the state alteration
  244. operation = migrations.CreateModel(
  245. "ProxyPony",
  246. [],
  247. options={"proxy": True},
  248. bases=("test_crprmo.Pony",),
  249. )
  250. self.assertEqual(operation.describe(), "Create proxy model ProxyPony")
  251. new_state = project_state.clone()
  252. operation.state_forwards("test_crprmo", new_state)
  253. self.assertIn(("test_crprmo", "proxypony"), new_state.models)
  254. # Test the database alteration
  255. self.assertTableNotExists("test_crprmo_proxypony")
  256. self.assertTableExists("test_crprmo_pony")
  257. with connection.schema_editor() as editor:
  258. operation.database_forwards("test_crprmo", editor, project_state, new_state)
  259. self.assertTableNotExists("test_crprmo_proxypony")
  260. self.assertTableExists("test_crprmo_pony")
  261. # And test reversal
  262. with connection.schema_editor() as editor:
  263. operation.database_backwards("test_crprmo", editor, new_state, project_state)
  264. self.assertTableNotExists("test_crprmo_proxypony")
  265. self.assertTableExists("test_crprmo_pony")
  266. # And deconstruction
  267. definition = operation.deconstruct()
  268. self.assertEqual(definition[0], "CreateModel")
  269. self.assertEqual(definition[1], [])
  270. self.assertEqual(sorted(definition[2]), ["bases", "fields", "name", "options"])
  271. def test_create_unmanaged_model(self):
  272. """
  273. CreateModel ignores unmanaged models.
  274. """
  275. project_state = self.set_up_test_model("test_crummo")
  276. # Test the state alteration
  277. operation = migrations.CreateModel(
  278. "UnmanagedPony",
  279. [],
  280. options={"proxy": True},
  281. bases=("test_crummo.Pony",),
  282. )
  283. self.assertEqual(operation.describe(), "Create proxy model UnmanagedPony")
  284. new_state = project_state.clone()
  285. operation.state_forwards("test_crummo", new_state)
  286. self.assertIn(("test_crummo", "unmanagedpony"), new_state.models)
  287. # Test the database alteration
  288. self.assertTableNotExists("test_crummo_unmanagedpony")
  289. self.assertTableExists("test_crummo_pony")
  290. with connection.schema_editor() as editor:
  291. operation.database_forwards("test_crummo", editor, project_state, new_state)
  292. self.assertTableNotExists("test_crummo_unmanagedpony")
  293. self.assertTableExists("test_crummo_pony")
  294. # And test reversal
  295. with connection.schema_editor() as editor:
  296. operation.database_backwards("test_crummo", editor, new_state, project_state)
  297. self.assertTableNotExists("test_crummo_unmanagedpony")
  298. self.assertTableExists("test_crummo_pony")
  299. @skipUnlessDBFeature('supports_table_check_constraints')
  300. def test_create_model_with_constraint(self):
  301. where = models.Q(pink__gt=2)
  302. check_constraint = models.CheckConstraint(check=where, name='test_constraint_pony_pink_gt_2')
  303. operation = migrations.CreateModel(
  304. "Pony",
  305. [
  306. ("id", models.AutoField(primary_key=True)),
  307. ("pink", models.IntegerField(default=3)),
  308. ],
  309. options={'constraints': [check_constraint]},
  310. )
  311. # Test the state alteration
  312. project_state = ProjectState()
  313. new_state = project_state.clone()
  314. operation.state_forwards("test_crmo", new_state)
  315. self.assertEqual(len(new_state.models['test_crmo', 'pony'].options['constraints']), 1)
  316. # Test database alteration
  317. self.assertTableNotExists("test_crmo_pony")
  318. with connection.schema_editor() as editor:
  319. operation.database_forwards("test_crmo", editor, project_state, new_state)
  320. self.assertTableExists("test_crmo_pony")
  321. with connection.cursor() as cursor:
  322. with self.assertRaises(IntegrityError):
  323. cursor.execute("INSERT INTO test_crmo_pony (id, pink) VALUES (1, 1)")
  324. # Test reversal
  325. with connection.schema_editor() as editor:
  326. operation.database_backwards("test_crmo", editor, new_state, project_state)
  327. self.assertTableNotExists("test_crmo_pony")
  328. # Test deconstruction
  329. definition = operation.deconstruct()
  330. self.assertEqual(definition[0], "CreateModel")
  331. self.assertEqual(definition[1], [])
  332. self.assertEqual(definition[2]['options']['constraints'], [check_constraint])
  333. def test_create_model_with_partial_unique_constraint(self):
  334. partial_unique_constraint = models.UniqueConstraint(
  335. fields=['pink'],
  336. condition=models.Q(weight__gt=5),
  337. name='test_constraint_pony_pink_for_weight_gt_5_uniq',
  338. )
  339. operation = migrations.CreateModel(
  340. 'Pony',
  341. [
  342. ('id', models.AutoField(primary_key=True)),
  343. ('pink', models.IntegerField(default=3)),
  344. ('weight', models.FloatField()),
  345. ],
  346. options={'constraints': [partial_unique_constraint]},
  347. )
  348. # Test the state alteration
  349. project_state = ProjectState()
  350. new_state = project_state.clone()
  351. operation.state_forwards('test_crmo', new_state)
  352. self.assertEqual(len(new_state.models['test_crmo', 'pony'].options['constraints']), 1)
  353. # Test database alteration
  354. self.assertTableNotExists('test_crmo_pony')
  355. with connection.schema_editor() as editor:
  356. operation.database_forwards('test_crmo', editor, project_state, new_state)
  357. self.assertTableExists('test_crmo_pony')
  358. # Test constraint works
  359. Pony = new_state.apps.get_model('test_crmo', 'Pony')
  360. Pony.objects.create(pink=1, weight=4.0)
  361. Pony.objects.create(pink=1, weight=4.0)
  362. Pony.objects.create(pink=1, weight=6.0)
  363. if connection.features.supports_partial_indexes:
  364. with self.assertRaises(IntegrityError):
  365. Pony.objects.create(pink=1, weight=7.0)
  366. else:
  367. Pony.objects.create(pink=1, weight=7.0)
  368. # Test reversal
  369. with connection.schema_editor() as editor:
  370. operation.database_backwards('test_crmo', editor, new_state, project_state)
  371. self.assertTableNotExists('test_crmo_pony')
  372. # Test deconstruction
  373. definition = operation.deconstruct()
  374. self.assertEqual(definition[0], 'CreateModel')
  375. self.assertEqual(definition[1], [])
  376. self.assertEqual(definition[2]['options']['constraints'], [partial_unique_constraint])
  377. def test_create_model_with_deferred_unique_constraint(self):
  378. deferred_unique_constraint = models.UniqueConstraint(
  379. fields=['pink'],
  380. name='deferrable_pink_constraint',
  381. deferrable=models.Deferrable.DEFERRED,
  382. )
  383. operation = migrations.CreateModel(
  384. 'Pony',
  385. [
  386. ('id', models.AutoField(primary_key=True)),
  387. ('pink', models.IntegerField(default=3)),
  388. ],
  389. options={'constraints': [deferred_unique_constraint]},
  390. )
  391. project_state = ProjectState()
  392. new_state = project_state.clone()
  393. operation.state_forwards('test_crmo', new_state)
  394. self.assertEqual(len(new_state.models['test_crmo', 'pony'].options['constraints']), 1)
  395. self.assertTableNotExists('test_crmo_pony')
  396. # Create table.
  397. with connection.schema_editor() as editor:
  398. operation.database_forwards('test_crmo', editor, project_state, new_state)
  399. self.assertTableExists('test_crmo_pony')
  400. Pony = new_state.apps.get_model('test_crmo', 'Pony')
  401. Pony.objects.create(pink=1)
  402. if connection.features.supports_deferrable_unique_constraints:
  403. # Unique constraint is deferred.
  404. with transaction.atomic():
  405. obj = Pony.objects.create(pink=1)
  406. obj.pink = 2
  407. obj.save()
  408. # Constraint behavior can be changed with SET CONSTRAINTS.
  409. with self.assertRaises(IntegrityError):
  410. with transaction.atomic(), connection.cursor() as cursor:
  411. quoted_name = connection.ops.quote_name(deferred_unique_constraint.name)
  412. cursor.execute('SET CONSTRAINTS %s IMMEDIATE' % quoted_name)
  413. obj = Pony.objects.create(pink=1)
  414. obj.pink = 3
  415. obj.save()
  416. else:
  417. Pony.objects.create(pink=1)
  418. # Reversal.
  419. with connection.schema_editor() as editor:
  420. operation.database_backwards('test_crmo', editor, new_state, project_state)
  421. self.assertTableNotExists('test_crmo_pony')
  422. # Deconstruction.
  423. definition = operation.deconstruct()
  424. self.assertEqual(definition[0], 'CreateModel')
  425. self.assertEqual(definition[1], [])
  426. self.assertEqual(
  427. definition[2]['options']['constraints'],
  428. [deferred_unique_constraint],
  429. )
  430. @skipUnlessDBFeature('supports_covering_indexes')
  431. def test_create_model_with_covering_unique_constraint(self):
  432. covering_unique_constraint = models.UniqueConstraint(
  433. fields=['pink'],
  434. include=['weight'],
  435. name='test_constraint_pony_pink_covering_weight',
  436. )
  437. operation = migrations.CreateModel(
  438. 'Pony',
  439. [
  440. ('id', models.AutoField(primary_key=True)),
  441. ('pink', models.IntegerField(default=3)),
  442. ('weight', models.FloatField()),
  443. ],
  444. options={'constraints': [covering_unique_constraint]},
  445. )
  446. project_state = ProjectState()
  447. new_state = project_state.clone()
  448. operation.state_forwards('test_crmo', new_state)
  449. self.assertEqual(len(new_state.models['test_crmo', 'pony'].options['constraints']), 1)
  450. self.assertTableNotExists('test_crmo_pony')
  451. # Create table.
  452. with connection.schema_editor() as editor:
  453. operation.database_forwards('test_crmo', editor, project_state, new_state)
  454. self.assertTableExists('test_crmo_pony')
  455. Pony = new_state.apps.get_model('test_crmo', 'Pony')
  456. Pony.objects.create(pink=1, weight=4.0)
  457. with self.assertRaises(IntegrityError):
  458. Pony.objects.create(pink=1, weight=7.0)
  459. # Reversal.
  460. with connection.schema_editor() as editor:
  461. operation.database_backwards('test_crmo', editor, new_state, project_state)
  462. self.assertTableNotExists('test_crmo_pony')
  463. # Deconstruction.
  464. definition = operation.deconstruct()
  465. self.assertEqual(definition[0], 'CreateModel')
  466. self.assertEqual(definition[1], [])
  467. self.assertEqual(
  468. definition[2]['options']['constraints'],
  469. [covering_unique_constraint],
  470. )
  471. def test_create_model_managers(self):
  472. """
  473. The managers on a model are set.
  474. """
  475. project_state = self.set_up_test_model("test_cmoma")
  476. # Test the state alteration
  477. operation = migrations.CreateModel(
  478. "Food",
  479. fields=[
  480. ("id", models.AutoField(primary_key=True)),
  481. ],
  482. managers=[
  483. ("food_qs", FoodQuerySet.as_manager()),
  484. ("food_mgr", FoodManager("a", "b")),
  485. ("food_mgr_kwargs", FoodManager("x", "y", 3, 4)),
  486. ]
  487. )
  488. self.assertEqual(operation.describe(), "Create model Food")
  489. new_state = project_state.clone()
  490. operation.state_forwards("test_cmoma", new_state)
  491. self.assertIn(("test_cmoma", "food"), new_state.models)
  492. managers = new_state.models["test_cmoma", "food"].managers
  493. self.assertEqual(managers[0][0], "food_qs")
  494. self.assertIsInstance(managers[0][1], models.Manager)
  495. self.assertEqual(managers[1][0], "food_mgr")
  496. self.assertIsInstance(managers[1][1], FoodManager)
  497. self.assertEqual(managers[1][1].args, ("a", "b", 1, 2))
  498. self.assertEqual(managers[2][0], "food_mgr_kwargs")
  499. self.assertIsInstance(managers[2][1], FoodManager)
  500. self.assertEqual(managers[2][1].args, ("x", "y", 3, 4))
  501. def test_delete_model(self):
  502. """
  503. Tests the DeleteModel operation.
  504. """
  505. project_state = self.set_up_test_model("test_dlmo")
  506. # Test the state alteration
  507. operation = migrations.DeleteModel("Pony")
  508. self.assertEqual(operation.describe(), "Delete model Pony")
  509. self.assertEqual(operation.migration_name_fragment, 'delete_pony')
  510. new_state = project_state.clone()
  511. operation.state_forwards("test_dlmo", new_state)
  512. self.assertNotIn(("test_dlmo", "pony"), new_state.models)
  513. # Test the database alteration
  514. self.assertTableExists("test_dlmo_pony")
  515. with connection.schema_editor() as editor:
  516. operation.database_forwards("test_dlmo", editor, project_state, new_state)
  517. self.assertTableNotExists("test_dlmo_pony")
  518. # And test reversal
  519. with connection.schema_editor() as editor:
  520. operation.database_backwards("test_dlmo", editor, new_state, project_state)
  521. self.assertTableExists("test_dlmo_pony")
  522. # And deconstruction
  523. definition = operation.deconstruct()
  524. self.assertEqual(definition[0], "DeleteModel")
  525. self.assertEqual(definition[1], [])
  526. self.assertEqual(list(definition[2]), ["name"])
  527. def test_delete_proxy_model(self):
  528. """
  529. Tests the DeleteModel operation ignores proxy models.
  530. """
  531. project_state = self.set_up_test_model("test_dlprmo", proxy_model=True)
  532. # Test the state alteration
  533. operation = migrations.DeleteModel("ProxyPony")
  534. new_state = project_state.clone()
  535. operation.state_forwards("test_dlprmo", new_state)
  536. self.assertIn(("test_dlprmo", "proxypony"), project_state.models)
  537. self.assertNotIn(("test_dlprmo", "proxypony"), new_state.models)
  538. # Test the database alteration
  539. self.assertTableExists("test_dlprmo_pony")
  540. self.assertTableNotExists("test_dlprmo_proxypony")
  541. with connection.schema_editor() as editor:
  542. operation.database_forwards("test_dlprmo", editor, project_state, new_state)
  543. self.assertTableExists("test_dlprmo_pony")
  544. self.assertTableNotExists("test_dlprmo_proxypony")
  545. # And test reversal
  546. with connection.schema_editor() as editor:
  547. operation.database_backwards("test_dlprmo", editor, new_state, project_state)
  548. self.assertTableExists("test_dlprmo_pony")
  549. self.assertTableNotExists("test_dlprmo_proxypony")
  550. def test_delete_mti_model(self):
  551. project_state = self.set_up_test_model('test_dlmtimo', mti_model=True)
  552. # Test the state alteration
  553. operation = migrations.DeleteModel('ShetlandPony')
  554. new_state = project_state.clone()
  555. operation.state_forwards('test_dlmtimo', new_state)
  556. self.assertIn(('test_dlmtimo', 'shetlandpony'), project_state.models)
  557. self.assertNotIn(('test_dlmtimo', 'shetlandpony'), new_state.models)
  558. # Test the database alteration
  559. self.assertTableExists('test_dlmtimo_pony')
  560. self.assertTableExists('test_dlmtimo_shetlandpony')
  561. self.assertColumnExists('test_dlmtimo_shetlandpony', 'pony_ptr_id')
  562. with connection.schema_editor() as editor:
  563. operation.database_forwards('test_dlmtimo', editor, project_state, new_state)
  564. self.assertTableExists('test_dlmtimo_pony')
  565. self.assertTableNotExists('test_dlmtimo_shetlandpony')
  566. # And test reversal
  567. with connection.schema_editor() as editor:
  568. operation.database_backwards('test_dlmtimo', editor, new_state, project_state)
  569. self.assertTableExists('test_dlmtimo_pony')
  570. self.assertTableExists('test_dlmtimo_shetlandpony')
  571. self.assertColumnExists('test_dlmtimo_shetlandpony', 'pony_ptr_id')
  572. def test_rename_model(self):
  573. """
  574. Tests the RenameModel operation.
  575. """
  576. project_state = self.set_up_test_model("test_rnmo", related_model=True)
  577. # Test the state alteration
  578. operation = migrations.RenameModel("Pony", "Horse")
  579. self.assertEqual(operation.describe(), "Rename model Pony to Horse")
  580. self.assertEqual(operation.migration_name_fragment, 'rename_pony_horse')
  581. # Test initial state and database
  582. self.assertIn(("test_rnmo", "pony"), project_state.models)
  583. self.assertNotIn(("test_rnmo", "horse"), project_state.models)
  584. self.assertTableExists("test_rnmo_pony")
  585. self.assertTableNotExists("test_rnmo_horse")
  586. if connection.features.supports_foreign_keys:
  587. self.assertFKExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id"))
  588. self.assertFKNotExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id"))
  589. # Migrate forwards
  590. new_state = project_state.clone()
  591. atomic_rename = connection.features.supports_atomic_references_rename
  592. new_state = self.apply_operations("test_rnmo", new_state, [operation], atomic=atomic_rename)
  593. # Test new state and database
  594. self.assertNotIn(("test_rnmo", "pony"), new_state.models)
  595. self.assertIn(("test_rnmo", "horse"), new_state.models)
  596. # RenameModel also repoints all incoming FKs and M2Ms
  597. self.assertEqual(
  598. new_state.models['test_rnmo', 'rider'].fields['pony'].remote_field.model,
  599. 'test_rnmo.Horse',
  600. )
  601. self.assertTableNotExists("test_rnmo_pony")
  602. self.assertTableExists("test_rnmo_horse")
  603. if connection.features.supports_foreign_keys:
  604. self.assertFKNotExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id"))
  605. self.assertFKExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id"))
  606. # Migrate backwards
  607. original_state = self.unapply_operations("test_rnmo", project_state, [operation], atomic=atomic_rename)
  608. # Test original state and database
  609. self.assertIn(("test_rnmo", "pony"), original_state.models)
  610. self.assertNotIn(("test_rnmo", "horse"), original_state.models)
  611. self.assertEqual(
  612. original_state.models['test_rnmo', 'rider'].fields['pony'].remote_field.model,
  613. 'Pony',
  614. )
  615. self.assertTableExists("test_rnmo_pony")
  616. self.assertTableNotExists("test_rnmo_horse")
  617. if connection.features.supports_foreign_keys:
  618. self.assertFKExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id"))
  619. self.assertFKNotExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id"))
  620. # And deconstruction
  621. definition = operation.deconstruct()
  622. self.assertEqual(definition[0], "RenameModel")
  623. self.assertEqual(definition[1], [])
  624. self.assertEqual(definition[2], {'old_name': "Pony", 'new_name': "Horse"})
  625. def test_rename_model_state_forwards(self):
  626. """
  627. RenameModel operations shouldn't trigger the caching of rendered apps
  628. on state without prior apps.
  629. """
  630. state = ProjectState()
  631. state.add_model(ModelState('migrations', 'Foo', []))
  632. operation = migrations.RenameModel('Foo', 'Bar')
  633. operation.state_forwards('migrations', state)
  634. self.assertNotIn('apps', state.__dict__)
  635. self.assertNotIn(('migrations', 'foo'), state.models)
  636. self.assertIn(('migrations', 'bar'), state.models)
  637. # Now with apps cached.
  638. apps = state.apps
  639. operation = migrations.RenameModel('Bar', 'Foo')
  640. operation.state_forwards('migrations', state)
  641. self.assertIs(state.apps, apps)
  642. self.assertNotIn(('migrations', 'bar'), state.models)
  643. self.assertIn(('migrations', 'foo'), state.models)
  644. def test_rename_model_with_self_referential_fk(self):
  645. """
  646. Tests the RenameModel operation on model with self referential FK.
  647. """
  648. project_state = self.set_up_test_model("test_rmwsrf", related_model=True)
  649. # Test the state alteration
  650. operation = migrations.RenameModel("Rider", "HorseRider")
  651. self.assertEqual(operation.describe(), "Rename model Rider to HorseRider")
  652. new_state = project_state.clone()
  653. operation.state_forwards("test_rmwsrf", new_state)
  654. self.assertNotIn(("test_rmwsrf", "rider"), new_state.models)
  655. self.assertIn(("test_rmwsrf", "horserider"), new_state.models)
  656. # Remember, RenameModel also repoints all incoming FKs and M2Ms
  657. self.assertEqual(
  658. 'self',
  659. new_state.models["test_rmwsrf", "horserider"].fields['friend'].remote_field.model
  660. )
  661. HorseRider = new_state.apps.get_model('test_rmwsrf', 'horserider')
  662. self.assertIs(HorseRider._meta.get_field('horserider').remote_field.model, HorseRider)
  663. # Test the database alteration
  664. self.assertTableExists("test_rmwsrf_rider")
  665. self.assertTableNotExists("test_rmwsrf_horserider")
  666. if connection.features.supports_foreign_keys:
  667. self.assertFKExists("test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_rider", "id"))
  668. self.assertFKNotExists("test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_horserider", "id"))
  669. atomic_rename = connection.features.supports_atomic_references_rename
  670. with connection.schema_editor(atomic=atomic_rename) as editor:
  671. operation.database_forwards("test_rmwsrf", editor, project_state, new_state)
  672. self.assertTableNotExists("test_rmwsrf_rider")
  673. self.assertTableExists("test_rmwsrf_horserider")
  674. if connection.features.supports_foreign_keys:
  675. self.assertFKNotExists("test_rmwsrf_horserider", ["friend_id"], ("test_rmwsrf_rider", "id"))
  676. self.assertFKExists("test_rmwsrf_horserider", ["friend_id"], ("test_rmwsrf_horserider", "id"))
  677. # And test reversal
  678. with connection.schema_editor(atomic=atomic_rename) as editor:
  679. operation.database_backwards("test_rmwsrf", editor, new_state, project_state)
  680. self.assertTableExists("test_rmwsrf_rider")
  681. self.assertTableNotExists("test_rmwsrf_horserider")
  682. if connection.features.supports_foreign_keys:
  683. self.assertFKExists("test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_rider", "id"))
  684. self.assertFKNotExists("test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_horserider", "id"))
  685. def test_rename_model_with_superclass_fk(self):
  686. """
  687. Tests the RenameModel operation on a model which has a superclass that
  688. has a foreign key.
  689. """
  690. project_state = self.set_up_test_model("test_rmwsc", related_model=True, mti_model=True)
  691. # Test the state alteration
  692. operation = migrations.RenameModel("ShetlandPony", "LittleHorse")
  693. self.assertEqual(operation.describe(), "Rename model ShetlandPony to LittleHorse")
  694. new_state = project_state.clone()
  695. operation.state_forwards("test_rmwsc", new_state)
  696. self.assertNotIn(("test_rmwsc", "shetlandpony"), new_state.models)
  697. self.assertIn(("test_rmwsc", "littlehorse"), new_state.models)
  698. # RenameModel shouldn't repoint the superclass's relations, only local ones
  699. self.assertEqual(
  700. project_state.models['test_rmwsc', 'rider'].fields['pony'].remote_field.model,
  701. new_state.models['test_rmwsc', 'rider'].fields['pony'].remote_field.model,
  702. )
  703. # Before running the migration we have a table for Shetland Pony, not Little Horse
  704. self.assertTableExists("test_rmwsc_shetlandpony")
  705. self.assertTableNotExists("test_rmwsc_littlehorse")
  706. if connection.features.supports_foreign_keys:
  707. # and the foreign key on rider points to pony, not shetland pony
  708. self.assertFKExists("test_rmwsc_rider", ["pony_id"], ("test_rmwsc_pony", "id"))
  709. self.assertFKNotExists("test_rmwsc_rider", ["pony_id"], ("test_rmwsc_shetlandpony", "id"))
  710. with connection.schema_editor(atomic=connection.features.supports_atomic_references_rename) as editor:
  711. operation.database_forwards("test_rmwsc", editor, project_state, new_state)
  712. # Now we have a little horse table, not shetland pony
  713. self.assertTableNotExists("test_rmwsc_shetlandpony")
  714. self.assertTableExists("test_rmwsc_littlehorse")
  715. if connection.features.supports_foreign_keys:
  716. # but the Foreign keys still point at pony, not little horse
  717. self.assertFKExists("test_rmwsc_rider", ["pony_id"], ("test_rmwsc_pony", "id"))
  718. self.assertFKNotExists("test_rmwsc_rider", ["pony_id"], ("test_rmwsc_littlehorse", "id"))
  719. def test_rename_model_with_self_referential_m2m(self):
  720. app_label = "test_rename_model_with_self_referential_m2m"
  721. project_state = self.apply_operations(app_label, ProjectState(), operations=[
  722. migrations.CreateModel("ReflexivePony", fields=[
  723. ("id", models.AutoField(primary_key=True)),
  724. ("ponies", models.ManyToManyField("self")),
  725. ]),
  726. ])
  727. project_state = self.apply_operations(app_label, project_state, operations=[
  728. migrations.RenameModel("ReflexivePony", "ReflexivePony2"),
  729. ], atomic=connection.features.supports_atomic_references_rename)
  730. Pony = project_state.apps.get_model(app_label, "ReflexivePony2")
  731. pony = Pony.objects.create()
  732. pony.ponies.add(pony)
  733. def test_rename_model_with_m2m(self):
  734. app_label = "test_rename_model_with_m2m"
  735. project_state = self.apply_operations(app_label, ProjectState(), operations=[
  736. migrations.CreateModel("Rider", fields=[
  737. ("id", models.AutoField(primary_key=True)),
  738. ]),
  739. migrations.CreateModel("Pony", fields=[
  740. ("id", models.AutoField(primary_key=True)),
  741. ("riders", models.ManyToManyField("Rider")),
  742. ]),
  743. ])
  744. Pony = project_state.apps.get_model(app_label, "Pony")
  745. Rider = project_state.apps.get_model(app_label, "Rider")
  746. pony = Pony.objects.create()
  747. rider = Rider.objects.create()
  748. pony.riders.add(rider)
  749. project_state = self.apply_operations(app_label, project_state, operations=[
  750. migrations.RenameModel("Pony", "Pony2"),
  751. ], atomic=connection.features.supports_atomic_references_rename)
  752. Pony = project_state.apps.get_model(app_label, "Pony2")
  753. Rider = project_state.apps.get_model(app_label, "Rider")
  754. pony = Pony.objects.create()
  755. rider = Rider.objects.create()
  756. pony.riders.add(rider)
  757. self.assertEqual(Pony.objects.count(), 2)
  758. self.assertEqual(Rider.objects.count(), 2)
  759. self.assertEqual(Pony._meta.get_field('riders').remote_field.through.objects.count(), 2)
  760. def test_rename_m2m_target_model(self):
  761. app_label = "test_rename_m2m_target_model"
  762. project_state = self.apply_operations(app_label, ProjectState(), operations=[
  763. migrations.CreateModel("Rider", fields=[
  764. ("id", models.AutoField(primary_key=True)),
  765. ]),
  766. migrations.CreateModel("Pony", fields=[
  767. ("id", models.AutoField(primary_key=True)),
  768. ("riders", models.ManyToManyField("Rider")),
  769. ]),
  770. ])
  771. Pony = project_state.apps.get_model(app_label, "Pony")
  772. Rider = project_state.apps.get_model(app_label, "Rider")
  773. pony = Pony.objects.create()
  774. rider = Rider.objects.create()
  775. pony.riders.add(rider)
  776. project_state = self.apply_operations(app_label, project_state, operations=[
  777. migrations.RenameModel("Rider", "Rider2"),
  778. ], atomic=connection.features.supports_atomic_references_rename)
  779. Pony = project_state.apps.get_model(app_label, "Pony")
  780. Rider = project_state.apps.get_model(app_label, "Rider2")
  781. pony = Pony.objects.create()
  782. rider = Rider.objects.create()
  783. pony.riders.add(rider)
  784. self.assertEqual(Pony.objects.count(), 2)
  785. self.assertEqual(Rider.objects.count(), 2)
  786. self.assertEqual(Pony._meta.get_field('riders').remote_field.through.objects.count(), 2)
  787. def test_rename_m2m_through_model(self):
  788. app_label = "test_rename_through"
  789. project_state = self.apply_operations(app_label, ProjectState(), operations=[
  790. migrations.CreateModel("Rider", fields=[
  791. ("id", models.AutoField(primary_key=True)),
  792. ]),
  793. migrations.CreateModel("Pony", fields=[
  794. ("id", models.AutoField(primary_key=True)),
  795. ]),
  796. migrations.CreateModel("PonyRider", fields=[
  797. ("id", models.AutoField(primary_key=True)),
  798. ("rider", models.ForeignKey("test_rename_through.Rider", models.CASCADE)),
  799. ("pony", models.ForeignKey("test_rename_through.Pony", models.CASCADE)),
  800. ]),
  801. migrations.AddField(
  802. "Pony",
  803. "riders",
  804. models.ManyToManyField("test_rename_through.Rider", through="test_rename_through.PonyRider"),
  805. ),
  806. ])
  807. Pony = project_state.apps.get_model(app_label, "Pony")
  808. Rider = project_state.apps.get_model(app_label, "Rider")
  809. PonyRider = project_state.apps.get_model(app_label, "PonyRider")
  810. pony = Pony.objects.create()
  811. rider = Rider.objects.create()
  812. PonyRider.objects.create(pony=pony, rider=rider)
  813. project_state = self.apply_operations(app_label, project_state, operations=[
  814. migrations.RenameModel("PonyRider", "PonyRider2"),
  815. ])
  816. Pony = project_state.apps.get_model(app_label, "Pony")
  817. Rider = project_state.apps.get_model(app_label, "Rider")
  818. PonyRider = project_state.apps.get_model(app_label, "PonyRider2")
  819. pony = Pony.objects.first()
  820. rider = Rider.objects.create()
  821. PonyRider.objects.create(pony=pony, rider=rider)
  822. self.assertEqual(Pony.objects.count(), 1)
  823. self.assertEqual(Rider.objects.count(), 2)
  824. self.assertEqual(PonyRider.objects.count(), 2)
  825. self.assertEqual(pony.riders.count(), 2)
  826. def test_rename_m2m_model_after_rename_field(self):
  827. """RenameModel renames a many-to-many column after a RenameField."""
  828. app_label = 'test_rename_multiple'
  829. project_state = self.apply_operations(app_label, ProjectState(), operations=[
  830. migrations.CreateModel('Pony', fields=[
  831. ('id', models.AutoField(primary_key=True)),
  832. ('name', models.CharField(max_length=20)),
  833. ]),
  834. migrations.CreateModel('Rider', fields=[
  835. ('id', models.AutoField(primary_key=True)),
  836. ('pony', models.ForeignKey('test_rename_multiple.Pony', models.CASCADE)),
  837. ]),
  838. migrations.CreateModel('PonyRider', fields=[
  839. ('id', models.AutoField(primary_key=True)),
  840. ('riders', models.ManyToManyField('Rider')),
  841. ]),
  842. migrations.RenameField(model_name='pony', old_name='name', new_name='fancy_name'),
  843. migrations.RenameModel(old_name='Rider', new_name='Jockey'),
  844. ], atomic=connection.features.supports_atomic_references_rename)
  845. Pony = project_state.apps.get_model(app_label, 'Pony')
  846. Jockey = project_state.apps.get_model(app_label, 'Jockey')
  847. PonyRider = project_state.apps.get_model(app_label, 'PonyRider')
  848. # No "no such column" error means the column was renamed correctly.
  849. pony = Pony.objects.create(fancy_name='a good name')
  850. jockey = Jockey.objects.create(pony=pony)
  851. ponyrider = PonyRider.objects.create()
  852. ponyrider.riders.add(jockey)
  853. def test_add_field(self):
  854. """
  855. Tests the AddField operation.
  856. """
  857. # Test the state alteration
  858. operation = migrations.AddField(
  859. "Pony",
  860. "height",
  861. models.FloatField(null=True, default=5),
  862. )
  863. self.assertEqual(operation.describe(), "Add field height to Pony")
  864. self.assertEqual(operation.migration_name_fragment, 'pony_height')
  865. project_state, new_state = self.make_test_state("test_adfl", operation)
  866. self.assertEqual(len(new_state.models["test_adfl", "pony"].fields), 4)
  867. field = new_state.models['test_adfl', 'pony'].fields['height']
  868. self.assertEqual(field.default, 5)
  869. # Test the database alteration
  870. self.assertColumnNotExists("test_adfl_pony", "height")
  871. with connection.schema_editor() as editor:
  872. operation.database_forwards("test_adfl", editor, project_state, new_state)
  873. self.assertColumnExists("test_adfl_pony", "height")
  874. # And test reversal
  875. with connection.schema_editor() as editor:
  876. operation.database_backwards("test_adfl", editor, new_state, project_state)
  877. self.assertColumnNotExists("test_adfl_pony", "height")
  878. # And deconstruction
  879. definition = operation.deconstruct()
  880. self.assertEqual(definition[0], "AddField")
  881. self.assertEqual(definition[1], [])
  882. self.assertEqual(sorted(definition[2]), ["field", "model_name", "name"])
  883. def test_add_charfield(self):
  884. """
  885. Tests the AddField operation on TextField.
  886. """
  887. project_state = self.set_up_test_model("test_adchfl")
  888. Pony = project_state.apps.get_model("test_adchfl", "Pony")
  889. pony = Pony.objects.create(weight=42)
  890. new_state = self.apply_operations("test_adchfl", project_state, [
  891. migrations.AddField(
  892. "Pony",
  893. "text",
  894. models.CharField(max_length=10, default="some text"),
  895. ),
  896. migrations.AddField(
  897. "Pony",
  898. "empty",
  899. models.CharField(max_length=10, default=""),
  900. ),
  901. # If not properly quoted digits would be interpreted as an int.
  902. migrations.AddField(
  903. "Pony",
  904. "digits",
  905. models.CharField(max_length=10, default="42"),
  906. ),
  907. # Manual quoting is fragile and could trip on quotes. Refs #xyz.
  908. migrations.AddField(
  909. "Pony",
  910. "quotes",
  911. models.CharField(max_length=10, default='"\'"'),
  912. ),
  913. ])
  914. Pony = new_state.apps.get_model("test_adchfl", "Pony")
  915. pony = Pony.objects.get(pk=pony.pk)
  916. self.assertEqual(pony.text, "some text")
  917. self.assertEqual(pony.empty, "")
  918. self.assertEqual(pony.digits, "42")
  919. self.assertEqual(pony.quotes, '"\'"')
  920. def test_add_textfield(self):
  921. """
  922. Tests the AddField operation on TextField.
  923. """
  924. project_state = self.set_up_test_model("test_adtxtfl")
  925. Pony = project_state.apps.get_model("test_adtxtfl", "Pony")
  926. pony = Pony.objects.create(weight=42)
  927. new_state = self.apply_operations("test_adtxtfl", project_state, [
  928. migrations.AddField(
  929. "Pony",
  930. "text",
  931. models.TextField(default="some text"),
  932. ),
  933. migrations.AddField(
  934. "Pony",
  935. "empty",
  936. models.TextField(default=""),
  937. ),
  938. # If not properly quoted digits would be interpreted as an int.
  939. migrations.AddField(
  940. "Pony",
  941. "digits",
  942. models.TextField(default="42"),
  943. ),
  944. # Manual quoting is fragile and could trip on quotes. Refs #xyz.
  945. migrations.AddField(
  946. "Pony",
  947. "quotes",
  948. models.TextField(default='"\'"'),
  949. ),
  950. ])
  951. Pony = new_state.apps.get_model("test_adtxtfl", "Pony")
  952. pony = Pony.objects.get(pk=pony.pk)
  953. self.assertEqual(pony.text, "some text")
  954. self.assertEqual(pony.empty, "")
  955. self.assertEqual(pony.digits, "42")
  956. self.assertEqual(pony.quotes, '"\'"')
  957. def test_add_binaryfield(self):
  958. """
  959. Tests the AddField operation on TextField/BinaryField.
  960. """
  961. project_state = self.set_up_test_model("test_adbinfl")
  962. Pony = project_state.apps.get_model("test_adbinfl", "Pony")
  963. pony = Pony.objects.create(weight=42)
  964. new_state = self.apply_operations("test_adbinfl", project_state, [
  965. migrations.AddField(
  966. "Pony",
  967. "blob",
  968. models.BinaryField(default=b"some text"),
  969. ),
  970. migrations.AddField(
  971. "Pony",
  972. "empty",
  973. models.BinaryField(default=b""),
  974. ),
  975. # If not properly quoted digits would be interpreted as an int.
  976. migrations.AddField(
  977. "Pony",
  978. "digits",
  979. models.BinaryField(default=b"42"),
  980. ),
  981. # Manual quoting is fragile and could trip on quotes. Refs #xyz.
  982. migrations.AddField(
  983. "Pony",
  984. "quotes",
  985. models.BinaryField(default=b'"\'"'),
  986. ),
  987. ])
  988. Pony = new_state.apps.get_model("test_adbinfl", "Pony")
  989. pony = Pony.objects.get(pk=pony.pk)
  990. # SQLite returns buffer/memoryview, cast to bytes for checking.
  991. self.assertEqual(bytes(pony.blob), b"some text")
  992. self.assertEqual(bytes(pony.empty), b"")
  993. self.assertEqual(bytes(pony.digits), b"42")
  994. self.assertEqual(bytes(pony.quotes), b'"\'"')
  995. def test_column_name_quoting(self):
  996. """
  997. Column names that are SQL keywords shouldn't cause problems when used
  998. in migrations (#22168).
  999. """
  1000. project_state = self.set_up_test_model("test_regr22168")
  1001. operation = migrations.AddField(
  1002. "Pony",
  1003. "order",
  1004. models.IntegerField(default=0),
  1005. )
  1006. new_state = project_state.clone()
  1007. operation.state_forwards("test_regr22168", new_state)
  1008. with connection.schema_editor() as editor:
  1009. operation.database_forwards("test_regr22168", editor, project_state, new_state)
  1010. self.assertColumnExists("test_regr22168_pony", "order")
  1011. def test_add_field_preserve_default(self):
  1012. """
  1013. Tests the AddField operation's state alteration
  1014. when preserve_default = False.
  1015. """
  1016. project_state = self.set_up_test_model("test_adflpd")
  1017. # Test the state alteration
  1018. operation = migrations.AddField(
  1019. "Pony",
  1020. "height",
  1021. models.FloatField(null=True, default=4),
  1022. preserve_default=False,
  1023. )
  1024. new_state = project_state.clone()
  1025. operation.state_forwards("test_adflpd", new_state)
  1026. self.assertEqual(len(new_state.models["test_adflpd", "pony"].fields), 4)
  1027. field = new_state.models['test_adflpd', 'pony'].fields['height']
  1028. self.assertEqual(field.default, models.NOT_PROVIDED)
  1029. # Test the database alteration
  1030. project_state.apps.get_model("test_adflpd", "pony").objects.create(
  1031. weight=4,
  1032. )
  1033. self.assertColumnNotExists("test_adflpd_pony", "height")
  1034. with connection.schema_editor() as editor:
  1035. operation.database_forwards("test_adflpd", editor, project_state, new_state)
  1036. self.assertColumnExists("test_adflpd_pony", "height")
  1037. # And deconstruction
  1038. definition = operation.deconstruct()
  1039. self.assertEqual(definition[0], "AddField")
  1040. self.assertEqual(definition[1], [])
  1041. self.assertEqual(sorted(definition[2]), ["field", "model_name", "name", "preserve_default"])
  1042. def test_add_field_m2m(self):
  1043. """
  1044. Tests the AddField operation with a ManyToManyField.
  1045. """
  1046. project_state = self.set_up_test_model("test_adflmm", second_model=True)
  1047. # Test the state alteration
  1048. operation = migrations.AddField("Pony", "stables", models.ManyToManyField("Stable", related_name="ponies"))
  1049. new_state = project_state.clone()
  1050. operation.state_forwards("test_adflmm", new_state)
  1051. self.assertEqual(len(new_state.models["test_adflmm", "pony"].fields), 4)
  1052. # Test the database alteration
  1053. self.assertTableNotExists("test_adflmm_pony_stables")
  1054. with connection.schema_editor() as editor:
  1055. operation.database_forwards("test_adflmm", editor, project_state, new_state)
  1056. self.assertTableExists("test_adflmm_pony_stables")
  1057. self.assertColumnNotExists("test_adflmm_pony", "stables")
  1058. # Make sure the M2M field actually works
  1059. with atomic():
  1060. Pony = new_state.apps.get_model("test_adflmm", "Pony")
  1061. p = Pony.objects.create(pink=False, weight=4.55)
  1062. p.stables.create()
  1063. self.assertEqual(p.stables.count(), 1)
  1064. p.stables.all().delete()
  1065. # And test reversal
  1066. with connection.schema_editor() as editor:
  1067. operation.database_backwards("test_adflmm", editor, new_state, project_state)
  1068. self.assertTableNotExists("test_adflmm_pony_stables")
  1069. def test_alter_field_m2m(self):
  1070. project_state = self.set_up_test_model("test_alflmm", second_model=True)
  1071. project_state = self.apply_operations("test_alflmm", project_state, operations=[
  1072. migrations.AddField("Pony", "stables", models.ManyToManyField("Stable", related_name="ponies"))
  1073. ])
  1074. Pony = project_state.apps.get_model("test_alflmm", "Pony")
  1075. self.assertFalse(Pony._meta.get_field('stables').blank)
  1076. project_state = self.apply_operations("test_alflmm", project_state, operations=[
  1077. migrations.AlterField(
  1078. "Pony", "stables", models.ManyToManyField(to="Stable", related_name="ponies", blank=True)
  1079. )
  1080. ])
  1081. Pony = project_state.apps.get_model("test_alflmm", "Pony")
  1082. self.assertTrue(Pony._meta.get_field('stables').blank)
  1083. def test_repoint_field_m2m(self):
  1084. project_state = self.set_up_test_model("test_alflmm", second_model=True, third_model=True)
  1085. project_state = self.apply_operations("test_alflmm", project_state, operations=[
  1086. migrations.AddField("Pony", "places", models.ManyToManyField("Stable", related_name="ponies"))
  1087. ])
  1088. Pony = project_state.apps.get_model("test_alflmm", "Pony")
  1089. project_state = self.apply_operations("test_alflmm", project_state, operations=[
  1090. migrations.AlterField("Pony", "places", models.ManyToManyField(to="Van", related_name="ponies"))
  1091. ])
  1092. # Ensure the new field actually works
  1093. Pony = project_state.apps.get_model("test_alflmm", "Pony")
  1094. p = Pony.objects.create(pink=False, weight=4.55)
  1095. p.places.create()
  1096. self.assertEqual(p.places.count(), 1)
  1097. p.places.all().delete()
  1098. def test_remove_field_m2m(self):
  1099. project_state = self.set_up_test_model("test_rmflmm", second_model=True)
  1100. project_state = self.apply_operations("test_rmflmm", project_state, operations=[
  1101. migrations.AddField("Pony", "stables", models.ManyToManyField("Stable", related_name="ponies"))
  1102. ])
  1103. self.assertTableExists("test_rmflmm_pony_stables")
  1104. with_field_state = project_state.clone()
  1105. operations = [migrations.RemoveField("Pony", "stables")]
  1106. project_state = self.apply_operations("test_rmflmm", project_state, operations=operations)
  1107. self.assertTableNotExists("test_rmflmm_pony_stables")
  1108. # And test reversal
  1109. self.unapply_operations("test_rmflmm", with_field_state, operations=operations)
  1110. self.assertTableExists("test_rmflmm_pony_stables")
  1111. def test_remove_field_m2m_with_through(self):
  1112. project_state = self.set_up_test_model("test_rmflmmwt", second_model=True)
  1113. self.assertTableNotExists("test_rmflmmwt_ponystables")
  1114. project_state = self.apply_operations("test_rmflmmwt", project_state, operations=[
  1115. migrations.CreateModel("PonyStables", fields=[
  1116. ("pony", models.ForeignKey('test_rmflmmwt.Pony', models.CASCADE)),
  1117. ("stable", models.ForeignKey('test_rmflmmwt.Stable', models.CASCADE)),
  1118. ]),
  1119. migrations.AddField(
  1120. "Pony", "stables",
  1121. models.ManyToManyField("Stable", related_name="ponies", through='test_rmflmmwt.PonyStables')
  1122. )
  1123. ])
  1124. self.assertTableExists("test_rmflmmwt_ponystables")
  1125. operations = [migrations.RemoveField("Pony", "stables"), migrations.DeleteModel("PonyStables")]
  1126. self.apply_operations("test_rmflmmwt", project_state, operations=operations)
  1127. def test_remove_field(self):
  1128. """
  1129. Tests the RemoveField operation.
  1130. """
  1131. project_state = self.set_up_test_model("test_rmfl")
  1132. # Test the state alteration
  1133. operation = migrations.RemoveField("Pony", "pink")
  1134. self.assertEqual(operation.describe(), "Remove field pink from Pony")
  1135. self.assertEqual(operation.migration_name_fragment, 'remove_pony_pink')
  1136. new_state = project_state.clone()
  1137. operation.state_forwards("test_rmfl", new_state)
  1138. self.assertEqual(len(new_state.models["test_rmfl", "pony"].fields), 2)
  1139. # Test the database alteration
  1140. self.assertColumnExists("test_rmfl_pony", "pink")
  1141. with connection.schema_editor() as editor:
  1142. operation.database_forwards("test_rmfl", editor, project_state, new_state)
  1143. self.assertColumnNotExists("test_rmfl_pony", "pink")
  1144. # And test reversal
  1145. with connection.schema_editor() as editor:
  1146. operation.database_backwards("test_rmfl", editor, new_state, project_state)
  1147. self.assertColumnExists("test_rmfl_pony", "pink")
  1148. # And deconstruction
  1149. definition = operation.deconstruct()
  1150. self.assertEqual(definition[0], "RemoveField")
  1151. self.assertEqual(definition[1], [])
  1152. self.assertEqual(definition[2], {'model_name': "Pony", 'name': 'pink'})
  1153. def test_remove_fk(self):
  1154. """
  1155. Tests the RemoveField operation on a foreign key.
  1156. """
  1157. project_state = self.set_up_test_model("test_rfk", related_model=True)
  1158. self.assertColumnExists("test_rfk_rider", "pony_id")
  1159. operation = migrations.RemoveField("Rider", "pony")
  1160. new_state = project_state.clone()
  1161. operation.state_forwards("test_rfk", new_state)
  1162. with connection.schema_editor() as editor:
  1163. operation.database_forwards("test_rfk", editor, project_state, new_state)
  1164. self.assertColumnNotExists("test_rfk_rider", "pony_id")
  1165. with connection.schema_editor() as editor:
  1166. operation.database_backwards("test_rfk", editor, new_state, project_state)
  1167. self.assertColumnExists("test_rfk_rider", "pony_id")
  1168. def test_alter_model_table(self):
  1169. """
  1170. Tests the AlterModelTable operation.
  1171. """
  1172. project_state = self.set_up_test_model("test_almota")
  1173. # Test the state alteration
  1174. operation = migrations.AlterModelTable("Pony", "test_almota_pony_2")
  1175. self.assertEqual(operation.describe(), "Rename table for Pony to test_almota_pony_2")
  1176. self.assertEqual(operation.migration_name_fragment, 'alter_pony_table')
  1177. new_state = project_state.clone()
  1178. operation.state_forwards("test_almota", new_state)
  1179. self.assertEqual(new_state.models["test_almota", "pony"].options["db_table"], "test_almota_pony_2")
  1180. # Test the database alteration
  1181. self.assertTableExists("test_almota_pony")
  1182. self.assertTableNotExists("test_almota_pony_2")
  1183. with connection.schema_editor() as editor:
  1184. operation.database_forwards("test_almota", editor, project_state, new_state)
  1185. self.assertTableNotExists("test_almota_pony")
  1186. self.assertTableExists("test_almota_pony_2")
  1187. # And test reversal
  1188. with connection.schema_editor() as editor:
  1189. operation.database_backwards("test_almota", editor, new_state, project_state)
  1190. self.assertTableExists("test_almota_pony")
  1191. self.assertTableNotExists("test_almota_pony_2")
  1192. # And deconstruction
  1193. definition = operation.deconstruct()
  1194. self.assertEqual(definition[0], "AlterModelTable")
  1195. self.assertEqual(definition[1], [])
  1196. self.assertEqual(definition[2], {'name': "Pony", 'table': "test_almota_pony_2"})
  1197. def test_alter_model_table_none(self):
  1198. """
  1199. Tests the AlterModelTable operation if the table name is set to None.
  1200. """
  1201. operation = migrations.AlterModelTable("Pony", None)
  1202. self.assertEqual(operation.describe(), "Rename table for Pony to (default)")
  1203. def test_alter_model_table_noop(self):
  1204. """
  1205. Tests the AlterModelTable operation if the table name is not changed.
  1206. """
  1207. project_state = self.set_up_test_model("test_almota")
  1208. # Test the state alteration
  1209. operation = migrations.AlterModelTable("Pony", "test_almota_pony")
  1210. new_state = project_state.clone()
  1211. operation.state_forwards("test_almota", new_state)
  1212. self.assertEqual(new_state.models["test_almota", "pony"].options["db_table"], "test_almota_pony")
  1213. # Test the database alteration
  1214. self.assertTableExists("test_almota_pony")
  1215. with connection.schema_editor() as editor:
  1216. operation.database_forwards("test_almota", editor, project_state, new_state)
  1217. self.assertTableExists("test_almota_pony")
  1218. # And test reversal
  1219. with connection.schema_editor() as editor:
  1220. operation.database_backwards("test_almota", editor, new_state, project_state)
  1221. self.assertTableExists("test_almota_pony")
  1222. def test_alter_model_table_m2m(self):
  1223. """
  1224. AlterModelTable should rename auto-generated M2M tables.
  1225. """
  1226. app_label = "test_talflmltlm2m"
  1227. pony_db_table = 'pony_foo'
  1228. project_state = self.set_up_test_model(app_label, second_model=True, db_table=pony_db_table)
  1229. # Add the M2M field
  1230. first_state = project_state.clone()
  1231. operation = migrations.AddField("Pony", "stables", models.ManyToManyField("Stable"))
  1232. operation.state_forwards(app_label, first_state)
  1233. with connection.schema_editor() as editor:
  1234. operation.database_forwards(app_label, editor, project_state, first_state)
  1235. original_m2m_table = "%s_%s" % (pony_db_table, "stables")
  1236. new_m2m_table = "%s_%s" % (app_label, "pony_stables")
  1237. self.assertTableExists(original_m2m_table)
  1238. self.assertTableNotExists(new_m2m_table)
  1239. # Rename the Pony db_table which should also rename the m2m table.
  1240. second_state = first_state.clone()
  1241. operation = migrations.AlterModelTable(name='pony', table=None)
  1242. operation.state_forwards(app_label, second_state)
  1243. atomic_rename = connection.features.supports_atomic_references_rename
  1244. with connection.schema_editor(atomic=atomic_rename) as editor:
  1245. operation.database_forwards(app_label, editor, first_state, second_state)
  1246. self.assertTableExists(new_m2m_table)
  1247. self.assertTableNotExists(original_m2m_table)
  1248. # And test reversal
  1249. with connection.schema_editor(atomic=atomic_rename) as editor:
  1250. operation.database_backwards(app_label, editor, second_state, first_state)
  1251. self.assertTableExists(original_m2m_table)
  1252. self.assertTableNotExists(new_m2m_table)
  1253. def test_alter_field(self):
  1254. """
  1255. Tests the AlterField operation.
  1256. """
  1257. project_state = self.set_up_test_model("test_alfl")
  1258. # Test the state alteration
  1259. operation = migrations.AlterField("Pony", "pink", models.IntegerField(null=True))
  1260. self.assertEqual(operation.describe(), "Alter field pink on Pony")
  1261. self.assertEqual(operation.migration_name_fragment, 'alter_pony_pink')
  1262. new_state = project_state.clone()
  1263. operation.state_forwards("test_alfl", new_state)
  1264. self.assertIs(project_state.models['test_alfl', 'pony'].fields['pink'].null, False)
  1265. self.assertIs(new_state.models['test_alfl', 'pony'].fields['pink'].null, True)
  1266. # Test the database alteration
  1267. self.assertColumnNotNull("test_alfl_pony", "pink")
  1268. with connection.schema_editor() as editor:
  1269. operation.database_forwards("test_alfl", editor, project_state, new_state)
  1270. self.assertColumnNull("test_alfl_pony", "pink")
  1271. # And test reversal
  1272. with connection.schema_editor() as editor:
  1273. operation.database_backwards("test_alfl", editor, new_state, project_state)
  1274. self.assertColumnNotNull("test_alfl_pony", "pink")
  1275. # And deconstruction
  1276. definition = operation.deconstruct()
  1277. self.assertEqual(definition[0], "AlterField")
  1278. self.assertEqual(definition[1], [])
  1279. self.assertEqual(sorted(definition[2]), ["field", "model_name", "name"])
  1280. def test_alter_field_add_db_column_noop(self):
  1281. """
  1282. AlterField operation is a noop when adding only a db_column and the
  1283. column name is not changed.
  1284. """
  1285. app_label = 'test_afadbn'
  1286. project_state = self.set_up_test_model(app_label, related_model=True)
  1287. pony_table = '%s_pony' % app_label
  1288. new_state = project_state.clone()
  1289. operation = migrations.AlterField('Pony', 'weight', models.FloatField(db_column='weight'))
  1290. operation.state_forwards(app_label, new_state)
  1291. self.assertIsNone(
  1292. project_state.models[app_label, 'pony'].fields['weight'].db_column,
  1293. )
  1294. self.assertEqual(
  1295. new_state.models[app_label, 'pony'].fields['weight'].db_column,
  1296. 'weight',
  1297. )
  1298. self.assertColumnExists(pony_table, 'weight')
  1299. with connection.schema_editor() as editor:
  1300. with self.assertNumQueries(0):
  1301. operation.database_forwards(app_label, editor, project_state, new_state)
  1302. self.assertColumnExists(pony_table, 'weight')
  1303. with connection.schema_editor() as editor:
  1304. with self.assertNumQueries(0):
  1305. operation.database_backwards(app_label, editor, new_state, project_state)
  1306. self.assertColumnExists(pony_table, 'weight')
  1307. rider_table = '%s_rider' % app_label
  1308. new_state = project_state.clone()
  1309. operation = migrations.AlterField(
  1310. 'Rider',
  1311. 'pony',
  1312. models.ForeignKey('Pony', models.CASCADE, db_column='pony_id'),
  1313. )
  1314. operation.state_forwards(app_label, new_state)
  1315. self.assertIsNone(
  1316. project_state.models[app_label, 'rider'].fields['pony'].db_column,
  1317. )
  1318. self.assertIs(
  1319. new_state.models[app_label, 'rider'].fields['pony'].db_column,
  1320. 'pony_id',
  1321. )
  1322. self.assertColumnExists(rider_table, 'pony_id')
  1323. with connection.schema_editor() as editor:
  1324. with self.assertNumQueries(0):
  1325. operation.database_forwards(app_label, editor, project_state, new_state)
  1326. self.assertColumnExists(rider_table, 'pony_id')
  1327. with connection.schema_editor() as editor:
  1328. with self.assertNumQueries(0):
  1329. operation.database_forwards(app_label, editor, new_state, project_state)
  1330. self.assertColumnExists(rider_table, 'pony_id')
  1331. def test_alter_field_pk(self):
  1332. """
  1333. Tests the AlterField operation on primary keys (for things like PostgreSQL's SERIAL weirdness)
  1334. """
  1335. project_state = self.set_up_test_model("test_alflpk")
  1336. # Test the state alteration
  1337. operation = migrations.AlterField("Pony", "id", models.IntegerField(primary_key=True))
  1338. new_state = project_state.clone()
  1339. operation.state_forwards("test_alflpk", new_state)
  1340. self.assertIsInstance(
  1341. project_state.models['test_alflpk', 'pony'].fields['id'],
  1342. models.AutoField,
  1343. )
  1344. self.assertIsInstance(
  1345. new_state.models['test_alflpk', 'pony'].fields['id'],
  1346. models.IntegerField,
  1347. )
  1348. # Test the database alteration
  1349. with connection.schema_editor() as editor:
  1350. operation.database_forwards("test_alflpk", editor, project_state, new_state)
  1351. # And test reversal
  1352. with connection.schema_editor() as editor:
  1353. operation.database_backwards("test_alflpk", editor, new_state, project_state)
  1354. @skipUnlessDBFeature('supports_foreign_keys')
  1355. def test_alter_field_pk_fk(self):
  1356. """
  1357. Tests the AlterField operation on primary keys changes any FKs pointing to it.
  1358. """
  1359. project_state = self.set_up_test_model("test_alflpkfk", related_model=True)
  1360. project_state = self.apply_operations('test_alflpkfk', project_state, [
  1361. migrations.CreateModel('Stable', fields=[
  1362. ('ponies', models.ManyToManyField('Pony')),
  1363. ]),
  1364. migrations.AddField(
  1365. 'Pony',
  1366. 'stables',
  1367. models.ManyToManyField('Stable'),
  1368. ),
  1369. ])
  1370. # Test the state alteration
  1371. operation = migrations.AlterField("Pony", "id", models.FloatField(primary_key=True))
  1372. new_state = project_state.clone()
  1373. operation.state_forwards("test_alflpkfk", new_state)
  1374. self.assertIsInstance(
  1375. project_state.models['test_alflpkfk', 'pony'].fields['id'],
  1376. models.AutoField,
  1377. )
  1378. self.assertIsInstance(
  1379. new_state.models['test_alflpkfk', 'pony'].fields['id'],
  1380. models.FloatField,
  1381. )
  1382. def assertIdTypeEqualsFkType():
  1383. with connection.cursor() as cursor:
  1384. id_type, id_null = [
  1385. (c.type_code, c.null_ok)
  1386. for c in connection.introspection.get_table_description(cursor, "test_alflpkfk_pony")
  1387. if c.name == "id"
  1388. ][0]
  1389. fk_type, fk_null = [
  1390. (c.type_code, c.null_ok)
  1391. for c in connection.introspection.get_table_description(cursor, "test_alflpkfk_rider")
  1392. if c.name == "pony_id"
  1393. ][0]
  1394. m2m_fk_type, m2m_fk_null = [
  1395. (c.type_code, c.null_ok)
  1396. for c in connection.introspection.get_table_description(
  1397. cursor,
  1398. 'test_alflpkfk_pony_stables',
  1399. ) if c.name == 'pony_id'
  1400. ][0]
  1401. remote_m2m_fk_type, remote_m2m_fk_null = [
  1402. (c.type_code, c.null_ok)
  1403. for c in connection.introspection.get_table_description(
  1404. cursor,
  1405. 'test_alflpkfk_stable_ponies',
  1406. ) if c.name == 'pony_id'
  1407. ][0]
  1408. self.assertEqual(id_type, fk_type)
  1409. self.assertEqual(id_type, m2m_fk_type)
  1410. self.assertEqual(id_type, remote_m2m_fk_type)
  1411. self.assertEqual(id_null, fk_null)
  1412. self.assertEqual(id_null, m2m_fk_null)
  1413. self.assertEqual(id_null, remote_m2m_fk_null)
  1414. assertIdTypeEqualsFkType()
  1415. # Test the database alteration
  1416. with connection.schema_editor() as editor:
  1417. operation.database_forwards("test_alflpkfk", editor, project_state, new_state)
  1418. assertIdTypeEqualsFkType()
  1419. # And test reversal
  1420. with connection.schema_editor() as editor:
  1421. operation.database_backwards("test_alflpkfk", editor, new_state, project_state)
  1422. assertIdTypeEqualsFkType()
  1423. @skipUnlessDBFeature('supports_foreign_keys')
  1424. def test_alter_field_reloads_state_on_fk_with_to_field_target_type_change(self):
  1425. app_label = 'test_alflrsfkwtflttc'
  1426. project_state = self.apply_operations(app_label, ProjectState(), operations=[
  1427. migrations.CreateModel('Rider', fields=[
  1428. ('id', models.AutoField(primary_key=True)),
  1429. ('code', models.IntegerField(unique=True)),
  1430. ]),
  1431. migrations.CreateModel('Pony', fields=[
  1432. ('id', models.AutoField(primary_key=True)),
  1433. ('rider', models.ForeignKey('%s.Rider' % app_label, models.CASCADE, to_field='code')),
  1434. ]),
  1435. ])
  1436. operation = migrations.AlterField(
  1437. 'Rider',
  1438. 'code',
  1439. models.CharField(max_length=100, unique=True),
  1440. )
  1441. self.apply_operations(app_label, project_state, operations=[operation])
  1442. id_type, id_null = [
  1443. (c.type_code, c.null_ok)
  1444. for c in self.get_table_description('%s_rider' % app_label)
  1445. if c.name == 'code'
  1446. ][0]
  1447. fk_type, fk_null = [
  1448. (c.type_code, c.null_ok)
  1449. for c in self.get_table_description('%s_pony' % app_label)
  1450. if c.name == 'rider_id'
  1451. ][0]
  1452. self.assertEqual(id_type, fk_type)
  1453. self.assertEqual(id_null, fk_null)
  1454. @skipUnlessDBFeature('supports_foreign_keys')
  1455. def test_alter_field_reloads_state_on_fk_with_to_field_related_name_target_type_change(self):
  1456. app_label = 'test_alflrsfkwtflrnttc'
  1457. project_state = self.apply_operations(app_label, ProjectState(), operations=[
  1458. migrations.CreateModel('Rider', fields=[
  1459. ('id', models.AutoField(primary_key=True)),
  1460. ('code', models.PositiveIntegerField(unique=True)),
  1461. ]),
  1462. migrations.CreateModel('Pony', fields=[
  1463. ('id', models.AutoField(primary_key=True)),
  1464. ('rider', models.ForeignKey(
  1465. '%s.Rider' % app_label,
  1466. models.CASCADE,
  1467. to_field='code',
  1468. related_name='+',
  1469. )),
  1470. ]),
  1471. ])
  1472. operation = migrations.AlterField(
  1473. 'Rider',
  1474. 'code',
  1475. models.CharField(max_length=100, unique=True),
  1476. )
  1477. self.apply_operations(app_label, project_state, operations=[operation])
  1478. def test_alter_field_reloads_state_on_fk_target_changes(self):
  1479. """
  1480. If AlterField doesn't reload state appropriately, the second AlterField
  1481. crashes on MySQL due to not dropping the PonyRider.pony foreign key
  1482. constraint before modifying the column.
  1483. """
  1484. app_label = 'alter_alter_field_reloads_state_on_fk_target_changes'
  1485. project_state = self.apply_operations(app_label, ProjectState(), operations=[
  1486. migrations.CreateModel('Rider', fields=[
  1487. ('id', models.CharField(primary_key=True, max_length=100)),
  1488. ]),
  1489. migrations.CreateModel('Pony', fields=[
  1490. ('id', models.CharField(primary_key=True, max_length=100)),
  1491. ('rider', models.ForeignKey('%s.Rider' % app_label, models.CASCADE)),
  1492. ]),
  1493. migrations.CreateModel('PonyRider', fields=[
  1494. ('id', models.AutoField(primary_key=True)),
  1495. ('pony', models.ForeignKey('%s.Pony' % app_label, models.CASCADE)),
  1496. ]),
  1497. ])
  1498. project_state = self.apply_operations(app_label, project_state, operations=[
  1499. migrations.AlterField('Rider', 'id', models.CharField(primary_key=True, max_length=99)),
  1500. migrations.AlterField('Pony', 'id', models.CharField(primary_key=True, max_length=99)),
  1501. ])
  1502. def test_alter_field_reloads_state_on_fk_with_to_field_target_changes(self):
  1503. """
  1504. If AlterField doesn't reload state appropriately, the second AlterField
  1505. crashes on MySQL due to not dropping the PonyRider.pony foreign key
  1506. constraint before modifying the column.
  1507. """
  1508. app_label = 'alter_alter_field_reloads_state_on_fk_with_to_field_target_changes'
  1509. project_state = self.apply_operations(app_label, ProjectState(), operations=[
  1510. migrations.CreateModel('Rider', fields=[
  1511. ('id', models.CharField(primary_key=True, max_length=100)),
  1512. ('slug', models.CharField(unique=True, max_length=100)),
  1513. ]),
  1514. migrations.CreateModel('Pony', fields=[
  1515. ('id', models.CharField(primary_key=True, max_length=100)),
  1516. ('rider', models.ForeignKey('%s.Rider' % app_label, models.CASCADE, to_field='slug')),
  1517. ('slug', models.CharField(unique=True, max_length=100)),
  1518. ]),
  1519. migrations.CreateModel('PonyRider', fields=[
  1520. ('id', models.AutoField(primary_key=True)),
  1521. ('pony', models.ForeignKey('%s.Pony' % app_label, models.CASCADE, to_field='slug')),
  1522. ]),
  1523. ])
  1524. project_state = self.apply_operations(app_label, project_state, operations=[
  1525. migrations.AlterField('Rider', 'slug', models.CharField(unique=True, max_length=99)),
  1526. migrations.AlterField('Pony', 'slug', models.CharField(unique=True, max_length=99)),
  1527. ])
  1528. def test_rename_field_reloads_state_on_fk_target_changes(self):
  1529. """
  1530. If RenameField doesn't reload state appropriately, the AlterField
  1531. crashes on MySQL due to not dropping the PonyRider.pony foreign key
  1532. constraint before modifying the column.
  1533. """
  1534. app_label = 'alter_rename_field_reloads_state_on_fk_target_changes'
  1535. project_state = self.apply_operations(app_label, ProjectState(), operations=[
  1536. migrations.CreateModel('Rider', fields=[
  1537. ('id', models.CharField(primary_key=True, max_length=100)),
  1538. ]),
  1539. migrations.CreateModel('Pony', fields=[
  1540. ('id', models.CharField(primary_key=True, max_length=100)),
  1541. ('rider', models.ForeignKey('%s.Rider' % app_label, models.CASCADE)),
  1542. ]),
  1543. migrations.CreateModel('PonyRider', fields=[
  1544. ('id', models.AutoField(primary_key=True)),
  1545. ('pony', models.ForeignKey('%s.Pony' % app_label, models.CASCADE)),
  1546. ]),
  1547. ])
  1548. project_state = self.apply_operations(app_label, project_state, operations=[
  1549. migrations.RenameField('Rider', 'id', 'id2'),
  1550. migrations.AlterField('Pony', 'id', models.CharField(primary_key=True, max_length=99)),
  1551. ], atomic=connection.features.supports_atomic_references_rename)
  1552. def test_rename_field(self):
  1553. """
  1554. Tests the RenameField operation.
  1555. """
  1556. project_state = self.set_up_test_model("test_rnfl", unique_together=True, index_together=True)
  1557. # Test the state alteration
  1558. operation = migrations.RenameField("Pony", "pink", "blue")
  1559. self.assertEqual(operation.describe(), "Rename field pink on Pony to blue")
  1560. self.assertEqual(operation.migration_name_fragment, 'rename_pink_pony_blue')
  1561. new_state = project_state.clone()
  1562. operation.state_forwards("test_rnfl", new_state)
  1563. self.assertIn("blue", new_state.models["test_rnfl", "pony"].fields)
  1564. self.assertNotIn("pink", new_state.models["test_rnfl", "pony"].fields)
  1565. # Make sure the unique_together has the renamed column too
  1566. self.assertIn("blue", new_state.models["test_rnfl", "pony"].options['unique_together'][0])
  1567. self.assertNotIn("pink", new_state.models["test_rnfl", "pony"].options['unique_together'][0])
  1568. # Make sure the index_together has the renamed column too
  1569. self.assertIn("blue", new_state.models["test_rnfl", "pony"].options['index_together'][0])
  1570. self.assertNotIn("pink", new_state.models["test_rnfl", "pony"].options['index_together'][0])
  1571. # Test the database alteration
  1572. self.assertColumnExists("test_rnfl_pony", "pink")
  1573. self.assertColumnNotExists("test_rnfl_pony", "blue")
  1574. with connection.schema_editor() as editor:
  1575. operation.database_forwards("test_rnfl", editor, project_state, new_state)
  1576. self.assertColumnExists("test_rnfl_pony", "blue")
  1577. self.assertColumnNotExists("test_rnfl_pony", "pink")
  1578. # Ensure the unique constraint has been ported over
  1579. with connection.cursor() as cursor:
  1580. cursor.execute("INSERT INTO test_rnfl_pony (blue, weight) VALUES (1, 1)")
  1581. with self.assertRaises(IntegrityError):
  1582. with atomic():
  1583. cursor.execute("INSERT INTO test_rnfl_pony (blue, weight) VALUES (1, 1)")
  1584. cursor.execute("DELETE FROM test_rnfl_pony")
  1585. # Ensure the index constraint has been ported over
  1586. self.assertIndexExists("test_rnfl_pony", ["weight", "blue"])
  1587. # And test reversal
  1588. with connection.schema_editor() as editor:
  1589. operation.database_backwards("test_rnfl", editor, new_state, project_state)
  1590. self.assertColumnExists("test_rnfl_pony", "pink")
  1591. self.assertColumnNotExists("test_rnfl_pony", "blue")
  1592. # Ensure the index constraint has been reset
  1593. self.assertIndexExists("test_rnfl_pony", ["weight", "pink"])
  1594. # And deconstruction
  1595. definition = operation.deconstruct()
  1596. self.assertEqual(definition[0], "RenameField")
  1597. self.assertEqual(definition[1], [])
  1598. self.assertEqual(definition[2], {'model_name': "Pony", 'old_name': "pink", 'new_name': "blue"})
  1599. def test_rename_field_with_db_column(self):
  1600. project_state = self.apply_operations('test_rfwdbc', ProjectState(), operations=[
  1601. migrations.CreateModel('Pony', fields=[
  1602. ('id', models.AutoField(primary_key=True)),
  1603. ('field', models.IntegerField(db_column='db_field')),
  1604. ('fk_field', models.ForeignKey(
  1605. 'Pony',
  1606. models.CASCADE,
  1607. db_column='db_fk_field',
  1608. )),
  1609. ]),
  1610. ])
  1611. new_state = project_state.clone()
  1612. operation = migrations.RenameField('Pony', 'field', 'renamed_field')
  1613. operation.state_forwards('test_rfwdbc', new_state)
  1614. self.assertIn('renamed_field', new_state.models['test_rfwdbc', 'pony'].fields)
  1615. self.assertNotIn('field', new_state.models['test_rfwdbc', 'pony'].fields)
  1616. self.assertColumnExists('test_rfwdbc_pony', 'db_field')
  1617. with connection.schema_editor() as editor:
  1618. with self.assertNumQueries(0):
  1619. operation.database_forwards('test_rfwdbc', editor, project_state, new_state)
  1620. self.assertColumnExists('test_rfwdbc_pony', 'db_field')
  1621. with connection.schema_editor() as editor:
  1622. with self.assertNumQueries(0):
  1623. operation.database_backwards('test_rfwdbc', editor, new_state, project_state)
  1624. self.assertColumnExists('test_rfwdbc_pony', 'db_field')
  1625. new_state = project_state.clone()
  1626. operation = migrations.RenameField('Pony', 'fk_field', 'renamed_fk_field')
  1627. operation.state_forwards('test_rfwdbc', new_state)
  1628. self.assertIn('renamed_fk_field', new_state.models['test_rfwdbc', 'pony'].fields)
  1629. self.assertNotIn('fk_field', new_state.models['test_rfwdbc', 'pony'].fields)
  1630. self.assertColumnExists('test_rfwdbc_pony', 'db_fk_field')
  1631. with connection.schema_editor() as editor:
  1632. with self.assertNumQueries(0):
  1633. operation.database_forwards('test_rfwdbc', editor, project_state, new_state)
  1634. self.assertColumnExists('test_rfwdbc_pony', 'db_fk_field')
  1635. with connection.schema_editor() as editor:
  1636. with self.assertNumQueries(0):
  1637. operation.database_backwards('test_rfwdbc', editor, new_state, project_state)
  1638. self.assertColumnExists('test_rfwdbc_pony', 'db_fk_field')
  1639. def test_rename_field_case(self):
  1640. project_state = self.apply_operations('test_rfmx', ProjectState(), operations=[
  1641. migrations.CreateModel('Pony', fields=[
  1642. ('id', models.AutoField(primary_key=True)),
  1643. ('field', models.IntegerField()),
  1644. ]),
  1645. ])
  1646. new_state = project_state.clone()
  1647. operation = migrations.RenameField('Pony', 'field', 'FiElD')
  1648. operation.state_forwards('test_rfmx', new_state)
  1649. self.assertIn('FiElD', new_state.models['test_rfmx', 'pony'].fields)
  1650. self.assertColumnExists('test_rfmx_pony', 'field')
  1651. with connection.schema_editor() as editor:
  1652. operation.database_forwards('test_rfmx', editor, project_state, new_state)
  1653. self.assertColumnExists(
  1654. 'test_rfmx_pony',
  1655. connection.introspection.identifier_converter('FiElD'),
  1656. )
  1657. with connection.schema_editor() as editor:
  1658. operation.database_backwards('test_rfmx', editor, new_state, project_state)
  1659. self.assertColumnExists('test_rfmx_pony', 'field')
  1660. def test_rename_missing_field(self):
  1661. state = ProjectState()
  1662. state.add_model(ModelState('app', 'model', []))
  1663. with self.assertRaisesMessage(FieldDoesNotExist, "app.model has no field named 'field'"):
  1664. migrations.RenameField('model', 'field', 'new_field').state_forwards('app', state)
  1665. def test_rename_referenced_field_state_forward(self):
  1666. state = ProjectState()
  1667. state.add_model(ModelState('app', 'Model', [
  1668. ('id', models.AutoField(primary_key=True)),
  1669. ('field', models.IntegerField(unique=True)),
  1670. ]))
  1671. state.add_model(ModelState('app', 'OtherModel', [
  1672. ('id', models.AutoField(primary_key=True)),
  1673. ('fk', models.ForeignKey('Model', models.CASCADE, to_field='field')),
  1674. ('fo', models.ForeignObject('Model', models.CASCADE, from_fields=('fk',), to_fields=('field',))),
  1675. ]))
  1676. operation = migrations.RenameField('Model', 'field', 'renamed')
  1677. new_state = state.clone()
  1678. operation.state_forwards('app', new_state)
  1679. self.assertEqual(new_state.models['app', 'othermodel'].fields['fk'].remote_field.field_name, 'renamed')
  1680. self.assertEqual(new_state.models['app', 'othermodel'].fields['fk'].from_fields, ['self'])
  1681. self.assertEqual(new_state.models['app', 'othermodel'].fields['fk'].to_fields, ('renamed',))
  1682. self.assertEqual(new_state.models['app', 'othermodel'].fields['fo'].from_fields, ('fk',))
  1683. self.assertEqual(new_state.models['app', 'othermodel'].fields['fo'].to_fields, ('renamed',))
  1684. operation = migrations.RenameField('OtherModel', 'fk', 'renamed_fk')
  1685. new_state = state.clone()
  1686. operation.state_forwards('app', new_state)
  1687. self.assertEqual(new_state.models['app', 'othermodel'].fields['renamed_fk'].remote_field.field_name, 'renamed')
  1688. self.assertEqual(new_state.models['app', 'othermodel'].fields['renamed_fk'].from_fields, ('self',))
  1689. self.assertEqual(new_state.models['app', 'othermodel'].fields['renamed_fk'].to_fields, ('renamed',))
  1690. self.assertEqual(new_state.models['app', 'othermodel'].fields['fo'].from_fields, ('renamed_fk',))
  1691. self.assertEqual(new_state.models['app', 'othermodel'].fields['fo'].to_fields, ('renamed',))
  1692. def test_alter_unique_together(self):
  1693. """
  1694. Tests the AlterUniqueTogether operation.
  1695. """
  1696. project_state = self.set_up_test_model("test_alunto")
  1697. # Test the state alteration
  1698. operation = migrations.AlterUniqueTogether("Pony", [("pink", "weight")])
  1699. self.assertEqual(operation.describe(), "Alter unique_together for Pony (1 constraint(s))")
  1700. self.assertEqual(
  1701. operation.migration_name_fragment,
  1702. 'alter_pony_unique_together',
  1703. )
  1704. new_state = project_state.clone()
  1705. operation.state_forwards("test_alunto", new_state)
  1706. self.assertEqual(len(project_state.models["test_alunto", "pony"].options.get("unique_together", set())), 0)
  1707. self.assertEqual(len(new_state.models["test_alunto", "pony"].options.get("unique_together", set())), 1)
  1708. # Make sure we can insert duplicate rows
  1709. with connection.cursor() as cursor:
  1710. cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
  1711. cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
  1712. cursor.execute("DELETE FROM test_alunto_pony")
  1713. # Test the database alteration
  1714. with connection.schema_editor() as editor:
  1715. operation.database_forwards("test_alunto", editor, project_state, new_state)
  1716. cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
  1717. with self.assertRaises(IntegrityError):
  1718. with atomic():
  1719. cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
  1720. cursor.execute("DELETE FROM test_alunto_pony")
  1721. # And test reversal
  1722. with connection.schema_editor() as editor:
  1723. operation.database_backwards("test_alunto", editor, new_state, project_state)
  1724. cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
  1725. cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
  1726. cursor.execute("DELETE FROM test_alunto_pony")
  1727. # Test flat unique_together
  1728. operation = migrations.AlterUniqueTogether("Pony", ("pink", "weight"))
  1729. operation.state_forwards("test_alunto", new_state)
  1730. self.assertEqual(len(new_state.models["test_alunto", "pony"].options.get("unique_together", set())), 1)
  1731. # And deconstruction
  1732. definition = operation.deconstruct()
  1733. self.assertEqual(definition[0], "AlterUniqueTogether")
  1734. self.assertEqual(definition[1], [])
  1735. self.assertEqual(definition[2], {'name': "Pony", 'unique_together': {("pink", "weight")}})
  1736. def test_alter_unique_together_remove(self):
  1737. operation = migrations.AlterUniqueTogether("Pony", None)
  1738. self.assertEqual(operation.describe(), "Alter unique_together for Pony (0 constraint(s))")
  1739. def test_add_index(self):
  1740. """
  1741. Test the AddIndex operation.
  1742. """
  1743. project_state = self.set_up_test_model("test_adin")
  1744. msg = (
  1745. "Indexes passed to AddIndex operations require a name argument. "
  1746. "<Index: fields=['pink']> doesn't have one."
  1747. )
  1748. with self.assertRaisesMessage(ValueError, msg):
  1749. migrations.AddIndex("Pony", models.Index(fields=["pink"]))
  1750. index = models.Index(fields=["pink"], name="test_adin_pony_pink_idx")
  1751. operation = migrations.AddIndex("Pony", index)
  1752. self.assertEqual(operation.describe(), "Create index test_adin_pony_pink_idx on field(s) pink of model Pony")
  1753. self.assertEqual(
  1754. operation.migration_name_fragment,
  1755. 'pony_test_adin_pony_pink_idx',
  1756. )
  1757. new_state = project_state.clone()
  1758. operation.state_forwards("test_adin", new_state)
  1759. # Test the database alteration
  1760. self.assertEqual(len(new_state.models["test_adin", "pony"].options['indexes']), 1)
  1761. self.assertIndexNotExists("test_adin_pony", ["pink"])
  1762. with connection.schema_editor() as editor:
  1763. operation.database_forwards("test_adin", editor, project_state, new_state)
  1764. self.assertIndexExists("test_adin_pony", ["pink"])
  1765. # And test reversal
  1766. with connection.schema_editor() as editor:
  1767. operation.database_backwards("test_adin", editor, new_state, project_state)
  1768. self.assertIndexNotExists("test_adin_pony", ["pink"])
  1769. # And deconstruction
  1770. definition = operation.deconstruct()
  1771. self.assertEqual(definition[0], "AddIndex")
  1772. self.assertEqual(definition[1], [])
  1773. self.assertEqual(definition[2], {'model_name': "Pony", 'index': index})
  1774. def test_remove_index(self):
  1775. """
  1776. Test the RemoveIndex operation.
  1777. """
  1778. project_state = self.set_up_test_model("test_rmin", multicol_index=True)
  1779. self.assertTableExists("test_rmin_pony")
  1780. self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
  1781. operation = migrations.RemoveIndex("Pony", "pony_test_idx")
  1782. self.assertEqual(operation.describe(), "Remove index pony_test_idx from Pony")
  1783. self.assertEqual(
  1784. operation.migration_name_fragment,
  1785. 'remove_pony_pony_test_idx',
  1786. )
  1787. new_state = project_state.clone()
  1788. operation.state_forwards("test_rmin", new_state)
  1789. # Test the state alteration
  1790. self.assertEqual(len(new_state.models["test_rmin", "pony"].options['indexes']), 0)
  1791. self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
  1792. # Test the database alteration
  1793. with connection.schema_editor() as editor:
  1794. operation.database_forwards("test_rmin", editor, project_state, new_state)
  1795. self.assertIndexNotExists("test_rmin_pony", ["pink", "weight"])
  1796. # And test reversal
  1797. with connection.schema_editor() as editor:
  1798. operation.database_backwards("test_rmin", editor, new_state, project_state)
  1799. self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
  1800. # And deconstruction
  1801. definition = operation.deconstruct()
  1802. self.assertEqual(definition[0], "RemoveIndex")
  1803. self.assertEqual(definition[1], [])
  1804. self.assertEqual(definition[2], {'model_name': "Pony", 'name': "pony_test_idx"})
  1805. # Also test a field dropped with index - sqlite remake issue
  1806. operations = [
  1807. migrations.RemoveIndex("Pony", "pony_test_idx"),
  1808. migrations.RemoveField("Pony", "pink"),
  1809. ]
  1810. self.assertColumnExists("test_rmin_pony", "pink")
  1811. self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
  1812. # Test database alteration
  1813. new_state = project_state.clone()
  1814. self.apply_operations('test_rmin', new_state, operations=operations)
  1815. self.assertColumnNotExists("test_rmin_pony", "pink")
  1816. self.assertIndexNotExists("test_rmin_pony", ["pink", "weight"])
  1817. # And test reversal
  1818. self.unapply_operations("test_rmin", project_state, operations=operations)
  1819. self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
  1820. def test_add_index_state_forwards(self):
  1821. project_state = self.set_up_test_model('test_adinsf')
  1822. index = models.Index(fields=['pink'], name='test_adinsf_pony_pink_idx')
  1823. old_model = project_state.apps.get_model('test_adinsf', 'Pony')
  1824. new_state = project_state.clone()
  1825. operation = migrations.AddIndex('Pony', index)
  1826. operation.state_forwards('test_adinsf', new_state)
  1827. new_model = new_state.apps.get_model('test_adinsf', 'Pony')
  1828. self.assertIsNot(old_model, new_model)
  1829. def test_remove_index_state_forwards(self):
  1830. project_state = self.set_up_test_model('test_rminsf')
  1831. index = models.Index(fields=['pink'], name='test_rminsf_pony_pink_idx')
  1832. migrations.AddIndex('Pony', index).state_forwards('test_rminsf', project_state)
  1833. old_model = project_state.apps.get_model('test_rminsf', 'Pony')
  1834. new_state = project_state.clone()
  1835. operation = migrations.RemoveIndex('Pony', 'test_rminsf_pony_pink_idx')
  1836. operation.state_forwards('test_rminsf', new_state)
  1837. new_model = new_state.apps.get_model('test_rminsf', 'Pony')
  1838. self.assertIsNot(old_model, new_model)
  1839. @skipUnlessDBFeature('supports_expression_indexes')
  1840. def test_add_func_index(self):
  1841. app_label = 'test_addfuncin'
  1842. index_name = f'{app_label}_pony_abs_idx'
  1843. table_name = f'{app_label}_pony'
  1844. project_state = self.set_up_test_model(app_label)
  1845. index = models.Index(Abs('weight'), name=index_name)
  1846. operation = migrations.AddIndex('Pony', index)
  1847. self.assertEqual(
  1848. operation.describe(),
  1849. 'Create index test_addfuncin_pony_abs_idx on Abs(F(weight)) on model Pony',
  1850. )
  1851. self.assertEqual(
  1852. operation.migration_name_fragment,
  1853. 'pony_test_addfuncin_pony_abs_idx',
  1854. )
  1855. new_state = project_state.clone()
  1856. operation.state_forwards(app_label, new_state)
  1857. self.assertEqual(len(new_state.models[app_label, 'pony'].options['indexes']), 1)
  1858. self.assertIndexNameNotExists(table_name, index_name)
  1859. # Add index.
  1860. with connection.schema_editor() as editor:
  1861. operation.database_forwards(app_label, editor, project_state, new_state)
  1862. self.assertIndexNameExists(table_name, index_name)
  1863. # Reversal.
  1864. with connection.schema_editor() as editor:
  1865. operation.database_backwards(app_label, editor, new_state, project_state)
  1866. self.assertIndexNameNotExists(table_name, index_name)
  1867. # Deconstruction.
  1868. definition = operation.deconstruct()
  1869. self.assertEqual(definition[0], 'AddIndex')
  1870. self.assertEqual(definition[1], [])
  1871. self.assertEqual(definition[2], {'model_name': 'Pony', 'index': index})
  1872. @skipUnlessDBFeature('supports_expression_indexes')
  1873. def test_remove_func_index(self):
  1874. app_label = 'test_rmfuncin'
  1875. index_name = f'{app_label}_pony_abs_idx'
  1876. table_name = f'{app_label}_pony'
  1877. project_state = self.set_up_test_model(app_label, indexes=[
  1878. models.Index(Abs('weight'), name=index_name),
  1879. ])
  1880. self.assertTableExists(table_name)
  1881. self.assertIndexNameExists(table_name, index_name)
  1882. operation = migrations.RemoveIndex('Pony', index_name)
  1883. self.assertEqual(
  1884. operation.describe(),
  1885. 'Remove index test_rmfuncin_pony_abs_idx from Pony',
  1886. )
  1887. self.assertEqual(
  1888. operation.migration_name_fragment,
  1889. 'remove_pony_test_rmfuncin_pony_abs_idx',
  1890. )
  1891. new_state = project_state.clone()
  1892. operation.state_forwards(app_label, new_state)
  1893. self.assertEqual(len(new_state.models[app_label, 'pony'].options['indexes']), 0)
  1894. # Remove index.
  1895. with connection.schema_editor() as editor:
  1896. operation.database_forwards(app_label, editor, project_state, new_state)
  1897. self.assertIndexNameNotExists(table_name, index_name)
  1898. # Reversal.
  1899. with connection.schema_editor() as editor:
  1900. operation.database_backwards(app_label, editor, new_state, project_state)
  1901. self.assertIndexNameExists(table_name, index_name)
  1902. # Deconstruction.
  1903. definition = operation.deconstruct()
  1904. self.assertEqual(definition[0], 'RemoveIndex')
  1905. self.assertEqual(definition[1], [])
  1906. self.assertEqual(definition[2], {'model_name': 'Pony', 'name': index_name})
  1907. def test_alter_field_with_index(self):
  1908. """
  1909. Test AlterField operation with an index to ensure indexes created via
  1910. Meta.indexes don't get dropped with sqlite3 remake.
  1911. """
  1912. project_state = self.set_up_test_model("test_alflin", index=True)
  1913. operation = migrations.AlterField("Pony", "pink", models.IntegerField(null=True))
  1914. new_state = project_state.clone()
  1915. operation.state_forwards("test_alflin", new_state)
  1916. # Test the database alteration
  1917. self.assertColumnNotNull("test_alflin_pony", "pink")
  1918. with connection.schema_editor() as editor:
  1919. operation.database_forwards("test_alflin", editor, project_state, new_state)
  1920. # Index hasn't been dropped
  1921. self.assertIndexExists("test_alflin_pony", ["pink"])
  1922. # And test reversal
  1923. with connection.schema_editor() as editor:
  1924. operation.database_backwards("test_alflin", editor, new_state, project_state)
  1925. # Ensure the index is still there
  1926. self.assertIndexExists("test_alflin_pony", ["pink"])
  1927. def test_alter_index_together(self):
  1928. """
  1929. Tests the AlterIndexTogether operation.
  1930. """
  1931. project_state = self.set_up_test_model("test_alinto")
  1932. # Test the state alteration
  1933. operation = migrations.AlterIndexTogether("Pony", [("pink", "weight")])
  1934. self.assertEqual(operation.describe(), "Alter index_together for Pony (1 constraint(s))")
  1935. self.assertEqual(
  1936. operation.migration_name_fragment,
  1937. 'alter_pony_index_together',
  1938. )
  1939. new_state = project_state.clone()
  1940. operation.state_forwards("test_alinto", new_state)
  1941. self.assertEqual(len(project_state.models["test_alinto", "pony"].options.get("index_together", set())), 0)
  1942. self.assertEqual(len(new_state.models["test_alinto", "pony"].options.get("index_together", set())), 1)
  1943. # Make sure there's no matching index
  1944. self.assertIndexNotExists("test_alinto_pony", ["pink", "weight"])
  1945. # Test the database alteration
  1946. with connection.schema_editor() as editor:
  1947. operation.database_forwards("test_alinto", editor, project_state, new_state)
  1948. self.assertIndexExists("test_alinto_pony", ["pink", "weight"])
  1949. # And test reversal
  1950. with connection.schema_editor() as editor:
  1951. operation.database_backwards("test_alinto", editor, new_state, project_state)
  1952. self.assertIndexNotExists("test_alinto_pony", ["pink", "weight"])
  1953. # And deconstruction
  1954. definition = operation.deconstruct()
  1955. self.assertEqual(definition[0], "AlterIndexTogether")
  1956. self.assertEqual(definition[1], [])
  1957. self.assertEqual(definition[2], {'name': "Pony", 'index_together': {("pink", "weight")}})
  1958. def test_alter_index_together_remove(self):
  1959. operation = migrations.AlterIndexTogether("Pony", None)
  1960. self.assertEqual(operation.describe(), "Alter index_together for Pony (0 constraint(s))")
  1961. @skipUnlessDBFeature('allows_multiple_constraints_on_same_fields')
  1962. def test_alter_index_together_remove_with_unique_together(self):
  1963. app_label = 'test_alintoremove_wunto'
  1964. table_name = '%s_pony' % app_label
  1965. project_state = self.set_up_test_model(app_label, unique_together=True)
  1966. self.assertUniqueConstraintExists(table_name, ['pink', 'weight'])
  1967. # Add index together.
  1968. new_state = project_state.clone()
  1969. operation = migrations.AlterIndexTogether('Pony', [('pink', 'weight')])
  1970. operation.state_forwards(app_label, new_state)
  1971. with connection.schema_editor() as editor:
  1972. operation.database_forwards(app_label, editor, project_state, new_state)
  1973. self.assertIndexExists(table_name, ['pink', 'weight'])
  1974. # Remove index together.
  1975. project_state = new_state
  1976. new_state = project_state.clone()
  1977. operation = migrations.AlterIndexTogether('Pony', set())
  1978. operation.state_forwards(app_label, new_state)
  1979. with connection.schema_editor() as editor:
  1980. operation.database_forwards(app_label, editor, project_state, new_state)
  1981. self.assertIndexNotExists(table_name, ['pink', 'weight'])
  1982. self.assertUniqueConstraintExists(table_name, ['pink', 'weight'])
  1983. @skipUnlessDBFeature('supports_table_check_constraints')
  1984. def test_add_constraint(self):
  1985. project_state = self.set_up_test_model("test_addconstraint")
  1986. gt_check = models.Q(pink__gt=2)
  1987. gt_constraint = models.CheckConstraint(check=gt_check, name="test_add_constraint_pony_pink_gt_2")
  1988. gt_operation = migrations.AddConstraint("Pony", gt_constraint)
  1989. self.assertEqual(
  1990. gt_operation.describe(), "Create constraint test_add_constraint_pony_pink_gt_2 on model Pony"
  1991. )
  1992. self.assertEqual(
  1993. gt_operation.migration_name_fragment,
  1994. 'pony_test_add_constraint_pony_pink_gt_2',
  1995. )
  1996. # Test the state alteration
  1997. new_state = project_state.clone()
  1998. gt_operation.state_forwards("test_addconstraint", new_state)
  1999. self.assertEqual(len(new_state.models["test_addconstraint", "pony"].options["constraints"]), 1)
  2000. Pony = new_state.apps.get_model("test_addconstraint", "Pony")
  2001. self.assertEqual(len(Pony._meta.constraints), 1)
  2002. # Test the database alteration
  2003. with connection.schema_editor() as editor:
  2004. gt_operation.database_forwards("test_addconstraint", editor, project_state, new_state)
  2005. with self.assertRaises(IntegrityError), transaction.atomic():
  2006. Pony.objects.create(pink=1, weight=1.0)
  2007. # Add another one.
  2008. lt_check = models.Q(pink__lt=100)
  2009. lt_constraint = models.CheckConstraint(check=lt_check, name="test_add_constraint_pony_pink_lt_100")
  2010. lt_operation = migrations.AddConstraint("Pony", lt_constraint)
  2011. lt_operation.state_forwards("test_addconstraint", new_state)
  2012. self.assertEqual(len(new_state.models["test_addconstraint", "pony"].options["constraints"]), 2)
  2013. Pony = new_state.apps.get_model("test_addconstraint", "Pony")
  2014. self.assertEqual(len(Pony._meta.constraints), 2)
  2015. with connection.schema_editor() as editor:
  2016. lt_operation.database_forwards("test_addconstraint", editor, project_state, new_state)
  2017. with self.assertRaises(IntegrityError), transaction.atomic():
  2018. Pony.objects.create(pink=100, weight=1.0)
  2019. # Test reversal
  2020. with connection.schema_editor() as editor:
  2021. gt_operation.database_backwards("test_addconstraint", editor, new_state, project_state)
  2022. Pony.objects.create(pink=1, weight=1.0)
  2023. # Test deconstruction
  2024. definition = gt_operation.deconstruct()
  2025. self.assertEqual(definition[0], "AddConstraint")
  2026. self.assertEqual(definition[1], [])
  2027. self.assertEqual(definition[2], {'model_name': "Pony", 'constraint': gt_constraint})
  2028. @skipUnlessDBFeature('supports_table_check_constraints')
  2029. def test_add_constraint_percent_escaping(self):
  2030. app_label = 'add_constraint_string_quoting'
  2031. operations = [
  2032. migrations.CreateModel(
  2033. 'Author',
  2034. fields=[
  2035. ('id', models.AutoField(primary_key=True)),
  2036. ('name', models.CharField(max_length=100)),
  2037. ('surname', models.CharField(max_length=100, default='')),
  2038. ('rebate', models.CharField(max_length=100)),
  2039. ],
  2040. ),
  2041. ]
  2042. from_state = self.apply_operations(app_label, ProjectState(), operations)
  2043. # "%" generated in startswith lookup should be escaped in a way that is
  2044. # considered a leading wildcard.
  2045. check = models.Q(name__startswith='Albert')
  2046. constraint = models.CheckConstraint(check=check, name='name_constraint')
  2047. operation = migrations.AddConstraint('Author', constraint)
  2048. to_state = from_state.clone()
  2049. operation.state_forwards(app_label, to_state)
  2050. with connection.schema_editor() as editor:
  2051. operation.database_forwards(app_label, editor, from_state, to_state)
  2052. Author = to_state.apps.get_model(app_label, 'Author')
  2053. with self.assertRaises(IntegrityError), transaction.atomic():
  2054. Author.objects.create(name='Artur')
  2055. # Literal "%" should be escaped in a way that is not a considered a
  2056. # wildcard.
  2057. check = models.Q(rebate__endswith='%')
  2058. constraint = models.CheckConstraint(check=check, name='rebate_constraint')
  2059. operation = migrations.AddConstraint('Author', constraint)
  2060. from_state = to_state
  2061. to_state = from_state.clone()
  2062. operation.state_forwards(app_label, to_state)
  2063. Author = to_state.apps.get_model(app_label, 'Author')
  2064. with connection.schema_editor() as editor:
  2065. operation.database_forwards(app_label, editor, from_state, to_state)
  2066. Author = to_state.apps.get_model(app_label, 'Author')
  2067. with self.assertRaises(IntegrityError), transaction.atomic():
  2068. Author.objects.create(name='Albert', rebate='10$')
  2069. author = Author.objects.create(name='Albert', rebate='10%')
  2070. self.assertEqual(Author.objects.get(), author)
  2071. # Right-hand-side baked "%" literals should not be used for parameters
  2072. # interpolation.
  2073. check = ~models.Q(surname__startswith=models.F('name'))
  2074. constraint = models.CheckConstraint(check=check, name='name_constraint_rhs')
  2075. operation = migrations.AddConstraint('Author', constraint)
  2076. from_state = to_state
  2077. to_state = from_state.clone()
  2078. operation.state_forwards(app_label, to_state)
  2079. with connection.schema_editor() as editor:
  2080. operation.database_forwards(app_label, editor, from_state, to_state)
  2081. Author = to_state.apps.get_model(app_label, 'Author')
  2082. with self.assertRaises(IntegrityError), transaction.atomic():
  2083. Author.objects.create(name='Albert', surname='Alberto')
  2084. @skipUnlessDBFeature('supports_table_check_constraints')
  2085. def test_add_or_constraint(self):
  2086. app_label = 'test_addorconstraint'
  2087. constraint_name = 'add_constraint_or'
  2088. from_state = self.set_up_test_model(app_label)
  2089. check = models.Q(pink__gt=2, weight__gt=2) | models.Q(weight__lt=0)
  2090. constraint = models.CheckConstraint(check=check, name=constraint_name)
  2091. operation = migrations.AddConstraint('Pony', constraint)
  2092. to_state = from_state.clone()
  2093. operation.state_forwards(app_label, to_state)
  2094. with connection.schema_editor() as editor:
  2095. operation.database_forwards(app_label, editor, from_state, to_state)
  2096. Pony = to_state.apps.get_model(app_label, 'Pony')
  2097. with self.assertRaises(IntegrityError), transaction.atomic():
  2098. Pony.objects.create(pink=2, weight=3.0)
  2099. with self.assertRaises(IntegrityError), transaction.atomic():
  2100. Pony.objects.create(pink=3, weight=1.0)
  2101. Pony.objects.bulk_create([
  2102. Pony(pink=3, weight=-1.0),
  2103. Pony(pink=1, weight=-1.0),
  2104. Pony(pink=3, weight=3.0),
  2105. ])
  2106. @skipUnlessDBFeature('supports_table_check_constraints')
  2107. def test_add_constraint_combinable(self):
  2108. app_label = 'test_addconstraint_combinable'
  2109. operations = [
  2110. migrations.CreateModel(
  2111. 'Book',
  2112. fields=[
  2113. ('id', models.AutoField(primary_key=True)),
  2114. ('read', models.PositiveIntegerField()),
  2115. ('unread', models.PositiveIntegerField()),
  2116. ],
  2117. ),
  2118. ]
  2119. from_state = self.apply_operations(app_label, ProjectState(), operations)
  2120. constraint = models.CheckConstraint(
  2121. check=models.Q(read=(100 - models.F('unread'))),
  2122. name='test_addconstraint_combinable_sum_100',
  2123. )
  2124. operation = migrations.AddConstraint('Book', constraint)
  2125. to_state = from_state.clone()
  2126. operation.state_forwards(app_label, to_state)
  2127. with connection.schema_editor() as editor:
  2128. operation.database_forwards(app_label, editor, from_state, to_state)
  2129. Book = to_state.apps.get_model(app_label, 'Book')
  2130. with self.assertRaises(IntegrityError), transaction.atomic():
  2131. Book.objects.create(read=70, unread=10)
  2132. Book.objects.create(read=70, unread=30)
  2133. @skipUnlessDBFeature('supports_table_check_constraints')
  2134. def test_remove_constraint(self):
  2135. project_state = self.set_up_test_model("test_removeconstraint", constraints=[
  2136. models.CheckConstraint(check=models.Q(pink__gt=2), name="test_remove_constraint_pony_pink_gt_2"),
  2137. models.CheckConstraint(check=models.Q(pink__lt=100), name="test_remove_constraint_pony_pink_lt_100"),
  2138. ])
  2139. gt_operation = migrations.RemoveConstraint("Pony", "test_remove_constraint_pony_pink_gt_2")
  2140. self.assertEqual(
  2141. gt_operation.describe(), "Remove constraint test_remove_constraint_pony_pink_gt_2 from model Pony"
  2142. )
  2143. self.assertEqual(
  2144. gt_operation.migration_name_fragment,
  2145. 'remove_pony_test_remove_constraint_pony_pink_gt_2',
  2146. )
  2147. # Test state alteration
  2148. new_state = project_state.clone()
  2149. gt_operation.state_forwards("test_removeconstraint", new_state)
  2150. self.assertEqual(len(new_state.models["test_removeconstraint", "pony"].options['constraints']), 1)
  2151. Pony = new_state.apps.get_model("test_removeconstraint", "Pony")
  2152. self.assertEqual(len(Pony._meta.constraints), 1)
  2153. # Test database alteration
  2154. with connection.schema_editor() as editor:
  2155. gt_operation.database_forwards("test_removeconstraint", editor, project_state, new_state)
  2156. Pony.objects.create(pink=1, weight=1.0).delete()
  2157. with self.assertRaises(IntegrityError), transaction.atomic():
  2158. Pony.objects.create(pink=100, weight=1.0)
  2159. # Remove the other one.
  2160. lt_operation = migrations.RemoveConstraint("Pony", "test_remove_constraint_pony_pink_lt_100")
  2161. lt_operation.state_forwards("test_removeconstraint", new_state)
  2162. self.assertEqual(len(new_state.models["test_removeconstraint", "pony"].options['constraints']), 0)
  2163. Pony = new_state.apps.get_model("test_removeconstraint", "Pony")
  2164. self.assertEqual(len(Pony._meta.constraints), 0)
  2165. with connection.schema_editor() as editor:
  2166. lt_operation.database_forwards("test_removeconstraint", editor, project_state, new_state)
  2167. Pony.objects.create(pink=100, weight=1.0).delete()
  2168. # Test reversal
  2169. with connection.schema_editor() as editor:
  2170. gt_operation.database_backwards("test_removeconstraint", editor, new_state, project_state)
  2171. with self.assertRaises(IntegrityError), transaction.atomic():
  2172. Pony.objects.create(pink=1, weight=1.0)
  2173. # Test deconstruction
  2174. definition = gt_operation.deconstruct()
  2175. self.assertEqual(definition[0], "RemoveConstraint")
  2176. self.assertEqual(definition[1], [])
  2177. self.assertEqual(definition[2], {'model_name': "Pony", 'name': "test_remove_constraint_pony_pink_gt_2"})
  2178. def test_add_partial_unique_constraint(self):
  2179. project_state = self.set_up_test_model('test_addpartialuniqueconstraint')
  2180. partial_unique_constraint = models.UniqueConstraint(
  2181. fields=['pink'],
  2182. condition=models.Q(weight__gt=5),
  2183. name='test_constraint_pony_pink_for_weight_gt_5_uniq',
  2184. )
  2185. operation = migrations.AddConstraint('Pony', partial_unique_constraint)
  2186. self.assertEqual(
  2187. operation.describe(),
  2188. 'Create constraint test_constraint_pony_pink_for_weight_gt_5_uniq '
  2189. 'on model Pony'
  2190. )
  2191. # Test the state alteration
  2192. new_state = project_state.clone()
  2193. operation.state_forwards('test_addpartialuniqueconstraint', new_state)
  2194. self.assertEqual(len(new_state.models['test_addpartialuniqueconstraint', 'pony'].options['constraints']), 1)
  2195. Pony = new_state.apps.get_model('test_addpartialuniqueconstraint', 'Pony')
  2196. self.assertEqual(len(Pony._meta.constraints), 1)
  2197. # Test the database alteration
  2198. with connection.schema_editor() as editor:
  2199. operation.database_forwards('test_addpartialuniqueconstraint', editor, project_state, new_state)
  2200. # Test constraint works
  2201. Pony.objects.create(pink=1, weight=4.0)
  2202. Pony.objects.create(pink=1, weight=4.0)
  2203. Pony.objects.create(pink=1, weight=6.0)
  2204. if connection.features.supports_partial_indexes:
  2205. with self.assertRaises(IntegrityError), transaction.atomic():
  2206. Pony.objects.create(pink=1, weight=7.0)
  2207. else:
  2208. Pony.objects.create(pink=1, weight=7.0)
  2209. # Test reversal
  2210. with connection.schema_editor() as editor:
  2211. operation.database_backwards('test_addpartialuniqueconstraint', editor, new_state, project_state)
  2212. # Test constraint doesn't work
  2213. Pony.objects.create(pink=1, weight=7.0)
  2214. # Test deconstruction
  2215. definition = operation.deconstruct()
  2216. self.assertEqual(definition[0], 'AddConstraint')
  2217. self.assertEqual(definition[1], [])
  2218. self.assertEqual(definition[2], {'model_name': 'Pony', 'constraint': partial_unique_constraint})
  2219. def test_remove_partial_unique_constraint(self):
  2220. project_state = self.set_up_test_model('test_removepartialuniqueconstraint', constraints=[
  2221. models.UniqueConstraint(
  2222. fields=['pink'],
  2223. condition=models.Q(weight__gt=5),
  2224. name='test_constraint_pony_pink_for_weight_gt_5_uniq',
  2225. ),
  2226. ])
  2227. gt_operation = migrations.RemoveConstraint('Pony', 'test_constraint_pony_pink_for_weight_gt_5_uniq')
  2228. self.assertEqual(
  2229. gt_operation.describe(), 'Remove constraint test_constraint_pony_pink_for_weight_gt_5_uniq from model Pony'
  2230. )
  2231. # Test state alteration
  2232. new_state = project_state.clone()
  2233. gt_operation.state_forwards('test_removepartialuniqueconstraint', new_state)
  2234. self.assertEqual(len(new_state.models['test_removepartialuniqueconstraint', 'pony'].options['constraints']), 0)
  2235. Pony = new_state.apps.get_model('test_removepartialuniqueconstraint', 'Pony')
  2236. self.assertEqual(len(Pony._meta.constraints), 0)
  2237. # Test database alteration
  2238. with connection.schema_editor() as editor:
  2239. gt_operation.database_forwards('test_removepartialuniqueconstraint', editor, project_state, new_state)
  2240. # Test constraint doesn't work
  2241. Pony.objects.create(pink=1, weight=4.0)
  2242. Pony.objects.create(pink=1, weight=4.0)
  2243. Pony.objects.create(pink=1, weight=6.0)
  2244. Pony.objects.create(pink=1, weight=7.0).delete()
  2245. # Test reversal
  2246. with connection.schema_editor() as editor:
  2247. gt_operation.database_backwards('test_removepartialuniqueconstraint', editor, new_state, project_state)
  2248. # Test constraint works
  2249. if connection.features.supports_partial_indexes:
  2250. with self.assertRaises(IntegrityError), transaction.atomic():
  2251. Pony.objects.create(pink=1, weight=7.0)
  2252. else:
  2253. Pony.objects.create(pink=1, weight=7.0)
  2254. # Test deconstruction
  2255. definition = gt_operation.deconstruct()
  2256. self.assertEqual(definition[0], 'RemoveConstraint')
  2257. self.assertEqual(definition[1], [])
  2258. self.assertEqual(definition[2], {
  2259. 'model_name': 'Pony',
  2260. 'name': 'test_constraint_pony_pink_for_weight_gt_5_uniq',
  2261. })
  2262. def test_add_deferred_unique_constraint(self):
  2263. app_label = 'test_adddeferred_uc'
  2264. project_state = self.set_up_test_model(app_label)
  2265. deferred_unique_constraint = models.UniqueConstraint(
  2266. fields=['pink'],
  2267. name='deferred_pink_constraint_add',
  2268. deferrable=models.Deferrable.DEFERRED,
  2269. )
  2270. operation = migrations.AddConstraint('Pony', deferred_unique_constraint)
  2271. self.assertEqual(
  2272. operation.describe(),
  2273. 'Create constraint deferred_pink_constraint_add on model Pony',
  2274. )
  2275. # Add constraint.
  2276. new_state = project_state.clone()
  2277. operation.state_forwards(app_label, new_state)
  2278. self.assertEqual(len(new_state.models[app_label, 'pony'].options['constraints']), 1)
  2279. Pony = new_state.apps.get_model(app_label, 'Pony')
  2280. self.assertEqual(len(Pony._meta.constraints), 1)
  2281. with connection.schema_editor() as editor:
  2282. operation.database_forwards(app_label, editor, project_state, new_state)
  2283. Pony.objects.create(pink=1, weight=4.0)
  2284. if connection.features.supports_deferrable_unique_constraints:
  2285. # Unique constraint is deferred.
  2286. with transaction.atomic():
  2287. obj = Pony.objects.create(pink=1, weight=4.0)
  2288. obj.pink = 2
  2289. obj.save()
  2290. # Constraint behavior can be changed with SET CONSTRAINTS.
  2291. with self.assertRaises(IntegrityError):
  2292. with transaction.atomic(), connection.cursor() as cursor:
  2293. quoted_name = connection.ops.quote_name(deferred_unique_constraint.name)
  2294. cursor.execute('SET CONSTRAINTS %s IMMEDIATE' % quoted_name)
  2295. obj = Pony.objects.create(pink=1, weight=4.0)
  2296. obj.pink = 3
  2297. obj.save()
  2298. else:
  2299. Pony.objects.create(pink=1, weight=4.0)
  2300. # Reversal.
  2301. with connection.schema_editor() as editor:
  2302. operation.database_backwards(app_label, editor, new_state, project_state)
  2303. # Constraint doesn't work.
  2304. Pony.objects.create(pink=1, weight=4.0)
  2305. # Deconstruction.
  2306. definition = operation.deconstruct()
  2307. self.assertEqual(definition[0], 'AddConstraint')
  2308. self.assertEqual(definition[1], [])
  2309. self.assertEqual(
  2310. definition[2],
  2311. {'model_name': 'Pony', 'constraint': deferred_unique_constraint},
  2312. )
  2313. def test_remove_deferred_unique_constraint(self):
  2314. app_label = 'test_removedeferred_uc'
  2315. deferred_unique_constraint = models.UniqueConstraint(
  2316. fields=['pink'],
  2317. name='deferred_pink_constraint_rm',
  2318. deferrable=models.Deferrable.DEFERRED,
  2319. )
  2320. project_state = self.set_up_test_model(app_label, constraints=[deferred_unique_constraint])
  2321. operation = migrations.RemoveConstraint('Pony', deferred_unique_constraint.name)
  2322. self.assertEqual(
  2323. operation.describe(),
  2324. 'Remove constraint deferred_pink_constraint_rm from model Pony',
  2325. )
  2326. # Remove constraint.
  2327. new_state = project_state.clone()
  2328. operation.state_forwards(app_label, new_state)
  2329. self.assertEqual(len(new_state.models[app_label, 'pony'].options['constraints']), 0)
  2330. Pony = new_state.apps.get_model(app_label, 'Pony')
  2331. self.assertEqual(len(Pony._meta.constraints), 0)
  2332. with connection.schema_editor() as editor:
  2333. operation.database_forwards(app_label, editor, project_state, new_state)
  2334. # Constraint doesn't work.
  2335. Pony.objects.create(pink=1, weight=4.0)
  2336. Pony.objects.create(pink=1, weight=4.0).delete()
  2337. # Reversal.
  2338. with connection.schema_editor() as editor:
  2339. operation.database_backwards(app_label, editor, new_state, project_state)
  2340. if connection.features.supports_deferrable_unique_constraints:
  2341. # Unique constraint is deferred.
  2342. with transaction.atomic():
  2343. obj = Pony.objects.create(pink=1, weight=4.0)
  2344. obj.pink = 2
  2345. obj.save()
  2346. # Constraint behavior can be changed with SET CONSTRAINTS.
  2347. with self.assertRaises(IntegrityError):
  2348. with transaction.atomic(), connection.cursor() as cursor:
  2349. quoted_name = connection.ops.quote_name(deferred_unique_constraint.name)
  2350. cursor.execute('SET CONSTRAINTS %s IMMEDIATE' % quoted_name)
  2351. obj = Pony.objects.create(pink=1, weight=4.0)
  2352. obj.pink = 3
  2353. obj.save()
  2354. else:
  2355. Pony.objects.create(pink=1, weight=4.0)
  2356. # Deconstruction.
  2357. definition = operation.deconstruct()
  2358. self.assertEqual(definition[0], 'RemoveConstraint')
  2359. self.assertEqual(definition[1], [])
  2360. self.assertEqual(definition[2], {
  2361. 'model_name': 'Pony',
  2362. 'name': 'deferred_pink_constraint_rm',
  2363. })
  2364. def test_add_covering_unique_constraint(self):
  2365. app_label = 'test_addcovering_uc'
  2366. project_state = self.set_up_test_model(app_label)
  2367. covering_unique_constraint = models.UniqueConstraint(
  2368. fields=['pink'],
  2369. name='covering_pink_constraint_add',
  2370. include=['weight'],
  2371. )
  2372. operation = migrations.AddConstraint('Pony', covering_unique_constraint)
  2373. self.assertEqual(
  2374. operation.describe(),
  2375. 'Create constraint covering_pink_constraint_add on model Pony',
  2376. )
  2377. # Add constraint.
  2378. new_state = project_state.clone()
  2379. operation.state_forwards(app_label, new_state)
  2380. self.assertEqual(len(new_state.models[app_label, 'pony'].options['constraints']), 1)
  2381. Pony = new_state.apps.get_model(app_label, 'Pony')
  2382. self.assertEqual(len(Pony._meta.constraints), 1)
  2383. with connection.schema_editor() as editor:
  2384. operation.database_forwards(app_label, editor, project_state, new_state)
  2385. Pony.objects.create(pink=1, weight=4.0)
  2386. if connection.features.supports_covering_indexes:
  2387. with self.assertRaises(IntegrityError):
  2388. Pony.objects.create(pink=1, weight=4.0)
  2389. else:
  2390. Pony.objects.create(pink=1, weight=4.0)
  2391. # Reversal.
  2392. with connection.schema_editor() as editor:
  2393. operation.database_backwards(app_label, editor, new_state, project_state)
  2394. # Constraint doesn't work.
  2395. Pony.objects.create(pink=1, weight=4.0)
  2396. # Deconstruction.
  2397. definition = operation.deconstruct()
  2398. self.assertEqual(definition[0], 'AddConstraint')
  2399. self.assertEqual(definition[1], [])
  2400. self.assertEqual(
  2401. definition[2],
  2402. {'model_name': 'Pony', 'constraint': covering_unique_constraint},
  2403. )
  2404. def test_remove_covering_unique_constraint(self):
  2405. app_label = 'test_removecovering_uc'
  2406. covering_unique_constraint = models.UniqueConstraint(
  2407. fields=['pink'],
  2408. name='covering_pink_constraint_rm',
  2409. include=['weight'],
  2410. )
  2411. project_state = self.set_up_test_model(app_label, constraints=[covering_unique_constraint])
  2412. operation = migrations.RemoveConstraint('Pony', covering_unique_constraint.name)
  2413. self.assertEqual(
  2414. operation.describe(),
  2415. 'Remove constraint covering_pink_constraint_rm from model Pony',
  2416. )
  2417. # Remove constraint.
  2418. new_state = project_state.clone()
  2419. operation.state_forwards(app_label, new_state)
  2420. self.assertEqual(len(new_state.models[app_label, 'pony'].options['constraints']), 0)
  2421. Pony = new_state.apps.get_model(app_label, 'Pony')
  2422. self.assertEqual(len(Pony._meta.constraints), 0)
  2423. with connection.schema_editor() as editor:
  2424. operation.database_forwards(app_label, editor, project_state, new_state)
  2425. # Constraint doesn't work.
  2426. Pony.objects.create(pink=1, weight=4.0)
  2427. Pony.objects.create(pink=1, weight=4.0).delete()
  2428. # Reversal.
  2429. with connection.schema_editor() as editor:
  2430. operation.database_backwards(app_label, editor, new_state, project_state)
  2431. if connection.features.supports_covering_indexes:
  2432. with self.assertRaises(IntegrityError):
  2433. Pony.objects.create(pink=1, weight=4.0)
  2434. else:
  2435. Pony.objects.create(pink=1, weight=4.0)
  2436. # Deconstruction.
  2437. definition = operation.deconstruct()
  2438. self.assertEqual(definition[0], 'RemoveConstraint')
  2439. self.assertEqual(definition[1], [])
  2440. self.assertEqual(definition[2], {
  2441. 'model_name': 'Pony',
  2442. 'name': 'covering_pink_constraint_rm',
  2443. })
  2444. def test_add_func_unique_constraint(self):
  2445. app_label = 'test_adfuncuc'
  2446. constraint_name = f'{app_label}_pony_abs_uq'
  2447. table_name = f'{app_label}_pony'
  2448. project_state = self.set_up_test_model(app_label)
  2449. constraint = models.UniqueConstraint(Abs('weight'), name=constraint_name)
  2450. operation = migrations.AddConstraint('Pony', constraint)
  2451. self.assertEqual(
  2452. operation.describe(),
  2453. 'Create constraint test_adfuncuc_pony_abs_uq on model Pony',
  2454. )
  2455. self.assertEqual(
  2456. operation.migration_name_fragment,
  2457. 'pony_test_adfuncuc_pony_abs_uq',
  2458. )
  2459. new_state = project_state.clone()
  2460. operation.state_forwards(app_label, new_state)
  2461. self.assertEqual(len(new_state.models[app_label, 'pony'].options['constraints']), 1)
  2462. self.assertIndexNameNotExists(table_name, constraint_name)
  2463. # Add constraint.
  2464. with connection.schema_editor() as editor:
  2465. operation.database_forwards(app_label, editor, project_state, new_state)
  2466. Pony = new_state.apps.get_model(app_label, 'Pony')
  2467. Pony.objects.create(weight=4.0)
  2468. if connection.features.supports_expression_indexes:
  2469. self.assertIndexNameExists(table_name, constraint_name)
  2470. with self.assertRaises(IntegrityError):
  2471. Pony.objects.create(weight=-4.0)
  2472. else:
  2473. self.assertIndexNameNotExists(table_name, constraint_name)
  2474. Pony.objects.create(weight=-4.0)
  2475. # Reversal.
  2476. with connection.schema_editor() as editor:
  2477. operation.database_backwards(app_label, editor, new_state, project_state)
  2478. self.assertIndexNameNotExists(table_name, constraint_name)
  2479. # Constraint doesn't work.
  2480. Pony.objects.create(weight=-4.0)
  2481. # Deconstruction.
  2482. definition = operation.deconstruct()
  2483. self.assertEqual(definition[0], 'AddConstraint')
  2484. self.assertEqual(definition[1], [])
  2485. self.assertEqual(
  2486. definition[2],
  2487. {'model_name': 'Pony', 'constraint': constraint},
  2488. )
  2489. def test_remove_func_unique_constraint(self):
  2490. app_label = 'test_rmfuncuc'
  2491. constraint_name = f'{app_label}_pony_abs_uq'
  2492. table_name = f'{app_label}_pony'
  2493. project_state = self.set_up_test_model(app_label, constraints=[
  2494. models.UniqueConstraint(Abs('weight'), name=constraint_name),
  2495. ])
  2496. self.assertTableExists(table_name)
  2497. if connection.features.supports_expression_indexes:
  2498. self.assertIndexNameExists(table_name, constraint_name)
  2499. operation = migrations.RemoveConstraint('Pony', constraint_name)
  2500. self.assertEqual(
  2501. operation.describe(),
  2502. 'Remove constraint test_rmfuncuc_pony_abs_uq from model Pony',
  2503. )
  2504. self.assertEqual(
  2505. operation.migration_name_fragment,
  2506. 'remove_pony_test_rmfuncuc_pony_abs_uq',
  2507. )
  2508. new_state = project_state.clone()
  2509. operation.state_forwards(app_label, new_state)
  2510. self.assertEqual(len(new_state.models[app_label, 'pony'].options['constraints']), 0)
  2511. Pony = new_state.apps.get_model(app_label, 'Pony')
  2512. self.assertEqual(len(Pony._meta.constraints), 0)
  2513. # Remove constraint.
  2514. with connection.schema_editor() as editor:
  2515. operation.database_forwards(app_label, editor, project_state, new_state)
  2516. self.assertIndexNameNotExists(table_name, constraint_name)
  2517. # Constraint doesn't work.
  2518. Pony.objects.create(pink=1, weight=4.0)
  2519. Pony.objects.create(pink=1, weight=-4.0).delete()
  2520. # Reversal.
  2521. with connection.schema_editor() as editor:
  2522. operation.database_backwards(app_label, editor, new_state, project_state)
  2523. if connection.features.supports_expression_indexes:
  2524. self.assertIndexNameExists(table_name, constraint_name)
  2525. with self.assertRaises(IntegrityError):
  2526. Pony.objects.create(weight=-4.0)
  2527. else:
  2528. self.assertIndexNameNotExists(table_name, constraint_name)
  2529. Pony.objects.create(weight=-4.0)
  2530. # Deconstruction.
  2531. definition = operation.deconstruct()
  2532. self.assertEqual(definition[0], 'RemoveConstraint')
  2533. self.assertEqual(definition[1], [])
  2534. self.assertEqual(definition[2], {'model_name': 'Pony', 'name': constraint_name})
  2535. def test_alter_model_options(self):
  2536. """
  2537. Tests the AlterModelOptions operation.
  2538. """
  2539. project_state = self.set_up_test_model("test_almoop")
  2540. # Test the state alteration (no DB alteration to test)
  2541. operation = migrations.AlterModelOptions("Pony", {"permissions": [("can_groom", "Can groom")]})
  2542. self.assertEqual(operation.describe(), "Change Meta options on Pony")
  2543. self.assertEqual(operation.migration_name_fragment, 'alter_pony_options')
  2544. new_state = project_state.clone()
  2545. operation.state_forwards("test_almoop", new_state)
  2546. self.assertEqual(len(project_state.models["test_almoop", "pony"].options.get("permissions", [])), 0)
  2547. self.assertEqual(len(new_state.models["test_almoop", "pony"].options.get("permissions", [])), 1)
  2548. self.assertEqual(new_state.models["test_almoop", "pony"].options["permissions"][0][0], "can_groom")
  2549. # And deconstruction
  2550. definition = operation.deconstruct()
  2551. self.assertEqual(definition[0], "AlterModelOptions")
  2552. self.assertEqual(definition[1], [])
  2553. self.assertEqual(definition[2], {'name': "Pony", 'options': {"permissions": [("can_groom", "Can groom")]}})
  2554. def test_alter_model_options_emptying(self):
  2555. """
  2556. The AlterModelOptions operation removes keys from the dict (#23121)
  2557. """
  2558. project_state = self.set_up_test_model("test_almoop", options=True)
  2559. # Test the state alteration (no DB alteration to test)
  2560. operation = migrations.AlterModelOptions("Pony", {})
  2561. self.assertEqual(operation.describe(), "Change Meta options on Pony")
  2562. new_state = project_state.clone()
  2563. operation.state_forwards("test_almoop", new_state)
  2564. self.assertEqual(len(project_state.models["test_almoop", "pony"].options.get("permissions", [])), 1)
  2565. self.assertEqual(len(new_state.models["test_almoop", "pony"].options.get("permissions", [])), 0)
  2566. # And deconstruction
  2567. definition = operation.deconstruct()
  2568. self.assertEqual(definition[0], "AlterModelOptions")
  2569. self.assertEqual(definition[1], [])
  2570. self.assertEqual(definition[2], {'name': "Pony", 'options': {}})
  2571. def test_alter_order_with_respect_to(self):
  2572. """
  2573. Tests the AlterOrderWithRespectTo operation.
  2574. """
  2575. project_state = self.set_up_test_model("test_alorwrtto", related_model=True)
  2576. # Test the state alteration
  2577. operation = migrations.AlterOrderWithRespectTo("Rider", "pony")
  2578. self.assertEqual(operation.describe(), "Set order_with_respect_to on Rider to pony")
  2579. self.assertEqual(
  2580. operation.migration_name_fragment,
  2581. 'alter_rider_order_with_respect_to',
  2582. )
  2583. new_state = project_state.clone()
  2584. operation.state_forwards("test_alorwrtto", new_state)
  2585. self.assertIsNone(
  2586. project_state.models["test_alorwrtto", "rider"].options.get("order_with_respect_to", None)
  2587. )
  2588. self.assertEqual(
  2589. new_state.models["test_alorwrtto", "rider"].options.get("order_with_respect_to", None),
  2590. "pony"
  2591. )
  2592. # Make sure there's no matching index
  2593. self.assertColumnNotExists("test_alorwrtto_rider", "_order")
  2594. # Create some rows before alteration
  2595. rendered_state = project_state.apps
  2596. pony = rendered_state.get_model("test_alorwrtto", "Pony").objects.create(weight=50)
  2597. rider1 = rendered_state.get_model("test_alorwrtto", "Rider").objects.create(pony=pony)
  2598. rider1.friend = rider1
  2599. rider1.save()
  2600. rider2 = rendered_state.get_model("test_alorwrtto", "Rider").objects.create(pony=pony)
  2601. rider2.friend = rider2
  2602. rider2.save()
  2603. # Test the database alteration
  2604. with connection.schema_editor() as editor:
  2605. operation.database_forwards("test_alorwrtto", editor, project_state, new_state)
  2606. self.assertColumnExists("test_alorwrtto_rider", "_order")
  2607. # Check for correct value in rows
  2608. updated_riders = new_state.apps.get_model("test_alorwrtto", "Rider").objects.all()
  2609. self.assertEqual(updated_riders[0]._order, 0)
  2610. self.assertEqual(updated_riders[1]._order, 0)
  2611. # And test reversal
  2612. with connection.schema_editor() as editor:
  2613. operation.database_backwards("test_alorwrtto", editor, new_state, project_state)
  2614. self.assertColumnNotExists("test_alorwrtto_rider", "_order")
  2615. # And deconstruction
  2616. definition = operation.deconstruct()
  2617. self.assertEqual(definition[0], "AlterOrderWithRespectTo")
  2618. self.assertEqual(definition[1], [])
  2619. self.assertEqual(definition[2], {'name': "Rider", 'order_with_respect_to': "pony"})
  2620. def test_alter_model_managers(self):
  2621. """
  2622. The managers on a model are set.
  2623. """
  2624. project_state = self.set_up_test_model("test_almoma")
  2625. # Test the state alteration
  2626. operation = migrations.AlterModelManagers(
  2627. "Pony",
  2628. managers=[
  2629. ("food_qs", FoodQuerySet.as_manager()),
  2630. ("food_mgr", FoodManager("a", "b")),
  2631. ("food_mgr_kwargs", FoodManager("x", "y", 3, 4)),
  2632. ]
  2633. )
  2634. self.assertEqual(operation.describe(), "Change managers on Pony")
  2635. self.assertEqual(operation.migration_name_fragment, 'alter_pony_managers')
  2636. managers = project_state.models["test_almoma", "pony"].managers
  2637. self.assertEqual(managers, [])
  2638. new_state = project_state.clone()
  2639. operation.state_forwards("test_almoma", new_state)
  2640. self.assertIn(("test_almoma", "pony"), new_state.models)
  2641. managers = new_state.models["test_almoma", "pony"].managers
  2642. self.assertEqual(managers[0][0], "food_qs")
  2643. self.assertIsInstance(managers[0][1], models.Manager)
  2644. self.assertEqual(managers[1][0], "food_mgr")
  2645. self.assertIsInstance(managers[1][1], FoodManager)
  2646. self.assertEqual(managers[1][1].args, ("a", "b", 1, 2))
  2647. self.assertEqual(managers[2][0], "food_mgr_kwargs")
  2648. self.assertIsInstance(managers[2][1], FoodManager)
  2649. self.assertEqual(managers[2][1].args, ("x", "y", 3, 4))
  2650. rendered_state = new_state.apps
  2651. model = rendered_state.get_model('test_almoma', 'pony')
  2652. self.assertIsInstance(model.food_qs, models.Manager)
  2653. self.assertIsInstance(model.food_mgr, FoodManager)
  2654. self.assertIsInstance(model.food_mgr_kwargs, FoodManager)
  2655. def test_alter_model_managers_emptying(self):
  2656. """
  2657. The managers on a model are set.
  2658. """
  2659. project_state = self.set_up_test_model("test_almomae", manager_model=True)
  2660. # Test the state alteration
  2661. operation = migrations.AlterModelManagers("Food", managers=[])
  2662. self.assertEqual(operation.describe(), "Change managers on Food")
  2663. self.assertIn(("test_almomae", "food"), project_state.models)
  2664. managers = project_state.models["test_almomae", "food"].managers
  2665. self.assertEqual(managers[0][0], "food_qs")
  2666. self.assertIsInstance(managers[0][1], models.Manager)
  2667. self.assertEqual(managers[1][0], "food_mgr")
  2668. self.assertIsInstance(managers[1][1], FoodManager)
  2669. self.assertEqual(managers[1][1].args, ("a", "b", 1, 2))
  2670. self.assertEqual(managers[2][0], "food_mgr_kwargs")
  2671. self.assertIsInstance(managers[2][1], FoodManager)
  2672. self.assertEqual(managers[2][1].args, ("x", "y", 3, 4))
  2673. new_state = project_state.clone()
  2674. operation.state_forwards("test_almomae", new_state)
  2675. managers = new_state.models["test_almomae", "food"].managers
  2676. self.assertEqual(managers, [])
  2677. def test_alter_fk(self):
  2678. """
  2679. Creating and then altering an FK works correctly
  2680. and deals with the pending SQL (#23091)
  2681. """
  2682. project_state = self.set_up_test_model("test_alfk")
  2683. # Test adding and then altering the FK in one go
  2684. create_operation = migrations.CreateModel(
  2685. name="Rider",
  2686. fields=[
  2687. ("id", models.AutoField(primary_key=True)),
  2688. ("pony", models.ForeignKey("Pony", models.CASCADE)),
  2689. ],
  2690. )
  2691. create_state = project_state.clone()
  2692. create_operation.state_forwards("test_alfk", create_state)
  2693. alter_operation = migrations.AlterField(
  2694. model_name='Rider',
  2695. name='pony',
  2696. field=models.ForeignKey("Pony", models.CASCADE, editable=False),
  2697. )
  2698. alter_state = create_state.clone()
  2699. alter_operation.state_forwards("test_alfk", alter_state)
  2700. with connection.schema_editor() as editor:
  2701. create_operation.database_forwards("test_alfk", editor, project_state, create_state)
  2702. alter_operation.database_forwards("test_alfk", editor, create_state, alter_state)
  2703. def test_alter_fk_non_fk(self):
  2704. """
  2705. Altering an FK to a non-FK works (#23244)
  2706. """
  2707. # Test the state alteration
  2708. operation = migrations.AlterField(
  2709. model_name="Rider",
  2710. name="pony",
  2711. field=models.FloatField(),
  2712. )
  2713. project_state, new_state = self.make_test_state("test_afknfk", operation, related_model=True)
  2714. # Test the database alteration
  2715. self.assertColumnExists("test_afknfk_rider", "pony_id")
  2716. self.assertColumnNotExists("test_afknfk_rider", "pony")
  2717. with connection.schema_editor() as editor:
  2718. operation.database_forwards("test_afknfk", editor, project_state, new_state)
  2719. self.assertColumnExists("test_afknfk_rider", "pony")
  2720. self.assertColumnNotExists("test_afknfk_rider", "pony_id")
  2721. # And test reversal
  2722. with connection.schema_editor() as editor:
  2723. operation.database_backwards("test_afknfk", editor, new_state, project_state)
  2724. self.assertColumnExists("test_afknfk_rider", "pony_id")
  2725. self.assertColumnNotExists("test_afknfk_rider", "pony")
  2726. def test_run_sql(self):
  2727. """
  2728. Tests the RunSQL operation.
  2729. """
  2730. project_state = self.set_up_test_model("test_runsql")
  2731. # Create the operation
  2732. operation = migrations.RunSQL(
  2733. # Use a multi-line string with a comment to test splitting on SQLite and MySQL respectively
  2734. "CREATE TABLE i_love_ponies (id int, special_thing varchar(15));\n"
  2735. "INSERT INTO i_love_ponies (id, special_thing) VALUES (1, 'i love ponies'); -- this is magic!\n"
  2736. "INSERT INTO i_love_ponies (id, special_thing) VALUES (2, 'i love django');\n"
  2737. "UPDATE i_love_ponies SET special_thing = 'Ponies' WHERE special_thing LIKE '%%ponies';"
  2738. "UPDATE i_love_ponies SET special_thing = 'Django' WHERE special_thing LIKE '%django';",
  2739. # Run delete queries to test for parameter substitution failure
  2740. # reported in #23426
  2741. "DELETE FROM i_love_ponies WHERE special_thing LIKE '%Django%';"
  2742. "DELETE FROM i_love_ponies WHERE special_thing LIKE '%%Ponies%%';"
  2743. "DROP TABLE i_love_ponies",
  2744. state_operations=[migrations.CreateModel("SomethingElse", [("id", models.AutoField(primary_key=True))])],
  2745. )
  2746. self.assertEqual(operation.describe(), "Raw SQL operation")
  2747. # Test the state alteration
  2748. new_state = project_state.clone()
  2749. operation.state_forwards("test_runsql", new_state)
  2750. self.assertEqual(len(new_state.models["test_runsql", "somethingelse"].fields), 1)
  2751. # Make sure there's no table
  2752. self.assertTableNotExists("i_love_ponies")
  2753. # Test SQL collection
  2754. with connection.schema_editor(collect_sql=True) as editor:
  2755. operation.database_forwards("test_runsql", editor, project_state, new_state)
  2756. self.assertIn("LIKE '%%ponies';", "\n".join(editor.collected_sql))
  2757. operation.database_backwards("test_runsql", editor, project_state, new_state)
  2758. self.assertIn("LIKE '%%Ponies%%';", "\n".join(editor.collected_sql))
  2759. # Test the database alteration
  2760. with connection.schema_editor() as editor:
  2761. operation.database_forwards("test_runsql", editor, project_state, new_state)
  2762. self.assertTableExists("i_love_ponies")
  2763. # Make sure all the SQL was processed
  2764. with connection.cursor() as cursor:
  2765. cursor.execute("SELECT COUNT(*) FROM i_love_ponies")
  2766. self.assertEqual(cursor.fetchall()[0][0], 2)
  2767. cursor.execute("SELECT COUNT(*) FROM i_love_ponies WHERE special_thing = 'Django'")
  2768. self.assertEqual(cursor.fetchall()[0][0], 1)
  2769. cursor.execute("SELECT COUNT(*) FROM i_love_ponies WHERE special_thing = 'Ponies'")
  2770. self.assertEqual(cursor.fetchall()[0][0], 1)
  2771. # And test reversal
  2772. self.assertTrue(operation.reversible)
  2773. with connection.schema_editor() as editor:
  2774. operation.database_backwards("test_runsql", editor, new_state, project_state)
  2775. self.assertTableNotExists("i_love_ponies")
  2776. # And deconstruction
  2777. definition = operation.deconstruct()
  2778. self.assertEqual(definition[0], "RunSQL")
  2779. self.assertEqual(definition[1], [])
  2780. self.assertEqual(sorted(definition[2]), ["reverse_sql", "sql", "state_operations"])
  2781. # And elidable reduction
  2782. self.assertIs(False, operation.reduce(operation, []))
  2783. elidable_operation = migrations.RunSQL('SELECT 1 FROM void;', elidable=True)
  2784. self.assertEqual(elidable_operation.reduce(operation, []), [operation])
  2785. def test_run_sql_params(self):
  2786. """
  2787. #23426 - RunSQL should accept parameters.
  2788. """
  2789. project_state = self.set_up_test_model("test_runsql")
  2790. # Create the operation
  2791. operation = migrations.RunSQL(
  2792. ["CREATE TABLE i_love_ponies (id int, special_thing varchar(15));"],
  2793. ["DROP TABLE i_love_ponies"],
  2794. )
  2795. param_operation = migrations.RunSQL(
  2796. # forwards
  2797. (
  2798. "INSERT INTO i_love_ponies (id, special_thing) VALUES (1, 'Django');",
  2799. ["INSERT INTO i_love_ponies (id, special_thing) VALUES (2, %s);", ['Ponies']],
  2800. ("INSERT INTO i_love_ponies (id, special_thing) VALUES (%s, %s);", (3, 'Python',)),
  2801. ),
  2802. # backwards
  2803. [
  2804. "DELETE FROM i_love_ponies WHERE special_thing = 'Django';",
  2805. ["DELETE FROM i_love_ponies WHERE special_thing = 'Ponies';", None],
  2806. ("DELETE FROM i_love_ponies WHERE id = %s OR special_thing = %s;", [3, 'Python']),
  2807. ]
  2808. )
  2809. # Make sure there's no table
  2810. self.assertTableNotExists("i_love_ponies")
  2811. new_state = project_state.clone()
  2812. # Test the database alteration
  2813. with connection.schema_editor() as editor:
  2814. operation.database_forwards("test_runsql", editor, project_state, new_state)
  2815. # Test parameter passing
  2816. with connection.schema_editor() as editor:
  2817. param_operation.database_forwards("test_runsql", editor, project_state, new_state)
  2818. # Make sure all the SQL was processed
  2819. with connection.cursor() as cursor:
  2820. cursor.execute("SELECT COUNT(*) FROM i_love_ponies")
  2821. self.assertEqual(cursor.fetchall()[0][0], 3)
  2822. with connection.schema_editor() as editor:
  2823. param_operation.database_backwards("test_runsql", editor, new_state, project_state)
  2824. with connection.cursor() as cursor:
  2825. cursor.execute("SELECT COUNT(*) FROM i_love_ponies")
  2826. self.assertEqual(cursor.fetchall()[0][0], 0)
  2827. # And test reversal
  2828. with connection.schema_editor() as editor:
  2829. operation.database_backwards("test_runsql", editor, new_state, project_state)
  2830. self.assertTableNotExists("i_love_ponies")
  2831. def test_run_sql_params_invalid(self):
  2832. """
  2833. #23426 - RunSQL should fail when a list of statements with an incorrect
  2834. number of tuples is given.
  2835. """
  2836. project_state = self.set_up_test_model("test_runsql")
  2837. new_state = project_state.clone()
  2838. operation = migrations.RunSQL(
  2839. # forwards
  2840. [
  2841. ["INSERT INTO foo (bar) VALUES ('buz');"]
  2842. ],
  2843. # backwards
  2844. (
  2845. ("DELETE FROM foo WHERE bar = 'buz';", 'invalid', 'parameter count'),
  2846. ),
  2847. )
  2848. with connection.schema_editor() as editor:
  2849. with self.assertRaisesMessage(ValueError, "Expected a 2-tuple but got 1"):
  2850. operation.database_forwards("test_runsql", editor, project_state, new_state)
  2851. with connection.schema_editor() as editor:
  2852. with self.assertRaisesMessage(ValueError, "Expected a 2-tuple but got 3"):
  2853. operation.database_backwards("test_runsql", editor, new_state, project_state)
  2854. def test_run_sql_noop(self):
  2855. """
  2856. #24098 - Tests no-op RunSQL operations.
  2857. """
  2858. operation = migrations.RunSQL(migrations.RunSQL.noop, migrations.RunSQL.noop)
  2859. with connection.schema_editor() as editor:
  2860. operation.database_forwards("test_runsql", editor, None, None)
  2861. operation.database_backwards("test_runsql", editor, None, None)
  2862. def test_run_python(self):
  2863. """
  2864. Tests the RunPython operation
  2865. """
  2866. project_state = self.set_up_test_model("test_runpython", mti_model=True)
  2867. # Create the operation
  2868. def inner_method(models, schema_editor):
  2869. Pony = models.get_model("test_runpython", "Pony")
  2870. Pony.objects.create(pink=1, weight=3.55)
  2871. Pony.objects.create(weight=5)
  2872. def inner_method_reverse(models, schema_editor):
  2873. Pony = models.get_model("test_runpython", "Pony")
  2874. Pony.objects.filter(pink=1, weight=3.55).delete()
  2875. Pony.objects.filter(weight=5).delete()
  2876. operation = migrations.RunPython(inner_method, reverse_code=inner_method_reverse)
  2877. self.assertEqual(operation.describe(), "Raw Python operation")
  2878. # Test the state alteration does nothing
  2879. new_state = project_state.clone()
  2880. operation.state_forwards("test_runpython", new_state)
  2881. self.assertEqual(new_state, project_state)
  2882. # Test the database alteration
  2883. self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 0)
  2884. with connection.schema_editor() as editor:
  2885. operation.database_forwards("test_runpython", editor, project_state, new_state)
  2886. self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 2)
  2887. # Now test reversal
  2888. self.assertTrue(operation.reversible)
  2889. with connection.schema_editor() as editor:
  2890. operation.database_backwards("test_runpython", editor, project_state, new_state)
  2891. self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 0)
  2892. # Now test we can't use a string
  2893. with self.assertRaisesMessage(ValueError, 'RunPython must be supplied with a callable'):
  2894. migrations.RunPython("print 'ahahaha'")
  2895. # And deconstruction
  2896. definition = operation.deconstruct()
  2897. self.assertEqual(definition[0], "RunPython")
  2898. self.assertEqual(definition[1], [])
  2899. self.assertEqual(sorted(definition[2]), ["code", "reverse_code"])
  2900. # Also test reversal fails, with an operation identical to above but without reverse_code set
  2901. no_reverse_operation = migrations.RunPython(inner_method)
  2902. self.assertFalse(no_reverse_operation.reversible)
  2903. with connection.schema_editor() as editor:
  2904. no_reverse_operation.database_forwards("test_runpython", editor, project_state, new_state)
  2905. with self.assertRaises(NotImplementedError):
  2906. no_reverse_operation.database_backwards("test_runpython", editor, new_state, project_state)
  2907. self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 2)
  2908. def create_ponies(models, schema_editor):
  2909. Pony = models.get_model("test_runpython", "Pony")
  2910. pony1 = Pony.objects.create(pink=1, weight=3.55)
  2911. self.assertIsNot(pony1.pk, None)
  2912. pony2 = Pony.objects.create(weight=5)
  2913. self.assertIsNot(pony2.pk, None)
  2914. self.assertNotEqual(pony1.pk, pony2.pk)
  2915. operation = migrations.RunPython(create_ponies)
  2916. with connection.schema_editor() as editor:
  2917. operation.database_forwards("test_runpython", editor, project_state, new_state)
  2918. self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 4)
  2919. # And deconstruction
  2920. definition = operation.deconstruct()
  2921. self.assertEqual(definition[0], "RunPython")
  2922. self.assertEqual(definition[1], [])
  2923. self.assertEqual(sorted(definition[2]), ["code"])
  2924. def create_shetlandponies(models, schema_editor):
  2925. ShetlandPony = models.get_model("test_runpython", "ShetlandPony")
  2926. pony1 = ShetlandPony.objects.create(weight=4.0)
  2927. self.assertIsNot(pony1.pk, None)
  2928. pony2 = ShetlandPony.objects.create(weight=5.0)
  2929. self.assertIsNot(pony2.pk, None)
  2930. self.assertNotEqual(pony1.pk, pony2.pk)
  2931. operation = migrations.RunPython(create_shetlandponies)
  2932. with connection.schema_editor() as editor:
  2933. operation.database_forwards("test_runpython", editor, project_state, new_state)
  2934. self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 6)
  2935. self.assertEqual(project_state.apps.get_model("test_runpython", "ShetlandPony").objects.count(), 2)
  2936. # And elidable reduction
  2937. self.assertIs(False, operation.reduce(operation, []))
  2938. elidable_operation = migrations.RunPython(inner_method, elidable=True)
  2939. self.assertEqual(elidable_operation.reduce(operation, []), [operation])
  2940. def test_run_python_atomic(self):
  2941. """
  2942. Tests the RunPython operation correctly handles the "atomic" keyword
  2943. """
  2944. project_state = self.set_up_test_model("test_runpythonatomic", mti_model=True)
  2945. def inner_method(models, schema_editor):
  2946. Pony = models.get_model("test_runpythonatomic", "Pony")
  2947. Pony.objects.create(pink=1, weight=3.55)
  2948. raise ValueError("Adrian hates ponies.")
  2949. # Verify atomicity when applying.
  2950. atomic_migration = Migration("test", "test_runpythonatomic")
  2951. atomic_migration.operations = [migrations.RunPython(inner_method, reverse_code=inner_method)]
  2952. non_atomic_migration = Migration("test", "test_runpythonatomic")
  2953. non_atomic_migration.operations = [migrations.RunPython(inner_method, reverse_code=inner_method, atomic=False)]
  2954. # If we're a fully-transactional database, both versions should rollback
  2955. if connection.features.can_rollback_ddl:
  2956. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2957. with self.assertRaises(ValueError):
  2958. with connection.schema_editor() as editor:
  2959. atomic_migration.apply(project_state, editor)
  2960. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2961. with self.assertRaises(ValueError):
  2962. with connection.schema_editor() as editor:
  2963. non_atomic_migration.apply(project_state, editor)
  2964. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2965. # Otherwise, the non-atomic operation should leave a row there
  2966. else:
  2967. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2968. with self.assertRaises(ValueError):
  2969. with connection.schema_editor() as editor:
  2970. atomic_migration.apply(project_state, editor)
  2971. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2972. with self.assertRaises(ValueError):
  2973. with connection.schema_editor() as editor:
  2974. non_atomic_migration.apply(project_state, editor)
  2975. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 1)
  2976. # Reset object count to zero and verify atomicity when unapplying.
  2977. project_state.apps.get_model("test_runpythonatomic", "Pony").objects.all().delete()
  2978. # On a fully-transactional database, both versions rollback.
  2979. if connection.features.can_rollback_ddl:
  2980. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2981. with self.assertRaises(ValueError):
  2982. with connection.schema_editor() as editor:
  2983. atomic_migration.unapply(project_state, editor)
  2984. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2985. with self.assertRaises(ValueError):
  2986. with connection.schema_editor() as editor:
  2987. non_atomic_migration.unapply(project_state, editor)
  2988. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2989. # Otherwise, the non-atomic operation leaves a row there.
  2990. else:
  2991. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2992. with self.assertRaises(ValueError):
  2993. with connection.schema_editor() as editor:
  2994. atomic_migration.unapply(project_state, editor)
  2995. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2996. with self.assertRaises(ValueError):
  2997. with connection.schema_editor() as editor:
  2998. non_atomic_migration.unapply(project_state, editor)
  2999. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 1)
  3000. # Verify deconstruction.
  3001. definition = non_atomic_migration.operations[0].deconstruct()
  3002. self.assertEqual(definition[0], "RunPython")
  3003. self.assertEqual(definition[1], [])
  3004. self.assertEqual(sorted(definition[2]), ["atomic", "code", "reverse_code"])
  3005. def test_run_python_related_assignment(self):
  3006. """
  3007. #24282 - Model changes to a FK reverse side update the model
  3008. on the FK side as well.
  3009. """
  3010. def inner_method(models, schema_editor):
  3011. Author = models.get_model("test_authors", "Author")
  3012. Book = models.get_model("test_books", "Book")
  3013. author = Author.objects.create(name="Hemingway")
  3014. Book.objects.create(title="Old Man and The Sea", author=author)
  3015. create_author = migrations.CreateModel(
  3016. "Author",
  3017. [
  3018. ("id", models.AutoField(primary_key=True)),
  3019. ("name", models.CharField(max_length=100)),
  3020. ],
  3021. options={},
  3022. )
  3023. create_book = migrations.CreateModel(
  3024. "Book",
  3025. [
  3026. ("id", models.AutoField(primary_key=True)),
  3027. ("title", models.CharField(max_length=100)),
  3028. ("author", models.ForeignKey("test_authors.Author", models.CASCADE))
  3029. ],
  3030. options={},
  3031. )
  3032. add_hometown = migrations.AddField(
  3033. "Author",
  3034. "hometown",
  3035. models.CharField(max_length=100),
  3036. )
  3037. create_old_man = migrations.RunPython(inner_method, inner_method)
  3038. project_state = ProjectState()
  3039. new_state = project_state.clone()
  3040. with connection.schema_editor() as editor:
  3041. create_author.state_forwards("test_authors", new_state)
  3042. create_author.database_forwards("test_authors", editor, project_state, new_state)
  3043. project_state = new_state
  3044. new_state = new_state.clone()
  3045. with connection.schema_editor() as editor:
  3046. create_book.state_forwards("test_books", new_state)
  3047. create_book.database_forwards("test_books", editor, project_state, new_state)
  3048. project_state = new_state
  3049. new_state = new_state.clone()
  3050. with connection.schema_editor() as editor:
  3051. add_hometown.state_forwards("test_authors", new_state)
  3052. add_hometown.database_forwards("test_authors", editor, project_state, new_state)
  3053. project_state = new_state
  3054. new_state = new_state.clone()
  3055. with connection.schema_editor() as editor:
  3056. create_old_man.state_forwards("test_books", new_state)
  3057. create_old_man.database_forwards("test_books", editor, project_state, new_state)
  3058. def test_model_with_bigautofield(self):
  3059. """
  3060. A model with BigAutoField can be created.
  3061. """
  3062. def create_data(models, schema_editor):
  3063. Author = models.get_model("test_author", "Author")
  3064. Book = models.get_model("test_book", "Book")
  3065. author1 = Author.objects.create(name="Hemingway")
  3066. Book.objects.create(title="Old Man and The Sea", author=author1)
  3067. Book.objects.create(id=2 ** 33, title="A farewell to arms", author=author1)
  3068. author2 = Author.objects.create(id=2 ** 33, name="Remarque")
  3069. Book.objects.create(title="All quiet on the western front", author=author2)
  3070. Book.objects.create(title="Arc de Triomphe", author=author2)
  3071. create_author = migrations.CreateModel(
  3072. "Author",
  3073. [
  3074. ("id", models.BigAutoField(primary_key=True)),
  3075. ("name", models.CharField(max_length=100)),
  3076. ],
  3077. options={},
  3078. )
  3079. create_book = migrations.CreateModel(
  3080. "Book",
  3081. [
  3082. ("id", models.BigAutoField(primary_key=True)),
  3083. ("title", models.CharField(max_length=100)),
  3084. ("author", models.ForeignKey(to="test_author.Author", on_delete=models.CASCADE))
  3085. ],
  3086. options={},
  3087. )
  3088. fill_data = migrations.RunPython(create_data)
  3089. project_state = ProjectState()
  3090. new_state = project_state.clone()
  3091. with connection.schema_editor() as editor:
  3092. create_author.state_forwards("test_author", new_state)
  3093. create_author.database_forwards("test_author", editor, project_state, new_state)
  3094. project_state = new_state
  3095. new_state = new_state.clone()
  3096. with connection.schema_editor() as editor:
  3097. create_book.state_forwards("test_book", new_state)
  3098. create_book.database_forwards("test_book", editor, project_state, new_state)
  3099. project_state = new_state
  3100. new_state = new_state.clone()
  3101. with connection.schema_editor() as editor:
  3102. fill_data.state_forwards("fill_data", new_state)
  3103. fill_data.database_forwards("fill_data", editor, project_state, new_state)
  3104. def _test_autofield_foreignfield_growth(self, source_field, target_field, target_value):
  3105. """
  3106. A field may be migrated in the following ways:
  3107. - AutoField to BigAutoField
  3108. - SmallAutoField to AutoField
  3109. - SmallAutoField to BigAutoField
  3110. """
  3111. def create_initial_data(models, schema_editor):
  3112. Article = models.get_model("test_article", "Article")
  3113. Blog = models.get_model("test_blog", "Blog")
  3114. blog = Blog.objects.create(name="web development done right")
  3115. Article.objects.create(name="Frameworks", blog=blog)
  3116. Article.objects.create(name="Programming Languages", blog=blog)
  3117. def create_big_data(models, schema_editor):
  3118. Article = models.get_model("test_article", "Article")
  3119. Blog = models.get_model("test_blog", "Blog")
  3120. blog2 = Blog.objects.create(name="Frameworks", id=target_value)
  3121. Article.objects.create(name="Django", blog=blog2)
  3122. Article.objects.create(id=target_value, name="Django2", blog=blog2)
  3123. create_blog = migrations.CreateModel(
  3124. "Blog",
  3125. [
  3126. ("id", source_field(primary_key=True)),
  3127. ("name", models.CharField(max_length=100)),
  3128. ],
  3129. options={},
  3130. )
  3131. create_article = migrations.CreateModel(
  3132. "Article",
  3133. [
  3134. ("id", source_field(primary_key=True)),
  3135. ("blog", models.ForeignKey(to="test_blog.Blog", on_delete=models.CASCADE)),
  3136. ("name", models.CharField(max_length=100)),
  3137. ("data", models.TextField(default="")),
  3138. ],
  3139. options={},
  3140. )
  3141. fill_initial_data = migrations.RunPython(create_initial_data, create_initial_data)
  3142. fill_big_data = migrations.RunPython(create_big_data, create_big_data)
  3143. grow_article_id = migrations.AlterField('Article', 'id', target_field(primary_key=True))
  3144. grow_blog_id = migrations.AlterField('Blog', 'id', target_field(primary_key=True))
  3145. project_state = ProjectState()
  3146. new_state = project_state.clone()
  3147. with connection.schema_editor() as editor:
  3148. create_blog.state_forwards("test_blog", new_state)
  3149. create_blog.database_forwards("test_blog", editor, project_state, new_state)
  3150. project_state = new_state
  3151. new_state = new_state.clone()
  3152. with connection.schema_editor() as editor:
  3153. create_article.state_forwards("test_article", new_state)
  3154. create_article.database_forwards("test_article", editor, project_state, new_state)
  3155. project_state = new_state
  3156. new_state = new_state.clone()
  3157. with connection.schema_editor() as editor:
  3158. fill_initial_data.state_forwards("fill_initial_data", new_state)
  3159. fill_initial_data.database_forwards("fill_initial_data", editor, project_state, new_state)
  3160. project_state = new_state
  3161. new_state = new_state.clone()
  3162. with connection.schema_editor() as editor:
  3163. grow_article_id.state_forwards("test_article", new_state)
  3164. grow_article_id.database_forwards("test_article", editor, project_state, new_state)
  3165. state = new_state.clone()
  3166. article = state.apps.get_model("test_article.Article")
  3167. self.assertIsInstance(article._meta.pk, target_field)
  3168. project_state = new_state
  3169. new_state = new_state.clone()
  3170. with connection.schema_editor() as editor:
  3171. grow_blog_id.state_forwards("test_blog", new_state)
  3172. grow_blog_id.database_forwards("test_blog", editor, project_state, new_state)
  3173. state = new_state.clone()
  3174. blog = state.apps.get_model("test_blog.Blog")
  3175. self.assertIsInstance(blog._meta.pk, target_field)
  3176. project_state = new_state
  3177. new_state = new_state.clone()
  3178. with connection.schema_editor() as editor:
  3179. fill_big_data.state_forwards("fill_big_data", new_state)
  3180. fill_big_data.database_forwards("fill_big_data", editor, project_state, new_state)
  3181. def test_autofield__bigautofield_foreignfield_growth(self):
  3182. """A field may be migrated from AutoField to BigAutoField."""
  3183. self._test_autofield_foreignfield_growth(
  3184. models.AutoField,
  3185. models.BigAutoField,
  3186. 2 ** 33,
  3187. )
  3188. def test_smallfield_autofield_foreignfield_growth(self):
  3189. """A field may be migrated from SmallAutoField to AutoField."""
  3190. self._test_autofield_foreignfield_growth(
  3191. models.SmallAutoField,
  3192. models.AutoField,
  3193. 2 ** 22,
  3194. )
  3195. def test_smallfield_bigautofield_foreignfield_growth(self):
  3196. """A field may be migrated from SmallAutoField to BigAutoField."""
  3197. self._test_autofield_foreignfield_growth(
  3198. models.SmallAutoField,
  3199. models.BigAutoField,
  3200. 2 ** 33,
  3201. )
  3202. def test_run_python_noop(self):
  3203. """
  3204. #24098 - Tests no-op RunPython operations.
  3205. """
  3206. project_state = ProjectState()
  3207. new_state = project_state.clone()
  3208. operation = migrations.RunPython(migrations.RunPython.noop, migrations.RunPython.noop)
  3209. with connection.schema_editor() as editor:
  3210. operation.database_forwards("test_runpython", editor, project_state, new_state)
  3211. operation.database_backwards("test_runpython", editor, new_state, project_state)
  3212. def test_separate_database_and_state(self):
  3213. """
  3214. Tests the SeparateDatabaseAndState operation.
  3215. """
  3216. project_state = self.set_up_test_model("test_separatedatabaseandstate")
  3217. # Create the operation
  3218. database_operation = migrations.RunSQL(
  3219. "CREATE TABLE i_love_ponies (id int, special_thing int);",
  3220. "DROP TABLE i_love_ponies;"
  3221. )
  3222. state_operation = migrations.CreateModel("SomethingElse", [("id", models.AutoField(primary_key=True))])
  3223. operation = migrations.SeparateDatabaseAndState(
  3224. state_operations=[state_operation],
  3225. database_operations=[database_operation]
  3226. )
  3227. self.assertEqual(operation.describe(), "Custom state/database change combination")
  3228. # Test the state alteration
  3229. new_state = project_state.clone()
  3230. operation.state_forwards("test_separatedatabaseandstate", new_state)
  3231. self.assertEqual(len(new_state.models["test_separatedatabaseandstate", "somethingelse"].fields), 1)
  3232. # Make sure there's no table
  3233. self.assertTableNotExists("i_love_ponies")
  3234. # Test the database alteration
  3235. with connection.schema_editor() as editor:
  3236. operation.database_forwards("test_separatedatabaseandstate", editor, project_state, new_state)
  3237. self.assertTableExists("i_love_ponies")
  3238. # And test reversal
  3239. self.assertTrue(operation.reversible)
  3240. with connection.schema_editor() as editor:
  3241. operation.database_backwards("test_separatedatabaseandstate", editor, new_state, project_state)
  3242. self.assertTableNotExists("i_love_ponies")
  3243. # And deconstruction
  3244. definition = operation.deconstruct()
  3245. self.assertEqual(definition[0], "SeparateDatabaseAndState")
  3246. self.assertEqual(definition[1], [])
  3247. self.assertEqual(sorted(definition[2]), ["database_operations", "state_operations"])
  3248. def test_separate_database_and_state2(self):
  3249. """
  3250. A complex SeparateDatabaseAndState operation: Multiple operations both
  3251. for state and database. Verify the state dependencies within each list
  3252. and that state ops don't affect the database.
  3253. """
  3254. app_label = "test_separatedatabaseandstate2"
  3255. project_state = self.set_up_test_model(app_label)
  3256. # Create the operation
  3257. database_operations = [
  3258. migrations.CreateModel(
  3259. "ILovePonies",
  3260. [("id", models.AutoField(primary_key=True))],
  3261. options={"db_table": "iloveponies"},
  3262. ),
  3263. migrations.CreateModel(
  3264. "ILoveMorePonies",
  3265. # We use IntegerField and not AutoField because
  3266. # the model is going to be deleted immediately
  3267. # and with an AutoField this fails on Oracle
  3268. [("id", models.IntegerField(primary_key=True))],
  3269. options={"db_table": "ilovemoreponies"},
  3270. ),
  3271. migrations.DeleteModel("ILoveMorePonies"),
  3272. migrations.CreateModel(
  3273. "ILoveEvenMorePonies",
  3274. [("id", models.AutoField(primary_key=True))],
  3275. options={"db_table": "iloveevenmoreponies"},
  3276. ),
  3277. ]
  3278. state_operations = [
  3279. migrations.CreateModel(
  3280. "SomethingElse",
  3281. [("id", models.AutoField(primary_key=True))],
  3282. options={"db_table": "somethingelse"},
  3283. ),
  3284. migrations.DeleteModel("SomethingElse"),
  3285. migrations.CreateModel(
  3286. "SomethingCompletelyDifferent",
  3287. [("id", models.AutoField(primary_key=True))],
  3288. options={"db_table": "somethingcompletelydifferent"},
  3289. ),
  3290. ]
  3291. operation = migrations.SeparateDatabaseAndState(
  3292. state_operations=state_operations,
  3293. database_operations=database_operations,
  3294. )
  3295. # Test the state alteration
  3296. new_state = project_state.clone()
  3297. operation.state_forwards(app_label, new_state)
  3298. def assertModelsAndTables(after_db):
  3299. # Tables and models exist, or don't, as they should:
  3300. self.assertNotIn((app_label, "somethingelse"), new_state.models)
  3301. self.assertEqual(len(new_state.models[app_label, "somethingcompletelydifferent"].fields), 1)
  3302. self.assertNotIn((app_label, "iloveponiesonies"), new_state.models)
  3303. self.assertNotIn((app_label, "ilovemoreponies"), new_state.models)
  3304. self.assertNotIn((app_label, "iloveevenmoreponies"), new_state.models)
  3305. self.assertTableNotExists("somethingelse")
  3306. self.assertTableNotExists("somethingcompletelydifferent")
  3307. self.assertTableNotExists("ilovemoreponies")
  3308. if after_db:
  3309. self.assertTableExists("iloveponies")
  3310. self.assertTableExists("iloveevenmoreponies")
  3311. else:
  3312. self.assertTableNotExists("iloveponies")
  3313. self.assertTableNotExists("iloveevenmoreponies")
  3314. assertModelsAndTables(after_db=False)
  3315. # Test the database alteration
  3316. with connection.schema_editor() as editor:
  3317. operation.database_forwards(app_label, editor, project_state, new_state)
  3318. assertModelsAndTables(after_db=True)
  3319. # And test reversal
  3320. self.assertTrue(operation.reversible)
  3321. with connection.schema_editor() as editor:
  3322. operation.database_backwards(app_label, editor, new_state, project_state)
  3323. assertModelsAndTables(after_db=False)
  3324. class SwappableOperationTests(OperationTestBase):
  3325. """
  3326. Key operations ignore swappable models
  3327. (we don't want to replicate all of them here, as the functionality
  3328. is in a common base class anyway)
  3329. """
  3330. available_apps = ['migrations']
  3331. @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
  3332. def test_create_ignore_swapped(self):
  3333. """
  3334. The CreateTable operation ignores swapped models.
  3335. """
  3336. operation = migrations.CreateModel(
  3337. "Pony",
  3338. [
  3339. ("id", models.AutoField(primary_key=True)),
  3340. ("pink", models.IntegerField(default=1)),
  3341. ],
  3342. options={
  3343. "swappable": "TEST_SWAP_MODEL",
  3344. },
  3345. )
  3346. # Test the state alteration (it should still be there!)
  3347. project_state = ProjectState()
  3348. new_state = project_state.clone()
  3349. operation.state_forwards("test_crigsw", new_state)
  3350. self.assertEqual(new_state.models["test_crigsw", "pony"].name, "Pony")
  3351. self.assertEqual(len(new_state.models["test_crigsw", "pony"].fields), 2)
  3352. # Test the database alteration
  3353. self.assertTableNotExists("test_crigsw_pony")
  3354. with connection.schema_editor() as editor:
  3355. operation.database_forwards("test_crigsw", editor, project_state, new_state)
  3356. self.assertTableNotExists("test_crigsw_pony")
  3357. # And test reversal
  3358. with connection.schema_editor() as editor:
  3359. operation.database_backwards("test_crigsw", editor, new_state, project_state)
  3360. self.assertTableNotExists("test_crigsw_pony")
  3361. @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
  3362. def test_delete_ignore_swapped(self):
  3363. """
  3364. Tests the DeleteModel operation ignores swapped models.
  3365. """
  3366. operation = migrations.DeleteModel("Pony")
  3367. project_state, new_state = self.make_test_state("test_dligsw", operation)
  3368. # Test the database alteration
  3369. self.assertTableNotExists("test_dligsw_pony")
  3370. with connection.schema_editor() as editor:
  3371. operation.database_forwards("test_dligsw", editor, project_state, new_state)
  3372. self.assertTableNotExists("test_dligsw_pony")
  3373. # And test reversal
  3374. with connection.schema_editor() as editor:
  3375. operation.database_backwards("test_dligsw", editor, new_state, project_state)
  3376. self.assertTableNotExists("test_dligsw_pony")
  3377. @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
  3378. def test_add_field_ignore_swapped(self):
  3379. """
  3380. Tests the AddField operation.
  3381. """
  3382. # Test the state alteration
  3383. operation = migrations.AddField(
  3384. "Pony",
  3385. "height",
  3386. models.FloatField(null=True, default=5),
  3387. )
  3388. project_state, new_state = self.make_test_state("test_adfligsw", operation)
  3389. # Test the database alteration
  3390. self.assertTableNotExists("test_adfligsw_pony")
  3391. with connection.schema_editor() as editor:
  3392. operation.database_forwards("test_adfligsw", editor, project_state, new_state)
  3393. self.assertTableNotExists("test_adfligsw_pony")
  3394. # And test reversal
  3395. with connection.schema_editor() as editor:
  3396. operation.database_backwards("test_adfligsw", editor, new_state, project_state)
  3397. self.assertTableNotExists("test_adfligsw_pony")
  3398. @override_settings(TEST_SWAP_MODEL='migrations.SomeFakeModel')
  3399. def test_indexes_ignore_swapped(self):
  3400. """
  3401. Add/RemoveIndex operations ignore swapped models.
  3402. """
  3403. operation = migrations.AddIndex('Pony', models.Index(fields=['pink'], name='my_name_idx'))
  3404. project_state, new_state = self.make_test_state('test_adinigsw', operation)
  3405. with connection.schema_editor() as editor:
  3406. # No database queries should be run for swapped models
  3407. operation.database_forwards('test_adinigsw', editor, project_state, new_state)
  3408. operation.database_backwards('test_adinigsw', editor, new_state, project_state)
  3409. operation = migrations.RemoveIndex('Pony', models.Index(fields=['pink'], name='my_name_idx'))
  3410. project_state, new_state = self.make_test_state("test_rminigsw", operation)
  3411. with connection.schema_editor() as editor:
  3412. operation.database_forwards('test_rminigsw', editor, project_state, new_state)
  3413. operation.database_backwards('test_rminigsw', editor, new_state, project_state)
  3414. class TestCreateModel(SimpleTestCase):
  3415. def test_references_model_mixin(self):
  3416. migrations.CreateModel(
  3417. 'name',
  3418. fields=[],
  3419. bases=(Mixin, models.Model),
  3420. ).references_model('other_model', 'migrations')
  3421. class FieldOperationTests(SimpleTestCase):
  3422. def test_references_model(self):
  3423. operation = FieldOperation('MoDel', 'field', models.ForeignKey('Other', models.CASCADE))
  3424. # Model name match.
  3425. self.assertIs(operation.references_model('mOdEl', 'migrations'), True)
  3426. # Referenced field.
  3427. self.assertIs(operation.references_model('oTher', 'migrations'), True)
  3428. # Doesn't reference.
  3429. self.assertIs(operation.references_model('Whatever', 'migrations'), False)
  3430. def test_references_field_by_name(self):
  3431. operation = FieldOperation('MoDel', 'field', models.BooleanField(default=False))
  3432. self.assertIs(operation.references_field('model', 'field', 'migrations'), True)
  3433. def test_references_field_by_remote_field_model(self):
  3434. operation = FieldOperation('Model', 'field', models.ForeignKey('Other', models.CASCADE))
  3435. self.assertIs(operation.references_field('Other', 'whatever', 'migrations'), True)
  3436. self.assertIs(operation.references_field('Missing', 'whatever', 'migrations'), False)
  3437. def test_references_field_by_from_fields(self):
  3438. operation = FieldOperation(
  3439. 'Model', 'field', models.fields.related.ForeignObject('Other', models.CASCADE, ['from'], ['to'])
  3440. )
  3441. self.assertIs(operation.references_field('Model', 'from', 'migrations'), True)
  3442. self.assertIs(operation.references_field('Model', 'to', 'migrations'), False)
  3443. self.assertIs(operation.references_field('Other', 'from', 'migrations'), False)
  3444. self.assertIs(operation.references_field('Model', 'to', 'migrations'), False)
  3445. def test_references_field_by_to_fields(self):
  3446. operation = FieldOperation('Model', 'field', models.ForeignKey('Other', models.CASCADE, to_field='field'))
  3447. self.assertIs(operation.references_field('Other', 'field', 'migrations'), True)
  3448. self.assertIs(operation.references_field('Other', 'whatever', 'migrations'), False)
  3449. self.assertIs(operation.references_field('Missing', 'whatever', 'migrations'), False)
  3450. def test_references_field_by_through(self):
  3451. operation = FieldOperation('Model', 'field', models.ManyToManyField('Other', through='Through'))
  3452. self.assertIs(operation.references_field('Other', 'whatever', 'migrations'), True)
  3453. self.assertIs(operation.references_field('Through', 'whatever', 'migrations'), True)
  3454. self.assertIs(operation.references_field('Missing', 'whatever', 'migrations'), False)
  3455. def test_reference_field_by_through_fields(self):
  3456. operation = FieldOperation(
  3457. 'Model', 'field', models.ManyToManyField('Other', through='Through', through_fields=('first', 'second'))
  3458. )
  3459. self.assertIs(operation.references_field('Other', 'whatever', 'migrations'), True)
  3460. self.assertIs(operation.references_field('Through', 'whatever', 'migrations'), False)
  3461. self.assertIs(operation.references_field('Through', 'first', 'migrations'), True)
  3462. self.assertIs(operation.references_field('Through', 'second', 'migrations'), True)