test_operations.py 132 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912191319141915191619171918191919201921192219231924192519261927192819291930193119321933193419351936193719381939194019411942194319441945194619471948194919501951195219531954195519561957195819591960196119621963196419651966196719681969197019711972197319741975197619771978197919801981198219831984198519861987198819891990199119921993199419951996199719981999200020012002200320042005200620072008200920102011201220132014201520162017201820192020202120222023202420252026202720282029203020312032203320342035203620372038203920402041204220432044204520462047204820492050205120522053205420552056205720582059206020612062206320642065206620672068206920702071207220732074207520762077207820792080208120822083208420852086208720882089209020912092209320942095209620972098209921002101210221032104210521062107210821092110211121122113211421152116211721182119212021212122212321242125212621272128212921302131213221332134213521362137213821392140214121422143214421452146214721482149215021512152215321542155215621572158215921602161216221632164216521662167216821692170217121722173217421752176217721782179218021812182218321842185218621872188218921902191219221932194219521962197219821992200220122022203220422052206220722082209221022112212221322142215221622172218221922202221222222232224222522262227222822292230223122322233223422352236223722382239224022412242224322442245224622472248224922502251225222532254225522562257225822592260226122622263226422652266226722682269227022712272227322742275227622772278227922802281228222832284228522862287228822892290229122922293229422952296229722982299230023012302230323042305230623072308230923102311231223132314231523162317231823192320232123222323232423252326232723282329233023312332233323342335233623372338233923402341234223432344234523462347234823492350235123522353235423552356235723582359236023612362236323642365236623672368236923702371237223732374237523762377237823792380238123822383238423852386238723882389239023912392239323942395239623972398239924002401240224032404240524062407240824092410241124122413241424152416241724182419242024212422242324242425242624272428242924302431243224332434243524362437243824392440244124422443244424452446244724482449245024512452245324542455245624572458245924602461246224632464246524662467246824692470247124722473247424752476247724782479248024812482248324842485248624872488248924902491249224932494249524962497249824992500250125022503250425052506250725082509251025112512251325142515251625172518251925202521252225232524252525262527252825292530253125322533253425352536253725382539254025412542254325442545254625472548254925502551255225532554255525562557255825592560256125622563256425652566256725682569257025712572257325742575257625772578257925802581258225832584258525862587258825892590259125922593259425952596259725982599260026012602260326042605260626072608260926102611261226132614261526162617261826192620262126222623262426252626262726282629263026312632263326342635263626372638263926402641264226432644264526462647264826492650265126522653265426552656265726582659266026612662266326642665266626672668266926702671267226732674267526762677267826792680268126822683268426852686268726882689269026912692269326942695269626972698269927002701270227032704270527062707270827092710271127122713271427152716271727182719272027212722272327242725272627272728272927302731273227332734273527362737273827392740274127422743274427452746274727482749275027512752275327542755
  1. import unittest
  2. from django.core.exceptions import FieldDoesNotExist
  3. from django.db import connection, migrations, models, transaction
  4. from django.db.migrations.migration import Migration
  5. from django.db.migrations.operations import CreateModel
  6. from django.db.migrations.state import ModelState, ProjectState
  7. from django.db.models.fields import NOT_PROVIDED
  8. from django.db.transaction import atomic
  9. from django.db.utils import IntegrityError
  10. from django.test import SimpleTestCase, override_settings, skipUnlessDBFeature
  11. from .models import FoodManager, FoodQuerySet, UnicodeModel
  12. from .test_base import MigrationTestBase
  13. try:
  14. import sqlparse
  15. except ImportError:
  16. sqlparse = None
  17. class Mixin:
  18. pass
  19. class OperationTestBase(MigrationTestBase):
  20. """
  21. Common functions to help test operations.
  22. """
  23. def apply_operations(self, app_label, project_state, operations, atomic=True):
  24. migration = Migration('name', app_label)
  25. migration.operations = operations
  26. with connection.schema_editor(atomic=atomic) as editor:
  27. return migration.apply(project_state, editor)
  28. def unapply_operations(self, app_label, project_state, operations, atomic=True):
  29. migration = Migration('name', app_label)
  30. migration.operations = operations
  31. with connection.schema_editor(atomic=atomic) as editor:
  32. return migration.unapply(project_state, editor)
  33. def make_test_state(self, app_label, operation, **kwargs):
  34. """
  35. Makes a test state using set_up_test_model and returns the
  36. original state and the state after the migration is applied.
  37. """
  38. project_state = self.set_up_test_model(app_label, **kwargs)
  39. new_state = project_state.clone()
  40. operation.state_forwards(app_label, new_state)
  41. return project_state, new_state
  42. def set_up_test_model(
  43. self, app_label, second_model=False, third_model=False, index=False, multicol_index=False,
  44. related_model=False, mti_model=False, proxy_model=False, manager_model=False,
  45. unique_together=False, options=False, db_table=None, index_together=False, check_constraint=False):
  46. """
  47. Creates a test model state and database table.
  48. """
  49. # Delete the tables if they already exist
  50. table_names = [
  51. # Start with ManyToMany tables
  52. '_pony_stables', '_pony_vans',
  53. # Then standard model tables
  54. '_pony', '_stable', '_van',
  55. ]
  56. tables = [(app_label + table_name) for table_name in table_names]
  57. with connection.cursor() as cursor:
  58. table_names = connection.introspection.table_names(cursor)
  59. connection.disable_constraint_checking()
  60. sql_delete_table = connection.schema_editor().sql_delete_table
  61. with transaction.atomic():
  62. for table in tables:
  63. if table in table_names:
  64. cursor.execute(sql_delete_table % {
  65. "table": connection.ops.quote_name(table),
  66. })
  67. connection.enable_constraint_checking()
  68. # Make the "current" state
  69. model_options = {
  70. "swappable": "TEST_SWAP_MODEL",
  71. "index_together": [["weight", "pink"]] if index_together else [],
  72. "unique_together": [["pink", "weight"]] if unique_together else [],
  73. }
  74. if options:
  75. model_options["permissions"] = [("can_groom", "Can groom")]
  76. if db_table:
  77. model_options["db_table"] = db_table
  78. operations = [migrations.CreateModel(
  79. "Pony",
  80. [
  81. ("id", models.AutoField(primary_key=True)),
  82. ("pink", models.IntegerField(default=3)),
  83. ("weight", models.FloatField()),
  84. ],
  85. options=model_options,
  86. )]
  87. if index:
  88. operations.append(migrations.AddIndex(
  89. "Pony",
  90. models.Index(fields=["pink"], name="pony_pink_idx")
  91. ))
  92. if multicol_index:
  93. operations.append(migrations.AddIndex(
  94. "Pony",
  95. models.Index(fields=["pink", "weight"], name="pony_test_idx")
  96. ))
  97. if check_constraint:
  98. operations.append(migrations.AddConstraint(
  99. "Pony",
  100. models.CheckConstraint(models.Q(pink__gt=2), name="pony_test_constraint")
  101. ))
  102. if second_model:
  103. operations.append(migrations.CreateModel(
  104. "Stable",
  105. [
  106. ("id", models.AutoField(primary_key=True)),
  107. ]
  108. ))
  109. if third_model:
  110. operations.append(migrations.CreateModel(
  111. "Van",
  112. [
  113. ("id", models.AutoField(primary_key=True)),
  114. ]
  115. ))
  116. if related_model:
  117. operations.append(migrations.CreateModel(
  118. "Rider",
  119. [
  120. ("id", models.AutoField(primary_key=True)),
  121. ("pony", models.ForeignKey("Pony", models.CASCADE)),
  122. ("friend", models.ForeignKey("self", models.CASCADE))
  123. ],
  124. ))
  125. if mti_model:
  126. operations.append(migrations.CreateModel(
  127. "ShetlandPony",
  128. fields=[
  129. ('pony_ptr', models.OneToOneField(
  130. 'Pony',
  131. models.CASCADE,
  132. auto_created=True,
  133. parent_link=True,
  134. primary_key=True,
  135. to_field='id',
  136. serialize=False,
  137. )),
  138. ("cuteness", models.IntegerField(default=1)),
  139. ],
  140. bases=['%s.Pony' % app_label],
  141. ))
  142. if proxy_model:
  143. operations.append(migrations.CreateModel(
  144. "ProxyPony",
  145. fields=[],
  146. options={"proxy": True},
  147. bases=['%s.Pony' % app_label],
  148. ))
  149. if manager_model:
  150. operations.append(migrations.CreateModel(
  151. "Food",
  152. fields=[
  153. ("id", models.AutoField(primary_key=True)),
  154. ],
  155. managers=[
  156. ("food_qs", FoodQuerySet.as_manager()),
  157. ("food_mgr", FoodManager("a", "b")),
  158. ("food_mgr_kwargs", FoodManager("x", "y", 3, 4)),
  159. ]
  160. ))
  161. return self.apply_operations(app_label, ProjectState(), operations)
  162. class OperationTests(OperationTestBase):
  163. """
  164. Tests running the operations and making sure they do what they say they do.
  165. Each test looks at their state changing, and then their database operation -
  166. both forwards and backwards.
  167. """
  168. def test_create_model(self):
  169. """
  170. Tests the CreateModel operation.
  171. Most other tests use this operation as part of setup, so check failures here first.
  172. """
  173. operation = migrations.CreateModel(
  174. "Pony",
  175. [
  176. ("id", models.AutoField(primary_key=True)),
  177. ("pink", models.IntegerField(default=1)),
  178. ],
  179. )
  180. self.assertEqual(operation.describe(), "Create model Pony")
  181. # Test the state alteration
  182. project_state = ProjectState()
  183. new_state = project_state.clone()
  184. operation.state_forwards("test_crmo", new_state)
  185. self.assertEqual(new_state.models["test_crmo", "pony"].name, "Pony")
  186. self.assertEqual(len(new_state.models["test_crmo", "pony"].fields), 2)
  187. # Test the database alteration
  188. self.assertTableNotExists("test_crmo_pony")
  189. with connection.schema_editor() as editor:
  190. operation.database_forwards("test_crmo", editor, project_state, new_state)
  191. self.assertTableExists("test_crmo_pony")
  192. # And test reversal
  193. with connection.schema_editor() as editor:
  194. operation.database_backwards("test_crmo", editor, new_state, project_state)
  195. self.assertTableNotExists("test_crmo_pony")
  196. # And deconstruction
  197. definition = operation.deconstruct()
  198. self.assertEqual(definition[0], "CreateModel")
  199. self.assertEqual(definition[1], [])
  200. self.assertEqual(sorted(definition[2]), ["fields", "name"])
  201. # And default manager not in set
  202. operation = migrations.CreateModel("Foo", fields=[], managers=[("objects", models.Manager())])
  203. definition = operation.deconstruct()
  204. self.assertNotIn('managers', definition[2])
  205. def test_create_model_with_duplicate_field_name(self):
  206. with self.assertRaisesMessage(ValueError, 'Found duplicate value pink in CreateModel fields argument.'):
  207. migrations.CreateModel(
  208. "Pony",
  209. [
  210. ("id", models.AutoField(primary_key=True)),
  211. ("pink", models.TextField()),
  212. ("pink", models.IntegerField(default=1)),
  213. ],
  214. )
  215. def test_create_model_with_duplicate_base(self):
  216. message = 'Found duplicate value test_crmo.pony in CreateModel bases argument.'
  217. with self.assertRaisesMessage(ValueError, message):
  218. migrations.CreateModel(
  219. "Pony",
  220. fields=[],
  221. bases=("test_crmo.Pony", "test_crmo.Pony",),
  222. )
  223. with self.assertRaisesMessage(ValueError, message):
  224. migrations.CreateModel(
  225. "Pony",
  226. fields=[],
  227. bases=("test_crmo.Pony", "test_crmo.pony",),
  228. )
  229. message = 'Found duplicate value migrations.unicodemodel in CreateModel bases argument.'
  230. with self.assertRaisesMessage(ValueError, message):
  231. migrations.CreateModel(
  232. "Pony",
  233. fields=[],
  234. bases=(UnicodeModel, UnicodeModel,),
  235. )
  236. with self.assertRaisesMessage(ValueError, message):
  237. migrations.CreateModel(
  238. "Pony",
  239. fields=[],
  240. bases=(UnicodeModel, 'migrations.unicodemodel',),
  241. )
  242. with self.assertRaisesMessage(ValueError, message):
  243. migrations.CreateModel(
  244. "Pony",
  245. fields=[],
  246. bases=(UnicodeModel, 'migrations.UnicodeModel',),
  247. )
  248. message = "Found duplicate value <class 'django.db.models.base.Model'> in CreateModel bases argument."
  249. with self.assertRaisesMessage(ValueError, message):
  250. migrations.CreateModel(
  251. "Pony",
  252. fields=[],
  253. bases=(models.Model, models.Model,),
  254. )
  255. message = "Found duplicate value <class 'migrations.test_operations.Mixin'> in CreateModel bases argument."
  256. with self.assertRaisesMessage(ValueError, message):
  257. migrations.CreateModel(
  258. "Pony",
  259. fields=[],
  260. bases=(Mixin, Mixin,),
  261. )
  262. def test_create_model_with_duplicate_manager_name(self):
  263. with self.assertRaisesMessage(ValueError, 'Found duplicate value objects in CreateModel managers argument.'):
  264. migrations.CreateModel(
  265. "Pony",
  266. fields=[],
  267. managers=[
  268. ("objects", models.Manager()),
  269. ("objects", models.Manager()),
  270. ],
  271. )
  272. def test_create_model_with_unique_after(self):
  273. """
  274. Tests the CreateModel operation directly followed by an
  275. AlterUniqueTogether (bug #22844 - sqlite remake issues)
  276. """
  277. operation1 = migrations.CreateModel(
  278. "Pony",
  279. [
  280. ("id", models.AutoField(primary_key=True)),
  281. ("pink", models.IntegerField(default=1)),
  282. ],
  283. )
  284. operation2 = migrations.CreateModel(
  285. "Rider",
  286. [
  287. ("id", models.AutoField(primary_key=True)),
  288. ("number", models.IntegerField(default=1)),
  289. ("pony", models.ForeignKey("test_crmoua.Pony", models.CASCADE)),
  290. ],
  291. )
  292. operation3 = migrations.AlterUniqueTogether(
  293. "Rider",
  294. [
  295. ("number", "pony"),
  296. ],
  297. )
  298. # Test the database alteration
  299. project_state = ProjectState()
  300. self.assertTableNotExists("test_crmoua_pony")
  301. self.assertTableNotExists("test_crmoua_rider")
  302. with connection.schema_editor() as editor:
  303. new_state = project_state.clone()
  304. operation1.state_forwards("test_crmoua", new_state)
  305. operation1.database_forwards("test_crmoua", editor, project_state, new_state)
  306. project_state, new_state = new_state, new_state.clone()
  307. operation2.state_forwards("test_crmoua", new_state)
  308. operation2.database_forwards("test_crmoua", editor, project_state, new_state)
  309. project_state, new_state = new_state, new_state.clone()
  310. operation3.state_forwards("test_crmoua", new_state)
  311. operation3.database_forwards("test_crmoua", editor, project_state, new_state)
  312. self.assertTableExists("test_crmoua_pony")
  313. self.assertTableExists("test_crmoua_rider")
  314. def test_create_model_m2m(self):
  315. """
  316. Test the creation of a model with a ManyToMany field and the
  317. auto-created "through" model.
  318. """
  319. project_state = self.set_up_test_model("test_crmomm")
  320. operation = migrations.CreateModel(
  321. "Stable",
  322. [
  323. ("id", models.AutoField(primary_key=True)),
  324. ("ponies", models.ManyToManyField("Pony", related_name="stables"))
  325. ]
  326. )
  327. # Test the state alteration
  328. new_state = project_state.clone()
  329. operation.state_forwards("test_crmomm", new_state)
  330. # Test the database alteration
  331. self.assertTableNotExists("test_crmomm_stable_ponies")
  332. with connection.schema_editor() as editor:
  333. operation.database_forwards("test_crmomm", editor, project_state, new_state)
  334. self.assertTableExists("test_crmomm_stable")
  335. self.assertTableExists("test_crmomm_stable_ponies")
  336. self.assertColumnNotExists("test_crmomm_stable", "ponies")
  337. # Make sure the M2M field actually works
  338. with atomic():
  339. Pony = new_state.apps.get_model("test_crmomm", "Pony")
  340. Stable = new_state.apps.get_model("test_crmomm", "Stable")
  341. stable = Stable.objects.create()
  342. p1 = Pony.objects.create(pink=False, weight=4.55)
  343. p2 = Pony.objects.create(pink=True, weight=5.43)
  344. stable.ponies.add(p1, p2)
  345. self.assertEqual(stable.ponies.count(), 2)
  346. stable.ponies.all().delete()
  347. # And test reversal
  348. with connection.schema_editor() as editor:
  349. operation.database_backwards("test_crmomm", editor, new_state, project_state)
  350. self.assertTableNotExists("test_crmomm_stable")
  351. self.assertTableNotExists("test_crmomm_stable_ponies")
  352. def test_create_model_inheritance(self):
  353. """
  354. Tests the CreateModel operation on a multi-table inheritance setup.
  355. """
  356. project_state = self.set_up_test_model("test_crmoih")
  357. # Test the state alteration
  358. operation = migrations.CreateModel(
  359. "ShetlandPony",
  360. [
  361. ('pony_ptr', models.OneToOneField(
  362. 'test_crmoih.Pony',
  363. models.CASCADE,
  364. auto_created=True,
  365. primary_key=True,
  366. to_field='id',
  367. serialize=False,
  368. )),
  369. ("cuteness", models.IntegerField(default=1)),
  370. ],
  371. )
  372. new_state = project_state.clone()
  373. operation.state_forwards("test_crmoih", new_state)
  374. self.assertIn(("test_crmoih", "shetlandpony"), new_state.models)
  375. # Test the database alteration
  376. self.assertTableNotExists("test_crmoih_shetlandpony")
  377. with connection.schema_editor() as editor:
  378. operation.database_forwards("test_crmoih", editor, project_state, new_state)
  379. self.assertTableExists("test_crmoih_shetlandpony")
  380. # And test reversal
  381. with connection.schema_editor() as editor:
  382. operation.database_backwards("test_crmoih", editor, new_state, project_state)
  383. self.assertTableNotExists("test_crmoih_shetlandpony")
  384. def test_create_proxy_model(self):
  385. """
  386. CreateModel ignores proxy models.
  387. """
  388. project_state = self.set_up_test_model("test_crprmo")
  389. # Test the state alteration
  390. operation = migrations.CreateModel(
  391. "ProxyPony",
  392. [],
  393. options={"proxy": True},
  394. bases=("test_crprmo.Pony",),
  395. )
  396. self.assertEqual(operation.describe(), "Create proxy model ProxyPony")
  397. new_state = project_state.clone()
  398. operation.state_forwards("test_crprmo", new_state)
  399. self.assertIn(("test_crprmo", "proxypony"), new_state.models)
  400. # Test the database alteration
  401. self.assertTableNotExists("test_crprmo_proxypony")
  402. self.assertTableExists("test_crprmo_pony")
  403. with connection.schema_editor() as editor:
  404. operation.database_forwards("test_crprmo", editor, project_state, new_state)
  405. self.assertTableNotExists("test_crprmo_proxypony")
  406. self.assertTableExists("test_crprmo_pony")
  407. # And test reversal
  408. with connection.schema_editor() as editor:
  409. operation.database_backwards("test_crprmo", editor, new_state, project_state)
  410. self.assertTableNotExists("test_crprmo_proxypony")
  411. self.assertTableExists("test_crprmo_pony")
  412. # And deconstruction
  413. definition = operation.deconstruct()
  414. self.assertEqual(definition[0], "CreateModel")
  415. self.assertEqual(definition[1], [])
  416. self.assertEqual(sorted(definition[2]), ["bases", "fields", "name", "options"])
  417. def test_create_unmanaged_model(self):
  418. """
  419. CreateModel ignores unmanaged models.
  420. """
  421. project_state = self.set_up_test_model("test_crummo")
  422. # Test the state alteration
  423. operation = migrations.CreateModel(
  424. "UnmanagedPony",
  425. [],
  426. options={"proxy": True},
  427. bases=("test_crummo.Pony",),
  428. )
  429. self.assertEqual(operation.describe(), "Create proxy model UnmanagedPony")
  430. new_state = project_state.clone()
  431. operation.state_forwards("test_crummo", new_state)
  432. self.assertIn(("test_crummo", "unmanagedpony"), new_state.models)
  433. # Test the database alteration
  434. self.assertTableNotExists("test_crummo_unmanagedpony")
  435. self.assertTableExists("test_crummo_pony")
  436. with connection.schema_editor() as editor:
  437. operation.database_forwards("test_crummo", editor, project_state, new_state)
  438. self.assertTableNotExists("test_crummo_unmanagedpony")
  439. self.assertTableExists("test_crummo_pony")
  440. # And test reversal
  441. with connection.schema_editor() as editor:
  442. operation.database_backwards("test_crummo", editor, new_state, project_state)
  443. self.assertTableNotExists("test_crummo_unmanagedpony")
  444. self.assertTableExists("test_crummo_pony")
  445. @skipUnlessDBFeature('supports_table_check_constraints')
  446. def test_create_model_with_constraint(self):
  447. where = models.Q(pink__gt=2)
  448. check_constraint = models.CheckConstraint(where, name='test_constraint_pony_pink_gt_2')
  449. operation = migrations.CreateModel(
  450. "Pony",
  451. [
  452. ("id", models.AutoField(primary_key=True)),
  453. ("pink", models.IntegerField(default=3)),
  454. ],
  455. options={'constraints': [check_constraint]},
  456. )
  457. # Test the state alteration
  458. project_state = ProjectState()
  459. new_state = project_state.clone()
  460. operation.state_forwards("test_crmo", new_state)
  461. self.assertEqual(len(new_state.models['test_crmo', 'pony'].options['constraints']), 1)
  462. # Test database alteration
  463. self.assertTableNotExists("test_crmo_pony")
  464. with connection.schema_editor() as editor:
  465. operation.database_forwards("test_crmo", editor, project_state, new_state)
  466. self.assertTableExists("test_crmo_pony")
  467. with connection.cursor() as cursor:
  468. with self.assertRaises(IntegrityError):
  469. cursor.execute("INSERT INTO test_crmo_pony (id, pink) VALUES (1, 1)")
  470. # Test reversal
  471. with connection.schema_editor() as editor:
  472. operation.database_backwards("test_crmo", editor, new_state, project_state)
  473. self.assertTableNotExists("test_crmo_pony")
  474. # Test deconstruction
  475. definition = operation.deconstruct()
  476. self.assertEqual(definition[0], "CreateModel")
  477. self.assertEqual(definition[1], [])
  478. self.assertEqual(definition[2]['options']['constraints'], [check_constraint])
  479. def test_create_model_managers(self):
  480. """
  481. The managers on a model are set.
  482. """
  483. project_state = self.set_up_test_model("test_cmoma")
  484. # Test the state alteration
  485. operation = migrations.CreateModel(
  486. "Food",
  487. fields=[
  488. ("id", models.AutoField(primary_key=True)),
  489. ],
  490. managers=[
  491. ("food_qs", FoodQuerySet.as_manager()),
  492. ("food_mgr", FoodManager("a", "b")),
  493. ("food_mgr_kwargs", FoodManager("x", "y", 3, 4)),
  494. ]
  495. )
  496. self.assertEqual(operation.describe(), "Create model Food")
  497. new_state = project_state.clone()
  498. operation.state_forwards("test_cmoma", new_state)
  499. self.assertIn(("test_cmoma", "food"), new_state.models)
  500. managers = new_state.models["test_cmoma", "food"].managers
  501. self.assertEqual(managers[0][0], "food_qs")
  502. self.assertIsInstance(managers[0][1], models.Manager)
  503. self.assertEqual(managers[1][0], "food_mgr")
  504. self.assertIsInstance(managers[1][1], FoodManager)
  505. self.assertEqual(managers[1][1].args, ("a", "b", 1, 2))
  506. self.assertEqual(managers[2][0], "food_mgr_kwargs")
  507. self.assertIsInstance(managers[2][1], FoodManager)
  508. self.assertEqual(managers[2][1].args, ("x", "y", 3, 4))
  509. def test_delete_model(self):
  510. """
  511. Tests the DeleteModel operation.
  512. """
  513. project_state = self.set_up_test_model("test_dlmo")
  514. # Test the state alteration
  515. operation = migrations.DeleteModel("Pony")
  516. self.assertEqual(operation.describe(), "Delete model Pony")
  517. new_state = project_state.clone()
  518. operation.state_forwards("test_dlmo", new_state)
  519. self.assertNotIn(("test_dlmo", "pony"), new_state.models)
  520. # Test the database alteration
  521. self.assertTableExists("test_dlmo_pony")
  522. with connection.schema_editor() as editor:
  523. operation.database_forwards("test_dlmo", editor, project_state, new_state)
  524. self.assertTableNotExists("test_dlmo_pony")
  525. # And test reversal
  526. with connection.schema_editor() as editor:
  527. operation.database_backwards("test_dlmo", editor, new_state, project_state)
  528. self.assertTableExists("test_dlmo_pony")
  529. # And deconstruction
  530. definition = operation.deconstruct()
  531. self.assertEqual(definition[0], "DeleteModel")
  532. self.assertEqual(definition[1], [])
  533. self.assertEqual(list(definition[2]), ["name"])
  534. def test_delete_proxy_model(self):
  535. """
  536. Tests the DeleteModel operation ignores proxy models.
  537. """
  538. project_state = self.set_up_test_model("test_dlprmo", proxy_model=True)
  539. # Test the state alteration
  540. operation = migrations.DeleteModel("ProxyPony")
  541. new_state = project_state.clone()
  542. operation.state_forwards("test_dlprmo", new_state)
  543. self.assertIn(("test_dlprmo", "proxypony"), project_state.models)
  544. self.assertNotIn(("test_dlprmo", "proxypony"), new_state.models)
  545. # Test the database alteration
  546. self.assertTableExists("test_dlprmo_pony")
  547. self.assertTableNotExists("test_dlprmo_proxypony")
  548. with connection.schema_editor() as editor:
  549. operation.database_forwards("test_dlprmo", editor, project_state, new_state)
  550. self.assertTableExists("test_dlprmo_pony")
  551. self.assertTableNotExists("test_dlprmo_proxypony")
  552. # And test reversal
  553. with connection.schema_editor() as editor:
  554. operation.database_backwards("test_dlprmo", editor, new_state, project_state)
  555. self.assertTableExists("test_dlprmo_pony")
  556. self.assertTableNotExists("test_dlprmo_proxypony")
  557. def test_rename_model(self):
  558. """
  559. Tests the RenameModel operation.
  560. """
  561. project_state = self.set_up_test_model("test_rnmo", related_model=True)
  562. # Test the state alteration
  563. operation = migrations.RenameModel("Pony", "Horse")
  564. self.assertEqual(operation.describe(), "Rename model Pony to Horse")
  565. # Test initial state and database
  566. self.assertIn(("test_rnmo", "pony"), project_state.models)
  567. self.assertNotIn(("test_rnmo", "horse"), project_state.models)
  568. self.assertTableExists("test_rnmo_pony")
  569. self.assertTableNotExists("test_rnmo_horse")
  570. if connection.features.supports_foreign_keys:
  571. self.assertFKExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id"))
  572. self.assertFKNotExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id"))
  573. # Migrate forwards
  574. new_state = project_state.clone()
  575. atomic_rename = connection.features.supports_atomic_references_rename
  576. new_state = self.apply_operations("test_rnmo", new_state, [operation], atomic=atomic_rename)
  577. # Test new state and database
  578. self.assertNotIn(("test_rnmo", "pony"), new_state.models)
  579. self.assertIn(("test_rnmo", "horse"), new_state.models)
  580. # RenameModel also repoints all incoming FKs and M2Ms
  581. self.assertEqual("test_rnmo.Horse", new_state.models["test_rnmo", "rider"].fields[1][1].remote_field.model)
  582. self.assertTableNotExists("test_rnmo_pony")
  583. self.assertTableExists("test_rnmo_horse")
  584. if connection.features.supports_foreign_keys:
  585. self.assertFKNotExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id"))
  586. self.assertFKExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id"))
  587. # Migrate backwards
  588. original_state = self.unapply_operations("test_rnmo", project_state, [operation], atomic=atomic_rename)
  589. # Test original state and database
  590. self.assertIn(("test_rnmo", "pony"), original_state.models)
  591. self.assertNotIn(("test_rnmo", "horse"), original_state.models)
  592. self.assertEqual("Pony", original_state.models["test_rnmo", "rider"].fields[1][1].remote_field.model)
  593. self.assertTableExists("test_rnmo_pony")
  594. self.assertTableNotExists("test_rnmo_horse")
  595. if connection.features.supports_foreign_keys:
  596. self.assertFKExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id"))
  597. self.assertFKNotExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id"))
  598. # And deconstruction
  599. definition = operation.deconstruct()
  600. self.assertEqual(definition[0], "RenameModel")
  601. self.assertEqual(definition[1], [])
  602. self.assertEqual(definition[2], {'old_name': "Pony", 'new_name': "Horse"})
  603. def test_rename_model_state_forwards(self):
  604. """
  605. RenameModel operations shouldn't trigger the caching of rendered apps
  606. on state without prior apps.
  607. """
  608. state = ProjectState()
  609. state.add_model(ModelState('migrations', 'Foo', []))
  610. operation = migrations.RenameModel('Foo', 'Bar')
  611. operation.state_forwards('migrations', state)
  612. self.assertNotIn('apps', state.__dict__)
  613. self.assertNotIn(('migrations', 'foo'), state.models)
  614. self.assertIn(('migrations', 'bar'), state.models)
  615. # Now with apps cached.
  616. apps = state.apps
  617. operation = migrations.RenameModel('Bar', 'Foo')
  618. operation.state_forwards('migrations', state)
  619. self.assertIs(state.apps, apps)
  620. self.assertNotIn(('migrations', 'bar'), state.models)
  621. self.assertIn(('migrations', 'foo'), state.models)
  622. def test_rename_model_with_self_referential_fk(self):
  623. """
  624. Tests the RenameModel operation on model with self referential FK.
  625. """
  626. project_state = self.set_up_test_model("test_rmwsrf", related_model=True)
  627. # Test the state alteration
  628. operation = migrations.RenameModel("Rider", "HorseRider")
  629. self.assertEqual(operation.describe(), "Rename model Rider to HorseRider")
  630. new_state = project_state.clone()
  631. operation.state_forwards("test_rmwsrf", new_state)
  632. self.assertNotIn(("test_rmwsrf", "rider"), new_state.models)
  633. self.assertIn(("test_rmwsrf", "horserider"), new_state.models)
  634. # Remember, RenameModel also repoints all incoming FKs and M2Ms
  635. self.assertEqual(
  636. 'self',
  637. new_state.models["test_rmwsrf", "horserider"].fields[2][1].remote_field.model
  638. )
  639. HorseRider = new_state.apps.get_model('test_rmwsrf', 'horserider')
  640. self.assertIs(HorseRider._meta.get_field('horserider').remote_field.model, HorseRider)
  641. # Test the database alteration
  642. self.assertTableExists("test_rmwsrf_rider")
  643. self.assertTableNotExists("test_rmwsrf_horserider")
  644. if connection.features.supports_foreign_keys:
  645. self.assertFKExists("test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_rider", "id"))
  646. self.assertFKNotExists("test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_horserider", "id"))
  647. atomic_rename = connection.features.supports_atomic_references_rename
  648. with connection.schema_editor(atomic=atomic_rename) as editor:
  649. operation.database_forwards("test_rmwsrf", editor, project_state, new_state)
  650. self.assertTableNotExists("test_rmwsrf_rider")
  651. self.assertTableExists("test_rmwsrf_horserider")
  652. if connection.features.supports_foreign_keys:
  653. self.assertFKNotExists("test_rmwsrf_horserider", ["friend_id"], ("test_rmwsrf_rider", "id"))
  654. self.assertFKExists("test_rmwsrf_horserider", ["friend_id"], ("test_rmwsrf_horserider", "id"))
  655. # And test reversal
  656. with connection.schema_editor(atomic=atomic_rename) as editor:
  657. operation.database_backwards("test_rmwsrf", editor, new_state, project_state)
  658. self.assertTableExists("test_rmwsrf_rider")
  659. self.assertTableNotExists("test_rmwsrf_horserider")
  660. if connection.features.supports_foreign_keys:
  661. self.assertFKExists("test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_rider", "id"))
  662. self.assertFKNotExists("test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_horserider", "id"))
  663. def test_rename_model_with_superclass_fk(self):
  664. """
  665. Tests the RenameModel operation on a model which has a superclass that
  666. has a foreign key.
  667. """
  668. project_state = self.set_up_test_model("test_rmwsc", related_model=True, mti_model=True)
  669. # Test the state alteration
  670. operation = migrations.RenameModel("ShetlandPony", "LittleHorse")
  671. self.assertEqual(operation.describe(), "Rename model ShetlandPony to LittleHorse")
  672. new_state = project_state.clone()
  673. operation.state_forwards("test_rmwsc", new_state)
  674. self.assertNotIn(("test_rmwsc", "shetlandpony"), new_state.models)
  675. self.assertIn(("test_rmwsc", "littlehorse"), new_state.models)
  676. # RenameModel shouldn't repoint the superclass's relations, only local ones
  677. self.assertEqual(
  678. project_state.models["test_rmwsc", "rider"].fields[1][1].remote_field.model,
  679. new_state.models["test_rmwsc", "rider"].fields[1][1].remote_field.model
  680. )
  681. # Before running the migration we have a table for Shetland Pony, not Little Horse
  682. self.assertTableExists("test_rmwsc_shetlandpony")
  683. self.assertTableNotExists("test_rmwsc_littlehorse")
  684. if connection.features.supports_foreign_keys:
  685. # and the foreign key on rider points to pony, not shetland pony
  686. self.assertFKExists("test_rmwsc_rider", ["pony_id"], ("test_rmwsc_pony", "id"))
  687. self.assertFKNotExists("test_rmwsc_rider", ["pony_id"], ("test_rmwsc_shetlandpony", "id"))
  688. with connection.schema_editor(atomic=connection.features.supports_atomic_references_rename) as editor:
  689. operation.database_forwards("test_rmwsc", editor, project_state, new_state)
  690. # Now we have a little horse table, not shetland pony
  691. self.assertTableNotExists("test_rmwsc_shetlandpony")
  692. self.assertTableExists("test_rmwsc_littlehorse")
  693. if connection.features.supports_foreign_keys:
  694. # but the Foreign keys still point at pony, not little horse
  695. self.assertFKExists("test_rmwsc_rider", ["pony_id"], ("test_rmwsc_pony", "id"))
  696. self.assertFKNotExists("test_rmwsc_rider", ["pony_id"], ("test_rmwsc_littlehorse", "id"))
  697. def test_rename_model_with_self_referential_m2m(self):
  698. app_label = "test_rename_model_with_self_referential_m2m"
  699. project_state = self.apply_operations(app_label, ProjectState(), operations=[
  700. migrations.CreateModel("ReflexivePony", fields=[
  701. ("id", models.AutoField(primary_key=True)),
  702. ("ponies", models.ManyToManyField("self")),
  703. ]),
  704. ])
  705. project_state = self.apply_operations(app_label, project_state, operations=[
  706. migrations.RenameModel("ReflexivePony", "ReflexivePony2"),
  707. ], atomic=connection.features.supports_atomic_references_rename)
  708. Pony = project_state.apps.get_model(app_label, "ReflexivePony2")
  709. pony = Pony.objects.create()
  710. pony.ponies.add(pony)
  711. def test_rename_model_with_m2m(self):
  712. app_label = "test_rename_model_with_m2m"
  713. project_state = self.apply_operations(app_label, ProjectState(), operations=[
  714. migrations.CreateModel("Rider", fields=[
  715. ("id", models.AutoField(primary_key=True)),
  716. ]),
  717. migrations.CreateModel("Pony", fields=[
  718. ("id", models.AutoField(primary_key=True)),
  719. ("riders", models.ManyToManyField("Rider")),
  720. ]),
  721. ])
  722. Pony = project_state.apps.get_model(app_label, "Pony")
  723. Rider = project_state.apps.get_model(app_label, "Rider")
  724. pony = Pony.objects.create()
  725. rider = Rider.objects.create()
  726. pony.riders.add(rider)
  727. project_state = self.apply_operations(app_label, project_state, operations=[
  728. migrations.RenameModel("Pony", "Pony2"),
  729. ], atomic=connection.features.supports_atomic_references_rename)
  730. Pony = project_state.apps.get_model(app_label, "Pony2")
  731. Rider = project_state.apps.get_model(app_label, "Rider")
  732. pony = Pony.objects.create()
  733. rider = Rider.objects.create()
  734. pony.riders.add(rider)
  735. self.assertEqual(Pony.objects.count(), 2)
  736. self.assertEqual(Rider.objects.count(), 2)
  737. self.assertEqual(Pony._meta.get_field('riders').remote_field.through.objects.count(), 2)
  738. def test_rename_m2m_target_model(self):
  739. app_label = "test_rename_m2m_target_model"
  740. project_state = self.apply_operations(app_label, ProjectState(), operations=[
  741. migrations.CreateModel("Rider", fields=[
  742. ("id", models.AutoField(primary_key=True)),
  743. ]),
  744. migrations.CreateModel("Pony", fields=[
  745. ("id", models.AutoField(primary_key=True)),
  746. ("riders", models.ManyToManyField("Rider")),
  747. ]),
  748. ])
  749. Pony = project_state.apps.get_model(app_label, "Pony")
  750. Rider = project_state.apps.get_model(app_label, "Rider")
  751. pony = Pony.objects.create()
  752. rider = Rider.objects.create()
  753. pony.riders.add(rider)
  754. project_state = self.apply_operations(app_label, project_state, operations=[
  755. migrations.RenameModel("Rider", "Rider2"),
  756. ], atomic=connection.features.supports_atomic_references_rename)
  757. Pony = project_state.apps.get_model(app_label, "Pony")
  758. Rider = project_state.apps.get_model(app_label, "Rider2")
  759. pony = Pony.objects.create()
  760. rider = Rider.objects.create()
  761. pony.riders.add(rider)
  762. self.assertEqual(Pony.objects.count(), 2)
  763. self.assertEqual(Rider.objects.count(), 2)
  764. self.assertEqual(Pony._meta.get_field('riders').remote_field.through.objects.count(), 2)
  765. def test_rename_m2m_through_model(self):
  766. app_label = "test_rename_through"
  767. project_state = self.apply_operations(app_label, ProjectState(), operations=[
  768. migrations.CreateModel("Rider", fields=[
  769. ("id", models.AutoField(primary_key=True)),
  770. ]),
  771. migrations.CreateModel("Pony", fields=[
  772. ("id", models.AutoField(primary_key=True)),
  773. ]),
  774. migrations.CreateModel("PonyRider", fields=[
  775. ("id", models.AutoField(primary_key=True)),
  776. ("rider", models.ForeignKey("test_rename_through.Rider", models.CASCADE)),
  777. ("pony", models.ForeignKey("test_rename_through.Pony", models.CASCADE)),
  778. ]),
  779. migrations.AddField(
  780. "Pony",
  781. "riders",
  782. models.ManyToManyField("test_rename_through.Rider", through="test_rename_through.PonyRider"),
  783. ),
  784. ])
  785. Pony = project_state.apps.get_model(app_label, "Pony")
  786. Rider = project_state.apps.get_model(app_label, "Rider")
  787. PonyRider = project_state.apps.get_model(app_label, "PonyRider")
  788. pony = Pony.objects.create()
  789. rider = Rider.objects.create()
  790. PonyRider.objects.create(pony=pony, rider=rider)
  791. project_state = self.apply_operations(app_label, project_state, operations=[
  792. migrations.RenameModel("PonyRider", "PonyRider2"),
  793. ])
  794. Pony = project_state.apps.get_model(app_label, "Pony")
  795. Rider = project_state.apps.get_model(app_label, "Rider")
  796. PonyRider = project_state.apps.get_model(app_label, "PonyRider2")
  797. pony = Pony.objects.first()
  798. rider = Rider.objects.create()
  799. PonyRider.objects.create(pony=pony, rider=rider)
  800. self.assertEqual(Pony.objects.count(), 1)
  801. self.assertEqual(Rider.objects.count(), 2)
  802. self.assertEqual(PonyRider.objects.count(), 2)
  803. self.assertEqual(pony.riders.count(), 2)
  804. def test_rename_m2m_model_after_rename_field(self):
  805. """RenameModel renames a many-to-many column after a RenameField."""
  806. app_label = 'test_rename_multiple'
  807. project_state = self.apply_operations(app_label, ProjectState(), operations=[
  808. migrations.CreateModel('Pony', fields=[
  809. ('id', models.AutoField(primary_key=True)),
  810. ('name', models.CharField(max_length=20)),
  811. ]),
  812. migrations.CreateModel('Rider', fields=[
  813. ('id', models.AutoField(primary_key=True)),
  814. ('pony', models.ForeignKey('test_rename_multiple.Pony', models.CASCADE)),
  815. ]),
  816. migrations.CreateModel('PonyRider', fields=[
  817. ('id', models.AutoField(primary_key=True)),
  818. ('riders', models.ManyToManyField('Rider')),
  819. ]),
  820. migrations.RenameField(model_name='pony', old_name='name', new_name='fancy_name'),
  821. migrations.RenameModel(old_name='Rider', new_name='Jockey'),
  822. ], atomic=connection.features.supports_atomic_references_rename)
  823. Pony = project_state.apps.get_model(app_label, 'Pony')
  824. Jockey = project_state.apps.get_model(app_label, 'Jockey')
  825. PonyRider = project_state.apps.get_model(app_label, 'PonyRider')
  826. # No "no such column" error means the column was renamed correctly.
  827. pony = Pony.objects.create(fancy_name='a good name')
  828. jockey = Jockey.objects.create(pony=pony)
  829. ponyrider = PonyRider.objects.create()
  830. ponyrider.riders.add(jockey)
  831. def test_add_field(self):
  832. """
  833. Tests the AddField operation.
  834. """
  835. # Test the state alteration
  836. operation = migrations.AddField(
  837. "Pony",
  838. "height",
  839. models.FloatField(null=True, default=5),
  840. )
  841. self.assertEqual(operation.describe(), "Add field height to Pony")
  842. project_state, new_state = self.make_test_state("test_adfl", operation)
  843. self.assertEqual(len(new_state.models["test_adfl", "pony"].fields), 4)
  844. field = [
  845. f for n, f in new_state.models["test_adfl", "pony"].fields
  846. if n == "height"
  847. ][0]
  848. self.assertEqual(field.default, 5)
  849. # Test the database alteration
  850. self.assertColumnNotExists("test_adfl_pony", "height")
  851. with connection.schema_editor() as editor:
  852. operation.database_forwards("test_adfl", editor, project_state, new_state)
  853. self.assertColumnExists("test_adfl_pony", "height")
  854. # And test reversal
  855. with connection.schema_editor() as editor:
  856. operation.database_backwards("test_adfl", editor, new_state, project_state)
  857. self.assertColumnNotExists("test_adfl_pony", "height")
  858. # And deconstruction
  859. definition = operation.deconstruct()
  860. self.assertEqual(definition[0], "AddField")
  861. self.assertEqual(definition[1], [])
  862. self.assertEqual(sorted(definition[2]), ["field", "model_name", "name"])
  863. def test_add_charfield(self):
  864. """
  865. Tests the AddField operation on TextField.
  866. """
  867. project_state = self.set_up_test_model("test_adchfl")
  868. Pony = project_state.apps.get_model("test_adchfl", "Pony")
  869. pony = Pony.objects.create(weight=42)
  870. new_state = self.apply_operations("test_adchfl", project_state, [
  871. migrations.AddField(
  872. "Pony",
  873. "text",
  874. models.CharField(max_length=10, default="some text"),
  875. ),
  876. migrations.AddField(
  877. "Pony",
  878. "empty",
  879. models.CharField(max_length=10, default=""),
  880. ),
  881. # If not properly quoted digits would be interpreted as an int.
  882. migrations.AddField(
  883. "Pony",
  884. "digits",
  885. models.CharField(max_length=10, default="42"),
  886. ),
  887. # Manual quoting is fragile and could trip on quotes. Refs #xyz.
  888. migrations.AddField(
  889. "Pony",
  890. "quotes",
  891. models.CharField(max_length=10, default='"\'"'),
  892. ),
  893. ])
  894. Pony = new_state.apps.get_model("test_adchfl", "Pony")
  895. pony = Pony.objects.get(pk=pony.pk)
  896. self.assertEqual(pony.text, "some text")
  897. self.assertEqual(pony.empty, "")
  898. self.assertEqual(pony.digits, "42")
  899. self.assertEqual(pony.quotes, '"\'"')
  900. def test_add_textfield(self):
  901. """
  902. Tests the AddField operation on TextField.
  903. """
  904. project_state = self.set_up_test_model("test_adtxtfl")
  905. Pony = project_state.apps.get_model("test_adtxtfl", "Pony")
  906. pony = Pony.objects.create(weight=42)
  907. new_state = self.apply_operations("test_adtxtfl", project_state, [
  908. migrations.AddField(
  909. "Pony",
  910. "text",
  911. models.TextField(default="some text"),
  912. ),
  913. migrations.AddField(
  914. "Pony",
  915. "empty",
  916. models.TextField(default=""),
  917. ),
  918. # If not properly quoted digits would be interpreted as an int.
  919. migrations.AddField(
  920. "Pony",
  921. "digits",
  922. models.TextField(default="42"),
  923. ),
  924. # Manual quoting is fragile and could trip on quotes. Refs #xyz.
  925. migrations.AddField(
  926. "Pony",
  927. "quotes",
  928. models.TextField(default='"\'"'),
  929. ),
  930. ])
  931. Pony = new_state.apps.get_model("test_adtxtfl", "Pony")
  932. pony = Pony.objects.get(pk=pony.pk)
  933. self.assertEqual(pony.text, "some text")
  934. self.assertEqual(pony.empty, "")
  935. self.assertEqual(pony.digits, "42")
  936. self.assertEqual(pony.quotes, '"\'"')
  937. def test_add_binaryfield(self):
  938. """
  939. Tests the AddField operation on TextField/BinaryField.
  940. """
  941. project_state = self.set_up_test_model("test_adbinfl")
  942. Pony = project_state.apps.get_model("test_adbinfl", "Pony")
  943. pony = Pony.objects.create(weight=42)
  944. new_state = self.apply_operations("test_adbinfl", project_state, [
  945. migrations.AddField(
  946. "Pony",
  947. "blob",
  948. models.BinaryField(default=b"some text"),
  949. ),
  950. migrations.AddField(
  951. "Pony",
  952. "empty",
  953. models.BinaryField(default=b""),
  954. ),
  955. # If not properly quoted digits would be interpreted as an int.
  956. migrations.AddField(
  957. "Pony",
  958. "digits",
  959. models.BinaryField(default=b"42"),
  960. ),
  961. # Manual quoting is fragile and could trip on quotes. Refs #xyz.
  962. migrations.AddField(
  963. "Pony",
  964. "quotes",
  965. models.BinaryField(default=b'"\'"'),
  966. ),
  967. ])
  968. Pony = new_state.apps.get_model("test_adbinfl", "Pony")
  969. pony = Pony.objects.get(pk=pony.pk)
  970. # SQLite returns buffer/memoryview, cast to bytes for checking.
  971. self.assertEqual(bytes(pony.blob), b"some text")
  972. self.assertEqual(bytes(pony.empty), b"")
  973. self.assertEqual(bytes(pony.digits), b"42")
  974. self.assertEqual(bytes(pony.quotes), b'"\'"')
  975. def test_column_name_quoting(self):
  976. """
  977. Column names that are SQL keywords shouldn't cause problems when used
  978. in migrations (#22168).
  979. """
  980. project_state = self.set_up_test_model("test_regr22168")
  981. operation = migrations.AddField(
  982. "Pony",
  983. "order",
  984. models.IntegerField(default=0),
  985. )
  986. new_state = project_state.clone()
  987. operation.state_forwards("test_regr22168", new_state)
  988. with connection.schema_editor() as editor:
  989. operation.database_forwards("test_regr22168", editor, project_state, new_state)
  990. self.assertColumnExists("test_regr22168_pony", "order")
  991. def test_add_field_preserve_default(self):
  992. """
  993. Tests the AddField operation's state alteration
  994. when preserve_default = False.
  995. """
  996. project_state = self.set_up_test_model("test_adflpd")
  997. # Test the state alteration
  998. operation = migrations.AddField(
  999. "Pony",
  1000. "height",
  1001. models.FloatField(null=True, default=4),
  1002. preserve_default=False,
  1003. )
  1004. new_state = project_state.clone()
  1005. operation.state_forwards("test_adflpd", new_state)
  1006. self.assertEqual(len(new_state.models["test_adflpd", "pony"].fields), 4)
  1007. field = [
  1008. f for n, f in new_state.models["test_adflpd", "pony"].fields
  1009. if n == "height"
  1010. ][0]
  1011. self.assertEqual(field.default, NOT_PROVIDED)
  1012. # Test the database alteration
  1013. project_state.apps.get_model("test_adflpd", "pony").objects.create(
  1014. weight=4,
  1015. )
  1016. self.assertColumnNotExists("test_adflpd_pony", "height")
  1017. with connection.schema_editor() as editor:
  1018. operation.database_forwards("test_adflpd", editor, project_state, new_state)
  1019. self.assertColumnExists("test_adflpd_pony", "height")
  1020. # And deconstruction
  1021. definition = operation.deconstruct()
  1022. self.assertEqual(definition[0], "AddField")
  1023. self.assertEqual(definition[1], [])
  1024. self.assertEqual(sorted(definition[2]), ["field", "model_name", "name", "preserve_default"])
  1025. def test_add_field_m2m(self):
  1026. """
  1027. Tests the AddField operation with a ManyToManyField.
  1028. """
  1029. project_state = self.set_up_test_model("test_adflmm", second_model=True)
  1030. # Test the state alteration
  1031. operation = migrations.AddField("Pony", "stables", models.ManyToManyField("Stable", related_name="ponies"))
  1032. new_state = project_state.clone()
  1033. operation.state_forwards("test_adflmm", new_state)
  1034. self.assertEqual(len(new_state.models["test_adflmm", "pony"].fields), 4)
  1035. # Test the database alteration
  1036. self.assertTableNotExists("test_adflmm_pony_stables")
  1037. with connection.schema_editor() as editor:
  1038. operation.database_forwards("test_adflmm", editor, project_state, new_state)
  1039. self.assertTableExists("test_adflmm_pony_stables")
  1040. self.assertColumnNotExists("test_adflmm_pony", "stables")
  1041. # Make sure the M2M field actually works
  1042. with atomic():
  1043. Pony = new_state.apps.get_model("test_adflmm", "Pony")
  1044. p = Pony.objects.create(pink=False, weight=4.55)
  1045. p.stables.create()
  1046. self.assertEqual(p.stables.count(), 1)
  1047. p.stables.all().delete()
  1048. # And test reversal
  1049. with connection.schema_editor() as editor:
  1050. operation.database_backwards("test_adflmm", editor, new_state, project_state)
  1051. self.assertTableNotExists("test_adflmm_pony_stables")
  1052. def test_alter_field_m2m(self):
  1053. project_state = self.set_up_test_model("test_alflmm", second_model=True)
  1054. project_state = self.apply_operations("test_alflmm", project_state, operations=[
  1055. migrations.AddField("Pony", "stables", models.ManyToManyField("Stable", related_name="ponies"))
  1056. ])
  1057. Pony = project_state.apps.get_model("test_alflmm", "Pony")
  1058. self.assertFalse(Pony._meta.get_field('stables').blank)
  1059. project_state = self.apply_operations("test_alflmm", project_state, operations=[
  1060. migrations.AlterField(
  1061. "Pony", "stables", models.ManyToManyField(to="Stable", related_name="ponies", blank=True)
  1062. )
  1063. ])
  1064. Pony = project_state.apps.get_model("test_alflmm", "Pony")
  1065. self.assertTrue(Pony._meta.get_field('stables').blank)
  1066. def test_repoint_field_m2m(self):
  1067. project_state = self.set_up_test_model("test_alflmm", second_model=True, third_model=True)
  1068. project_state = self.apply_operations("test_alflmm", project_state, operations=[
  1069. migrations.AddField("Pony", "places", models.ManyToManyField("Stable", related_name="ponies"))
  1070. ])
  1071. Pony = project_state.apps.get_model("test_alflmm", "Pony")
  1072. project_state = self.apply_operations("test_alflmm", project_state, operations=[
  1073. migrations.AlterField("Pony", "places", models.ManyToManyField(to="Van", related_name="ponies"))
  1074. ])
  1075. # Ensure the new field actually works
  1076. Pony = project_state.apps.get_model("test_alflmm", "Pony")
  1077. p = Pony.objects.create(pink=False, weight=4.55)
  1078. p.places.create()
  1079. self.assertEqual(p.places.count(), 1)
  1080. p.places.all().delete()
  1081. def test_remove_field_m2m(self):
  1082. project_state = self.set_up_test_model("test_rmflmm", second_model=True)
  1083. project_state = self.apply_operations("test_rmflmm", project_state, operations=[
  1084. migrations.AddField("Pony", "stables", models.ManyToManyField("Stable", related_name="ponies"))
  1085. ])
  1086. self.assertTableExists("test_rmflmm_pony_stables")
  1087. with_field_state = project_state.clone()
  1088. operations = [migrations.RemoveField("Pony", "stables")]
  1089. project_state = self.apply_operations("test_rmflmm", project_state, operations=operations)
  1090. self.assertTableNotExists("test_rmflmm_pony_stables")
  1091. # And test reversal
  1092. self.unapply_operations("test_rmflmm", with_field_state, operations=operations)
  1093. self.assertTableExists("test_rmflmm_pony_stables")
  1094. def test_remove_field_m2m_with_through(self):
  1095. project_state = self.set_up_test_model("test_rmflmmwt", second_model=True)
  1096. self.assertTableNotExists("test_rmflmmwt_ponystables")
  1097. project_state = self.apply_operations("test_rmflmmwt", project_state, operations=[
  1098. migrations.CreateModel("PonyStables", fields=[
  1099. ("pony", models.ForeignKey('test_rmflmmwt.Pony', models.CASCADE)),
  1100. ("stable", models.ForeignKey('test_rmflmmwt.Stable', models.CASCADE)),
  1101. ]),
  1102. migrations.AddField(
  1103. "Pony", "stables",
  1104. models.ManyToManyField("Stable", related_name="ponies", through='test_rmflmmwt.PonyStables')
  1105. )
  1106. ])
  1107. self.assertTableExists("test_rmflmmwt_ponystables")
  1108. operations = [migrations.RemoveField("Pony", "stables"), migrations.DeleteModel("PonyStables")]
  1109. self.apply_operations("test_rmflmmwt", project_state, operations=operations)
  1110. def test_remove_field(self):
  1111. """
  1112. Tests the RemoveField operation.
  1113. """
  1114. project_state = self.set_up_test_model("test_rmfl")
  1115. # Test the state alteration
  1116. operation = migrations.RemoveField("Pony", "pink")
  1117. self.assertEqual(operation.describe(), "Remove field pink from Pony")
  1118. new_state = project_state.clone()
  1119. operation.state_forwards("test_rmfl", new_state)
  1120. self.assertEqual(len(new_state.models["test_rmfl", "pony"].fields), 2)
  1121. # Test the database alteration
  1122. self.assertColumnExists("test_rmfl_pony", "pink")
  1123. with connection.schema_editor() as editor:
  1124. operation.database_forwards("test_rmfl", editor, project_state, new_state)
  1125. self.assertColumnNotExists("test_rmfl_pony", "pink")
  1126. # And test reversal
  1127. with connection.schema_editor() as editor:
  1128. operation.database_backwards("test_rmfl", editor, new_state, project_state)
  1129. self.assertColumnExists("test_rmfl_pony", "pink")
  1130. # And deconstruction
  1131. definition = operation.deconstruct()
  1132. self.assertEqual(definition[0], "RemoveField")
  1133. self.assertEqual(definition[1], [])
  1134. self.assertEqual(definition[2], {'model_name': "Pony", 'name': 'pink'})
  1135. def test_remove_fk(self):
  1136. """
  1137. Tests the RemoveField operation on a foreign key.
  1138. """
  1139. project_state = self.set_up_test_model("test_rfk", related_model=True)
  1140. self.assertColumnExists("test_rfk_rider", "pony_id")
  1141. operation = migrations.RemoveField("Rider", "pony")
  1142. new_state = project_state.clone()
  1143. operation.state_forwards("test_rfk", new_state)
  1144. with connection.schema_editor() as editor:
  1145. operation.database_forwards("test_rfk", editor, project_state, new_state)
  1146. self.assertColumnNotExists("test_rfk_rider", "pony_id")
  1147. with connection.schema_editor() as editor:
  1148. operation.database_backwards("test_rfk", editor, new_state, project_state)
  1149. self.assertColumnExists("test_rfk_rider", "pony_id")
  1150. def test_alter_model_table(self):
  1151. """
  1152. Tests the AlterModelTable operation.
  1153. """
  1154. project_state = self.set_up_test_model("test_almota")
  1155. # Test the state alteration
  1156. operation = migrations.AlterModelTable("Pony", "test_almota_pony_2")
  1157. self.assertEqual(operation.describe(), "Rename table for Pony to test_almota_pony_2")
  1158. new_state = project_state.clone()
  1159. operation.state_forwards("test_almota", new_state)
  1160. self.assertEqual(new_state.models["test_almota", "pony"].options["db_table"], "test_almota_pony_2")
  1161. # Test the database alteration
  1162. self.assertTableExists("test_almota_pony")
  1163. self.assertTableNotExists("test_almota_pony_2")
  1164. with connection.schema_editor() as editor:
  1165. operation.database_forwards("test_almota", editor, project_state, new_state)
  1166. self.assertTableNotExists("test_almota_pony")
  1167. self.assertTableExists("test_almota_pony_2")
  1168. # And test reversal
  1169. with connection.schema_editor() as editor:
  1170. operation.database_backwards("test_almota", editor, new_state, project_state)
  1171. self.assertTableExists("test_almota_pony")
  1172. self.assertTableNotExists("test_almota_pony_2")
  1173. # And deconstruction
  1174. definition = operation.deconstruct()
  1175. self.assertEqual(definition[0], "AlterModelTable")
  1176. self.assertEqual(definition[1], [])
  1177. self.assertEqual(definition[2], {'name': "Pony", 'table': "test_almota_pony_2"})
  1178. def test_alter_model_table_none(self):
  1179. """
  1180. Tests the AlterModelTable operation if the table name is set to None.
  1181. """
  1182. operation = migrations.AlterModelTable("Pony", None)
  1183. self.assertEqual(operation.describe(), "Rename table for Pony to (default)")
  1184. def test_alter_model_table_noop(self):
  1185. """
  1186. Tests the AlterModelTable operation if the table name is not changed.
  1187. """
  1188. project_state = self.set_up_test_model("test_almota")
  1189. # Test the state alteration
  1190. operation = migrations.AlterModelTable("Pony", "test_almota_pony")
  1191. new_state = project_state.clone()
  1192. operation.state_forwards("test_almota", new_state)
  1193. self.assertEqual(new_state.models["test_almota", "pony"].options["db_table"], "test_almota_pony")
  1194. # Test the database alteration
  1195. self.assertTableExists("test_almota_pony")
  1196. with connection.schema_editor() as editor:
  1197. operation.database_forwards("test_almota", editor, project_state, new_state)
  1198. self.assertTableExists("test_almota_pony")
  1199. # And test reversal
  1200. with connection.schema_editor() as editor:
  1201. operation.database_backwards("test_almota", editor, new_state, project_state)
  1202. self.assertTableExists("test_almota_pony")
  1203. def test_alter_model_table_m2m(self):
  1204. """
  1205. AlterModelTable should rename auto-generated M2M tables.
  1206. """
  1207. app_label = "test_talflmltlm2m"
  1208. pony_db_table = 'pony_foo'
  1209. project_state = self.set_up_test_model(app_label, second_model=True, db_table=pony_db_table)
  1210. # Add the M2M field
  1211. first_state = project_state.clone()
  1212. operation = migrations.AddField("Pony", "stables", models.ManyToManyField("Stable"))
  1213. operation.state_forwards(app_label, first_state)
  1214. with connection.schema_editor() as editor:
  1215. operation.database_forwards(app_label, editor, project_state, first_state)
  1216. original_m2m_table = "%s_%s" % (pony_db_table, "stables")
  1217. new_m2m_table = "%s_%s" % (app_label, "pony_stables")
  1218. self.assertTableExists(original_m2m_table)
  1219. self.assertTableNotExists(new_m2m_table)
  1220. # Rename the Pony db_table which should also rename the m2m table.
  1221. second_state = first_state.clone()
  1222. operation = migrations.AlterModelTable(name='pony', table=None)
  1223. operation.state_forwards(app_label, second_state)
  1224. atomic_rename = connection.features.supports_atomic_references_rename
  1225. with connection.schema_editor(atomic=atomic_rename) as editor:
  1226. operation.database_forwards(app_label, editor, first_state, second_state)
  1227. self.assertTableExists(new_m2m_table)
  1228. self.assertTableNotExists(original_m2m_table)
  1229. # And test reversal
  1230. with connection.schema_editor(atomic=atomic_rename) as editor:
  1231. operation.database_backwards(app_label, editor, second_state, first_state)
  1232. self.assertTableExists(original_m2m_table)
  1233. self.assertTableNotExists(new_m2m_table)
  1234. def test_alter_field(self):
  1235. """
  1236. Tests the AlterField operation.
  1237. """
  1238. project_state = self.set_up_test_model("test_alfl")
  1239. # Test the state alteration
  1240. operation = migrations.AlterField("Pony", "pink", models.IntegerField(null=True))
  1241. self.assertEqual(operation.describe(), "Alter field pink on Pony")
  1242. new_state = project_state.clone()
  1243. operation.state_forwards("test_alfl", new_state)
  1244. self.assertIs(project_state.models["test_alfl", "pony"].get_field_by_name("pink").null, False)
  1245. self.assertIs(new_state.models["test_alfl", "pony"].get_field_by_name("pink").null, True)
  1246. # Test the database alteration
  1247. self.assertColumnNotNull("test_alfl_pony", "pink")
  1248. with connection.schema_editor() as editor:
  1249. operation.database_forwards("test_alfl", editor, project_state, new_state)
  1250. self.assertColumnNull("test_alfl_pony", "pink")
  1251. # And test reversal
  1252. with connection.schema_editor() as editor:
  1253. operation.database_backwards("test_alfl", editor, new_state, project_state)
  1254. self.assertColumnNotNull("test_alfl_pony", "pink")
  1255. # And deconstruction
  1256. definition = operation.deconstruct()
  1257. self.assertEqual(definition[0], "AlterField")
  1258. self.assertEqual(definition[1], [])
  1259. self.assertEqual(sorted(definition[2]), ["field", "model_name", "name"])
  1260. def test_alter_field_pk(self):
  1261. """
  1262. Tests the AlterField operation on primary keys (for things like PostgreSQL's SERIAL weirdness)
  1263. """
  1264. project_state = self.set_up_test_model("test_alflpk")
  1265. # Test the state alteration
  1266. operation = migrations.AlterField("Pony", "id", models.IntegerField(primary_key=True))
  1267. new_state = project_state.clone()
  1268. operation.state_forwards("test_alflpk", new_state)
  1269. self.assertIsInstance(project_state.models["test_alflpk", "pony"].get_field_by_name("id"), models.AutoField)
  1270. self.assertIsInstance(new_state.models["test_alflpk", "pony"].get_field_by_name("id"), models.IntegerField)
  1271. # Test the database alteration
  1272. with connection.schema_editor() as editor:
  1273. operation.database_forwards("test_alflpk", editor, project_state, new_state)
  1274. # And test reversal
  1275. with connection.schema_editor() as editor:
  1276. operation.database_backwards("test_alflpk", editor, new_state, project_state)
  1277. @skipUnlessDBFeature('supports_foreign_keys')
  1278. def test_alter_field_pk_fk(self):
  1279. """
  1280. Tests the AlterField operation on primary keys changes any FKs pointing to it.
  1281. """
  1282. project_state = self.set_up_test_model("test_alflpkfk", related_model=True)
  1283. # Test the state alteration
  1284. operation = migrations.AlterField("Pony", "id", models.FloatField(primary_key=True))
  1285. new_state = project_state.clone()
  1286. operation.state_forwards("test_alflpkfk", new_state)
  1287. self.assertIsInstance(project_state.models["test_alflpkfk", "pony"].get_field_by_name("id"), models.AutoField)
  1288. self.assertIsInstance(new_state.models["test_alflpkfk", "pony"].get_field_by_name("id"), models.FloatField)
  1289. def assertIdTypeEqualsFkType():
  1290. with connection.cursor() as cursor:
  1291. id_type, id_null = [
  1292. (c.type_code, c.null_ok)
  1293. for c in connection.introspection.get_table_description(cursor, "test_alflpkfk_pony")
  1294. if c.name == "id"
  1295. ][0]
  1296. fk_type, fk_null = [
  1297. (c.type_code, c.null_ok)
  1298. for c in connection.introspection.get_table_description(cursor, "test_alflpkfk_rider")
  1299. if c.name == "pony_id"
  1300. ][0]
  1301. self.assertEqual(id_type, fk_type)
  1302. self.assertEqual(id_null, fk_null)
  1303. assertIdTypeEqualsFkType()
  1304. # Test the database alteration
  1305. with connection.schema_editor() as editor:
  1306. operation.database_forwards("test_alflpkfk", editor, project_state, new_state)
  1307. assertIdTypeEqualsFkType()
  1308. # And test reversal
  1309. with connection.schema_editor() as editor:
  1310. operation.database_backwards("test_alflpkfk", editor, new_state, project_state)
  1311. assertIdTypeEqualsFkType()
  1312. def test_alter_field_reloads_state_on_fk_target_changes(self):
  1313. """
  1314. If AlterField doesn't reload state appropriately, the second AlterField
  1315. crashes on MySQL due to not dropping the PonyRider.pony foreign key
  1316. constraint before modifying the column.
  1317. """
  1318. app_label = 'alter_alter_field_reloads_state_on_fk_target_changes'
  1319. project_state = self.apply_operations(app_label, ProjectState(), operations=[
  1320. migrations.CreateModel('Rider', fields=[
  1321. ('id', models.CharField(primary_key=True, max_length=100)),
  1322. ]),
  1323. migrations.CreateModel('Pony', fields=[
  1324. ('id', models.CharField(primary_key=True, max_length=100)),
  1325. ('rider', models.ForeignKey('%s.Rider' % app_label, models.CASCADE)),
  1326. ]),
  1327. migrations.CreateModel('PonyRider', fields=[
  1328. ('id', models.AutoField(primary_key=True)),
  1329. ('pony', models.ForeignKey('%s.Pony' % app_label, models.CASCADE)),
  1330. ]),
  1331. ])
  1332. project_state = self.apply_operations(app_label, project_state, operations=[
  1333. migrations.AlterField('Rider', 'id', models.CharField(primary_key=True, max_length=99)),
  1334. migrations.AlterField('Pony', 'id', models.CharField(primary_key=True, max_length=99)),
  1335. ])
  1336. def test_alter_field_reloads_state_on_fk_with_to_field_target_changes(self):
  1337. """
  1338. If AlterField doesn't reload state appropriately, the second AlterField
  1339. crashes on MySQL due to not dropping the PonyRider.pony foreign key
  1340. constraint before modifying the column.
  1341. """
  1342. app_label = 'alter_alter_field_reloads_state_on_fk_with_to_field_target_changes'
  1343. project_state = self.apply_operations(app_label, ProjectState(), operations=[
  1344. migrations.CreateModel('Rider', fields=[
  1345. ('id', models.CharField(primary_key=True, max_length=100)),
  1346. ('slug', models.CharField(unique=True, max_length=100)),
  1347. ]),
  1348. migrations.CreateModel('Pony', fields=[
  1349. ('id', models.CharField(primary_key=True, max_length=100)),
  1350. ('rider', models.ForeignKey('%s.Rider' % app_label, models.CASCADE, to_field='slug')),
  1351. ('slug', models.CharField(unique=True, max_length=100)),
  1352. ]),
  1353. migrations.CreateModel('PonyRider', fields=[
  1354. ('id', models.AutoField(primary_key=True)),
  1355. ('pony', models.ForeignKey('%s.Pony' % app_label, models.CASCADE, to_field='slug')),
  1356. ]),
  1357. ])
  1358. project_state = self.apply_operations(app_label, project_state, operations=[
  1359. migrations.AlterField('Rider', 'slug', models.CharField(unique=True, max_length=99)),
  1360. migrations.AlterField('Pony', 'slug', models.CharField(unique=True, max_length=99)),
  1361. ])
  1362. def test_rename_field_reloads_state_on_fk_target_changes(self):
  1363. """
  1364. If RenameField doesn't reload state appropriately, the AlterField
  1365. crashes on MySQL due to not dropping the PonyRider.pony foreign key
  1366. constraint before modifying the column.
  1367. """
  1368. app_label = 'alter_rename_field_reloads_state_on_fk_target_changes'
  1369. project_state = self.apply_operations(app_label, ProjectState(), operations=[
  1370. migrations.CreateModel('Rider', fields=[
  1371. ('id', models.CharField(primary_key=True, max_length=100)),
  1372. ]),
  1373. migrations.CreateModel('Pony', fields=[
  1374. ('id', models.CharField(primary_key=True, max_length=100)),
  1375. ('rider', models.ForeignKey('%s.Rider' % app_label, models.CASCADE)),
  1376. ]),
  1377. migrations.CreateModel('PonyRider', fields=[
  1378. ('id', models.AutoField(primary_key=True)),
  1379. ('pony', models.ForeignKey('%s.Pony' % app_label, models.CASCADE)),
  1380. ]),
  1381. ])
  1382. project_state = self.apply_operations(app_label, project_state, operations=[
  1383. migrations.RenameField('Rider', 'id', 'id2'),
  1384. migrations.AlterField('Pony', 'id', models.CharField(primary_key=True, max_length=99)),
  1385. ], atomic=connection.features.supports_atomic_references_rename)
  1386. def test_rename_field(self):
  1387. """
  1388. Tests the RenameField operation.
  1389. """
  1390. project_state = self.set_up_test_model("test_rnfl", unique_together=True, index_together=True)
  1391. # Test the state alteration
  1392. operation = migrations.RenameField("Pony", "pink", "blue")
  1393. self.assertEqual(operation.describe(), "Rename field pink on Pony to blue")
  1394. new_state = project_state.clone()
  1395. operation.state_forwards("test_rnfl", new_state)
  1396. self.assertIn("blue", [n for n, f in new_state.models["test_rnfl", "pony"].fields])
  1397. self.assertNotIn("pink", [n for n, f in new_state.models["test_rnfl", "pony"].fields])
  1398. # Make sure the unique_together has the renamed column too
  1399. self.assertIn("blue", new_state.models["test_rnfl", "pony"].options['unique_together'][0])
  1400. self.assertNotIn("pink", new_state.models["test_rnfl", "pony"].options['unique_together'][0])
  1401. # Make sure the index_together has the renamed column too
  1402. self.assertIn("blue", new_state.models["test_rnfl", "pony"].options['index_together'][0])
  1403. self.assertNotIn("pink", new_state.models["test_rnfl", "pony"].options['index_together'][0])
  1404. # Test the database alteration
  1405. self.assertColumnExists("test_rnfl_pony", "pink")
  1406. self.assertColumnNotExists("test_rnfl_pony", "blue")
  1407. with connection.schema_editor() as editor:
  1408. operation.database_forwards("test_rnfl", editor, project_state, new_state)
  1409. self.assertColumnExists("test_rnfl_pony", "blue")
  1410. self.assertColumnNotExists("test_rnfl_pony", "pink")
  1411. # Ensure the unique constraint has been ported over
  1412. with connection.cursor() as cursor:
  1413. cursor.execute("INSERT INTO test_rnfl_pony (blue, weight) VALUES (1, 1)")
  1414. with self.assertRaises(IntegrityError):
  1415. with atomic():
  1416. cursor.execute("INSERT INTO test_rnfl_pony (blue, weight) VALUES (1, 1)")
  1417. cursor.execute("DELETE FROM test_rnfl_pony")
  1418. # Ensure the index constraint has been ported over
  1419. self.assertIndexExists("test_rnfl_pony", ["weight", "blue"])
  1420. # And test reversal
  1421. with connection.schema_editor() as editor:
  1422. operation.database_backwards("test_rnfl", editor, new_state, project_state)
  1423. self.assertColumnExists("test_rnfl_pony", "pink")
  1424. self.assertColumnNotExists("test_rnfl_pony", "blue")
  1425. # Ensure the index constraint has been reset
  1426. self.assertIndexExists("test_rnfl_pony", ["weight", "pink"])
  1427. # And deconstruction
  1428. definition = operation.deconstruct()
  1429. self.assertEqual(definition[0], "RenameField")
  1430. self.assertEqual(definition[1], [])
  1431. self.assertEqual(definition[2], {'model_name': "Pony", 'old_name': "pink", 'new_name': "blue"})
  1432. def test_rename_missing_field(self):
  1433. state = ProjectState()
  1434. state.add_model(ModelState('app', 'model', []))
  1435. with self.assertRaisesMessage(FieldDoesNotExist, "app.model has no field named 'field'"):
  1436. migrations.RenameField('model', 'field', 'new_field').state_forwards('app', state)
  1437. def test_rename_referenced_field_state_forward(self):
  1438. state = ProjectState()
  1439. state.add_model(ModelState('app', 'Model', [
  1440. ('id', models.AutoField(primary_key=True)),
  1441. ('field', models.IntegerField(unique=True)),
  1442. ]))
  1443. state.add_model(ModelState('app', 'OtherModel', [
  1444. ('id', models.AutoField(primary_key=True)),
  1445. ('fk', models.ForeignKey('Model', models.CASCADE, to_field='field')),
  1446. ('fo', models.ForeignObject('Model', models.CASCADE, from_fields=('fk',), to_fields=('field',))),
  1447. ]))
  1448. operation = migrations.RenameField('Model', 'field', 'renamed')
  1449. new_state = state.clone()
  1450. operation.state_forwards('app', new_state)
  1451. self.assertEqual(new_state.models['app', 'othermodel'].fields[1][1].remote_field.field_name, 'renamed')
  1452. self.assertEqual(new_state.models['app', 'othermodel'].fields[1][1].from_fields, ['self'])
  1453. self.assertEqual(new_state.models['app', 'othermodel'].fields[1][1].to_fields, ('renamed',))
  1454. self.assertEqual(new_state.models['app', 'othermodel'].fields[2][1].from_fields, ('fk',))
  1455. self.assertEqual(new_state.models['app', 'othermodel'].fields[2][1].to_fields, ('renamed',))
  1456. operation = migrations.RenameField('OtherModel', 'fk', 'renamed_fk')
  1457. new_state = state.clone()
  1458. operation.state_forwards('app', new_state)
  1459. self.assertEqual(new_state.models['app', 'othermodel'].fields[1][1].remote_field.field_name, 'renamed')
  1460. self.assertEqual(new_state.models['app', 'othermodel'].fields[1][1].from_fields, ('self',))
  1461. self.assertEqual(new_state.models['app', 'othermodel'].fields[1][1].to_fields, ('renamed',))
  1462. self.assertEqual(new_state.models['app', 'othermodel'].fields[2][1].from_fields, ('renamed_fk',))
  1463. self.assertEqual(new_state.models['app', 'othermodel'].fields[2][1].to_fields, ('renamed',))
  1464. def test_alter_unique_together(self):
  1465. """
  1466. Tests the AlterUniqueTogether operation.
  1467. """
  1468. project_state = self.set_up_test_model("test_alunto")
  1469. # Test the state alteration
  1470. operation = migrations.AlterUniqueTogether("Pony", [("pink", "weight")])
  1471. self.assertEqual(operation.describe(), "Alter unique_together for Pony (1 constraint(s))")
  1472. new_state = project_state.clone()
  1473. operation.state_forwards("test_alunto", new_state)
  1474. self.assertEqual(len(project_state.models["test_alunto", "pony"].options.get("unique_together", set())), 0)
  1475. self.assertEqual(len(new_state.models["test_alunto", "pony"].options.get("unique_together", set())), 1)
  1476. # Make sure we can insert duplicate rows
  1477. with connection.cursor() as cursor:
  1478. cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
  1479. cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
  1480. cursor.execute("DELETE FROM test_alunto_pony")
  1481. # Test the database alteration
  1482. with connection.schema_editor() as editor:
  1483. operation.database_forwards("test_alunto", editor, project_state, new_state)
  1484. cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
  1485. with self.assertRaises(IntegrityError):
  1486. with atomic():
  1487. cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
  1488. cursor.execute("DELETE FROM test_alunto_pony")
  1489. # And test reversal
  1490. with connection.schema_editor() as editor:
  1491. operation.database_backwards("test_alunto", editor, new_state, project_state)
  1492. cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
  1493. cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
  1494. cursor.execute("DELETE FROM test_alunto_pony")
  1495. # Test flat unique_together
  1496. operation = migrations.AlterUniqueTogether("Pony", ("pink", "weight"))
  1497. operation.state_forwards("test_alunto", new_state)
  1498. self.assertEqual(len(new_state.models["test_alunto", "pony"].options.get("unique_together", set())), 1)
  1499. # And deconstruction
  1500. definition = operation.deconstruct()
  1501. self.assertEqual(definition[0], "AlterUniqueTogether")
  1502. self.assertEqual(definition[1], [])
  1503. self.assertEqual(definition[2], {'name': "Pony", 'unique_together': {("pink", "weight")}})
  1504. def test_alter_unique_together_remove(self):
  1505. operation = migrations.AlterUniqueTogether("Pony", None)
  1506. self.assertEqual(operation.describe(), "Alter unique_together for Pony (0 constraint(s))")
  1507. def test_add_index(self):
  1508. """
  1509. Test the AddIndex operation.
  1510. """
  1511. project_state = self.set_up_test_model("test_adin")
  1512. msg = (
  1513. "Indexes passed to AddIndex operations require a name argument. "
  1514. "<Index: fields='pink'> doesn't have one."
  1515. )
  1516. with self.assertRaisesMessage(ValueError, msg):
  1517. migrations.AddIndex("Pony", models.Index(fields=["pink"]))
  1518. index = models.Index(fields=["pink"], name="test_adin_pony_pink_idx")
  1519. operation = migrations.AddIndex("Pony", index)
  1520. self.assertEqual(operation.describe(), "Create index test_adin_pony_pink_idx on field(s) pink of model Pony")
  1521. new_state = project_state.clone()
  1522. operation.state_forwards("test_adin", new_state)
  1523. # Test the database alteration
  1524. self.assertEqual(len(new_state.models["test_adin", "pony"].options['indexes']), 1)
  1525. self.assertIndexNotExists("test_adin_pony", ["pink"])
  1526. with connection.schema_editor() as editor:
  1527. operation.database_forwards("test_adin", editor, project_state, new_state)
  1528. self.assertIndexExists("test_adin_pony", ["pink"])
  1529. # And test reversal
  1530. with connection.schema_editor() as editor:
  1531. operation.database_backwards("test_adin", editor, new_state, project_state)
  1532. self.assertIndexNotExists("test_adin_pony", ["pink"])
  1533. # And deconstruction
  1534. definition = operation.deconstruct()
  1535. self.assertEqual(definition[0], "AddIndex")
  1536. self.assertEqual(definition[1], [])
  1537. self.assertEqual(definition[2], {'model_name': "Pony", 'index': index})
  1538. def test_remove_index(self):
  1539. """
  1540. Test the RemoveIndex operation.
  1541. """
  1542. project_state = self.set_up_test_model("test_rmin", multicol_index=True)
  1543. self.assertTableExists("test_rmin_pony")
  1544. self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
  1545. operation = migrations.RemoveIndex("Pony", "pony_test_idx")
  1546. self.assertEqual(operation.describe(), "Remove index pony_test_idx from Pony")
  1547. new_state = project_state.clone()
  1548. operation.state_forwards("test_rmin", new_state)
  1549. # Test the state alteration
  1550. self.assertEqual(len(new_state.models["test_rmin", "pony"].options['indexes']), 0)
  1551. self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
  1552. # Test the database alteration
  1553. with connection.schema_editor() as editor:
  1554. operation.database_forwards("test_rmin", editor, project_state, new_state)
  1555. self.assertIndexNotExists("test_rmin_pony", ["pink", "weight"])
  1556. # And test reversal
  1557. with connection.schema_editor() as editor:
  1558. operation.database_backwards("test_rmin", editor, new_state, project_state)
  1559. self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
  1560. # And deconstruction
  1561. definition = operation.deconstruct()
  1562. self.assertEqual(definition[0], "RemoveIndex")
  1563. self.assertEqual(definition[1], [])
  1564. self.assertEqual(definition[2], {'model_name': "Pony", 'name': "pony_test_idx"})
  1565. # Also test a field dropped with index - sqlite remake issue
  1566. operations = [
  1567. migrations.RemoveIndex("Pony", "pony_test_idx"),
  1568. migrations.RemoveField("Pony", "pink"),
  1569. ]
  1570. self.assertColumnExists("test_rmin_pony", "pink")
  1571. self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
  1572. # Test database alteration
  1573. new_state = project_state.clone()
  1574. self.apply_operations('test_rmin', new_state, operations=operations)
  1575. self.assertColumnNotExists("test_rmin_pony", "pink")
  1576. self.assertIndexNotExists("test_rmin_pony", ["pink", "weight"])
  1577. # And test reversal
  1578. self.unapply_operations("test_rmin", project_state, operations=operations)
  1579. self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
  1580. def test_add_index_state_forwards(self):
  1581. project_state = self.set_up_test_model('test_adinsf')
  1582. index = models.Index(fields=['pink'], name='test_adinsf_pony_pink_idx')
  1583. old_model = project_state.apps.get_model('test_adinsf', 'Pony')
  1584. new_state = project_state.clone()
  1585. operation = migrations.AddIndex('Pony', index)
  1586. operation.state_forwards('test_adinsf', new_state)
  1587. new_model = new_state.apps.get_model('test_adinsf', 'Pony')
  1588. self.assertIsNot(old_model, new_model)
  1589. def test_remove_index_state_forwards(self):
  1590. project_state = self.set_up_test_model('test_rminsf')
  1591. index = models.Index(fields=['pink'], name='test_rminsf_pony_pink_idx')
  1592. migrations.AddIndex('Pony', index).state_forwards('test_rminsf', project_state)
  1593. old_model = project_state.apps.get_model('test_rminsf', 'Pony')
  1594. new_state = project_state.clone()
  1595. operation = migrations.RemoveIndex('Pony', 'test_rminsf_pony_pink_idx')
  1596. operation.state_forwards('test_rminsf', new_state)
  1597. new_model = new_state.apps.get_model('test_rminsf', 'Pony')
  1598. self.assertIsNot(old_model, new_model)
  1599. def test_alter_field_with_index(self):
  1600. """
  1601. Test AlterField operation with an index to ensure indexes created via
  1602. Meta.indexes don't get dropped with sqlite3 remake.
  1603. """
  1604. project_state = self.set_up_test_model("test_alflin", index=True)
  1605. operation = migrations.AlterField("Pony", "pink", models.IntegerField(null=True))
  1606. new_state = project_state.clone()
  1607. operation.state_forwards("test_alflin", new_state)
  1608. # Test the database alteration
  1609. self.assertColumnNotNull("test_alflin_pony", "pink")
  1610. with connection.schema_editor() as editor:
  1611. operation.database_forwards("test_alflin", editor, project_state, new_state)
  1612. # Index hasn't been dropped
  1613. self.assertIndexExists("test_alflin_pony", ["pink"])
  1614. # And test reversal
  1615. with connection.schema_editor() as editor:
  1616. operation.database_backwards("test_alflin", editor, new_state, project_state)
  1617. # Ensure the index is still there
  1618. self.assertIndexExists("test_alflin_pony", ["pink"])
  1619. def test_alter_index_together(self):
  1620. """
  1621. Tests the AlterIndexTogether operation.
  1622. """
  1623. project_state = self.set_up_test_model("test_alinto")
  1624. # Test the state alteration
  1625. operation = migrations.AlterIndexTogether("Pony", [("pink", "weight")])
  1626. self.assertEqual(operation.describe(), "Alter index_together for Pony (1 constraint(s))")
  1627. new_state = project_state.clone()
  1628. operation.state_forwards("test_alinto", new_state)
  1629. self.assertEqual(len(project_state.models["test_alinto", "pony"].options.get("index_together", set())), 0)
  1630. self.assertEqual(len(new_state.models["test_alinto", "pony"].options.get("index_together", set())), 1)
  1631. # Make sure there's no matching index
  1632. self.assertIndexNotExists("test_alinto_pony", ["pink", "weight"])
  1633. # Test the database alteration
  1634. with connection.schema_editor() as editor:
  1635. operation.database_forwards("test_alinto", editor, project_state, new_state)
  1636. self.assertIndexExists("test_alinto_pony", ["pink", "weight"])
  1637. # And test reversal
  1638. with connection.schema_editor() as editor:
  1639. operation.database_backwards("test_alinto", editor, new_state, project_state)
  1640. self.assertIndexNotExists("test_alinto_pony", ["pink", "weight"])
  1641. # And deconstruction
  1642. definition = operation.deconstruct()
  1643. self.assertEqual(definition[0], "AlterIndexTogether")
  1644. self.assertEqual(definition[1], [])
  1645. self.assertEqual(definition[2], {'name': "Pony", 'index_together': {("pink", "weight")}})
  1646. def test_alter_index_together_remove(self):
  1647. operation = migrations.AlterIndexTogether("Pony", None)
  1648. self.assertEqual(operation.describe(), "Alter index_together for Pony (0 constraint(s))")
  1649. @skipUnlessDBFeature('supports_table_check_constraints')
  1650. def test_add_constraint(self):
  1651. """Test the AddConstraint operation."""
  1652. project_state = self.set_up_test_model('test_addconstraint')
  1653. where = models.Q(pink__gt=2)
  1654. check_constraint = models.CheckConstraint(where, name='test_constraint_pony_pink_gt_2')
  1655. operation = migrations.AddConstraint('Pony', check_constraint)
  1656. self.assertEqual(operation.describe(), 'Create constraint test_constraint_pony_pink_gt_2 on model Pony')
  1657. new_state = project_state.clone()
  1658. operation.state_forwards('test_addconstraint', new_state)
  1659. self.assertEqual(len(new_state.models['test_addconstraint', 'pony'].options['constraints']), 1)
  1660. # Test database alteration
  1661. with connection.cursor() as cursor:
  1662. with atomic():
  1663. cursor.execute("INSERT INTO test_addconstraint_pony (id, pink, weight) VALUES (1, 1, 1.0)")
  1664. cursor.execute("DELETE FROM test_addconstraint_pony")
  1665. with connection.schema_editor() as editor:
  1666. operation.database_forwards("test_addconstraint", editor, project_state, new_state)
  1667. with connection.cursor() as cursor:
  1668. with self.assertRaises(IntegrityError):
  1669. cursor.execute("INSERT INTO test_addconstraint_pony (id, pink, weight) VALUES (1, 1, 1.0)")
  1670. # Test reversal
  1671. with connection.schema_editor() as editor:
  1672. operation.database_backwards("test_addconstraint", editor, new_state, project_state)
  1673. with connection.cursor() as cursor:
  1674. with atomic():
  1675. cursor.execute("INSERT INTO test_addconstraint_pony (id, pink, weight) VALUES (1, 1, 1.0)")
  1676. cursor.execute("DELETE FROM test_addconstraint_pony")
  1677. # Test deconstruction
  1678. definition = operation.deconstruct()
  1679. self.assertEqual(definition[0], "AddConstraint")
  1680. self.assertEqual(definition[1], [])
  1681. self.assertEqual(definition[2], {'model_name': "Pony", 'constraint': check_constraint})
  1682. @skipUnlessDBFeature('supports_table_check_constraints')
  1683. def test_remove_constraint(self):
  1684. """Test the RemoveConstraint operation."""
  1685. project_state = self.set_up_test_model("test_removeconstraint", check_constraint=True)
  1686. self.assertTableExists("test_removeconstraint_pony")
  1687. operation = migrations.RemoveConstraint("Pony", "pony_test_constraint")
  1688. self.assertEqual(operation.describe(), "Remove constraint pony_test_constraint from model Pony")
  1689. new_state = project_state.clone()
  1690. operation.state_forwards("test_removeconstraint", new_state)
  1691. # Test state alteration
  1692. self.assertEqual(len(new_state.models["test_removeconstraint", "pony"].options['constraints']), 0)
  1693. with connection.cursor() as cursor:
  1694. with self.assertRaises(IntegrityError):
  1695. cursor.execute("INSERT INTO test_removeconstraint_pony (id, pink, weight) VALUES (1, 1, 1.0)")
  1696. # Test database alteration
  1697. with connection.schema_editor() as editor:
  1698. operation.database_forwards("test_removeconstraint", editor, project_state, new_state)
  1699. with connection.cursor() as cursor:
  1700. with atomic():
  1701. cursor.execute("INSERT INTO test_removeconstraint_pony (id, pink, weight) VALUES (1, 1, 1.0)")
  1702. cursor.execute("DELETE FROM test_removeconstraint_pony")
  1703. # Test reversal
  1704. with connection.schema_editor() as editor:
  1705. operation.database_backwards("test_removeconstraint", editor, new_state, project_state)
  1706. with connection.cursor() as cursor:
  1707. with self.assertRaises(IntegrityError):
  1708. cursor.execute("INSERT INTO test_removeconstraint_pony (id, pink, weight) VALUES (1, 1, 1.0)")
  1709. # Test deconstruction
  1710. definition = operation.deconstruct()
  1711. self.assertEqual(definition[0], "RemoveConstraint")
  1712. self.assertEqual(definition[1], [])
  1713. self.assertEqual(definition[2], {'model_name': "Pony", 'name': "pony_test_constraint"})
  1714. def test_alter_model_options(self):
  1715. """
  1716. Tests the AlterModelOptions operation.
  1717. """
  1718. project_state = self.set_up_test_model("test_almoop")
  1719. # Test the state alteration (no DB alteration to test)
  1720. operation = migrations.AlterModelOptions("Pony", {"permissions": [("can_groom", "Can groom")]})
  1721. self.assertEqual(operation.describe(), "Change Meta options on Pony")
  1722. new_state = project_state.clone()
  1723. operation.state_forwards("test_almoop", new_state)
  1724. self.assertEqual(len(project_state.models["test_almoop", "pony"].options.get("permissions", [])), 0)
  1725. self.assertEqual(len(new_state.models["test_almoop", "pony"].options.get("permissions", [])), 1)
  1726. self.assertEqual(new_state.models["test_almoop", "pony"].options["permissions"][0][0], "can_groom")
  1727. # And deconstruction
  1728. definition = operation.deconstruct()
  1729. self.assertEqual(definition[0], "AlterModelOptions")
  1730. self.assertEqual(definition[1], [])
  1731. self.assertEqual(definition[2], {'name': "Pony", 'options': {"permissions": [("can_groom", "Can groom")]}})
  1732. def test_alter_model_options_emptying(self):
  1733. """
  1734. The AlterModelOptions operation removes keys from the dict (#23121)
  1735. """
  1736. project_state = self.set_up_test_model("test_almoop", options=True)
  1737. # Test the state alteration (no DB alteration to test)
  1738. operation = migrations.AlterModelOptions("Pony", {})
  1739. self.assertEqual(operation.describe(), "Change Meta options on Pony")
  1740. new_state = project_state.clone()
  1741. operation.state_forwards("test_almoop", new_state)
  1742. self.assertEqual(len(project_state.models["test_almoop", "pony"].options.get("permissions", [])), 1)
  1743. self.assertEqual(len(new_state.models["test_almoop", "pony"].options.get("permissions", [])), 0)
  1744. # And deconstruction
  1745. definition = operation.deconstruct()
  1746. self.assertEqual(definition[0], "AlterModelOptions")
  1747. self.assertEqual(definition[1], [])
  1748. self.assertEqual(definition[2], {'name': "Pony", 'options': {}})
  1749. def test_alter_order_with_respect_to(self):
  1750. """
  1751. Tests the AlterOrderWithRespectTo operation.
  1752. """
  1753. project_state = self.set_up_test_model("test_alorwrtto", related_model=True)
  1754. # Test the state alteration
  1755. operation = migrations.AlterOrderWithRespectTo("Rider", "pony")
  1756. self.assertEqual(operation.describe(), "Set order_with_respect_to on Rider to pony")
  1757. new_state = project_state.clone()
  1758. operation.state_forwards("test_alorwrtto", new_state)
  1759. self.assertIsNone(
  1760. project_state.models["test_alorwrtto", "rider"].options.get("order_with_respect_to", None)
  1761. )
  1762. self.assertEqual(
  1763. new_state.models["test_alorwrtto", "rider"].options.get("order_with_respect_to", None),
  1764. "pony"
  1765. )
  1766. # Make sure there's no matching index
  1767. self.assertColumnNotExists("test_alorwrtto_rider", "_order")
  1768. # Create some rows before alteration
  1769. rendered_state = project_state.apps
  1770. pony = rendered_state.get_model("test_alorwrtto", "Pony").objects.create(weight=50)
  1771. rendered_state.get_model("test_alorwrtto", "Rider").objects.create(pony=pony, friend_id=1)
  1772. rendered_state.get_model("test_alorwrtto", "Rider").objects.create(pony=pony, friend_id=2)
  1773. # Test the database alteration
  1774. with connection.schema_editor() as editor:
  1775. operation.database_forwards("test_alorwrtto", editor, project_state, new_state)
  1776. self.assertColumnExists("test_alorwrtto_rider", "_order")
  1777. # Check for correct value in rows
  1778. updated_riders = new_state.apps.get_model("test_alorwrtto", "Rider").objects.all()
  1779. self.assertEqual(updated_riders[0]._order, 0)
  1780. self.assertEqual(updated_riders[1]._order, 0)
  1781. # And test reversal
  1782. with connection.schema_editor() as editor:
  1783. operation.database_backwards("test_alorwrtto", editor, new_state, project_state)
  1784. self.assertColumnNotExists("test_alorwrtto_rider", "_order")
  1785. # And deconstruction
  1786. definition = operation.deconstruct()
  1787. self.assertEqual(definition[0], "AlterOrderWithRespectTo")
  1788. self.assertEqual(definition[1], [])
  1789. self.assertEqual(definition[2], {'name': "Rider", 'order_with_respect_to': "pony"})
  1790. def test_alter_model_managers(self):
  1791. """
  1792. The managers on a model are set.
  1793. """
  1794. project_state = self.set_up_test_model("test_almoma")
  1795. # Test the state alteration
  1796. operation = migrations.AlterModelManagers(
  1797. "Pony",
  1798. managers=[
  1799. ("food_qs", FoodQuerySet.as_manager()),
  1800. ("food_mgr", FoodManager("a", "b")),
  1801. ("food_mgr_kwargs", FoodManager("x", "y", 3, 4)),
  1802. ]
  1803. )
  1804. self.assertEqual(operation.describe(), "Change managers on Pony")
  1805. managers = project_state.models["test_almoma", "pony"].managers
  1806. self.assertEqual(managers, [])
  1807. new_state = project_state.clone()
  1808. operation.state_forwards("test_almoma", new_state)
  1809. self.assertIn(("test_almoma", "pony"), new_state.models)
  1810. managers = new_state.models["test_almoma", "pony"].managers
  1811. self.assertEqual(managers[0][0], "food_qs")
  1812. self.assertIsInstance(managers[0][1], models.Manager)
  1813. self.assertEqual(managers[1][0], "food_mgr")
  1814. self.assertIsInstance(managers[1][1], FoodManager)
  1815. self.assertEqual(managers[1][1].args, ("a", "b", 1, 2))
  1816. self.assertEqual(managers[2][0], "food_mgr_kwargs")
  1817. self.assertIsInstance(managers[2][1], FoodManager)
  1818. self.assertEqual(managers[2][1].args, ("x", "y", 3, 4))
  1819. rendered_state = new_state.apps
  1820. model = rendered_state.get_model('test_almoma', 'pony')
  1821. self.assertIsInstance(model.food_qs, models.Manager)
  1822. self.assertIsInstance(model.food_mgr, FoodManager)
  1823. self.assertIsInstance(model.food_mgr_kwargs, FoodManager)
  1824. def test_alter_model_managers_emptying(self):
  1825. """
  1826. The managers on a model are set.
  1827. """
  1828. project_state = self.set_up_test_model("test_almomae", manager_model=True)
  1829. # Test the state alteration
  1830. operation = migrations.AlterModelManagers("Food", managers=[])
  1831. self.assertEqual(operation.describe(), "Change managers on Food")
  1832. self.assertIn(("test_almomae", "food"), project_state.models)
  1833. managers = project_state.models["test_almomae", "food"].managers
  1834. self.assertEqual(managers[0][0], "food_qs")
  1835. self.assertIsInstance(managers[0][1], models.Manager)
  1836. self.assertEqual(managers[1][0], "food_mgr")
  1837. self.assertIsInstance(managers[1][1], FoodManager)
  1838. self.assertEqual(managers[1][1].args, ("a", "b", 1, 2))
  1839. self.assertEqual(managers[2][0], "food_mgr_kwargs")
  1840. self.assertIsInstance(managers[2][1], FoodManager)
  1841. self.assertEqual(managers[2][1].args, ("x", "y", 3, 4))
  1842. new_state = project_state.clone()
  1843. operation.state_forwards("test_almomae", new_state)
  1844. managers = new_state.models["test_almomae", "food"].managers
  1845. self.assertEqual(managers, [])
  1846. def test_alter_fk(self):
  1847. """
  1848. Creating and then altering an FK works correctly
  1849. and deals with the pending SQL (#23091)
  1850. """
  1851. project_state = self.set_up_test_model("test_alfk")
  1852. # Test adding and then altering the FK in one go
  1853. create_operation = migrations.CreateModel(
  1854. name="Rider",
  1855. fields=[
  1856. ("id", models.AutoField(primary_key=True)),
  1857. ("pony", models.ForeignKey("Pony", models.CASCADE)),
  1858. ],
  1859. )
  1860. create_state = project_state.clone()
  1861. create_operation.state_forwards("test_alfk", create_state)
  1862. alter_operation = migrations.AlterField(
  1863. model_name='Rider',
  1864. name='pony',
  1865. field=models.ForeignKey("Pony", models.CASCADE, editable=False),
  1866. )
  1867. alter_state = create_state.clone()
  1868. alter_operation.state_forwards("test_alfk", alter_state)
  1869. with connection.schema_editor() as editor:
  1870. create_operation.database_forwards("test_alfk", editor, project_state, create_state)
  1871. alter_operation.database_forwards("test_alfk", editor, create_state, alter_state)
  1872. def test_alter_fk_non_fk(self):
  1873. """
  1874. Altering an FK to a non-FK works (#23244)
  1875. """
  1876. # Test the state alteration
  1877. operation = migrations.AlterField(
  1878. model_name="Rider",
  1879. name="pony",
  1880. field=models.FloatField(),
  1881. )
  1882. project_state, new_state = self.make_test_state("test_afknfk", operation, related_model=True)
  1883. # Test the database alteration
  1884. self.assertColumnExists("test_afknfk_rider", "pony_id")
  1885. self.assertColumnNotExists("test_afknfk_rider", "pony")
  1886. with connection.schema_editor() as editor:
  1887. operation.database_forwards("test_afknfk", editor, project_state, new_state)
  1888. self.assertColumnExists("test_afknfk_rider", "pony")
  1889. self.assertColumnNotExists("test_afknfk_rider", "pony_id")
  1890. # And test reversal
  1891. with connection.schema_editor() as editor:
  1892. operation.database_backwards("test_afknfk", editor, new_state, project_state)
  1893. self.assertColumnExists("test_afknfk_rider", "pony_id")
  1894. self.assertColumnNotExists("test_afknfk_rider", "pony")
  1895. @unittest.skipIf(sqlparse is None and connection.features.requires_sqlparse_for_splitting, "Missing sqlparse")
  1896. def test_run_sql(self):
  1897. """
  1898. Tests the RunSQL operation.
  1899. """
  1900. project_state = self.set_up_test_model("test_runsql")
  1901. # Create the operation
  1902. operation = migrations.RunSQL(
  1903. # Use a multi-line string with a comment to test splitting on SQLite and MySQL respectively
  1904. "CREATE TABLE i_love_ponies (id int, special_thing varchar(15));\n"
  1905. "INSERT INTO i_love_ponies (id, special_thing) VALUES (1, 'i love ponies'); -- this is magic!\n"
  1906. "INSERT INTO i_love_ponies (id, special_thing) VALUES (2, 'i love django');\n"
  1907. "UPDATE i_love_ponies SET special_thing = 'Ponies' WHERE special_thing LIKE '%%ponies';"
  1908. "UPDATE i_love_ponies SET special_thing = 'Django' WHERE special_thing LIKE '%django';",
  1909. # Run delete queries to test for parameter substitution failure
  1910. # reported in #23426
  1911. "DELETE FROM i_love_ponies WHERE special_thing LIKE '%Django%';"
  1912. "DELETE FROM i_love_ponies WHERE special_thing LIKE '%%Ponies%%';"
  1913. "DROP TABLE i_love_ponies",
  1914. state_operations=[migrations.CreateModel("SomethingElse", [("id", models.AutoField(primary_key=True))])],
  1915. )
  1916. self.assertEqual(operation.describe(), "Raw SQL operation")
  1917. # Test the state alteration
  1918. new_state = project_state.clone()
  1919. operation.state_forwards("test_runsql", new_state)
  1920. self.assertEqual(len(new_state.models["test_runsql", "somethingelse"].fields), 1)
  1921. # Make sure there's no table
  1922. self.assertTableNotExists("i_love_ponies")
  1923. # Test SQL collection
  1924. with connection.schema_editor(collect_sql=True) as editor:
  1925. operation.database_forwards("test_runsql", editor, project_state, new_state)
  1926. self.assertIn("LIKE '%%ponies';", "\n".join(editor.collected_sql))
  1927. operation.database_backwards("test_runsql", editor, project_state, new_state)
  1928. self.assertIn("LIKE '%%Ponies%%';", "\n".join(editor.collected_sql))
  1929. # Test the database alteration
  1930. with connection.schema_editor() as editor:
  1931. operation.database_forwards("test_runsql", editor, project_state, new_state)
  1932. self.assertTableExists("i_love_ponies")
  1933. # Make sure all the SQL was processed
  1934. with connection.cursor() as cursor:
  1935. cursor.execute("SELECT COUNT(*) FROM i_love_ponies")
  1936. self.assertEqual(cursor.fetchall()[0][0], 2)
  1937. cursor.execute("SELECT COUNT(*) FROM i_love_ponies WHERE special_thing = 'Django'")
  1938. self.assertEqual(cursor.fetchall()[0][0], 1)
  1939. cursor.execute("SELECT COUNT(*) FROM i_love_ponies WHERE special_thing = 'Ponies'")
  1940. self.assertEqual(cursor.fetchall()[0][0], 1)
  1941. # And test reversal
  1942. self.assertTrue(operation.reversible)
  1943. with connection.schema_editor() as editor:
  1944. operation.database_backwards("test_runsql", editor, new_state, project_state)
  1945. self.assertTableNotExists("i_love_ponies")
  1946. # And deconstruction
  1947. definition = operation.deconstruct()
  1948. self.assertEqual(definition[0], "RunSQL")
  1949. self.assertEqual(definition[1], [])
  1950. self.assertEqual(sorted(definition[2]), ["reverse_sql", "sql", "state_operations"])
  1951. # And elidable reduction
  1952. self.assertIs(False, operation.reduce(operation, []))
  1953. elidable_operation = migrations.RunSQL('SELECT 1 FROM void;', elidable=True)
  1954. self.assertEqual(elidable_operation.reduce(operation, []), [operation])
  1955. def test_run_sql_params(self):
  1956. """
  1957. #23426 - RunSQL should accept parameters.
  1958. """
  1959. project_state = self.set_up_test_model("test_runsql")
  1960. # Create the operation
  1961. operation = migrations.RunSQL(
  1962. ["CREATE TABLE i_love_ponies (id int, special_thing varchar(15));"],
  1963. ["DROP TABLE i_love_ponies"],
  1964. )
  1965. param_operation = migrations.RunSQL(
  1966. # forwards
  1967. (
  1968. "INSERT INTO i_love_ponies (id, special_thing) VALUES (1, 'Django');",
  1969. ["INSERT INTO i_love_ponies (id, special_thing) VALUES (2, %s);", ['Ponies']],
  1970. ("INSERT INTO i_love_ponies (id, special_thing) VALUES (%s, %s);", (3, 'Python',)),
  1971. ),
  1972. # backwards
  1973. [
  1974. "DELETE FROM i_love_ponies WHERE special_thing = 'Django';",
  1975. ["DELETE FROM i_love_ponies WHERE special_thing = 'Ponies';", None],
  1976. ("DELETE FROM i_love_ponies WHERE id = %s OR special_thing = %s;", [3, 'Python']),
  1977. ]
  1978. )
  1979. # Make sure there's no table
  1980. self.assertTableNotExists("i_love_ponies")
  1981. new_state = project_state.clone()
  1982. # Test the database alteration
  1983. with connection.schema_editor() as editor:
  1984. operation.database_forwards("test_runsql", editor, project_state, new_state)
  1985. # Test parameter passing
  1986. with connection.schema_editor() as editor:
  1987. param_operation.database_forwards("test_runsql", editor, project_state, new_state)
  1988. # Make sure all the SQL was processed
  1989. with connection.cursor() as cursor:
  1990. cursor.execute("SELECT COUNT(*) FROM i_love_ponies")
  1991. self.assertEqual(cursor.fetchall()[0][0], 3)
  1992. with connection.schema_editor() as editor:
  1993. param_operation.database_backwards("test_runsql", editor, new_state, project_state)
  1994. with connection.cursor() as cursor:
  1995. cursor.execute("SELECT COUNT(*) FROM i_love_ponies")
  1996. self.assertEqual(cursor.fetchall()[0][0], 0)
  1997. # And test reversal
  1998. with connection.schema_editor() as editor:
  1999. operation.database_backwards("test_runsql", editor, new_state, project_state)
  2000. self.assertTableNotExists("i_love_ponies")
  2001. def test_run_sql_params_invalid(self):
  2002. """
  2003. #23426 - RunSQL should fail when a list of statements with an incorrect
  2004. number of tuples is given.
  2005. """
  2006. project_state = self.set_up_test_model("test_runsql")
  2007. new_state = project_state.clone()
  2008. operation = migrations.RunSQL(
  2009. # forwards
  2010. [
  2011. ["INSERT INTO foo (bar) VALUES ('buz');"]
  2012. ],
  2013. # backwards
  2014. (
  2015. ("DELETE FROM foo WHERE bar = 'buz';", 'invalid', 'parameter count'),
  2016. ),
  2017. )
  2018. with connection.schema_editor() as editor:
  2019. with self.assertRaisesMessage(ValueError, "Expected a 2-tuple but got 1"):
  2020. operation.database_forwards("test_runsql", editor, project_state, new_state)
  2021. with connection.schema_editor() as editor:
  2022. with self.assertRaisesMessage(ValueError, "Expected a 2-tuple but got 3"):
  2023. operation.database_backwards("test_runsql", editor, new_state, project_state)
  2024. def test_run_sql_noop(self):
  2025. """
  2026. #24098 - Tests no-op RunSQL operations.
  2027. """
  2028. operation = migrations.RunSQL(migrations.RunSQL.noop, migrations.RunSQL.noop)
  2029. with connection.schema_editor() as editor:
  2030. operation.database_forwards("test_runsql", editor, None, None)
  2031. operation.database_backwards("test_runsql", editor, None, None)
  2032. def test_run_python(self):
  2033. """
  2034. Tests the RunPython operation
  2035. """
  2036. project_state = self.set_up_test_model("test_runpython", mti_model=True)
  2037. # Create the operation
  2038. def inner_method(models, schema_editor):
  2039. Pony = models.get_model("test_runpython", "Pony")
  2040. Pony.objects.create(pink=1, weight=3.55)
  2041. Pony.objects.create(weight=5)
  2042. def inner_method_reverse(models, schema_editor):
  2043. Pony = models.get_model("test_runpython", "Pony")
  2044. Pony.objects.filter(pink=1, weight=3.55).delete()
  2045. Pony.objects.filter(weight=5).delete()
  2046. operation = migrations.RunPython(inner_method, reverse_code=inner_method_reverse)
  2047. self.assertEqual(operation.describe(), "Raw Python operation")
  2048. # Test the state alteration does nothing
  2049. new_state = project_state.clone()
  2050. operation.state_forwards("test_runpython", new_state)
  2051. self.assertEqual(new_state, project_state)
  2052. # Test the database alteration
  2053. self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 0)
  2054. with connection.schema_editor() as editor:
  2055. operation.database_forwards("test_runpython", editor, project_state, new_state)
  2056. self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 2)
  2057. # Now test reversal
  2058. self.assertTrue(operation.reversible)
  2059. with connection.schema_editor() as editor:
  2060. operation.database_backwards("test_runpython", editor, project_state, new_state)
  2061. self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 0)
  2062. # Now test we can't use a string
  2063. with self.assertRaisesMessage(ValueError, 'RunPython must be supplied with a callable'):
  2064. migrations.RunPython("print 'ahahaha'")
  2065. # And deconstruction
  2066. definition = operation.deconstruct()
  2067. self.assertEqual(definition[0], "RunPython")
  2068. self.assertEqual(definition[1], [])
  2069. self.assertEqual(sorted(definition[2]), ["code", "reverse_code"])
  2070. # Also test reversal fails, with an operation identical to above but without reverse_code set
  2071. no_reverse_operation = migrations.RunPython(inner_method)
  2072. self.assertFalse(no_reverse_operation.reversible)
  2073. with connection.schema_editor() as editor:
  2074. no_reverse_operation.database_forwards("test_runpython", editor, project_state, new_state)
  2075. with self.assertRaises(NotImplementedError):
  2076. no_reverse_operation.database_backwards("test_runpython", editor, new_state, project_state)
  2077. self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 2)
  2078. def create_ponies(models, schema_editor):
  2079. Pony = models.get_model("test_runpython", "Pony")
  2080. pony1 = Pony.objects.create(pink=1, weight=3.55)
  2081. self.assertIsNot(pony1.pk, None)
  2082. pony2 = Pony.objects.create(weight=5)
  2083. self.assertIsNot(pony2.pk, None)
  2084. self.assertNotEqual(pony1.pk, pony2.pk)
  2085. operation = migrations.RunPython(create_ponies)
  2086. with connection.schema_editor() as editor:
  2087. operation.database_forwards("test_runpython", editor, project_state, new_state)
  2088. self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 4)
  2089. # And deconstruction
  2090. definition = operation.deconstruct()
  2091. self.assertEqual(definition[0], "RunPython")
  2092. self.assertEqual(definition[1], [])
  2093. self.assertEqual(sorted(definition[2]), ["code"])
  2094. def create_shetlandponies(models, schema_editor):
  2095. ShetlandPony = models.get_model("test_runpython", "ShetlandPony")
  2096. pony1 = ShetlandPony.objects.create(weight=4.0)
  2097. self.assertIsNot(pony1.pk, None)
  2098. pony2 = ShetlandPony.objects.create(weight=5.0)
  2099. self.assertIsNot(pony2.pk, None)
  2100. self.assertNotEqual(pony1.pk, pony2.pk)
  2101. operation = migrations.RunPython(create_shetlandponies)
  2102. with connection.schema_editor() as editor:
  2103. operation.database_forwards("test_runpython", editor, project_state, new_state)
  2104. self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 6)
  2105. self.assertEqual(project_state.apps.get_model("test_runpython", "ShetlandPony").objects.count(), 2)
  2106. # And elidable reduction
  2107. self.assertIs(False, operation.reduce(operation, []))
  2108. elidable_operation = migrations.RunPython(inner_method, elidable=True)
  2109. self.assertEqual(elidable_operation.reduce(operation, []), [operation])
  2110. def test_run_python_atomic(self):
  2111. """
  2112. Tests the RunPython operation correctly handles the "atomic" keyword
  2113. """
  2114. project_state = self.set_up_test_model("test_runpythonatomic", mti_model=True)
  2115. def inner_method(models, schema_editor):
  2116. Pony = models.get_model("test_runpythonatomic", "Pony")
  2117. Pony.objects.create(pink=1, weight=3.55)
  2118. raise ValueError("Adrian hates ponies.")
  2119. # Verify atomicity when applying.
  2120. atomic_migration = Migration("test", "test_runpythonatomic")
  2121. atomic_migration.operations = [migrations.RunPython(inner_method, reverse_code=inner_method)]
  2122. non_atomic_migration = Migration("test", "test_runpythonatomic")
  2123. non_atomic_migration.operations = [migrations.RunPython(inner_method, reverse_code=inner_method, atomic=False)]
  2124. # If we're a fully-transactional database, both versions should rollback
  2125. if connection.features.can_rollback_ddl:
  2126. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2127. with self.assertRaises(ValueError):
  2128. with connection.schema_editor() as editor:
  2129. atomic_migration.apply(project_state, editor)
  2130. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2131. with self.assertRaises(ValueError):
  2132. with connection.schema_editor() as editor:
  2133. non_atomic_migration.apply(project_state, editor)
  2134. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2135. # Otherwise, the non-atomic operation should leave a row there
  2136. else:
  2137. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2138. with self.assertRaises(ValueError):
  2139. with connection.schema_editor() as editor:
  2140. atomic_migration.apply(project_state, editor)
  2141. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2142. with self.assertRaises(ValueError):
  2143. with connection.schema_editor() as editor:
  2144. non_atomic_migration.apply(project_state, editor)
  2145. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 1)
  2146. # Reset object count to zero and verify atomicity when unapplying.
  2147. project_state.apps.get_model("test_runpythonatomic", "Pony").objects.all().delete()
  2148. # On a fully-transactional database, both versions rollback.
  2149. if connection.features.can_rollback_ddl:
  2150. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2151. with self.assertRaises(ValueError):
  2152. with connection.schema_editor() as editor:
  2153. atomic_migration.unapply(project_state, editor)
  2154. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2155. with self.assertRaises(ValueError):
  2156. with connection.schema_editor() as editor:
  2157. non_atomic_migration.unapply(project_state, editor)
  2158. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2159. # Otherwise, the non-atomic operation leaves a row there.
  2160. else:
  2161. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2162. with self.assertRaises(ValueError):
  2163. with connection.schema_editor() as editor:
  2164. atomic_migration.unapply(project_state, editor)
  2165. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
  2166. with self.assertRaises(ValueError):
  2167. with connection.schema_editor() as editor:
  2168. non_atomic_migration.unapply(project_state, editor)
  2169. self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 1)
  2170. # Verify deconstruction.
  2171. definition = non_atomic_migration.operations[0].deconstruct()
  2172. self.assertEqual(definition[0], "RunPython")
  2173. self.assertEqual(definition[1], [])
  2174. self.assertEqual(sorted(definition[2]), ["atomic", "code", "reverse_code"])
  2175. def test_run_python_related_assignment(self):
  2176. """
  2177. #24282 - Model changes to a FK reverse side update the model
  2178. on the FK side as well.
  2179. """
  2180. def inner_method(models, schema_editor):
  2181. Author = models.get_model("test_authors", "Author")
  2182. Book = models.get_model("test_books", "Book")
  2183. author = Author.objects.create(name="Hemingway")
  2184. Book.objects.create(title="Old Man and The Sea", author=author)
  2185. create_author = migrations.CreateModel(
  2186. "Author",
  2187. [
  2188. ("id", models.AutoField(primary_key=True)),
  2189. ("name", models.CharField(max_length=100)),
  2190. ],
  2191. options={},
  2192. )
  2193. create_book = migrations.CreateModel(
  2194. "Book",
  2195. [
  2196. ("id", models.AutoField(primary_key=True)),
  2197. ("title", models.CharField(max_length=100)),
  2198. ("author", models.ForeignKey("test_authors.Author", models.CASCADE))
  2199. ],
  2200. options={},
  2201. )
  2202. add_hometown = migrations.AddField(
  2203. "Author",
  2204. "hometown",
  2205. models.CharField(max_length=100),
  2206. )
  2207. create_old_man = migrations.RunPython(inner_method, inner_method)
  2208. project_state = ProjectState()
  2209. new_state = project_state.clone()
  2210. with connection.schema_editor() as editor:
  2211. create_author.state_forwards("test_authors", new_state)
  2212. create_author.database_forwards("test_authors", editor, project_state, new_state)
  2213. project_state = new_state
  2214. new_state = new_state.clone()
  2215. with connection.schema_editor() as editor:
  2216. create_book.state_forwards("test_books", new_state)
  2217. create_book.database_forwards("test_books", editor, project_state, new_state)
  2218. project_state = new_state
  2219. new_state = new_state.clone()
  2220. with connection.schema_editor() as editor:
  2221. add_hometown.state_forwards("test_authors", new_state)
  2222. add_hometown.database_forwards("test_authors", editor, project_state, new_state)
  2223. project_state = new_state
  2224. new_state = new_state.clone()
  2225. with connection.schema_editor() as editor:
  2226. create_old_man.state_forwards("test_books", new_state)
  2227. create_old_man.database_forwards("test_books", editor, project_state, new_state)
  2228. def test_model_with_bigautofield(self):
  2229. """
  2230. A model with BigAutoField can be created.
  2231. """
  2232. def create_data(models, schema_editor):
  2233. Author = models.get_model("test_author", "Author")
  2234. Book = models.get_model("test_book", "Book")
  2235. author1 = Author.objects.create(name="Hemingway")
  2236. Book.objects.create(title="Old Man and The Sea", author=author1)
  2237. Book.objects.create(id=2 ** 33, title="A farewell to arms", author=author1)
  2238. author2 = Author.objects.create(id=2 ** 33, name="Remarque")
  2239. Book.objects.create(title="All quiet on the western front", author=author2)
  2240. Book.objects.create(title="Arc de Triomphe", author=author2)
  2241. create_author = migrations.CreateModel(
  2242. "Author",
  2243. [
  2244. ("id", models.BigAutoField(primary_key=True)),
  2245. ("name", models.CharField(max_length=100)),
  2246. ],
  2247. options={},
  2248. )
  2249. create_book = migrations.CreateModel(
  2250. "Book",
  2251. [
  2252. ("id", models.BigAutoField(primary_key=True)),
  2253. ("title", models.CharField(max_length=100)),
  2254. ("author", models.ForeignKey(to="test_author.Author", on_delete=models.CASCADE))
  2255. ],
  2256. options={},
  2257. )
  2258. fill_data = migrations.RunPython(create_data)
  2259. project_state = ProjectState()
  2260. new_state = project_state.clone()
  2261. with connection.schema_editor() as editor:
  2262. create_author.state_forwards("test_author", new_state)
  2263. create_author.database_forwards("test_author", editor, project_state, new_state)
  2264. project_state = new_state
  2265. new_state = new_state.clone()
  2266. with connection.schema_editor() as editor:
  2267. create_book.state_forwards("test_book", new_state)
  2268. create_book.database_forwards("test_book", editor, project_state, new_state)
  2269. project_state = new_state
  2270. new_state = new_state.clone()
  2271. with connection.schema_editor() as editor:
  2272. fill_data.state_forwards("fill_data", new_state)
  2273. fill_data.database_forwards("fill_data", editor, project_state, new_state)
  2274. def test_autofield_foreignfield_growth(self):
  2275. """
  2276. A field may be migrated from AutoField to BigAutoField.
  2277. """
  2278. def create_initial_data(models, schema_editor):
  2279. Article = models.get_model("test_article", "Article")
  2280. Blog = models.get_model("test_blog", "Blog")
  2281. blog = Blog.objects.create(name="web development done right")
  2282. Article.objects.create(name="Frameworks", blog=blog)
  2283. Article.objects.create(name="Programming Languages", blog=blog)
  2284. def create_big_data(models, schema_editor):
  2285. Article = models.get_model("test_article", "Article")
  2286. Blog = models.get_model("test_blog", "Blog")
  2287. blog2 = Blog.objects.create(name="Frameworks", id=2 ** 33)
  2288. Article.objects.create(name="Django", blog=blog2)
  2289. Article.objects.create(id=2 ** 33, name="Django2", blog=blog2)
  2290. create_blog = migrations.CreateModel(
  2291. "Blog",
  2292. [
  2293. ("id", models.AutoField(primary_key=True)),
  2294. ("name", models.CharField(max_length=100)),
  2295. ],
  2296. options={},
  2297. )
  2298. create_article = migrations.CreateModel(
  2299. "Article",
  2300. [
  2301. ("id", models.AutoField(primary_key=True)),
  2302. ("blog", models.ForeignKey(to="test_blog.Blog", on_delete=models.CASCADE)),
  2303. ("name", models.CharField(max_length=100)),
  2304. ("data", models.TextField(default="")),
  2305. ],
  2306. options={},
  2307. )
  2308. fill_initial_data = migrations.RunPython(create_initial_data, create_initial_data)
  2309. fill_big_data = migrations.RunPython(create_big_data, create_big_data)
  2310. grow_article_id = migrations.AlterField("Article", "id", models.BigAutoField(primary_key=True))
  2311. grow_blog_id = migrations.AlterField("Blog", "id", models.BigAutoField(primary_key=True))
  2312. project_state = ProjectState()
  2313. new_state = project_state.clone()
  2314. with connection.schema_editor() as editor:
  2315. create_blog.state_forwards("test_blog", new_state)
  2316. create_blog.database_forwards("test_blog", editor, project_state, new_state)
  2317. project_state = new_state
  2318. new_state = new_state.clone()
  2319. with connection.schema_editor() as editor:
  2320. create_article.state_forwards("test_article", new_state)
  2321. create_article.database_forwards("test_article", editor, project_state, new_state)
  2322. project_state = new_state
  2323. new_state = new_state.clone()
  2324. with connection.schema_editor() as editor:
  2325. fill_initial_data.state_forwards("fill_initial_data", new_state)
  2326. fill_initial_data.database_forwards("fill_initial_data", editor, project_state, new_state)
  2327. project_state = new_state
  2328. new_state = new_state.clone()
  2329. with connection.schema_editor() as editor:
  2330. grow_article_id.state_forwards("test_article", new_state)
  2331. grow_article_id.database_forwards("test_article", editor, project_state, new_state)
  2332. state = new_state.clone()
  2333. article = state.apps.get_model("test_article.Article")
  2334. self.assertIsInstance(article._meta.pk, models.BigAutoField)
  2335. project_state = new_state
  2336. new_state = new_state.clone()
  2337. with connection.schema_editor() as editor:
  2338. grow_blog_id.state_forwards("test_blog", new_state)
  2339. grow_blog_id.database_forwards("test_blog", editor, project_state, new_state)
  2340. state = new_state.clone()
  2341. blog = state.apps.get_model("test_blog.Blog")
  2342. self.assertIsInstance(blog._meta.pk, models.BigAutoField)
  2343. project_state = new_state
  2344. new_state = new_state.clone()
  2345. with connection.schema_editor() as editor:
  2346. fill_big_data.state_forwards("fill_big_data", new_state)
  2347. fill_big_data.database_forwards("fill_big_data", editor, project_state, new_state)
  2348. def test_run_python_noop(self):
  2349. """
  2350. #24098 - Tests no-op RunPython operations.
  2351. """
  2352. project_state = ProjectState()
  2353. new_state = project_state.clone()
  2354. operation = migrations.RunPython(migrations.RunPython.noop, migrations.RunPython.noop)
  2355. with connection.schema_editor() as editor:
  2356. operation.database_forwards("test_runpython", editor, project_state, new_state)
  2357. operation.database_backwards("test_runpython", editor, new_state, project_state)
  2358. @unittest.skipIf(sqlparse is None and connection.features.requires_sqlparse_for_splitting, "Missing sqlparse")
  2359. def test_separate_database_and_state(self):
  2360. """
  2361. Tests the SeparateDatabaseAndState operation.
  2362. """
  2363. project_state = self.set_up_test_model("test_separatedatabaseandstate")
  2364. # Create the operation
  2365. database_operation = migrations.RunSQL(
  2366. "CREATE TABLE i_love_ponies (id int, special_thing int);",
  2367. "DROP TABLE i_love_ponies;"
  2368. )
  2369. state_operation = migrations.CreateModel("SomethingElse", [("id", models.AutoField(primary_key=True))])
  2370. operation = migrations.SeparateDatabaseAndState(
  2371. state_operations=[state_operation],
  2372. database_operations=[database_operation]
  2373. )
  2374. self.assertEqual(operation.describe(), "Custom state/database change combination")
  2375. # Test the state alteration
  2376. new_state = project_state.clone()
  2377. operation.state_forwards("test_separatedatabaseandstate", new_state)
  2378. self.assertEqual(len(new_state.models["test_separatedatabaseandstate", "somethingelse"].fields), 1)
  2379. # Make sure there's no table
  2380. self.assertTableNotExists("i_love_ponies")
  2381. # Test the database alteration
  2382. with connection.schema_editor() as editor:
  2383. operation.database_forwards("test_separatedatabaseandstate", editor, project_state, new_state)
  2384. self.assertTableExists("i_love_ponies")
  2385. # And test reversal
  2386. self.assertTrue(operation.reversible)
  2387. with connection.schema_editor() as editor:
  2388. operation.database_backwards("test_separatedatabaseandstate", editor, new_state, project_state)
  2389. self.assertTableNotExists("i_love_ponies")
  2390. # And deconstruction
  2391. definition = operation.deconstruct()
  2392. self.assertEqual(definition[0], "SeparateDatabaseAndState")
  2393. self.assertEqual(definition[1], [])
  2394. self.assertEqual(sorted(definition[2]), ["database_operations", "state_operations"])
  2395. def test_separate_database_and_state2(self):
  2396. """
  2397. A complex SeparateDatabaseAndState operation: Multiple operations both
  2398. for state and database. Verify the state dependencies within each list
  2399. and that state ops don't affect the database.
  2400. """
  2401. app_label = "test_separatedatabaseandstate2"
  2402. project_state = self.set_up_test_model(app_label)
  2403. # Create the operation
  2404. database_operations = [
  2405. migrations.CreateModel(
  2406. "ILovePonies",
  2407. [("id", models.AutoField(primary_key=True))],
  2408. options={"db_table": "iloveponies"},
  2409. ),
  2410. migrations.CreateModel(
  2411. "ILoveMorePonies",
  2412. # We use IntegerField and not AutoField because
  2413. # the model is going to be deleted immediately
  2414. # and with an AutoField this fails on Oracle
  2415. [("id", models.IntegerField(primary_key=True))],
  2416. options={"db_table": "ilovemoreponies"},
  2417. ),
  2418. migrations.DeleteModel("ILoveMorePonies"),
  2419. migrations.CreateModel(
  2420. "ILoveEvenMorePonies",
  2421. [("id", models.AutoField(primary_key=True))],
  2422. options={"db_table": "iloveevenmoreponies"},
  2423. ),
  2424. ]
  2425. state_operations = [
  2426. migrations.CreateModel(
  2427. "SomethingElse",
  2428. [("id", models.AutoField(primary_key=True))],
  2429. options={"db_table": "somethingelse"},
  2430. ),
  2431. migrations.DeleteModel("SomethingElse"),
  2432. migrations.CreateModel(
  2433. "SomethingCompletelyDifferent",
  2434. [("id", models.AutoField(primary_key=True))],
  2435. options={"db_table": "somethingcompletelydifferent"},
  2436. ),
  2437. ]
  2438. operation = migrations.SeparateDatabaseAndState(
  2439. state_operations=state_operations,
  2440. database_operations=database_operations,
  2441. )
  2442. # Test the state alteration
  2443. new_state = project_state.clone()
  2444. operation.state_forwards(app_label, new_state)
  2445. def assertModelsAndTables(after_db):
  2446. # Tables and models exist, or don't, as they should:
  2447. self.assertNotIn((app_label, "somethingelse"), new_state.models)
  2448. self.assertEqual(len(new_state.models[app_label, "somethingcompletelydifferent"].fields), 1)
  2449. self.assertNotIn((app_label, "iloveponiesonies"), new_state.models)
  2450. self.assertNotIn((app_label, "ilovemoreponies"), new_state.models)
  2451. self.assertNotIn((app_label, "iloveevenmoreponies"), new_state.models)
  2452. self.assertTableNotExists("somethingelse")
  2453. self.assertTableNotExists("somethingcompletelydifferent")
  2454. self.assertTableNotExists("ilovemoreponies")
  2455. if after_db:
  2456. self.assertTableExists("iloveponies")
  2457. self.assertTableExists("iloveevenmoreponies")
  2458. else:
  2459. self.assertTableNotExists("iloveponies")
  2460. self.assertTableNotExists("iloveevenmoreponies")
  2461. assertModelsAndTables(after_db=False)
  2462. # Test the database alteration
  2463. with connection.schema_editor() as editor:
  2464. operation.database_forwards(app_label, editor, project_state, new_state)
  2465. assertModelsAndTables(after_db=True)
  2466. # And test reversal
  2467. self.assertTrue(operation.reversible)
  2468. with connection.schema_editor() as editor:
  2469. operation.database_backwards(app_label, editor, new_state, project_state)
  2470. assertModelsAndTables(after_db=False)
  2471. class SwappableOperationTests(OperationTestBase):
  2472. """
  2473. Key operations ignore swappable models
  2474. (we don't want to replicate all of them here, as the functionality
  2475. is in a common base class anyway)
  2476. """
  2477. available_apps = ['migrations']
  2478. @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
  2479. def test_create_ignore_swapped(self):
  2480. """
  2481. The CreateTable operation ignores swapped models.
  2482. """
  2483. operation = migrations.CreateModel(
  2484. "Pony",
  2485. [
  2486. ("id", models.AutoField(primary_key=True)),
  2487. ("pink", models.IntegerField(default=1)),
  2488. ],
  2489. options={
  2490. "swappable": "TEST_SWAP_MODEL",
  2491. },
  2492. )
  2493. # Test the state alteration (it should still be there!)
  2494. project_state = ProjectState()
  2495. new_state = project_state.clone()
  2496. operation.state_forwards("test_crigsw", new_state)
  2497. self.assertEqual(new_state.models["test_crigsw", "pony"].name, "Pony")
  2498. self.assertEqual(len(new_state.models["test_crigsw", "pony"].fields), 2)
  2499. # Test the database alteration
  2500. self.assertTableNotExists("test_crigsw_pony")
  2501. with connection.schema_editor() as editor:
  2502. operation.database_forwards("test_crigsw", editor, project_state, new_state)
  2503. self.assertTableNotExists("test_crigsw_pony")
  2504. # And test reversal
  2505. with connection.schema_editor() as editor:
  2506. operation.database_backwards("test_crigsw", editor, new_state, project_state)
  2507. self.assertTableNotExists("test_crigsw_pony")
  2508. @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
  2509. def test_delete_ignore_swapped(self):
  2510. """
  2511. Tests the DeleteModel operation ignores swapped models.
  2512. """
  2513. operation = migrations.DeleteModel("Pony")
  2514. project_state, new_state = self.make_test_state("test_dligsw", operation)
  2515. # Test the database alteration
  2516. self.assertTableNotExists("test_dligsw_pony")
  2517. with connection.schema_editor() as editor:
  2518. operation.database_forwards("test_dligsw", editor, project_state, new_state)
  2519. self.assertTableNotExists("test_dligsw_pony")
  2520. # And test reversal
  2521. with connection.schema_editor() as editor:
  2522. operation.database_backwards("test_dligsw", editor, new_state, project_state)
  2523. self.assertTableNotExists("test_dligsw_pony")
  2524. @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
  2525. def test_add_field_ignore_swapped(self):
  2526. """
  2527. Tests the AddField operation.
  2528. """
  2529. # Test the state alteration
  2530. operation = migrations.AddField(
  2531. "Pony",
  2532. "height",
  2533. models.FloatField(null=True, default=5),
  2534. )
  2535. project_state, new_state = self.make_test_state("test_adfligsw", operation)
  2536. # Test the database alteration
  2537. self.assertTableNotExists("test_adfligsw_pony")
  2538. with connection.schema_editor() as editor:
  2539. operation.database_forwards("test_adfligsw", editor, project_state, new_state)
  2540. self.assertTableNotExists("test_adfligsw_pony")
  2541. # And test reversal
  2542. with connection.schema_editor() as editor:
  2543. operation.database_backwards("test_adfligsw", editor, new_state, project_state)
  2544. self.assertTableNotExists("test_adfligsw_pony")
  2545. @override_settings(TEST_SWAP_MODEL='migrations.SomeFakeModel')
  2546. def test_indexes_ignore_swapped(self):
  2547. """
  2548. Add/RemoveIndex operations ignore swapped models.
  2549. """
  2550. operation = migrations.AddIndex('Pony', models.Index(fields=['pink'], name='my_name_idx'))
  2551. project_state, new_state = self.make_test_state('test_adinigsw', operation)
  2552. with connection.schema_editor() as editor:
  2553. # No database queries should be run for swapped models
  2554. operation.database_forwards('test_adinigsw', editor, project_state, new_state)
  2555. operation.database_backwards('test_adinigsw', editor, new_state, project_state)
  2556. operation = migrations.RemoveIndex('Pony', models.Index(fields=['pink'], name='my_name_idx'))
  2557. project_state, new_state = self.make_test_state("test_rminigsw", operation)
  2558. with connection.schema_editor() as editor:
  2559. operation.database_forwards('test_rminigsw', editor, project_state, new_state)
  2560. operation.database_backwards('test_rminigsw', editor, new_state, project_state)
  2561. class TestCreateModel(SimpleTestCase):
  2562. def test_references_model_mixin(self):
  2563. CreateModel('name', [], bases=(Mixin, models.Model)).references_model('other_model')