test_operations.py 270 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054205520562057205820592060206120622063206420652066206720682069207020712072207320742075207620772078207920802081208220832084208520862087208820892090209120922093209420952096209720982099210021012102210321042105210621072108210921102111211221132114211521162117211821192120212121222123212421252126212721282129213021312132213321342135213621372138213921402141214221432144214521462147214821492150215121522153215421552156215721582159216021612162216321642165216621672168216921702171217221732174217521762177217821792180218121822183218421852186218721882189219021912192219321942195219621972198219922002201220222032204220522062207220822092210221122122213221422152216221722182219222022212222222322242225222622272228222922302231223222332234223522362237223822392240224122422243224422452246224722482249225022512252225322542255225622572258225922602261226222632264226522662267226822692270227122722273227422752276227722782279228022812282228322842285228622872288228922902291229222932294229522962297229822992300230123022303230423052306230723082309231023112312231323142315231623172318231923202321232223232324232523262327232823292330233123322333233423352336233723382339234023412342234323442345234623472348234923502351235223532354235523562357235823592360236123622363236423652366236723682369237023712372237323742375237623772378237923802381238223832384238523862387238823892390239123922393239423952396239723982399240024012402240324042405240624072408240924102411241224132414241524162417241824192420242124222423242424252426242724282429243024312432243324342435243624372438243924402441244224432444244524462447244824492450245124522453245424552456245724582459246024612462246324642465246624672468246924702471247224732474247524762477247824792480248124822483248424852486248724882489249024912492249324942495249624972498249925002501250225032504250525062507250825092510251125122513251425152516251725182519252025212522252325242525252625272528252925302531253225332534253525362537253825392540254125422543254425452546254725482549255025512552255325542555255625572558255925602561256225632564256525662567256825692570257125722573257425752576257725782579258025812582258325842585258625872588258925902591259225932594259525962597259825992600260126022603260426052606260726082609261026112612261326142615261626172618261926202621262226232624262526262627262826292630263126322633263426352636263726382639264026412642264326442645264626472648264926502651265226532654265526562657265826592660266126622663266426652666266726682669267026712672267326742675267626772678267926802681268226832684268526862687268826892690269126922693269426952696269726982699270027012702270327042705270627072708270927102711271227132714271527162717271827192720272127222723272427252726272727282729273027312732273327342735273627372738273927402741274227432744274527462747274827492750275127522753275427552756275727582759276027612762276327642765276627672768276927702771277227732774277527762777277827792780278127822783278427852786278727882789279027912792279327942795279627972798279928002801280228032804280528062807280828092810281128122813281428152816281728182819282028212822282328242825282628272828282928302831283228332834283528362837283828392840284128422843284428452846284728482849285028512852285328542855285628572858285928602861286228632864286528662867286828692870287128722873287428752876287728782879288028812882288328842885288628872888288928902891289228932894289528962897289828992900290129022903290429052906290729082909291029112912291329142915291629172918291929202921292229232924292529262927292829292930293129322933293429352936293729382939294029412942294329442945294629472948294929502951295229532954295529562957295829592960296129622963296429652966296729682969297029712972297329742975297629772978297929802981298229832984298529862987298829892990299129922993299429952996299729982999300030013002300330043005300630073008300930103011301230133014301530163017301830193020302130223023302430253026302730283029303030313032303330343035303630373038303930403041304230433044304530463047304830493050305130523053305430553056305730583059306030613062306330643065306630673068306930703071307230733074307530763077307830793080308130823083308430853086308730883089309030913092309330943095309630973098309931003101310231033104310531063107310831093110311131123113311431153116311731183119312031213122312331243125312631273128312931303131313231333134313531363137313831393140314131423143314431453146314731483149315031513152315331543155315631573158315931603161316231633164316531663167316831693170317131723173317431753176317731783179318031813182318331843185318631873188318931903191319231933194319531963197319831993200320132023203320432053206320732083209321032113212321332143215321632173218321932203221322232233224322532263227322832293230323132323233323432353236323732383239324032413242324332443245324632473248324932503251325232533254325532563257325832593260326132623263326432653266326732683269327032713272327332743275327632773278327932803281328232833284328532863287328832893290329132923293329432953296329732983299330033013302330333043305330633073308330933103311331233133314331533163317331833193320332133223323332433253326332733283329333033313332333333343335333633373338333933403341334233433344334533463347334833493350335133523353335433553356335733583359336033613362336333643365336633673368336933703371337233733374337533763377337833793380338133823383338433853386338733883389339033913392339333943395339633973398339934003401340234033404340534063407340834093410341134123413341434153416341734183419342034213422342334243425342634273428342934303431343234333434343534363437343834393440344134423443344434453446344734483449345034513452345334543455345634573458345934603461346234633464346534663467346834693470347134723473347434753476347734783479348034813482348334843485348634873488348934903491349234933494349534963497349834993500350135023503350435053506350735083509351035113512351335143515351635173518351935203521352235233524352535263527352835293530353135323533353435353536353735383539354035413542354335443545354635473548354935503551355235533554355535563557355835593560356135623563356435653566356735683569357035713572357335743575357635773578357935803581358235833584358535863587358835893590359135923593359435953596359735983599360036013602360336043605360636073608360936103611361236133614361536163617361836193620362136223623362436253626362736283629363036313632363336343635363636373638363936403641364236433644364536463647364836493650365136523653365436553656365736583659366036613662366336643665366636673668366936703671367236733674367536763677367836793680368136823683368436853686368736883689369036913692369336943695369636973698369937003701370237033704370537063707370837093710371137123713371437153716371737183719372037213722372337243725372637273728372937303731373237333734373537363737373837393740374137423743374437453746374737483749375037513752375337543755375637573758375937603761376237633764376537663767376837693770377137723773377437753776377737783779378037813782378337843785378637873788378937903791379237933794379537963797379837993800380138023803380438053806380738083809381038113812381338143815381638173818381938203821382238233824382538263827382838293830383138323833383438353836383738383839384038413842384338443845384638473848384938503851385238533854385538563857385838593860386138623863386438653866386738683869387038713872387338743875387638773878387938803881388238833884388538863887388838893890389138923893389438953896389738983899390039013902390339043905390639073908390939103911391239133914391539163917391839193920392139223923392439253926392739283929393039313932393339343935393639373938393939403941394239433944394539463947394839493950395139523953395439553956395739583959396039613962396339643965396639673968396939703971397239733974397539763977397839793980398139823983398439853986398739883989399039913992399339943995399639973998399940004001400240034004400540064007400840094010401140124013401440154016401740184019402040214022402340244025402640274028402940304031403240334034403540364037403840394040404140424043404440454046404740484049405040514052405340544055405640574058405940604061406240634064406540664067406840694070407140724073407440754076407740784079408040814082408340844085408640874088408940904091409240934094409540964097409840994100410141024103410441054106410741084109411041114112411341144115411641174118411941204121412241234124412541264127412841294130413141324133413441354136413741384139414041414142414341444145414641474148414941504151415241534154415541564157415841594160416141624163416441654166416741684169417041714172417341744175417641774178417941804181418241834184418541864187418841894190419141924193419441954196419741984199420042014202420342044205420642074208420942104211421242134214421542164217421842194220422142224223422442254226422742284229423042314232423342344235423642374238423942404241424242434244424542464247424842494250425142524253425442554256425742584259426042614262426342644265426642674268426942704271427242734274427542764277427842794280428142824283428442854286428742884289429042914292429342944295429642974298429943004301430243034304430543064307430843094310431143124313431443154316431743184319432043214322432343244325432643274328432943304331433243334334433543364337433843394340434143424343434443454346434743484349435043514352435343544355435643574358435943604361436243634364436543664367436843694370437143724373437443754376437743784379438043814382438343844385438643874388438943904391439243934394439543964397439843994400440144024403440444054406440744084409441044114412441344144415441644174418441944204421442244234424442544264427442844294430443144324433443444354436443744384439444044414442444344444445444644474448444944504451445244534454445544564457445844594460446144624463446444654466446744684469447044714472447344744475447644774478447944804481448244834484448544864487448844894490449144924493449444954496449744984499450045014502450345044505450645074508450945104511451245134514451545164517451845194520452145224523452445254526452745284529453045314532453345344535453645374538453945404541454245434544454545464547454845494550455145524553455445554556455745584559456045614562456345644565456645674568456945704571457245734574457545764577457845794580458145824583458445854586458745884589459045914592459345944595459645974598459946004601460246034604460546064607460846094610461146124613461446154616461746184619462046214622462346244625462646274628462946304631463246334634463546364637463846394640464146424643464446454646464746484649465046514652465346544655465646574658465946604661466246634664466546664667466846694670467146724673467446754676467746784679468046814682468346844685468646874688468946904691469246934694469546964697469846994700470147024703470447054706470747084709471047114712471347144715471647174718471947204721472247234724472547264727472847294730473147324733473447354736473747384739474047414742474347444745474647474748474947504751475247534754475547564757475847594760476147624763476447654766476747684769477047714772477347744775477647774778477947804781478247834784478547864787478847894790479147924793479447954796479747984799480048014802480348044805480648074808480948104811481248134814481548164817481848194820482148224823482448254826482748284829483048314832483348344835483648374838483948404841484248434844484548464847484848494850485148524853485448554856485748584859486048614862486348644865486648674868486948704871487248734874487548764877487848794880488148824883488448854886488748884889489048914892489348944895489648974898489949004901490249034904490549064907490849094910491149124913491449154916491749184919492049214922492349244925492649274928492949304931493249334934493549364937493849394940494149424943494449454946494749484949495049514952495349544955495649574958495949604961496249634964496549664967496849694970497149724973497449754976497749784979498049814982498349844985498649874988498949904991499249934994499549964997499849995000500150025003500450055006500750085009501050115012501350145015501650175018501950205021502250235024502550265027502850295030503150325033503450355036503750385039504050415042504350445045504650475048504950505051505250535054505550565057505850595060506150625063506450655066506750685069507050715072507350745075507650775078507950805081508250835084508550865087508850895090509150925093509450955096509750985099510051015102510351045105510651075108510951105111511251135114511551165117511851195120512151225123512451255126512751285129513051315132513351345135513651375138513951405141514251435144514551465147514851495150515151525153515451555156515751585159516051615162516351645165516651675168516951705171517251735174517551765177517851795180518151825183518451855186518751885189519051915192519351945195519651975198519952005201520252035204520552065207520852095210521152125213521452155216521752185219522052215222522352245225522652275228522952305231523252335234523552365237523852395240524152425243524452455246524752485249525052515252525352545255525652575258525952605261526252635264526552665267526852695270527152725273527452755276527752785279528052815282528352845285528652875288528952905291529252935294529552965297529852995300530153025303530453055306530753085309531053115312531353145315531653175318531953205321532253235324532553265327532853295330533153325333533453355336533753385339534053415342534353445345534653475348534953505351535253535354535553565357535853595360536153625363536453655366536753685369537053715372537353745375537653775378537953805381538253835384538553865387538853895390539153925393539453955396539753985399540054015402540354045405540654075408540954105411541254135414541554165417541854195420542154225423542454255426542754285429543054315432543354345435543654375438543954405441544254435444544554465447544854495450545154525453545454555456545754585459546054615462546354645465546654675468546954705471547254735474547554765477547854795480548154825483548454855486548754885489549054915492549354945495549654975498549955005501550255035504550555065507550855095510551155125513551455155516551755185519552055215522552355245525552655275528552955305531553255335534553555365537553855395540554155425543554455455546554755485549555055515552555355545555555655575558555955605561556255635564556555665567556855695570557155725573557455755576557755785579558055815582558355845585558655875588558955905591559255935594559555965597559855995600560156025603560456055606560756085609561056115612561356145615561656175618561956205621562256235624562556265627562856295630563156325633563456355636563756385639564056415642564356445645564656475648564956505651565256535654565556565657565856595660566156625663566456655666566756685669567056715672567356745675567656775678567956805681568256835684568556865687568856895690569156925693569456955696569756985699570057015702570357045705570657075708570957105711571257135714571557165717571857195720572157225723572457255726572757285729573057315732573357345735573657375738573957405741574257435744574557465747574857495750575157525753575457555756575757585759576057615762576357645765576657675768576957705771577257735774577557765777577857795780578157825783578457855786578757885789579057915792579357945795579657975798579958005801580258035804580558065807580858095810581158125813581458155816581758185819582058215822582358245825582658275828582958305831583258335834583558365837583858395840584158425843584458455846584758485849585058515852585358545855585658575858585958605861586258635864586558665867586858695870587158725873587458755876587758785879588058815882588358845885588658875888588958905891589258935894589558965897589858995900590159025903590459055906590759085909591059115912591359145915591659175918591959205921592259235924592559265927592859295930593159325933593459355936593759385939594059415942594359445945594659475948594959505951595259535954595559565957595859595960596159625963596459655966596759685969597059715972597359745975597659775978597959805981598259835984598559865987598859895990599159925993599459955996599759985999600060016002600360046005600660076008600960106011601260136014601560166017601860196020602160226023602460256026602760286029603060316032603360346035603660376038603960406041604260436044604560466047604860496050605160526053605460556056605760586059606060616062606360646065606660676068606960706071607260736074607560766077607860796080608160826083608460856086608760886089609060916092609360946095609660976098609961006101610261036104610561066107610861096110611161126113611461156116611761186119612061216122612361246125612661276128612961306131613261336134613561366137613861396140614161426143614461456146614761486149615061516152615361546155615661576158615961606161616261636164616561666167616861696170617161726173617461756176617761786179618061816182618361846185618661876188618961906191619261936194619561966197619861996200620162026203620462056206620762086209621062116212621362146215621662176218621962206221622262236224622562266227622862296230623162326233623462356236623762386239624062416242624362446245624662476248624962506251625262536254625562566257625862596260626162626263626462656266626762686269627062716272627362746275627662776278627962806281628262836284628562866287628862896290629162926293629462956296629762986299630063016302630363046305630663076308630963106311631263136314631563166317631863196320632163226323632463256326632763286329633063316332633363346335633663376338633963406341634263436344634563466347634863496350635163526353635463556356635763586359636063616362636363646365636663676368636963706371637263736374637563766377637863796380638163826383638463856386638763886389639063916392639363946395639663976398639964006401640264036404640564066407640864096410641164126413641464156416641764186419642064216422642364246425642664276428642964306431643264336434643564366437643864396440644164426443644464456446644764486449645064516452645364546455645664576458645964606461646264636464646564666467646864696470647164726473647464756476647764786479648064816482648364846485648664876488648964906491649264936494649564966497649864996500650165026503650465056506650765086509651065116512651365146515651665176518651965206521652265236524652565266527652865296530653165326533653465356536653765386539654065416542654365446545654665476548654965506551655265536554655565566557655865596560656165626563656465656566
  1. import math
  2. from decimal import Decimal
  3. from django.core.exceptions import FieldDoesNotExist
  4. from django.db import IntegrityError, connection, migrations, models, transaction
  5. from django.db.migrations.migration import Migration
  6. from django.db.migrations.operations.base import Operation
  7. from django.db.migrations.operations.fields import FieldOperation
  8. from django.db.migrations.state import ModelState, ProjectState
  9. from django.db.models import F
  10. from django.db.models.expressions import Value
  11. from django.db.models.functions import Abs, Concat, Pi
  12. from django.db.transaction import atomic
  13. from django.test import (
  14. SimpleTestCase,
  15. override_settings,
  16. skipIfDBFeature,
  17. skipUnlessDBFeature,
  18. )
  19. from django.test.utils import CaptureQueriesContext
  20. from .models import FoodManager, FoodQuerySet, UnicodeModel
  21. from .test_base import OperationTestBase
  22. class Mixin:
  23. pass
  24. class OperationTests(OperationTestBase):
  25. """
  26. Tests running the operations and making sure they do what they say they do.
  27. Each test looks at their state changing, and then their database operation -
  28. both forwards and backwards.
  29. """
  30. def test_create_model(self):
  31. """
  32. Tests the CreateModel operation.
  33. Most other tests use this operation as part of setup, so check failures
  34. here first.
  35. """
  36. operation = migrations.CreateModel(
  37. "Pony",
  38. [
  39. ("id", models.AutoField(primary_key=True)),
  40. ("pink", models.IntegerField(default=1)),
  41. ],
  42. )
  43. self.assertEqual(operation.describe(), "Create model Pony")
  44. self.assertEqual(operation.formatted_description(), "+ Create model Pony")
  45. self.assertEqual(operation.migration_name_fragment, "pony")
  46. # Test the state alteration
  47. project_state = ProjectState()
  48. new_state = project_state.clone()
  49. operation.state_forwards("test_crmo", new_state)
  50. self.assertEqual(new_state.models["test_crmo", "pony"].name, "Pony")
  51. self.assertEqual(len(new_state.models["test_crmo", "pony"].fields), 2)
  52. # Test the database alteration
  53. self.assertTableNotExists("test_crmo_pony")
  54. with connection.schema_editor() as editor:
  55. operation.database_forwards("test_crmo", editor, project_state, new_state)
  56. self.assertTableExists("test_crmo_pony")
  57. # And test reversal
  58. with connection.schema_editor() as editor:
  59. operation.database_backwards("test_crmo", editor, new_state, project_state)
  60. self.assertTableNotExists("test_crmo_pony")
  61. # And deconstruction
  62. definition = operation.deconstruct()
  63. self.assertEqual(definition[0], "CreateModel")
  64. self.assertEqual(definition[1], [])
  65. self.assertEqual(sorted(definition[2]), ["fields", "name"])
  66. # And default manager not in set
  67. operation = migrations.CreateModel(
  68. "Foo", fields=[], managers=[("objects", models.Manager())]
  69. )
  70. definition = operation.deconstruct()
  71. self.assertNotIn("managers", definition[2])
  72. def test_create_model_with_duplicate_field_name(self):
  73. with self.assertRaisesMessage(
  74. ValueError, "Found duplicate value pink in CreateModel fields argument."
  75. ):
  76. migrations.CreateModel(
  77. "Pony",
  78. [
  79. ("id", models.AutoField(primary_key=True)),
  80. ("pink", models.TextField()),
  81. ("pink", models.IntegerField(default=1)),
  82. ],
  83. )
  84. def test_create_model_with_duplicate_base(self):
  85. message = "Found duplicate value test_crmo.pony in CreateModel bases argument."
  86. with self.assertRaisesMessage(ValueError, message):
  87. migrations.CreateModel(
  88. "Pony",
  89. fields=[],
  90. bases=(
  91. "test_crmo.Pony",
  92. "test_crmo.Pony",
  93. ),
  94. )
  95. with self.assertRaisesMessage(ValueError, message):
  96. migrations.CreateModel(
  97. "Pony",
  98. fields=[],
  99. bases=(
  100. "test_crmo.Pony",
  101. "test_crmo.pony",
  102. ),
  103. )
  104. message = (
  105. "Found duplicate value migrations.unicodemodel in CreateModel bases "
  106. "argument."
  107. )
  108. with self.assertRaisesMessage(ValueError, message):
  109. migrations.CreateModel(
  110. "Pony",
  111. fields=[],
  112. bases=(
  113. UnicodeModel,
  114. UnicodeModel,
  115. ),
  116. )
  117. with self.assertRaisesMessage(ValueError, message):
  118. migrations.CreateModel(
  119. "Pony",
  120. fields=[],
  121. bases=(
  122. UnicodeModel,
  123. "migrations.unicodemodel",
  124. ),
  125. )
  126. with self.assertRaisesMessage(ValueError, message):
  127. migrations.CreateModel(
  128. "Pony",
  129. fields=[],
  130. bases=(
  131. UnicodeModel,
  132. "migrations.UnicodeModel",
  133. ),
  134. )
  135. message = (
  136. "Found duplicate value <class 'django.db.models.base.Model'> in "
  137. "CreateModel bases argument."
  138. )
  139. with self.assertRaisesMessage(ValueError, message):
  140. migrations.CreateModel(
  141. "Pony",
  142. fields=[],
  143. bases=(
  144. models.Model,
  145. models.Model,
  146. ),
  147. )
  148. message = (
  149. "Found duplicate value <class 'migrations.test_operations.Mixin'> in "
  150. "CreateModel bases argument."
  151. )
  152. with self.assertRaisesMessage(ValueError, message):
  153. migrations.CreateModel(
  154. "Pony",
  155. fields=[],
  156. bases=(
  157. Mixin,
  158. Mixin,
  159. ),
  160. )
  161. def test_create_model_with_duplicate_manager_name(self):
  162. with self.assertRaisesMessage(
  163. ValueError,
  164. "Found duplicate value objects in CreateModel managers argument.",
  165. ):
  166. migrations.CreateModel(
  167. "Pony",
  168. fields=[],
  169. managers=[
  170. ("objects", models.Manager()),
  171. ("objects", models.Manager()),
  172. ],
  173. )
  174. def test_create_model_with_unique_after(self):
  175. """
  176. Tests the CreateModel operation directly followed by an
  177. AlterUniqueTogether (bug #22844 - sqlite remake issues)
  178. """
  179. operation1 = migrations.CreateModel(
  180. "Pony",
  181. [
  182. ("id", models.AutoField(primary_key=True)),
  183. ("pink", models.IntegerField(default=1)),
  184. ],
  185. )
  186. operation2 = migrations.CreateModel(
  187. "Rider",
  188. [
  189. ("id", models.AutoField(primary_key=True)),
  190. ("number", models.IntegerField(default=1)),
  191. ("pony", models.ForeignKey("test_crmoua.Pony", models.CASCADE)),
  192. ],
  193. )
  194. operation3 = migrations.AlterUniqueTogether(
  195. "Rider",
  196. [
  197. ("number", "pony"),
  198. ],
  199. )
  200. # Test the database alteration
  201. project_state = ProjectState()
  202. self.assertTableNotExists("test_crmoua_pony")
  203. self.assertTableNotExists("test_crmoua_rider")
  204. with connection.schema_editor() as editor:
  205. new_state = project_state.clone()
  206. operation1.state_forwards("test_crmoua", new_state)
  207. operation1.database_forwards(
  208. "test_crmoua", editor, project_state, new_state
  209. )
  210. project_state, new_state = new_state, new_state.clone()
  211. operation2.state_forwards("test_crmoua", new_state)
  212. operation2.database_forwards(
  213. "test_crmoua", editor, project_state, new_state
  214. )
  215. project_state, new_state = new_state, new_state.clone()
  216. operation3.state_forwards("test_crmoua", new_state)
  217. operation3.database_forwards(
  218. "test_crmoua", editor, project_state, new_state
  219. )
  220. self.assertTableExists("test_crmoua_pony")
  221. self.assertTableExists("test_crmoua_rider")
  222. def test_create_model_m2m(self):
  223. """
  224. Test the creation of a model with a ManyToMany field and the
  225. auto-created "through" model.
  226. """
  227. project_state = self.set_up_test_model("test_crmomm")
  228. operation = migrations.CreateModel(
  229. "Stable",
  230. [
  231. ("id", models.AutoField(primary_key=True)),
  232. ("ponies", models.ManyToManyField("Pony", related_name="stables")),
  233. ],
  234. )
  235. # Test the state alteration
  236. new_state = project_state.clone()
  237. operation.state_forwards("test_crmomm", new_state)
  238. # Test the database alteration
  239. self.assertTableNotExists("test_crmomm_stable_ponies")
  240. with connection.schema_editor() as editor:
  241. operation.database_forwards("test_crmomm", editor, project_state, new_state)
  242. self.assertTableExists("test_crmomm_stable")
  243. self.assertTableExists("test_crmomm_stable_ponies")
  244. self.assertColumnNotExists("test_crmomm_stable", "ponies")
  245. # Make sure the M2M field actually works
  246. with atomic():
  247. Pony = new_state.apps.get_model("test_crmomm", "Pony")
  248. Stable = new_state.apps.get_model("test_crmomm", "Stable")
  249. stable = Stable.objects.create()
  250. p1 = Pony.objects.create(pink=False, weight=4.55)
  251. p2 = Pony.objects.create(pink=True, weight=5.43)
  252. stable.ponies.add(p1, p2)
  253. self.assertEqual(stable.ponies.count(), 2)
  254. stable.ponies.all().delete()
  255. # And test reversal
  256. with connection.schema_editor() as editor:
  257. operation.database_backwards(
  258. "test_crmomm", editor, new_state, project_state
  259. )
  260. self.assertTableNotExists("test_crmomm_stable")
  261. self.assertTableNotExists("test_crmomm_stable_ponies")
  262. @skipUnlessDBFeature("supports_collation_on_charfield", "supports_foreign_keys")
  263. def test_create_fk_models_to_pk_field_db_collation(self):
  264. """Creation of models with a FK to a PK with db_collation."""
  265. collation = connection.features.test_collations.get("non_default")
  266. if not collation:
  267. self.skipTest("Language collations are not supported.")
  268. app_label = "test_cfkmtopkfdbc"
  269. operations = [
  270. migrations.CreateModel(
  271. "Pony",
  272. [
  273. (
  274. "id",
  275. models.CharField(
  276. primary_key=True,
  277. max_length=10,
  278. db_collation=collation,
  279. ),
  280. ),
  281. ],
  282. )
  283. ]
  284. project_state = self.apply_operations(app_label, ProjectState(), operations)
  285. # ForeignKey.
  286. new_state = project_state.clone()
  287. operation = migrations.CreateModel(
  288. "Rider",
  289. [
  290. ("id", models.AutoField(primary_key=True)),
  291. ("pony", models.ForeignKey("Pony", models.CASCADE)),
  292. ],
  293. )
  294. operation.state_forwards(app_label, new_state)
  295. with connection.schema_editor() as editor:
  296. operation.database_forwards(app_label, editor, project_state, new_state)
  297. self.assertColumnCollation(f"{app_label}_rider", "pony_id", collation)
  298. # Reversal.
  299. with connection.schema_editor() as editor:
  300. operation.database_backwards(app_label, editor, new_state, project_state)
  301. # OneToOneField.
  302. new_state = project_state.clone()
  303. operation = migrations.CreateModel(
  304. "ShetlandPony",
  305. [
  306. (
  307. "pony",
  308. models.OneToOneField("Pony", models.CASCADE, primary_key=True),
  309. ),
  310. ("cuteness", models.IntegerField(default=1)),
  311. ],
  312. )
  313. operation.state_forwards(app_label, new_state)
  314. with connection.schema_editor() as editor:
  315. operation.database_forwards(app_label, editor, project_state, new_state)
  316. self.assertColumnCollation(f"{app_label}_shetlandpony", "pony_id", collation)
  317. # Reversal.
  318. with connection.schema_editor() as editor:
  319. operation.database_backwards(app_label, editor, new_state, project_state)
  320. def test_create_model_inheritance(self):
  321. """
  322. Tests the CreateModel operation on a multi-table inheritance setup.
  323. """
  324. project_state = self.set_up_test_model("test_crmoih")
  325. # Test the state alteration
  326. operation = migrations.CreateModel(
  327. "ShetlandPony",
  328. [
  329. (
  330. "pony_ptr",
  331. models.OneToOneField(
  332. "test_crmoih.Pony",
  333. models.CASCADE,
  334. auto_created=True,
  335. primary_key=True,
  336. to_field="id",
  337. serialize=False,
  338. ),
  339. ),
  340. ("cuteness", models.IntegerField(default=1)),
  341. ],
  342. )
  343. new_state = project_state.clone()
  344. operation.state_forwards("test_crmoih", new_state)
  345. self.assertIn(("test_crmoih", "shetlandpony"), new_state.models)
  346. # Test the database alteration
  347. self.assertTableNotExists("test_crmoih_shetlandpony")
  348. with connection.schema_editor() as editor:
  349. operation.database_forwards("test_crmoih", editor, project_state, new_state)
  350. self.assertTableExists("test_crmoih_shetlandpony")
  351. # And test reversal
  352. with connection.schema_editor() as editor:
  353. operation.database_backwards(
  354. "test_crmoih", editor, new_state, project_state
  355. )
  356. self.assertTableNotExists("test_crmoih_shetlandpony")
  357. def test_create_proxy_model(self):
  358. """
  359. CreateModel ignores proxy models.
  360. """
  361. project_state = self.set_up_test_model("test_crprmo")
  362. # Test the state alteration
  363. operation = migrations.CreateModel(
  364. "ProxyPony",
  365. [],
  366. options={"proxy": True},
  367. bases=("test_crprmo.Pony",),
  368. )
  369. self.assertEqual(operation.describe(), "Create proxy model ProxyPony")
  370. new_state = project_state.clone()
  371. operation.state_forwards("test_crprmo", new_state)
  372. self.assertIn(("test_crprmo", "proxypony"), new_state.models)
  373. # Test the database alteration
  374. self.assertTableNotExists("test_crprmo_proxypony")
  375. self.assertTableExists("test_crprmo_pony")
  376. with connection.schema_editor() as editor:
  377. operation.database_forwards("test_crprmo", editor, project_state, new_state)
  378. self.assertTableNotExists("test_crprmo_proxypony")
  379. self.assertTableExists("test_crprmo_pony")
  380. # And test reversal
  381. with connection.schema_editor() as editor:
  382. operation.database_backwards(
  383. "test_crprmo", editor, new_state, project_state
  384. )
  385. self.assertTableNotExists("test_crprmo_proxypony")
  386. self.assertTableExists("test_crprmo_pony")
  387. # And deconstruction
  388. definition = operation.deconstruct()
  389. self.assertEqual(definition[0], "CreateModel")
  390. self.assertEqual(definition[1], [])
  391. self.assertEqual(sorted(definition[2]), ["bases", "fields", "name", "options"])
  392. def test_create_unmanaged_model(self):
  393. """
  394. CreateModel ignores unmanaged models.
  395. """
  396. project_state = self.set_up_test_model("test_crummo")
  397. # Test the state alteration
  398. operation = migrations.CreateModel(
  399. "UnmanagedPony",
  400. [],
  401. options={"proxy": True},
  402. bases=("test_crummo.Pony",),
  403. )
  404. self.assertEqual(operation.describe(), "Create proxy model UnmanagedPony")
  405. new_state = project_state.clone()
  406. operation.state_forwards("test_crummo", new_state)
  407. self.assertIn(("test_crummo", "unmanagedpony"), new_state.models)
  408. # Test the database alteration
  409. self.assertTableNotExists("test_crummo_unmanagedpony")
  410. self.assertTableExists("test_crummo_pony")
  411. with connection.schema_editor() as editor:
  412. operation.database_forwards("test_crummo", editor, project_state, new_state)
  413. self.assertTableNotExists("test_crummo_unmanagedpony")
  414. self.assertTableExists("test_crummo_pony")
  415. # And test reversal
  416. with connection.schema_editor() as editor:
  417. operation.database_backwards(
  418. "test_crummo", editor, new_state, project_state
  419. )
  420. self.assertTableNotExists("test_crummo_unmanagedpony")
  421. self.assertTableExists("test_crummo_pony")
  422. @skipUnlessDBFeature("supports_table_check_constraints")
  423. def test_create_model_with_constraint(self):
  424. where = models.Q(pink__gt=2)
  425. check_constraint = models.CheckConstraint(
  426. condition=where, name="test_constraint_pony_pink_gt_2"
  427. )
  428. operation = migrations.CreateModel(
  429. "Pony",
  430. [
  431. ("id", models.AutoField(primary_key=True)),
  432. ("pink", models.IntegerField(default=3)),
  433. ],
  434. options={"constraints": [check_constraint]},
  435. )
  436. # Test the state alteration
  437. project_state = ProjectState()
  438. new_state = project_state.clone()
  439. operation.state_forwards("test_crmo", new_state)
  440. self.assertEqual(
  441. len(new_state.models["test_crmo", "pony"].options["constraints"]), 1
  442. )
  443. # Test database alteration
  444. self.assertTableNotExists("test_crmo_pony")
  445. with connection.schema_editor() as editor:
  446. operation.database_forwards("test_crmo", editor, project_state, new_state)
  447. self.assertTableExists("test_crmo_pony")
  448. with connection.cursor() as cursor:
  449. with self.assertRaises(IntegrityError):
  450. cursor.execute("INSERT INTO test_crmo_pony (id, pink) VALUES (1, 1)")
  451. # Test reversal
  452. with connection.schema_editor() as editor:
  453. operation.database_backwards("test_crmo", editor, new_state, project_state)
  454. self.assertTableNotExists("test_crmo_pony")
  455. # Test deconstruction
  456. definition = operation.deconstruct()
  457. self.assertEqual(definition[0], "CreateModel")
  458. self.assertEqual(definition[1], [])
  459. self.assertEqual(definition[2]["options"]["constraints"], [check_constraint])
  460. @skipUnlessDBFeature("supports_table_check_constraints")
  461. def test_create_model_with_boolean_expression_in_check_constraint(self):
  462. app_label = "test_crmobechc"
  463. rawsql_constraint = models.CheckConstraint(
  464. condition=models.expressions.RawSQL(
  465. "price < %s", (1000,), output_field=models.BooleanField()
  466. ),
  467. name=f"{app_label}_price_lt_1000_raw",
  468. )
  469. wrapper_constraint = models.CheckConstraint(
  470. condition=models.expressions.ExpressionWrapper(
  471. models.Q(price__gt=500) | models.Q(price__lt=500),
  472. output_field=models.BooleanField(),
  473. ),
  474. name=f"{app_label}_price_neq_500_wrap",
  475. )
  476. operation = migrations.CreateModel(
  477. "Product",
  478. [
  479. ("id", models.AutoField(primary_key=True)),
  480. ("price", models.IntegerField(null=True)),
  481. ],
  482. options={"constraints": [rawsql_constraint, wrapper_constraint]},
  483. )
  484. project_state = ProjectState()
  485. new_state = project_state.clone()
  486. operation.state_forwards(app_label, new_state)
  487. # Add table.
  488. self.assertTableNotExists(app_label)
  489. with connection.schema_editor() as editor:
  490. operation.database_forwards(app_label, editor, project_state, new_state)
  491. self.assertTableExists(f"{app_label}_product")
  492. insert_sql = f"INSERT INTO {app_label}_product (id, price) VALUES (%d, %d)"
  493. with connection.cursor() as cursor:
  494. with self.assertRaises(IntegrityError):
  495. cursor.execute(insert_sql % (1, 1000))
  496. cursor.execute(insert_sql % (1, 999))
  497. with self.assertRaises(IntegrityError):
  498. cursor.execute(insert_sql % (2, 500))
  499. cursor.execute(insert_sql % (2, 499))
  500. def test_create_model_with_partial_unique_constraint(self):
  501. partial_unique_constraint = models.UniqueConstraint(
  502. fields=["pink"],
  503. condition=models.Q(weight__gt=5),
  504. name="test_constraint_pony_pink_for_weight_gt_5_uniq",
  505. )
  506. operation = migrations.CreateModel(
  507. "Pony",
  508. [
  509. ("id", models.AutoField(primary_key=True)),
  510. ("pink", models.IntegerField(default=3)),
  511. ("weight", models.FloatField()),
  512. ],
  513. options={"constraints": [partial_unique_constraint]},
  514. )
  515. # Test the state alteration
  516. project_state = ProjectState()
  517. new_state = project_state.clone()
  518. operation.state_forwards("test_crmo", new_state)
  519. self.assertEqual(
  520. len(new_state.models["test_crmo", "pony"].options["constraints"]), 1
  521. )
  522. # Test database alteration
  523. self.assertTableNotExists("test_crmo_pony")
  524. with connection.schema_editor() as editor:
  525. operation.database_forwards("test_crmo", editor, project_state, new_state)
  526. self.assertTableExists("test_crmo_pony")
  527. # Test constraint works
  528. Pony = new_state.apps.get_model("test_crmo", "Pony")
  529. Pony.objects.create(pink=1, weight=4.0)
  530. Pony.objects.create(pink=1, weight=4.0)
  531. Pony.objects.create(pink=1, weight=6.0)
  532. if connection.features.supports_partial_indexes:
  533. with self.assertRaises(IntegrityError):
  534. Pony.objects.create(pink=1, weight=7.0)
  535. else:
  536. Pony.objects.create(pink=1, weight=7.0)
  537. # Test reversal
  538. with connection.schema_editor() as editor:
  539. operation.database_backwards("test_crmo", editor, new_state, project_state)
  540. self.assertTableNotExists("test_crmo_pony")
  541. # Test deconstruction
  542. definition = operation.deconstruct()
  543. self.assertEqual(definition[0], "CreateModel")
  544. self.assertEqual(definition[1], [])
  545. self.assertEqual(
  546. definition[2]["options"]["constraints"], [partial_unique_constraint]
  547. )
  548. def test_create_model_with_deferred_unique_constraint(self):
  549. deferred_unique_constraint = models.UniqueConstraint(
  550. fields=["pink"],
  551. name="deferrable_pink_constraint",
  552. deferrable=models.Deferrable.DEFERRED,
  553. )
  554. operation = migrations.CreateModel(
  555. "Pony",
  556. [
  557. ("id", models.AutoField(primary_key=True)),
  558. ("pink", models.IntegerField(default=3)),
  559. ],
  560. options={"constraints": [deferred_unique_constraint]},
  561. )
  562. project_state = ProjectState()
  563. new_state = project_state.clone()
  564. operation.state_forwards("test_crmo", new_state)
  565. self.assertEqual(
  566. len(new_state.models["test_crmo", "pony"].options["constraints"]), 1
  567. )
  568. self.assertTableNotExists("test_crmo_pony")
  569. # Create table.
  570. with connection.schema_editor() as editor:
  571. operation.database_forwards("test_crmo", editor, project_state, new_state)
  572. self.assertTableExists("test_crmo_pony")
  573. Pony = new_state.apps.get_model("test_crmo", "Pony")
  574. Pony.objects.create(pink=1)
  575. if connection.features.supports_deferrable_unique_constraints:
  576. # Unique constraint is deferred.
  577. with transaction.atomic():
  578. obj = Pony.objects.create(pink=1)
  579. obj.pink = 2
  580. obj.save()
  581. # Constraint behavior can be changed with SET CONSTRAINTS.
  582. with self.assertRaises(IntegrityError):
  583. with transaction.atomic(), connection.cursor() as cursor:
  584. quoted_name = connection.ops.quote_name(
  585. deferred_unique_constraint.name
  586. )
  587. cursor.execute("SET CONSTRAINTS %s IMMEDIATE" % quoted_name)
  588. obj = Pony.objects.create(pink=1)
  589. obj.pink = 3
  590. obj.save()
  591. else:
  592. Pony.objects.create(pink=1)
  593. # Reversal.
  594. with connection.schema_editor() as editor:
  595. operation.database_backwards("test_crmo", editor, new_state, project_state)
  596. self.assertTableNotExists("test_crmo_pony")
  597. # Deconstruction.
  598. definition = operation.deconstruct()
  599. self.assertEqual(definition[0], "CreateModel")
  600. self.assertEqual(definition[1], [])
  601. self.assertEqual(
  602. definition[2]["options"]["constraints"],
  603. [deferred_unique_constraint],
  604. )
  605. @skipUnlessDBFeature("supports_covering_indexes")
  606. def test_create_model_with_covering_unique_constraint(self):
  607. covering_unique_constraint = models.UniqueConstraint(
  608. fields=["pink"],
  609. include=["weight"],
  610. name="test_constraint_pony_pink_covering_weight",
  611. )
  612. operation = migrations.CreateModel(
  613. "Pony",
  614. [
  615. ("id", models.AutoField(primary_key=True)),
  616. ("pink", models.IntegerField(default=3)),
  617. ("weight", models.FloatField()),
  618. ],
  619. options={"constraints": [covering_unique_constraint]},
  620. )
  621. project_state = ProjectState()
  622. new_state = project_state.clone()
  623. operation.state_forwards("test_crmo", new_state)
  624. self.assertEqual(
  625. len(new_state.models["test_crmo", "pony"].options["constraints"]), 1
  626. )
  627. self.assertTableNotExists("test_crmo_pony")
  628. # Create table.
  629. with connection.schema_editor() as editor:
  630. operation.database_forwards("test_crmo", editor, project_state, new_state)
  631. self.assertTableExists("test_crmo_pony")
  632. Pony = new_state.apps.get_model("test_crmo", "Pony")
  633. Pony.objects.create(pink=1, weight=4.0)
  634. with self.assertRaises(IntegrityError):
  635. Pony.objects.create(pink=1, weight=7.0)
  636. # Reversal.
  637. with connection.schema_editor() as editor:
  638. operation.database_backwards("test_crmo", editor, new_state, project_state)
  639. self.assertTableNotExists("test_crmo_pony")
  640. # Deconstruction.
  641. definition = operation.deconstruct()
  642. self.assertEqual(definition[0], "CreateModel")
  643. self.assertEqual(definition[1], [])
  644. self.assertEqual(
  645. definition[2]["options"]["constraints"],
  646. [covering_unique_constraint],
  647. )
  648. def test_create_model_managers(self):
  649. """
  650. The managers on a model are set.
  651. """
  652. project_state = self.set_up_test_model("test_cmoma")
  653. # Test the state alteration
  654. operation = migrations.CreateModel(
  655. "Food",
  656. fields=[
  657. ("id", models.AutoField(primary_key=True)),
  658. ],
  659. managers=[
  660. ("food_qs", FoodQuerySet.as_manager()),
  661. ("food_mgr", FoodManager("a", "b")),
  662. ("food_mgr_kwargs", FoodManager("x", "y", 3, 4)),
  663. ],
  664. )
  665. self.assertEqual(operation.describe(), "Create model Food")
  666. new_state = project_state.clone()
  667. operation.state_forwards("test_cmoma", new_state)
  668. self.assertIn(("test_cmoma", "food"), new_state.models)
  669. managers = new_state.models["test_cmoma", "food"].managers
  670. self.assertEqual(managers[0][0], "food_qs")
  671. self.assertIsInstance(managers[0][1], models.Manager)
  672. self.assertEqual(managers[1][0], "food_mgr")
  673. self.assertIsInstance(managers[1][1], FoodManager)
  674. self.assertEqual(managers[1][1].args, ("a", "b", 1, 2))
  675. self.assertEqual(managers[2][0], "food_mgr_kwargs")
  676. self.assertIsInstance(managers[2][1], FoodManager)
  677. self.assertEqual(managers[2][1].args, ("x", "y", 3, 4))
  678. def test_delete_model(self):
  679. """
  680. Tests the DeleteModel operation.
  681. """
  682. project_state = self.set_up_test_model("test_dlmo")
  683. # Test the state alteration
  684. operation = migrations.DeleteModel("Pony")
  685. self.assertEqual(operation.describe(), "Delete model Pony")
  686. self.assertEqual(operation.formatted_description(), "- Delete model Pony")
  687. self.assertEqual(operation.migration_name_fragment, "delete_pony")
  688. new_state = project_state.clone()
  689. operation.state_forwards("test_dlmo", new_state)
  690. self.assertNotIn(("test_dlmo", "pony"), new_state.models)
  691. # Test the database alteration
  692. self.assertTableExists("test_dlmo_pony")
  693. with connection.schema_editor() as editor:
  694. operation.database_forwards("test_dlmo", editor, project_state, new_state)
  695. self.assertTableNotExists("test_dlmo_pony")
  696. # And test reversal
  697. with connection.schema_editor() as editor:
  698. operation.database_backwards("test_dlmo", editor, new_state, project_state)
  699. self.assertTableExists("test_dlmo_pony")
  700. # And deconstruction
  701. definition = operation.deconstruct()
  702. self.assertEqual(definition[0], "DeleteModel")
  703. self.assertEqual(definition[1], [])
  704. self.assertEqual(list(definition[2]), ["name"])
  705. def test_delete_proxy_model(self):
  706. """
  707. Tests the DeleteModel operation ignores proxy models.
  708. """
  709. project_state = self.set_up_test_model("test_dlprmo", proxy_model=True)
  710. # Test the state alteration
  711. operation = migrations.DeleteModel("ProxyPony")
  712. new_state = project_state.clone()
  713. operation.state_forwards("test_dlprmo", new_state)
  714. self.assertIn(("test_dlprmo", "proxypony"), project_state.models)
  715. self.assertNotIn(("test_dlprmo", "proxypony"), new_state.models)
  716. # Test the database alteration
  717. self.assertTableExists("test_dlprmo_pony")
  718. self.assertTableNotExists("test_dlprmo_proxypony")
  719. with connection.schema_editor() as editor:
  720. operation.database_forwards("test_dlprmo", editor, project_state, new_state)
  721. self.assertTableExists("test_dlprmo_pony")
  722. self.assertTableNotExists("test_dlprmo_proxypony")
  723. # And test reversal
  724. with connection.schema_editor() as editor:
  725. operation.database_backwards(
  726. "test_dlprmo", editor, new_state, project_state
  727. )
  728. self.assertTableExists("test_dlprmo_pony")
  729. self.assertTableNotExists("test_dlprmo_proxypony")
  730. def test_delete_mti_model(self):
  731. project_state = self.set_up_test_model("test_dlmtimo", mti_model=True)
  732. # Test the state alteration
  733. operation = migrations.DeleteModel("ShetlandPony")
  734. new_state = project_state.clone()
  735. operation.state_forwards("test_dlmtimo", new_state)
  736. self.assertIn(("test_dlmtimo", "shetlandpony"), project_state.models)
  737. self.assertNotIn(("test_dlmtimo", "shetlandpony"), new_state.models)
  738. # Test the database alteration
  739. self.assertTableExists("test_dlmtimo_pony")
  740. self.assertTableExists("test_dlmtimo_shetlandpony")
  741. self.assertColumnExists("test_dlmtimo_shetlandpony", "pony_ptr_id")
  742. with connection.schema_editor() as editor:
  743. operation.database_forwards(
  744. "test_dlmtimo", editor, project_state, new_state
  745. )
  746. self.assertTableExists("test_dlmtimo_pony")
  747. self.assertTableNotExists("test_dlmtimo_shetlandpony")
  748. # And test reversal
  749. with connection.schema_editor() as editor:
  750. operation.database_backwards(
  751. "test_dlmtimo", editor, new_state, project_state
  752. )
  753. self.assertTableExists("test_dlmtimo_pony")
  754. self.assertTableExists("test_dlmtimo_shetlandpony")
  755. self.assertColumnExists("test_dlmtimo_shetlandpony", "pony_ptr_id")
  756. def test_rename_model(self):
  757. """
  758. Tests the RenameModel operation.
  759. """
  760. project_state = self.set_up_test_model("test_rnmo", related_model=True)
  761. # Test the state alteration
  762. operation = migrations.RenameModel("Pony", "Horse")
  763. self.assertEqual(operation.describe(), "Rename model Pony to Horse")
  764. self.assertEqual(
  765. operation.formatted_description(), "~ Rename model Pony to Horse"
  766. )
  767. self.assertEqual(operation.migration_name_fragment, "rename_pony_horse")
  768. # Test initial state and database
  769. self.assertIn(("test_rnmo", "pony"), project_state.models)
  770. self.assertNotIn(("test_rnmo", "horse"), project_state.models)
  771. self.assertTableExists("test_rnmo_pony")
  772. self.assertTableNotExists("test_rnmo_horse")
  773. if connection.features.supports_foreign_keys:
  774. self.assertFKExists(
  775. "test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id")
  776. )
  777. self.assertFKNotExists(
  778. "test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id")
  779. )
  780. # Migrate forwards
  781. new_state = project_state.clone()
  782. new_state = self.apply_operations("test_rnmo", new_state, [operation])
  783. # Test new state and database
  784. self.assertNotIn(("test_rnmo", "pony"), new_state.models)
  785. self.assertIn(("test_rnmo", "horse"), new_state.models)
  786. # RenameModel also repoints all incoming FKs and M2Ms
  787. self.assertEqual(
  788. new_state.models["test_rnmo", "rider"].fields["pony"].remote_field.model,
  789. "test_rnmo.Horse",
  790. )
  791. self.assertTableNotExists("test_rnmo_pony")
  792. self.assertTableExists("test_rnmo_horse")
  793. if connection.features.supports_foreign_keys:
  794. self.assertFKNotExists(
  795. "test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id")
  796. )
  797. self.assertFKExists(
  798. "test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id")
  799. )
  800. # Migrate backwards
  801. original_state = self.unapply_operations(
  802. "test_rnmo", project_state, [operation]
  803. )
  804. # Test original state and database
  805. self.assertIn(("test_rnmo", "pony"), original_state.models)
  806. self.assertNotIn(("test_rnmo", "horse"), original_state.models)
  807. self.assertEqual(
  808. original_state.models["test_rnmo", "rider"]
  809. .fields["pony"]
  810. .remote_field.model,
  811. "Pony",
  812. )
  813. self.assertTableExists("test_rnmo_pony")
  814. self.assertTableNotExists("test_rnmo_horse")
  815. if connection.features.supports_foreign_keys:
  816. self.assertFKExists(
  817. "test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id")
  818. )
  819. self.assertFKNotExists(
  820. "test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id")
  821. )
  822. # And deconstruction
  823. definition = operation.deconstruct()
  824. self.assertEqual(definition[0], "RenameModel")
  825. self.assertEqual(definition[1], [])
  826. self.assertEqual(definition[2], {"old_name": "Pony", "new_name": "Horse"})
  827. def test_rename_model_state_forwards(self):
  828. """
  829. RenameModel operations shouldn't trigger the caching of rendered apps
  830. on state without prior apps.
  831. """
  832. state = ProjectState()
  833. state.add_model(ModelState("migrations", "Foo", []))
  834. operation = migrations.RenameModel("Foo", "Bar")
  835. operation.state_forwards("migrations", state)
  836. self.assertNotIn("apps", state.__dict__)
  837. self.assertNotIn(("migrations", "foo"), state.models)
  838. self.assertIn(("migrations", "bar"), state.models)
  839. # Now with apps cached.
  840. apps = state.apps
  841. operation = migrations.RenameModel("Bar", "Foo")
  842. operation.state_forwards("migrations", state)
  843. self.assertIs(state.apps, apps)
  844. self.assertNotIn(("migrations", "bar"), state.models)
  845. self.assertIn(("migrations", "foo"), state.models)
  846. def test_rename_model_with_self_referential_fk(self):
  847. """
  848. Tests the RenameModel operation on model with self referential FK.
  849. """
  850. project_state = self.set_up_test_model("test_rmwsrf", related_model=True)
  851. # Test the state alteration
  852. operation = migrations.RenameModel("Rider", "HorseRider")
  853. self.assertEqual(operation.describe(), "Rename model Rider to HorseRider")
  854. new_state = project_state.clone()
  855. operation.state_forwards("test_rmwsrf", new_state)
  856. self.assertNotIn(("test_rmwsrf", "rider"), new_state.models)
  857. self.assertIn(("test_rmwsrf", "horserider"), new_state.models)
  858. # Remember, RenameModel also repoints all incoming FKs and M2Ms
  859. self.assertEqual(
  860. "self",
  861. new_state.models["test_rmwsrf", "horserider"]
  862. .fields["friend"]
  863. .remote_field.model,
  864. )
  865. HorseRider = new_state.apps.get_model("test_rmwsrf", "horserider")
  866. self.assertIs(
  867. HorseRider._meta.get_field("horserider").remote_field.model, HorseRider
  868. )
  869. # Test the database alteration
  870. self.assertTableExists("test_rmwsrf_rider")
  871. self.assertTableNotExists("test_rmwsrf_horserider")
  872. if connection.features.supports_foreign_keys:
  873. self.assertFKExists(
  874. "test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_rider", "id")
  875. )
  876. self.assertFKNotExists(
  877. "test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_horserider", "id")
  878. )
  879. with connection.schema_editor() as editor:
  880. operation.database_forwards("test_rmwsrf", editor, project_state, new_state)
  881. self.assertTableNotExists("test_rmwsrf_rider")
  882. self.assertTableExists("test_rmwsrf_horserider")
  883. if connection.features.supports_foreign_keys:
  884. self.assertFKNotExists(
  885. "test_rmwsrf_horserider", ["friend_id"], ("test_rmwsrf_rider", "id")
  886. )
  887. self.assertFKExists(
  888. "test_rmwsrf_horserider",
  889. ["friend_id"],
  890. ("test_rmwsrf_horserider", "id"),
  891. )
  892. # And test reversal
  893. with connection.schema_editor() as editor:
  894. operation.database_backwards(
  895. "test_rmwsrf", editor, new_state, project_state
  896. )
  897. self.assertTableExists("test_rmwsrf_rider")
  898. self.assertTableNotExists("test_rmwsrf_horserider")
  899. if connection.features.supports_foreign_keys:
  900. self.assertFKExists(
  901. "test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_rider", "id")
  902. )
  903. self.assertFKNotExists(
  904. "test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_horserider", "id")
  905. )
  906. def test_rename_model_with_superclass_fk(self):
  907. """
  908. Tests the RenameModel operation on a model which has a superclass that
  909. has a foreign key.
  910. """
  911. project_state = self.set_up_test_model(
  912. "test_rmwsc", related_model=True, mti_model=True
  913. )
  914. # Test the state alteration
  915. operation = migrations.RenameModel("ShetlandPony", "LittleHorse")
  916. self.assertEqual(
  917. operation.describe(), "Rename model ShetlandPony to LittleHorse"
  918. )
  919. new_state = project_state.clone()
  920. operation.state_forwards("test_rmwsc", new_state)
  921. self.assertNotIn(("test_rmwsc", "shetlandpony"), new_state.models)
  922. self.assertIn(("test_rmwsc", "littlehorse"), new_state.models)
  923. # RenameModel shouldn't repoint the superclass's relations, only local ones
  924. self.assertEqual(
  925. project_state.models["test_rmwsc", "rider"]
  926. .fields["pony"]
  927. .remote_field.model,
  928. new_state.models["test_rmwsc", "rider"].fields["pony"].remote_field.model,
  929. )
  930. # Before running the migration we have a table for Shetland Pony, not
  931. # Little Horse.
  932. self.assertTableExists("test_rmwsc_shetlandpony")
  933. self.assertTableNotExists("test_rmwsc_littlehorse")
  934. if connection.features.supports_foreign_keys:
  935. # and the foreign key on rider points to pony, not shetland pony
  936. self.assertFKExists(
  937. "test_rmwsc_rider", ["pony_id"], ("test_rmwsc_pony", "id")
  938. )
  939. self.assertFKNotExists(
  940. "test_rmwsc_rider", ["pony_id"], ("test_rmwsc_shetlandpony", "id")
  941. )
  942. with connection.schema_editor() as editor:
  943. operation.database_forwards("test_rmwsc", editor, project_state, new_state)
  944. # Now we have a little horse table, not shetland pony
  945. self.assertTableNotExists("test_rmwsc_shetlandpony")
  946. self.assertTableExists("test_rmwsc_littlehorse")
  947. if connection.features.supports_foreign_keys:
  948. # but the Foreign keys still point at pony, not little horse
  949. self.assertFKExists(
  950. "test_rmwsc_rider", ["pony_id"], ("test_rmwsc_pony", "id")
  951. )
  952. self.assertFKNotExists(
  953. "test_rmwsc_rider", ["pony_id"], ("test_rmwsc_littlehorse", "id")
  954. )
  955. def test_rename_model_no_relations_with_db_table_noop(self):
  956. app_label = "test_rmwdbtnoop"
  957. project_state = self.set_up_test_model(app_label, db_table="my_pony")
  958. operation = migrations.RenameModel("Pony", "LittleHorse")
  959. new_state = project_state.clone()
  960. operation.state_forwards(app_label, new_state)
  961. with connection.schema_editor() as editor, self.assertNumQueries(0):
  962. operation.database_forwards(app_label, editor, project_state, new_state)
  963. @skipUnlessDBFeature("supports_foreign_keys")
  964. def test_rename_model_with_db_table_and_fk_noop(self):
  965. app_label = "test_rmwdbtfk"
  966. project_state = self.set_up_test_model(
  967. app_label, db_table="my_pony", related_model=True
  968. )
  969. new_state = project_state.clone()
  970. operation = migrations.RenameModel("Pony", "LittleHorse")
  971. operation.state_forwards(app_label, new_state)
  972. with connection.schema_editor() as editor, self.assertNumQueries(0):
  973. operation.database_forwards(app_label, editor, project_state, new_state)
  974. def test_rename_model_with_self_referential_m2m(self):
  975. app_label = "test_rename_model_with_self_referential_m2m"
  976. project_state = self.apply_operations(
  977. app_label,
  978. ProjectState(),
  979. operations=[
  980. migrations.CreateModel(
  981. "ReflexivePony",
  982. fields=[
  983. ("id", models.AutoField(primary_key=True)),
  984. ("ponies", models.ManyToManyField("self")),
  985. ],
  986. ),
  987. ],
  988. )
  989. project_state = self.apply_operations(
  990. app_label,
  991. project_state,
  992. operations=[
  993. migrations.RenameModel("ReflexivePony", "ReflexivePony2"),
  994. ],
  995. )
  996. Pony = project_state.apps.get_model(app_label, "ReflexivePony2")
  997. pony = Pony.objects.create()
  998. pony.ponies.add(pony)
  999. def test_rename_model_with_m2m(self):
  1000. app_label = "test_rename_model_with_m2m"
  1001. project_state = self.apply_operations(
  1002. app_label,
  1003. ProjectState(),
  1004. operations=[
  1005. migrations.CreateModel(
  1006. "Rider",
  1007. fields=[
  1008. ("id", models.AutoField(primary_key=True)),
  1009. ],
  1010. ),
  1011. migrations.CreateModel(
  1012. "Pony",
  1013. fields=[
  1014. ("id", models.AutoField(primary_key=True)),
  1015. ("riders", models.ManyToManyField("Rider")),
  1016. ],
  1017. ),
  1018. ],
  1019. )
  1020. Pony = project_state.apps.get_model(app_label, "Pony")
  1021. Rider = project_state.apps.get_model(app_label, "Rider")
  1022. pony = Pony.objects.create()
  1023. rider = Rider.objects.create()
  1024. pony.riders.add(rider)
  1025. project_state = self.apply_operations(
  1026. app_label,
  1027. project_state,
  1028. operations=[
  1029. migrations.RenameModel("Pony", "Pony2"),
  1030. ],
  1031. )
  1032. Pony = project_state.apps.get_model(app_label, "Pony2")
  1033. Rider = project_state.apps.get_model(app_label, "Rider")
  1034. pony = Pony.objects.create()
  1035. rider = Rider.objects.create()
  1036. pony.riders.add(rider)
  1037. self.assertEqual(Pony.objects.count(), 2)
  1038. self.assertEqual(Rider.objects.count(), 2)
  1039. self.assertEqual(
  1040. Pony._meta.get_field("riders").remote_field.through.objects.count(), 2
  1041. )
  1042. def test_rename_model_with_m2m_models_in_different_apps_with_same_name(self):
  1043. app_label_1 = "test_rmw_m2m_1"
  1044. app_label_2 = "test_rmw_m2m_2"
  1045. project_state = self.apply_operations(
  1046. app_label_1,
  1047. ProjectState(),
  1048. operations=[
  1049. migrations.CreateModel(
  1050. "Rider",
  1051. fields=[
  1052. ("id", models.AutoField(primary_key=True)),
  1053. ],
  1054. ),
  1055. ],
  1056. )
  1057. project_state = self.apply_operations(
  1058. app_label_2,
  1059. project_state,
  1060. operations=[
  1061. migrations.CreateModel(
  1062. "Rider",
  1063. fields=[
  1064. ("id", models.AutoField(primary_key=True)),
  1065. ("riders", models.ManyToManyField(f"{app_label_1}.Rider")),
  1066. ],
  1067. ),
  1068. ],
  1069. )
  1070. m2m_table = f"{app_label_2}_rider_riders"
  1071. self.assertColumnExists(m2m_table, "from_rider_id")
  1072. self.assertColumnExists(m2m_table, "to_rider_id")
  1073. Rider_1 = project_state.apps.get_model(app_label_1, "Rider")
  1074. Rider_2 = project_state.apps.get_model(app_label_2, "Rider")
  1075. rider_2 = Rider_2.objects.create()
  1076. rider_2.riders.add(Rider_1.objects.create())
  1077. # Rename model.
  1078. project_state_2 = project_state.clone()
  1079. project_state = self.apply_operations(
  1080. app_label_2,
  1081. project_state,
  1082. operations=[migrations.RenameModel("Rider", "Pony")],
  1083. )
  1084. m2m_table = f"{app_label_2}_pony_riders"
  1085. self.assertColumnExists(m2m_table, "pony_id")
  1086. self.assertColumnExists(m2m_table, "rider_id")
  1087. Rider_1 = project_state.apps.get_model(app_label_1, "Rider")
  1088. Rider_2 = project_state.apps.get_model(app_label_2, "Pony")
  1089. rider_2 = Rider_2.objects.create()
  1090. rider_2.riders.add(Rider_1.objects.create())
  1091. self.assertEqual(Rider_1.objects.count(), 2)
  1092. self.assertEqual(Rider_2.objects.count(), 2)
  1093. self.assertEqual(
  1094. Rider_2._meta.get_field("riders").remote_field.through.objects.count(), 2
  1095. )
  1096. # Reversal.
  1097. self.unapply_operations(
  1098. app_label_2,
  1099. project_state_2,
  1100. operations=[migrations.RenameModel("Rider", "Pony")],
  1101. )
  1102. m2m_table = f"{app_label_2}_rider_riders"
  1103. self.assertColumnExists(m2m_table, "to_rider_id")
  1104. self.assertColumnExists(m2m_table, "from_rider_id")
  1105. def test_rename_model_with_db_table_rename_m2m(self):
  1106. app_label = "test_rmwdbrm2m"
  1107. project_state = self.apply_operations(
  1108. app_label,
  1109. ProjectState(),
  1110. operations=[
  1111. migrations.CreateModel(
  1112. "Rider",
  1113. fields=[
  1114. ("id", models.AutoField(primary_key=True)),
  1115. ],
  1116. ),
  1117. migrations.CreateModel(
  1118. "Pony",
  1119. fields=[
  1120. ("id", models.AutoField(primary_key=True)),
  1121. ("riders", models.ManyToManyField("Rider")),
  1122. ],
  1123. options={"db_table": "pony"},
  1124. ),
  1125. ],
  1126. )
  1127. new_state = self.apply_operations(
  1128. app_label,
  1129. project_state,
  1130. operations=[migrations.RenameModel("Pony", "PinkPony")],
  1131. )
  1132. Pony = new_state.apps.get_model(app_label, "PinkPony")
  1133. Rider = new_state.apps.get_model(app_label, "Rider")
  1134. pony = Pony.objects.create()
  1135. rider = Rider.objects.create()
  1136. pony.riders.add(rider)
  1137. def test_rename_m2m_target_model(self):
  1138. app_label = "test_rename_m2m_target_model"
  1139. project_state = self.apply_operations(
  1140. app_label,
  1141. ProjectState(),
  1142. operations=[
  1143. migrations.CreateModel(
  1144. "Rider",
  1145. fields=[
  1146. ("id", models.AutoField(primary_key=True)),
  1147. ],
  1148. ),
  1149. migrations.CreateModel(
  1150. "Pony",
  1151. fields=[
  1152. ("id", models.AutoField(primary_key=True)),
  1153. ("riders", models.ManyToManyField("Rider")),
  1154. ],
  1155. ),
  1156. ],
  1157. )
  1158. Pony = project_state.apps.get_model(app_label, "Pony")
  1159. Rider = project_state.apps.get_model(app_label, "Rider")
  1160. pony = Pony.objects.create()
  1161. rider = Rider.objects.create()
  1162. pony.riders.add(rider)
  1163. project_state = self.apply_operations(
  1164. app_label,
  1165. project_state,
  1166. operations=[
  1167. migrations.RenameModel("Rider", "Rider2"),
  1168. ],
  1169. )
  1170. Pony = project_state.apps.get_model(app_label, "Pony")
  1171. Rider = project_state.apps.get_model(app_label, "Rider2")
  1172. pony = Pony.objects.create()
  1173. rider = Rider.objects.create()
  1174. pony.riders.add(rider)
  1175. self.assertEqual(Pony.objects.count(), 2)
  1176. self.assertEqual(Rider.objects.count(), 2)
  1177. self.assertEqual(
  1178. Pony._meta.get_field("riders").remote_field.through.objects.count(), 2
  1179. )
  1180. def test_rename_m2m_through_model(self):
  1181. app_label = "test_rename_through"
  1182. project_state = self.apply_operations(
  1183. app_label,
  1184. ProjectState(),
  1185. operations=[
  1186. migrations.CreateModel(
  1187. "Rider",
  1188. fields=[
  1189. ("id", models.AutoField(primary_key=True)),
  1190. ],
  1191. ),
  1192. migrations.CreateModel(
  1193. "Pony",
  1194. fields=[
  1195. ("id", models.AutoField(primary_key=True)),
  1196. ],
  1197. ),
  1198. migrations.CreateModel(
  1199. "PonyRider",
  1200. fields=[
  1201. ("id", models.AutoField(primary_key=True)),
  1202. (
  1203. "rider",
  1204. models.ForeignKey(
  1205. "test_rename_through.Rider", models.CASCADE
  1206. ),
  1207. ),
  1208. (
  1209. "pony",
  1210. models.ForeignKey(
  1211. "test_rename_through.Pony", models.CASCADE
  1212. ),
  1213. ),
  1214. ],
  1215. ),
  1216. migrations.AddField(
  1217. "Pony",
  1218. "riders",
  1219. models.ManyToManyField(
  1220. "test_rename_through.Rider",
  1221. through="test_rename_through.PonyRider",
  1222. ),
  1223. ),
  1224. ],
  1225. )
  1226. Pony = project_state.apps.get_model(app_label, "Pony")
  1227. Rider = project_state.apps.get_model(app_label, "Rider")
  1228. PonyRider = project_state.apps.get_model(app_label, "PonyRider")
  1229. pony = Pony.objects.create()
  1230. rider = Rider.objects.create()
  1231. PonyRider.objects.create(pony=pony, rider=rider)
  1232. project_state = self.apply_operations(
  1233. app_label,
  1234. project_state,
  1235. operations=[
  1236. migrations.RenameModel("PonyRider", "PonyRider2"),
  1237. ],
  1238. )
  1239. Pony = project_state.apps.get_model(app_label, "Pony")
  1240. Rider = project_state.apps.get_model(app_label, "Rider")
  1241. PonyRider = project_state.apps.get_model(app_label, "PonyRider2")
  1242. pony = Pony.objects.first()
  1243. rider = Rider.objects.create()
  1244. PonyRider.objects.create(pony=pony, rider=rider)
  1245. self.assertEqual(Pony.objects.count(), 1)
  1246. self.assertEqual(Rider.objects.count(), 2)
  1247. self.assertEqual(PonyRider.objects.count(), 2)
  1248. self.assertEqual(pony.riders.count(), 2)
  1249. def test_rename_m2m_model_after_rename_field(self):
  1250. """RenameModel renames a many-to-many column after a RenameField."""
  1251. app_label = "test_rename_multiple"
  1252. project_state = self.apply_operations(
  1253. app_label,
  1254. ProjectState(),
  1255. operations=[
  1256. migrations.CreateModel(
  1257. "Pony",
  1258. fields=[
  1259. ("id", models.AutoField(primary_key=True)),
  1260. ("name", models.CharField(max_length=20)),
  1261. ],
  1262. ),
  1263. migrations.CreateModel(
  1264. "Rider",
  1265. fields=[
  1266. ("id", models.AutoField(primary_key=True)),
  1267. (
  1268. "pony",
  1269. models.ForeignKey(
  1270. "test_rename_multiple.Pony", models.CASCADE
  1271. ),
  1272. ),
  1273. ],
  1274. ),
  1275. migrations.CreateModel(
  1276. "PonyRider",
  1277. fields=[
  1278. ("id", models.AutoField(primary_key=True)),
  1279. ("riders", models.ManyToManyField("Rider")),
  1280. ],
  1281. ),
  1282. migrations.RenameField(
  1283. model_name="pony", old_name="name", new_name="fancy_name"
  1284. ),
  1285. migrations.RenameModel(old_name="Rider", new_name="Jockey"),
  1286. ],
  1287. )
  1288. Pony = project_state.apps.get_model(app_label, "Pony")
  1289. Jockey = project_state.apps.get_model(app_label, "Jockey")
  1290. PonyRider = project_state.apps.get_model(app_label, "PonyRider")
  1291. # No "no such column" error means the column was renamed correctly.
  1292. pony = Pony.objects.create(fancy_name="a good name")
  1293. jockey = Jockey.objects.create(pony=pony)
  1294. ponyrider = PonyRider.objects.create()
  1295. ponyrider.riders.add(jockey)
  1296. def test_rename_m2m_field_with_2_references(self):
  1297. app_label = "test_rename_many_refs"
  1298. project_state = self.apply_operations(
  1299. app_label,
  1300. ProjectState(),
  1301. operations=[
  1302. migrations.CreateModel(
  1303. name="Person",
  1304. fields=[
  1305. (
  1306. "id",
  1307. models.BigAutoField(
  1308. auto_created=True,
  1309. primary_key=True,
  1310. serialize=False,
  1311. verbose_name="ID",
  1312. ),
  1313. ),
  1314. ("name", models.CharField(max_length=255)),
  1315. ],
  1316. ),
  1317. migrations.CreateModel(
  1318. name="Relation",
  1319. fields=[
  1320. (
  1321. "id",
  1322. models.BigAutoField(
  1323. auto_created=True,
  1324. primary_key=True,
  1325. serialize=False,
  1326. verbose_name="ID",
  1327. ),
  1328. ),
  1329. (
  1330. "child",
  1331. models.ForeignKey(
  1332. on_delete=models.CASCADE,
  1333. related_name="relations_as_child",
  1334. to=f"{app_label}.person",
  1335. ),
  1336. ),
  1337. (
  1338. "parent",
  1339. models.ForeignKey(
  1340. on_delete=models.CASCADE,
  1341. related_name="relations_as_parent",
  1342. to=f"{app_label}.person",
  1343. ),
  1344. ),
  1345. ],
  1346. ),
  1347. migrations.AddField(
  1348. model_name="person",
  1349. name="parents_or_children",
  1350. field=models.ManyToManyField(
  1351. blank=True,
  1352. through=f"{app_label}.Relation",
  1353. to=f"{app_label}.person",
  1354. ),
  1355. ),
  1356. ],
  1357. )
  1358. Person = project_state.apps.get_model(app_label, "Person")
  1359. Relation = project_state.apps.get_model(app_label, "Relation")
  1360. person1 = Person.objects.create(name="John Doe")
  1361. person2 = Person.objects.create(name="Jane Smith")
  1362. Relation.objects.create(child=person2, parent=person1)
  1363. self.assertTableExists(app_label + "_person")
  1364. self.assertTableNotExists(app_label + "_other")
  1365. self.apply_operations(
  1366. app_label,
  1367. project_state,
  1368. operations=[
  1369. migrations.RenameModel(old_name="Person", new_name="Other"),
  1370. ],
  1371. )
  1372. self.assertTableNotExists(app_label + "_person")
  1373. self.assertTableExists(app_label + "_other")
  1374. def test_add_field(self):
  1375. """
  1376. Tests the AddField operation.
  1377. """
  1378. # Test the state alteration
  1379. operation = migrations.AddField(
  1380. "Pony",
  1381. "height",
  1382. models.FloatField(null=True, default=5),
  1383. )
  1384. self.assertEqual(operation.describe(), "Add field height to Pony")
  1385. self.assertEqual(
  1386. operation.formatted_description(), "+ Add field height to Pony"
  1387. )
  1388. self.assertEqual(operation.migration_name_fragment, "pony_height")
  1389. project_state, new_state = self.make_test_state("test_adfl", operation)
  1390. self.assertEqual(len(new_state.models["test_adfl", "pony"].fields), 6)
  1391. field = new_state.models["test_adfl", "pony"].fields["height"]
  1392. self.assertEqual(field.default, 5)
  1393. # Test the database alteration
  1394. self.assertColumnNotExists("test_adfl_pony", "height")
  1395. with connection.schema_editor() as editor:
  1396. operation.database_forwards("test_adfl", editor, project_state, new_state)
  1397. self.assertColumnExists("test_adfl_pony", "height")
  1398. # And test reversal
  1399. with connection.schema_editor() as editor:
  1400. operation.database_backwards("test_adfl", editor, new_state, project_state)
  1401. self.assertColumnNotExists("test_adfl_pony", "height")
  1402. # And deconstruction
  1403. definition = operation.deconstruct()
  1404. self.assertEqual(definition[0], "AddField")
  1405. self.assertEqual(definition[1], [])
  1406. self.assertEqual(sorted(definition[2]), ["field", "model_name", "name"])
  1407. @skipUnlessDBFeature("supports_stored_generated_columns")
  1408. def test_add_generated_field(self):
  1409. app_label = "test_add_generated_field"
  1410. project_state = self.apply_operations(
  1411. app_label,
  1412. ProjectState(),
  1413. operations=[
  1414. migrations.CreateModel(
  1415. "Rider",
  1416. fields=[
  1417. ("id", models.AutoField(primary_key=True)),
  1418. ],
  1419. ),
  1420. migrations.CreateModel(
  1421. "Pony",
  1422. fields=[
  1423. ("id", models.AutoField(primary_key=True)),
  1424. ("name", models.CharField(max_length=20)),
  1425. (
  1426. "rider",
  1427. models.ForeignKey(
  1428. f"{app_label}.Rider", on_delete=models.CASCADE
  1429. ),
  1430. ),
  1431. (
  1432. "name_and_id",
  1433. models.GeneratedField(
  1434. expression=Concat(("name"), ("rider_id")),
  1435. output_field=models.TextField(),
  1436. db_persist=True,
  1437. ),
  1438. ),
  1439. ],
  1440. ),
  1441. ],
  1442. )
  1443. Pony = project_state.apps.get_model(app_label, "Pony")
  1444. Rider = project_state.apps.get_model(app_label, "Rider")
  1445. rider = Rider.objects.create()
  1446. pony = Pony.objects.create(name="pony", rider=rider)
  1447. self.assertEqual(pony.name_and_id, str(pony.name) + str(rider.id))
  1448. new_rider = Rider.objects.create()
  1449. pony.rider = new_rider
  1450. pony.save()
  1451. pony.refresh_from_db()
  1452. self.assertEqual(pony.name_and_id, str(pony.name) + str(new_rider.id))
  1453. def test_add_charfield(self):
  1454. """
  1455. Tests the AddField operation on TextField.
  1456. """
  1457. project_state = self.set_up_test_model("test_adchfl")
  1458. Pony = project_state.apps.get_model("test_adchfl", "Pony")
  1459. pony = Pony.objects.create(weight=42)
  1460. new_state = self.apply_operations(
  1461. "test_adchfl",
  1462. project_state,
  1463. [
  1464. migrations.AddField(
  1465. "Pony",
  1466. "text",
  1467. models.CharField(max_length=10, default="some text"),
  1468. ),
  1469. migrations.AddField(
  1470. "Pony",
  1471. "empty",
  1472. models.CharField(max_length=10, default=""),
  1473. ),
  1474. # If not properly quoted digits would be interpreted as an int.
  1475. migrations.AddField(
  1476. "Pony",
  1477. "digits",
  1478. models.CharField(max_length=10, default="42"),
  1479. ),
  1480. # Manual quoting is fragile and could trip on quotes. Refs #xyz.
  1481. migrations.AddField(
  1482. "Pony",
  1483. "quotes",
  1484. models.CharField(max_length=10, default='"\'"'),
  1485. ),
  1486. ],
  1487. )
  1488. Pony = new_state.apps.get_model("test_adchfl", "Pony")
  1489. pony = Pony.objects.get(pk=pony.pk)
  1490. self.assertEqual(pony.text, "some text")
  1491. self.assertEqual(pony.empty, "")
  1492. self.assertEqual(pony.digits, "42")
  1493. self.assertEqual(pony.quotes, '"\'"')
  1494. def test_add_textfield(self):
  1495. """
  1496. Tests the AddField operation on TextField.
  1497. """
  1498. project_state = self.set_up_test_model("test_adtxtfl")
  1499. Pony = project_state.apps.get_model("test_adtxtfl", "Pony")
  1500. pony = Pony.objects.create(weight=42)
  1501. new_state = self.apply_operations(
  1502. "test_adtxtfl",
  1503. project_state,
  1504. [
  1505. migrations.AddField(
  1506. "Pony",
  1507. "text",
  1508. models.TextField(default="some text"),
  1509. ),
  1510. migrations.AddField(
  1511. "Pony",
  1512. "empty",
  1513. models.TextField(default=""),
  1514. ),
  1515. # If not properly quoted digits would be interpreted as an int.
  1516. migrations.AddField(
  1517. "Pony",
  1518. "digits",
  1519. models.TextField(default="42"),
  1520. ),
  1521. # Manual quoting is fragile and could trip on quotes. Refs #xyz.
  1522. migrations.AddField(
  1523. "Pony",
  1524. "quotes",
  1525. models.TextField(default='"\'"'),
  1526. ),
  1527. ],
  1528. )
  1529. Pony = new_state.apps.get_model("test_adtxtfl", "Pony")
  1530. pony = Pony.objects.get(pk=pony.pk)
  1531. self.assertEqual(pony.text, "some text")
  1532. self.assertEqual(pony.empty, "")
  1533. self.assertEqual(pony.digits, "42")
  1534. self.assertEqual(pony.quotes, '"\'"')
  1535. def test_add_binaryfield(self):
  1536. """
  1537. Tests the AddField operation on TextField/BinaryField.
  1538. """
  1539. project_state = self.set_up_test_model("test_adbinfl")
  1540. Pony = project_state.apps.get_model("test_adbinfl", "Pony")
  1541. pony = Pony.objects.create(weight=42)
  1542. new_state = self.apply_operations(
  1543. "test_adbinfl",
  1544. project_state,
  1545. [
  1546. migrations.AddField(
  1547. "Pony",
  1548. "blob",
  1549. models.BinaryField(default=b"some text"),
  1550. ),
  1551. migrations.AddField(
  1552. "Pony",
  1553. "empty",
  1554. models.BinaryField(default=b""),
  1555. ),
  1556. # If not properly quoted digits would be interpreted as an int.
  1557. migrations.AddField(
  1558. "Pony",
  1559. "digits",
  1560. models.BinaryField(default=b"42"),
  1561. ),
  1562. # Manual quoting is fragile and could trip on quotes. Refs #xyz.
  1563. migrations.AddField(
  1564. "Pony",
  1565. "quotes",
  1566. models.BinaryField(default=b'"\'"'),
  1567. ),
  1568. ],
  1569. )
  1570. Pony = new_state.apps.get_model("test_adbinfl", "Pony")
  1571. pony = Pony.objects.get(pk=pony.pk)
  1572. # SQLite returns buffer/memoryview, cast to bytes for checking.
  1573. self.assertEqual(bytes(pony.blob), b"some text")
  1574. self.assertEqual(bytes(pony.empty), b"")
  1575. self.assertEqual(bytes(pony.digits), b"42")
  1576. self.assertEqual(bytes(pony.quotes), b'"\'"')
  1577. def test_column_name_quoting(self):
  1578. """
  1579. Column names that are SQL keywords shouldn't cause problems when used
  1580. in migrations (#22168).
  1581. """
  1582. project_state = self.set_up_test_model("test_regr22168")
  1583. operation = migrations.AddField(
  1584. "Pony",
  1585. "order",
  1586. models.IntegerField(default=0),
  1587. )
  1588. new_state = project_state.clone()
  1589. operation.state_forwards("test_regr22168", new_state)
  1590. with connection.schema_editor() as editor:
  1591. operation.database_forwards(
  1592. "test_regr22168", editor, project_state, new_state
  1593. )
  1594. self.assertColumnExists("test_regr22168_pony", "order")
  1595. def test_add_field_preserve_default(self):
  1596. """
  1597. Tests the AddField operation's state alteration
  1598. when preserve_default = False.
  1599. """
  1600. project_state = self.set_up_test_model("test_adflpd")
  1601. # Test the state alteration
  1602. operation = migrations.AddField(
  1603. "Pony",
  1604. "height",
  1605. models.FloatField(null=True, default=4),
  1606. preserve_default=False,
  1607. )
  1608. new_state = project_state.clone()
  1609. operation.state_forwards("test_adflpd", new_state)
  1610. self.assertEqual(len(new_state.models["test_adflpd", "pony"].fields), 6)
  1611. field = new_state.models["test_adflpd", "pony"].fields["height"]
  1612. self.assertEqual(field.default, models.NOT_PROVIDED)
  1613. # Test the database alteration
  1614. project_state.apps.get_model("test_adflpd", "pony").objects.create(
  1615. weight=4,
  1616. )
  1617. self.assertColumnNotExists("test_adflpd_pony", "height")
  1618. with connection.schema_editor() as editor:
  1619. operation.database_forwards("test_adflpd", editor, project_state, new_state)
  1620. self.assertColumnExists("test_adflpd_pony", "height")
  1621. # And deconstruction
  1622. definition = operation.deconstruct()
  1623. self.assertEqual(definition[0], "AddField")
  1624. self.assertEqual(definition[1], [])
  1625. self.assertEqual(
  1626. sorted(definition[2]), ["field", "model_name", "name", "preserve_default"]
  1627. )
  1628. def test_add_field_database_default(self):
  1629. """The AddField operation can set and unset a database default."""
  1630. app_label = "test_adfldd"
  1631. table_name = f"{app_label}_pony"
  1632. project_state = self.set_up_test_model(app_label)
  1633. operation = migrations.AddField(
  1634. "Pony", "height", models.FloatField(null=True, db_default=4)
  1635. )
  1636. new_state = project_state.clone()
  1637. operation.state_forwards(app_label, new_state)
  1638. self.assertEqual(len(new_state.models[app_label, "pony"].fields), 6)
  1639. field = new_state.models[app_label, "pony"].fields["height"]
  1640. self.assertEqual(field.default, models.NOT_PROVIDED)
  1641. self.assertEqual(field.db_default, 4)
  1642. project_state.apps.get_model(app_label, "pony").objects.create(weight=4)
  1643. self.assertColumnNotExists(table_name, "height")
  1644. # Add field.
  1645. with connection.schema_editor() as editor:
  1646. operation.database_forwards(app_label, editor, project_state, new_state)
  1647. self.assertColumnExists(table_name, "height")
  1648. new_model = new_state.apps.get_model(app_label, "pony")
  1649. old_pony = new_model.objects.get()
  1650. self.assertEqual(old_pony.height, 4)
  1651. new_pony = new_model.objects.create(weight=5)
  1652. if not connection.features.can_return_columns_from_insert:
  1653. new_pony.refresh_from_db()
  1654. self.assertEqual(new_pony.height, 4)
  1655. # Reversal.
  1656. with connection.schema_editor() as editor:
  1657. operation.database_backwards(app_label, editor, new_state, project_state)
  1658. self.assertColumnNotExists(table_name, "height")
  1659. # Deconstruction.
  1660. definition = operation.deconstruct()
  1661. self.assertEqual(definition[0], "AddField")
  1662. self.assertEqual(definition[1], [])
  1663. self.assertEqual(
  1664. definition[2],
  1665. {
  1666. "field": field,
  1667. "model_name": "Pony",
  1668. "name": "height",
  1669. },
  1670. )
  1671. def test_add_field_database_default_special_char_escaping(self):
  1672. app_label = "test_adflddsce"
  1673. table_name = f"{app_label}_pony"
  1674. project_state = self.set_up_test_model(app_label)
  1675. old_pony_pk = (
  1676. project_state.apps.get_model(app_label, "pony").objects.create(weight=4).pk
  1677. )
  1678. tests = ["%", "'", '"']
  1679. for db_default in tests:
  1680. with self.subTest(db_default=db_default):
  1681. operation = migrations.AddField(
  1682. "Pony",
  1683. "special_char",
  1684. models.CharField(max_length=1, db_default=db_default),
  1685. )
  1686. new_state = project_state.clone()
  1687. operation.state_forwards(app_label, new_state)
  1688. self.assertEqual(len(new_state.models[app_label, "pony"].fields), 6)
  1689. field = new_state.models[app_label, "pony"].fields["special_char"]
  1690. self.assertEqual(field.default, models.NOT_PROVIDED)
  1691. self.assertEqual(field.db_default, db_default)
  1692. self.assertColumnNotExists(table_name, "special_char")
  1693. with connection.schema_editor() as editor:
  1694. operation.database_forwards(
  1695. app_label, editor, project_state, new_state
  1696. )
  1697. self.assertColumnExists(table_name, "special_char")
  1698. new_model = new_state.apps.get_model(app_label, "pony")
  1699. try:
  1700. new_pony = new_model.objects.create(weight=5)
  1701. if not connection.features.can_return_columns_from_insert:
  1702. new_pony.refresh_from_db()
  1703. self.assertEqual(new_pony.special_char, db_default)
  1704. old_pony = new_model.objects.get(pk=old_pony_pk)
  1705. if connection.vendor != "oracle" or db_default != "'":
  1706. # The single quotation mark ' is properly quoted and is
  1707. # set for new rows on Oracle, however it is not set on
  1708. # existing rows. Skip the assertion as it's probably a
  1709. # bug in Oracle.
  1710. self.assertEqual(old_pony.special_char, db_default)
  1711. finally:
  1712. with connection.schema_editor() as editor:
  1713. operation.database_backwards(
  1714. app_label, editor, new_state, project_state
  1715. )
  1716. @skipUnlessDBFeature("supports_expression_defaults")
  1717. def test_add_field_database_default_function(self):
  1718. app_label = "test_adflddf"
  1719. table_name = f"{app_label}_pony"
  1720. project_state = self.set_up_test_model(app_label)
  1721. operation = migrations.AddField(
  1722. "Pony", "height", models.FloatField(db_default=Pi())
  1723. )
  1724. new_state = project_state.clone()
  1725. operation.state_forwards(app_label, new_state)
  1726. self.assertEqual(len(new_state.models[app_label, "pony"].fields), 6)
  1727. field = new_state.models[app_label, "pony"].fields["height"]
  1728. self.assertEqual(field.default, models.NOT_PROVIDED)
  1729. self.assertEqual(field.db_default, Pi())
  1730. project_state.apps.get_model(app_label, "pony").objects.create(weight=4)
  1731. self.assertColumnNotExists(table_name, "height")
  1732. # Add field.
  1733. with connection.schema_editor() as editor:
  1734. operation.database_forwards(app_label, editor, project_state, new_state)
  1735. self.assertColumnExists(table_name, "height")
  1736. new_model = new_state.apps.get_model(app_label, "pony")
  1737. old_pony = new_model.objects.get()
  1738. self.assertAlmostEqual(old_pony.height, math.pi)
  1739. new_pony = new_model.objects.create(weight=5)
  1740. if not connection.features.can_return_columns_from_insert:
  1741. new_pony.refresh_from_db()
  1742. self.assertAlmostEqual(old_pony.height, math.pi)
  1743. def test_add_field_both_defaults(self):
  1744. """The AddField operation with both default and db_default."""
  1745. app_label = "test_adflbddd"
  1746. table_name = f"{app_label}_pony"
  1747. project_state = self.set_up_test_model(app_label)
  1748. operation = migrations.AddField(
  1749. "Pony", "height", models.FloatField(default=3, db_default=4)
  1750. )
  1751. new_state = project_state.clone()
  1752. operation.state_forwards(app_label, new_state)
  1753. self.assertEqual(len(new_state.models[app_label, "pony"].fields), 6)
  1754. field = new_state.models[app_label, "pony"].fields["height"]
  1755. self.assertEqual(field.default, 3)
  1756. self.assertEqual(field.db_default, 4)
  1757. pre_pony_pk = (
  1758. project_state.apps.get_model(app_label, "pony").objects.create(weight=4).pk
  1759. )
  1760. self.assertColumnNotExists(table_name, "height")
  1761. # Add field.
  1762. with connection.schema_editor() as editor:
  1763. operation.database_forwards(app_label, editor, project_state, new_state)
  1764. self.assertColumnExists(table_name, "height")
  1765. post_pony_pk = (
  1766. project_state.apps.get_model(app_label, "pony").objects.create(weight=10).pk
  1767. )
  1768. new_model = new_state.apps.get_model(app_label, "pony")
  1769. pre_pony = new_model.objects.get(pk=pre_pony_pk)
  1770. self.assertEqual(pre_pony.height, 4)
  1771. post_pony = new_model.objects.get(pk=post_pony_pk)
  1772. self.assertEqual(post_pony.height, 4)
  1773. new_pony = new_model.objects.create(weight=5)
  1774. if not connection.features.can_return_columns_from_insert:
  1775. new_pony.refresh_from_db()
  1776. self.assertEqual(new_pony.height, 3)
  1777. # Reversal.
  1778. with connection.schema_editor() as editor:
  1779. operation.database_backwards(app_label, editor, new_state, project_state)
  1780. self.assertColumnNotExists(table_name, "height")
  1781. # Deconstruction.
  1782. definition = operation.deconstruct()
  1783. self.assertEqual(definition[0], "AddField")
  1784. self.assertEqual(definition[1], [])
  1785. self.assertEqual(
  1786. definition[2],
  1787. {
  1788. "field": field,
  1789. "model_name": "Pony",
  1790. "name": "height",
  1791. },
  1792. )
  1793. def test_add_field_m2m(self):
  1794. """
  1795. Tests the AddField operation with a ManyToManyField.
  1796. """
  1797. project_state = self.set_up_test_model("test_adflmm", second_model=True)
  1798. # Test the state alteration
  1799. operation = migrations.AddField(
  1800. "Pony", "stables", models.ManyToManyField("Stable", related_name="ponies")
  1801. )
  1802. new_state = project_state.clone()
  1803. operation.state_forwards("test_adflmm", new_state)
  1804. self.assertEqual(len(new_state.models["test_adflmm", "pony"].fields), 6)
  1805. # Test the database alteration
  1806. self.assertTableNotExists("test_adflmm_pony_stables")
  1807. with connection.schema_editor() as editor:
  1808. operation.database_forwards("test_adflmm", editor, project_state, new_state)
  1809. self.assertTableExists("test_adflmm_pony_stables")
  1810. self.assertColumnNotExists("test_adflmm_pony", "stables")
  1811. # Make sure the M2M field actually works
  1812. with atomic():
  1813. Pony = new_state.apps.get_model("test_adflmm", "Pony")
  1814. p = Pony.objects.create(pink=False, weight=4.55)
  1815. p.stables.create()
  1816. self.assertEqual(p.stables.count(), 1)
  1817. p.stables.all().delete()
  1818. # And test reversal
  1819. with connection.schema_editor() as editor:
  1820. operation.database_backwards(
  1821. "test_adflmm", editor, new_state, project_state
  1822. )
  1823. self.assertTableNotExists("test_adflmm_pony_stables")
  1824. def test_alter_field_m2m(self):
  1825. project_state = self.set_up_test_model("test_alflmm", second_model=True)
  1826. project_state = self.apply_operations(
  1827. "test_alflmm",
  1828. project_state,
  1829. operations=[
  1830. migrations.AddField(
  1831. "Pony",
  1832. "stables",
  1833. models.ManyToManyField("Stable", related_name="ponies"),
  1834. )
  1835. ],
  1836. )
  1837. Pony = project_state.apps.get_model("test_alflmm", "Pony")
  1838. self.assertFalse(Pony._meta.get_field("stables").blank)
  1839. project_state = self.apply_operations(
  1840. "test_alflmm",
  1841. project_state,
  1842. operations=[
  1843. migrations.AlterField(
  1844. "Pony",
  1845. "stables",
  1846. models.ManyToManyField(
  1847. to="Stable", related_name="ponies", blank=True
  1848. ),
  1849. )
  1850. ],
  1851. )
  1852. Pony = project_state.apps.get_model("test_alflmm", "Pony")
  1853. self.assertTrue(Pony._meta.get_field("stables").blank)
  1854. def test_repoint_field_m2m(self):
  1855. project_state = self.set_up_test_model(
  1856. "test_alflmm", second_model=True, third_model=True
  1857. )
  1858. project_state = self.apply_operations(
  1859. "test_alflmm",
  1860. project_state,
  1861. operations=[
  1862. migrations.AddField(
  1863. "Pony",
  1864. "places",
  1865. models.ManyToManyField("Stable", related_name="ponies"),
  1866. )
  1867. ],
  1868. )
  1869. Pony = project_state.apps.get_model("test_alflmm", "Pony")
  1870. project_state = self.apply_operations(
  1871. "test_alflmm",
  1872. project_state,
  1873. operations=[
  1874. migrations.AlterField(
  1875. "Pony",
  1876. "places",
  1877. models.ManyToManyField(to="Van", related_name="ponies"),
  1878. )
  1879. ],
  1880. )
  1881. # Ensure the new field actually works
  1882. Pony = project_state.apps.get_model("test_alflmm", "Pony")
  1883. p = Pony.objects.create(pink=False, weight=4.55)
  1884. p.places.create()
  1885. self.assertEqual(p.places.count(), 1)
  1886. p.places.all().delete()
  1887. def test_remove_field_m2m(self):
  1888. project_state = self.set_up_test_model("test_rmflmm", second_model=True)
  1889. project_state = self.apply_operations(
  1890. "test_rmflmm",
  1891. project_state,
  1892. operations=[
  1893. migrations.AddField(
  1894. "Pony",
  1895. "stables",
  1896. models.ManyToManyField("Stable", related_name="ponies"),
  1897. )
  1898. ],
  1899. )
  1900. self.assertTableExists("test_rmflmm_pony_stables")
  1901. with_field_state = project_state.clone()
  1902. operations = [migrations.RemoveField("Pony", "stables")]
  1903. project_state = self.apply_operations(
  1904. "test_rmflmm", project_state, operations=operations
  1905. )
  1906. self.assertTableNotExists("test_rmflmm_pony_stables")
  1907. # And test reversal
  1908. self.unapply_operations("test_rmflmm", with_field_state, operations=operations)
  1909. self.assertTableExists("test_rmflmm_pony_stables")
  1910. def test_remove_field_m2m_with_through(self):
  1911. project_state = self.set_up_test_model("test_rmflmmwt", second_model=True)
  1912. self.assertTableNotExists("test_rmflmmwt_ponystables")
  1913. project_state = self.apply_operations(
  1914. "test_rmflmmwt",
  1915. project_state,
  1916. operations=[
  1917. migrations.CreateModel(
  1918. "PonyStables",
  1919. fields=[
  1920. (
  1921. "pony",
  1922. models.ForeignKey("test_rmflmmwt.Pony", models.CASCADE),
  1923. ),
  1924. (
  1925. "stable",
  1926. models.ForeignKey("test_rmflmmwt.Stable", models.CASCADE),
  1927. ),
  1928. ],
  1929. ),
  1930. migrations.AddField(
  1931. "Pony",
  1932. "stables",
  1933. models.ManyToManyField(
  1934. "Stable",
  1935. related_name="ponies",
  1936. through="test_rmflmmwt.PonyStables",
  1937. ),
  1938. ),
  1939. ],
  1940. )
  1941. self.assertTableExists("test_rmflmmwt_ponystables")
  1942. operations = [
  1943. migrations.RemoveField("Pony", "stables"),
  1944. migrations.DeleteModel("PonyStables"),
  1945. ]
  1946. self.apply_operations("test_rmflmmwt", project_state, operations=operations)
  1947. def test_remove_field(self):
  1948. """
  1949. Tests the RemoveField operation.
  1950. """
  1951. project_state = self.set_up_test_model("test_rmfl")
  1952. # Test the state alteration
  1953. operation = migrations.RemoveField("Pony", "pink")
  1954. self.assertEqual(operation.describe(), "Remove field pink from Pony")
  1955. self.assertEqual(
  1956. operation.formatted_description(), "- Remove field pink from Pony"
  1957. )
  1958. self.assertEqual(operation.migration_name_fragment, "remove_pony_pink")
  1959. new_state = project_state.clone()
  1960. operation.state_forwards("test_rmfl", new_state)
  1961. self.assertEqual(len(new_state.models["test_rmfl", "pony"].fields), 4)
  1962. # Test the database alteration
  1963. self.assertColumnExists("test_rmfl_pony", "pink")
  1964. with connection.schema_editor() as editor:
  1965. operation.database_forwards("test_rmfl", editor, project_state, new_state)
  1966. self.assertColumnNotExists("test_rmfl_pony", "pink")
  1967. # And test reversal
  1968. with connection.schema_editor() as editor:
  1969. operation.database_backwards("test_rmfl", editor, new_state, project_state)
  1970. self.assertColumnExists("test_rmfl_pony", "pink")
  1971. # And deconstruction
  1972. definition = operation.deconstruct()
  1973. self.assertEqual(definition[0], "RemoveField")
  1974. self.assertEqual(definition[1], [])
  1975. self.assertEqual(definition[2], {"model_name": "Pony", "name": "pink"})
  1976. def test_remove_fk(self):
  1977. """
  1978. Tests the RemoveField operation on a foreign key.
  1979. """
  1980. project_state = self.set_up_test_model("test_rfk", related_model=True)
  1981. self.assertColumnExists("test_rfk_rider", "pony_id")
  1982. operation = migrations.RemoveField("Rider", "pony")
  1983. new_state = project_state.clone()
  1984. operation.state_forwards("test_rfk", new_state)
  1985. with connection.schema_editor() as editor:
  1986. operation.database_forwards("test_rfk", editor, project_state, new_state)
  1987. self.assertColumnNotExists("test_rfk_rider", "pony_id")
  1988. with connection.schema_editor() as editor:
  1989. operation.database_backwards("test_rfk", editor, new_state, project_state)
  1990. self.assertColumnExists("test_rfk_rider", "pony_id")
  1991. def test_alter_model_table(self):
  1992. """
  1993. Tests the AlterModelTable operation.
  1994. """
  1995. project_state = self.set_up_test_model("test_almota")
  1996. # Test the state alteration
  1997. operation = migrations.AlterModelTable("Pony", "test_almota_pony_2")
  1998. self.assertEqual(
  1999. operation.describe(), "Rename table for Pony to test_almota_pony_2"
  2000. )
  2001. self.assertEqual(
  2002. operation.formatted_description(),
  2003. "~ Rename table for Pony to test_almota_pony_2",
  2004. )
  2005. self.assertEqual(operation.migration_name_fragment, "alter_pony_table")
  2006. new_state = project_state.clone()
  2007. operation.state_forwards("test_almota", new_state)
  2008. self.assertEqual(
  2009. new_state.models["test_almota", "pony"].options["db_table"],
  2010. "test_almota_pony_2",
  2011. )
  2012. # Test the database alteration
  2013. self.assertTableExists("test_almota_pony")
  2014. self.assertTableNotExists("test_almota_pony_2")
  2015. with connection.schema_editor() as editor:
  2016. operation.database_forwards("test_almota", editor, project_state, new_state)
  2017. self.assertTableNotExists("test_almota_pony")
  2018. self.assertTableExists("test_almota_pony_2")
  2019. # And test reversal
  2020. with connection.schema_editor() as editor:
  2021. operation.database_backwards(
  2022. "test_almota", editor, new_state, project_state
  2023. )
  2024. self.assertTableExists("test_almota_pony")
  2025. self.assertTableNotExists("test_almota_pony_2")
  2026. # And deconstruction
  2027. definition = operation.deconstruct()
  2028. self.assertEqual(definition[0], "AlterModelTable")
  2029. self.assertEqual(definition[1], [])
  2030. self.assertEqual(definition[2], {"name": "Pony", "table": "test_almota_pony_2"})
  2031. def test_alter_model_table_none(self):
  2032. """
  2033. Tests the AlterModelTable operation if the table name is set to None.
  2034. """
  2035. operation = migrations.AlterModelTable("Pony", None)
  2036. self.assertEqual(operation.describe(), "Rename table for Pony to (default)")
  2037. def test_alter_model_table_noop(self):
  2038. """
  2039. Tests the AlterModelTable operation if the table name is not changed.
  2040. """
  2041. project_state = self.set_up_test_model("test_almota")
  2042. # Test the state alteration
  2043. operation = migrations.AlterModelTable("Pony", "test_almota_pony")
  2044. new_state = project_state.clone()
  2045. operation.state_forwards("test_almota", new_state)
  2046. self.assertEqual(
  2047. new_state.models["test_almota", "pony"].options["db_table"],
  2048. "test_almota_pony",
  2049. )
  2050. # Test the database alteration
  2051. self.assertTableExists("test_almota_pony")
  2052. with connection.schema_editor() as editor:
  2053. operation.database_forwards("test_almota", editor, project_state, new_state)
  2054. self.assertTableExists("test_almota_pony")
  2055. # And test reversal
  2056. with connection.schema_editor() as editor:
  2057. operation.database_backwards(
  2058. "test_almota", editor, new_state, project_state
  2059. )
  2060. self.assertTableExists("test_almota_pony")
  2061. def test_alter_model_table_m2m(self):
  2062. """
  2063. AlterModelTable should rename auto-generated M2M tables.
  2064. """
  2065. app_label = "test_talflmltlm2m"
  2066. pony_db_table = "pony_foo"
  2067. project_state = self.set_up_test_model(
  2068. app_label, second_model=True, db_table=pony_db_table
  2069. )
  2070. # Add the M2M field
  2071. first_state = project_state.clone()
  2072. operation = migrations.AddField(
  2073. "Pony", "stables", models.ManyToManyField("Stable")
  2074. )
  2075. operation.state_forwards(app_label, first_state)
  2076. with connection.schema_editor() as editor:
  2077. operation.database_forwards(app_label, editor, project_state, first_state)
  2078. original_m2m_table = "%s_%s" % (pony_db_table, "stables")
  2079. new_m2m_table = "%s_%s" % (app_label, "pony_stables")
  2080. self.assertTableExists(original_m2m_table)
  2081. self.assertTableNotExists(new_m2m_table)
  2082. # Rename the Pony db_table which should also rename the m2m table.
  2083. second_state = first_state.clone()
  2084. operation = migrations.AlterModelTable(name="pony", table=None)
  2085. operation.state_forwards(app_label, second_state)
  2086. with connection.schema_editor() as editor:
  2087. operation.database_forwards(app_label, editor, first_state, second_state)
  2088. self.assertTableExists(new_m2m_table)
  2089. self.assertTableNotExists(original_m2m_table)
  2090. # And test reversal
  2091. with connection.schema_editor() as editor:
  2092. operation.database_backwards(app_label, editor, second_state, first_state)
  2093. self.assertTableExists(original_m2m_table)
  2094. self.assertTableNotExists(new_m2m_table)
  2095. def test_alter_model_table_m2m_field(self):
  2096. app_label = "test_talm2mfl"
  2097. project_state = self.set_up_test_model(app_label, second_model=True)
  2098. # Add the M2M field.
  2099. project_state = self.apply_operations(
  2100. app_label,
  2101. project_state,
  2102. operations=[
  2103. migrations.AddField(
  2104. "Pony",
  2105. "stables",
  2106. models.ManyToManyField("Stable"),
  2107. )
  2108. ],
  2109. )
  2110. m2m_table = f"{app_label}_pony_stables"
  2111. self.assertColumnExists(m2m_table, "pony_id")
  2112. self.assertColumnExists(m2m_table, "stable_id")
  2113. # Point the M2M field to self.
  2114. with_field_state = project_state.clone()
  2115. operations = [
  2116. migrations.AlterField(
  2117. model_name="Pony",
  2118. name="stables",
  2119. field=models.ManyToManyField("self"),
  2120. )
  2121. ]
  2122. project_state = self.apply_operations(
  2123. app_label, project_state, operations=operations
  2124. )
  2125. self.assertColumnExists(m2m_table, "from_pony_id")
  2126. self.assertColumnExists(m2m_table, "to_pony_id")
  2127. # Reversal.
  2128. self.unapply_operations(app_label, with_field_state, operations=operations)
  2129. self.assertColumnExists(m2m_table, "pony_id")
  2130. self.assertColumnExists(m2m_table, "stable_id")
  2131. def test_alter_field(self):
  2132. """
  2133. Tests the AlterField operation.
  2134. """
  2135. project_state = self.set_up_test_model("test_alfl")
  2136. # Test the state alteration
  2137. operation = migrations.AlterField(
  2138. "Pony", "pink", models.IntegerField(null=True)
  2139. )
  2140. self.assertEqual(operation.describe(), "Alter field pink on Pony")
  2141. self.assertEqual(
  2142. operation.formatted_description(), "~ Alter field pink on Pony"
  2143. )
  2144. self.assertEqual(operation.migration_name_fragment, "alter_pony_pink")
  2145. new_state = project_state.clone()
  2146. operation.state_forwards("test_alfl", new_state)
  2147. self.assertIs(
  2148. project_state.models["test_alfl", "pony"].fields["pink"].null, False
  2149. )
  2150. self.assertIs(new_state.models["test_alfl", "pony"].fields["pink"].null, True)
  2151. # Test the database alteration
  2152. self.assertColumnNotNull("test_alfl_pony", "pink")
  2153. with connection.schema_editor() as editor:
  2154. operation.database_forwards("test_alfl", editor, project_state, new_state)
  2155. self.assertColumnNull("test_alfl_pony", "pink")
  2156. # And test reversal
  2157. with connection.schema_editor() as editor:
  2158. operation.database_backwards("test_alfl", editor, new_state, project_state)
  2159. self.assertColumnNotNull("test_alfl_pony", "pink")
  2160. # And deconstruction
  2161. definition = operation.deconstruct()
  2162. self.assertEqual(definition[0], "AlterField")
  2163. self.assertEqual(definition[1], [])
  2164. self.assertEqual(sorted(definition[2]), ["field", "model_name", "name"])
  2165. def test_alter_field_add_database_default(self):
  2166. app_label = "test_alfladd"
  2167. project_state = self.set_up_test_model(app_label)
  2168. operation = migrations.AlterField(
  2169. "Pony", "weight", models.FloatField(db_default=4.5)
  2170. )
  2171. new_state = project_state.clone()
  2172. operation.state_forwards(app_label, new_state)
  2173. old_weight = project_state.models[app_label, "pony"].fields["weight"]
  2174. self.assertIs(old_weight.db_default, models.NOT_PROVIDED)
  2175. new_weight = new_state.models[app_label, "pony"].fields["weight"]
  2176. self.assertEqual(new_weight.db_default, 4.5)
  2177. with self.assertRaises(IntegrityError), transaction.atomic():
  2178. project_state.apps.get_model(app_label, "pony").objects.create()
  2179. # Alter field.
  2180. with connection.schema_editor() as editor:
  2181. operation.database_forwards(app_label, editor, project_state, new_state)
  2182. pony = new_state.apps.get_model(app_label, "pony").objects.create()
  2183. if not connection.features.can_return_columns_from_insert:
  2184. pony.refresh_from_db()
  2185. self.assertEqual(pony.weight, 4.5)
  2186. # Reversal.
  2187. with connection.schema_editor() as editor:
  2188. operation.database_backwards(app_label, editor, new_state, project_state)
  2189. with self.assertRaises(IntegrityError), transaction.atomic():
  2190. project_state.apps.get_model(app_label, "pony").objects.create()
  2191. # Deconstruction.
  2192. definition = operation.deconstruct()
  2193. self.assertEqual(definition[0], "AlterField")
  2194. self.assertEqual(definition[1], [])
  2195. self.assertEqual(
  2196. definition[2],
  2197. {
  2198. "field": new_weight,
  2199. "model_name": "Pony",
  2200. "name": "weight",
  2201. },
  2202. )
  2203. def test_alter_field_change_default_to_database_default(self):
  2204. """The AlterField operation changing default to db_default."""
  2205. app_label = "test_alflcdtdd"
  2206. project_state = self.set_up_test_model(app_label)
  2207. operation = migrations.AlterField(
  2208. "Pony", "pink", models.IntegerField(db_default=4)
  2209. )
  2210. new_state = project_state.clone()
  2211. operation.state_forwards(app_label, new_state)
  2212. old_pink = project_state.models[app_label, "pony"].fields["pink"]
  2213. self.assertEqual(old_pink.default, 3)
  2214. self.assertIs(old_pink.db_default, models.NOT_PROVIDED)
  2215. new_pink = new_state.models[app_label, "pony"].fields["pink"]
  2216. self.assertIs(new_pink.default, models.NOT_PROVIDED)
  2217. self.assertEqual(new_pink.db_default, 4)
  2218. pony = project_state.apps.get_model(app_label, "pony").objects.create(weight=1)
  2219. self.assertEqual(pony.pink, 3)
  2220. # Alter field.
  2221. with connection.schema_editor() as editor:
  2222. operation.database_forwards(app_label, editor, project_state, new_state)
  2223. pony = new_state.apps.get_model(app_label, "pony").objects.create(weight=1)
  2224. if not connection.features.can_return_columns_from_insert:
  2225. pony.refresh_from_db()
  2226. self.assertEqual(pony.pink, 4)
  2227. # Reversal.
  2228. with connection.schema_editor() as editor:
  2229. operation.database_backwards(app_label, editor, new_state, project_state)
  2230. pony = project_state.apps.get_model(app_label, "pony").objects.create(weight=1)
  2231. self.assertEqual(pony.pink, 3)
  2232. def test_alter_field_change_nullable_to_database_default_not_null(self):
  2233. """
  2234. The AlterField operation changing a null field to db_default.
  2235. """
  2236. app_label = "test_alflcntddnn"
  2237. project_state = self.set_up_test_model(app_label)
  2238. operation = migrations.AlterField(
  2239. "Pony", "green", models.IntegerField(db_default=4)
  2240. )
  2241. new_state = project_state.clone()
  2242. operation.state_forwards(app_label, new_state)
  2243. old_green = project_state.models[app_label, "pony"].fields["green"]
  2244. self.assertIs(old_green.db_default, models.NOT_PROVIDED)
  2245. new_green = new_state.models[app_label, "pony"].fields["green"]
  2246. self.assertEqual(new_green.db_default, 4)
  2247. old_pony = project_state.apps.get_model(app_label, "pony").objects.create(
  2248. weight=1
  2249. )
  2250. self.assertIsNone(old_pony.green)
  2251. # Alter field.
  2252. with connection.schema_editor() as editor:
  2253. operation.database_forwards(app_label, editor, project_state, new_state)
  2254. old_pony.refresh_from_db()
  2255. self.assertEqual(old_pony.green, 4)
  2256. pony = new_state.apps.get_model(app_label, "pony").objects.create(weight=1)
  2257. if not connection.features.can_return_columns_from_insert:
  2258. pony.refresh_from_db()
  2259. self.assertEqual(pony.green, 4)
  2260. # Reversal.
  2261. with connection.schema_editor() as editor:
  2262. operation.database_backwards(app_label, editor, new_state, project_state)
  2263. pony = project_state.apps.get_model(app_label, "pony").objects.create(weight=1)
  2264. self.assertIsNone(pony.green)
  2265. def test_alter_field_change_nullable_to_decimal_database_default_not_null(self):
  2266. app_label = "test_alflcntdddn"
  2267. project_state = self.set_up_test_model(app_label)
  2268. operation_1 = migrations.AddField(
  2269. "Pony",
  2270. "height",
  2271. models.DecimalField(null=True, max_digits=5, decimal_places=2),
  2272. )
  2273. operation_2 = migrations.AlterField(
  2274. "Pony",
  2275. "height",
  2276. models.DecimalField(
  2277. max_digits=5, decimal_places=2, db_default=Decimal("12.22")
  2278. ),
  2279. )
  2280. table_name = f"{app_label}_pony"
  2281. self.assertColumnNotExists(table_name, "height")
  2282. # Add field.
  2283. new_state = project_state.clone()
  2284. operation_1.state_forwards(app_label, new_state)
  2285. with connection.schema_editor() as editor:
  2286. operation_1.database_forwards(app_label, editor, project_state, new_state)
  2287. self.assertColumnExists(table_name, "height")
  2288. old_pony = new_state.apps.get_model(app_label, "pony").objects.create(weight=1)
  2289. self.assertIsNone(old_pony.height)
  2290. # Alter field.
  2291. project_state, new_state = new_state, new_state.clone()
  2292. operation_2.state_forwards(app_label, new_state)
  2293. with connection.schema_editor() as editor:
  2294. operation_2.database_forwards(app_label, editor, project_state, new_state)
  2295. old_pony.refresh_from_db()
  2296. self.assertEqual(old_pony.height, Decimal("12.22"))
  2297. pony = new_state.apps.get_model(app_label, "pony").objects.create(weight=2)
  2298. if not connection.features.can_return_columns_from_insert:
  2299. pony.refresh_from_db()
  2300. self.assertEqual(pony.height, Decimal("12.22"))
  2301. @skipIfDBFeature("interprets_empty_strings_as_nulls")
  2302. def test_alter_field_change_blank_nullable_database_default_to_not_null(self):
  2303. app_label = "test_alflcbnddnn"
  2304. table_name = f"{app_label}_pony"
  2305. project_state = self.set_up_test_model(app_label)
  2306. default = "Yellow"
  2307. operation = migrations.AlterField(
  2308. "Pony",
  2309. "yellow",
  2310. models.CharField(blank=True, db_default=default, max_length=20),
  2311. )
  2312. new_state = project_state.clone()
  2313. operation.state_forwards(app_label, new_state)
  2314. self.assertColumnNull(table_name, "yellow")
  2315. pony = project_state.apps.get_model(app_label, "pony").objects.create(
  2316. weight=1, yellow=None
  2317. )
  2318. self.assertIsNone(pony.yellow)
  2319. # Alter field.
  2320. with connection.schema_editor() as editor:
  2321. operation.database_forwards(app_label, editor, project_state, new_state)
  2322. self.assertColumnNotNull(table_name, "yellow")
  2323. pony.refresh_from_db()
  2324. self.assertEqual(pony.yellow, default)
  2325. pony = new_state.apps.get_model(app_label, "pony").objects.create(weight=1)
  2326. if not connection.features.can_return_columns_from_insert:
  2327. pony.refresh_from_db()
  2328. self.assertEqual(pony.yellow, default)
  2329. # Reversal.
  2330. with connection.schema_editor() as editor:
  2331. operation.database_backwards(app_label, editor, new_state, project_state)
  2332. self.assertColumnNull(table_name, "yellow")
  2333. pony = project_state.apps.get_model(app_label, "pony").objects.create(
  2334. weight=1, yellow=None
  2335. )
  2336. self.assertIsNone(pony.yellow)
  2337. def test_alter_field_add_db_column_noop(self):
  2338. """
  2339. AlterField operation is a noop when adding only a db_column and the
  2340. column name is not changed.
  2341. """
  2342. app_label = "test_afadbn"
  2343. project_state = self.set_up_test_model(app_label, related_model=True)
  2344. pony_table = "%s_pony" % app_label
  2345. new_state = project_state.clone()
  2346. operation = migrations.AlterField(
  2347. "Pony", "weight", models.FloatField(db_column="weight")
  2348. )
  2349. operation.state_forwards(app_label, new_state)
  2350. self.assertIsNone(
  2351. project_state.models[app_label, "pony"].fields["weight"].db_column,
  2352. )
  2353. self.assertEqual(
  2354. new_state.models[app_label, "pony"].fields["weight"].db_column,
  2355. "weight",
  2356. )
  2357. self.assertColumnExists(pony_table, "weight")
  2358. with connection.schema_editor() as editor:
  2359. with self.assertNumQueries(0):
  2360. operation.database_forwards(app_label, editor, project_state, new_state)
  2361. self.assertColumnExists(pony_table, "weight")
  2362. with connection.schema_editor() as editor:
  2363. with self.assertNumQueries(0):
  2364. operation.database_backwards(
  2365. app_label, editor, new_state, project_state
  2366. )
  2367. self.assertColumnExists(pony_table, "weight")
  2368. rider_table = "%s_rider" % app_label
  2369. new_state = project_state.clone()
  2370. operation = migrations.AlterField(
  2371. "Rider",
  2372. "pony",
  2373. models.ForeignKey("Pony", models.CASCADE, db_column="pony_id"),
  2374. )
  2375. operation.state_forwards(app_label, new_state)
  2376. self.assertIsNone(
  2377. project_state.models[app_label, "rider"].fields["pony"].db_column,
  2378. )
  2379. self.assertIs(
  2380. new_state.models[app_label, "rider"].fields["pony"].db_column,
  2381. "pony_id",
  2382. )
  2383. self.assertColumnExists(rider_table, "pony_id")
  2384. with connection.schema_editor() as editor:
  2385. with self.assertNumQueries(0):
  2386. operation.database_forwards(app_label, editor, project_state, new_state)
  2387. self.assertColumnExists(rider_table, "pony_id")
  2388. with connection.schema_editor() as editor:
  2389. with self.assertNumQueries(0):
  2390. operation.database_forwards(app_label, editor, new_state, project_state)
  2391. self.assertColumnExists(rider_table, "pony_id")
  2392. def test_alter_field_foreignobject_noop(self):
  2393. app_label = "test_alflfo_noop"
  2394. project_state = self.set_up_test_model(app_label)
  2395. project_state = self.apply_operations(
  2396. app_label,
  2397. project_state,
  2398. [
  2399. migrations.CreateModel(
  2400. "Rider",
  2401. fields=[
  2402. ("pony_id", models.IntegerField()),
  2403. (
  2404. "pony",
  2405. models.ForeignObject(
  2406. f"{app_label}.Pony",
  2407. models.CASCADE,
  2408. from_fields=("pony_id",),
  2409. to_fields=("id",),
  2410. ),
  2411. ),
  2412. ],
  2413. ),
  2414. ],
  2415. )
  2416. operation = migrations.AlterField(
  2417. "Rider",
  2418. "pony",
  2419. models.ForeignObject(
  2420. f"{app_label}.Pony",
  2421. models.CASCADE,
  2422. from_fields=("pony_id",),
  2423. to_fields=("id",),
  2424. null=True,
  2425. ),
  2426. )
  2427. new_state = project_state.clone()
  2428. operation.state_forwards(app_label, new_state)
  2429. with (
  2430. CaptureQueriesContext(connection) as ctx,
  2431. connection.schema_editor() as editor,
  2432. ):
  2433. operation.database_forwards(app_label, editor, project_state, new_state)
  2434. self.assertIs(
  2435. any("ALTER" in query["sql"] for query in ctx.captured_queries), False
  2436. )
  2437. @skipUnlessDBFeature("supports_comments")
  2438. def test_alter_model_table_comment(self):
  2439. app_label = "test_almotaco"
  2440. project_state = self.set_up_test_model(app_label)
  2441. pony_table = f"{app_label}_pony"
  2442. # Add table comment.
  2443. operation = migrations.AlterModelTableComment("Pony", "Custom pony comment")
  2444. self.assertEqual(operation.describe(), "Alter Pony table comment")
  2445. self.assertEqual(
  2446. operation.formatted_description(), "~ Alter Pony table comment"
  2447. )
  2448. self.assertEqual(operation.migration_name_fragment, "alter_pony_table_comment")
  2449. new_state = project_state.clone()
  2450. operation.state_forwards(app_label, new_state)
  2451. self.assertEqual(
  2452. new_state.models[app_label, "pony"].options["db_table_comment"],
  2453. "Custom pony comment",
  2454. )
  2455. self.assertTableCommentNotExists(pony_table)
  2456. with connection.schema_editor() as editor:
  2457. operation.database_forwards(app_label, editor, project_state, new_state)
  2458. self.assertTableComment(pony_table, "Custom pony comment")
  2459. # Reversal.
  2460. with connection.schema_editor() as editor:
  2461. operation.database_backwards(app_label, editor, new_state, project_state)
  2462. self.assertTableCommentNotExists(pony_table)
  2463. # Deconstruction.
  2464. definition = operation.deconstruct()
  2465. self.assertEqual(definition[0], "AlterModelTableComment")
  2466. self.assertEqual(definition[1], [])
  2467. self.assertEqual(
  2468. definition[2], {"name": "Pony", "table_comment": "Custom pony comment"}
  2469. )
  2470. def test_alter_field_pk(self):
  2471. """
  2472. The AlterField operation on primary keys (things like PostgreSQL's
  2473. SERIAL weirdness).
  2474. """
  2475. project_state = self.set_up_test_model("test_alflpk")
  2476. # Test the state alteration
  2477. operation = migrations.AlterField(
  2478. "Pony", "id", models.IntegerField(primary_key=True)
  2479. )
  2480. new_state = project_state.clone()
  2481. operation.state_forwards("test_alflpk", new_state)
  2482. self.assertIsInstance(
  2483. project_state.models["test_alflpk", "pony"].fields["id"],
  2484. models.AutoField,
  2485. )
  2486. self.assertIsInstance(
  2487. new_state.models["test_alflpk", "pony"].fields["id"],
  2488. models.IntegerField,
  2489. )
  2490. # Test the database alteration
  2491. with connection.schema_editor() as editor:
  2492. operation.database_forwards("test_alflpk", editor, project_state, new_state)
  2493. # And test reversal
  2494. with connection.schema_editor() as editor:
  2495. operation.database_backwards(
  2496. "test_alflpk", editor, new_state, project_state
  2497. )
  2498. @skipUnlessDBFeature("supports_foreign_keys")
  2499. def test_alter_field_pk_fk(self):
  2500. """
  2501. Tests the AlterField operation on primary keys changes any FKs pointing to it.
  2502. """
  2503. project_state = self.set_up_test_model("test_alflpkfk", related_model=True)
  2504. project_state = self.apply_operations(
  2505. "test_alflpkfk",
  2506. project_state,
  2507. [
  2508. migrations.CreateModel(
  2509. "Stable",
  2510. fields=[
  2511. ("ponies", models.ManyToManyField("Pony")),
  2512. ],
  2513. ),
  2514. migrations.AddField(
  2515. "Pony",
  2516. "stables",
  2517. models.ManyToManyField("Stable"),
  2518. ),
  2519. ],
  2520. )
  2521. # Test the state alteration
  2522. operation = migrations.AlterField(
  2523. "Pony", "id", models.FloatField(primary_key=True)
  2524. )
  2525. new_state = project_state.clone()
  2526. operation.state_forwards("test_alflpkfk", new_state)
  2527. self.assertIsInstance(
  2528. project_state.models["test_alflpkfk", "pony"].fields["id"],
  2529. models.AutoField,
  2530. )
  2531. self.assertIsInstance(
  2532. new_state.models["test_alflpkfk", "pony"].fields["id"],
  2533. models.FloatField,
  2534. )
  2535. def assertIdTypeEqualsFkType():
  2536. with connection.cursor() as cursor:
  2537. id_type, id_null = [
  2538. (c.type_code, c.null_ok)
  2539. for c in connection.introspection.get_table_description(
  2540. cursor, "test_alflpkfk_pony"
  2541. )
  2542. if c.name == "id"
  2543. ][0]
  2544. fk_type, fk_null = [
  2545. (c.type_code, c.null_ok)
  2546. for c in connection.introspection.get_table_description(
  2547. cursor, "test_alflpkfk_rider"
  2548. )
  2549. if c.name == "pony_id"
  2550. ][0]
  2551. m2m_fk_type, m2m_fk_null = [
  2552. (c.type_code, c.null_ok)
  2553. for c in connection.introspection.get_table_description(
  2554. cursor,
  2555. "test_alflpkfk_pony_stables",
  2556. )
  2557. if c.name == "pony_id"
  2558. ][0]
  2559. remote_m2m_fk_type, remote_m2m_fk_null = [
  2560. (c.type_code, c.null_ok)
  2561. for c in connection.introspection.get_table_description(
  2562. cursor,
  2563. "test_alflpkfk_stable_ponies",
  2564. )
  2565. if c.name == "pony_id"
  2566. ][0]
  2567. self.assertEqual(id_type, fk_type)
  2568. self.assertEqual(id_type, m2m_fk_type)
  2569. self.assertEqual(id_type, remote_m2m_fk_type)
  2570. self.assertEqual(id_null, fk_null)
  2571. self.assertEqual(id_null, m2m_fk_null)
  2572. self.assertEqual(id_null, remote_m2m_fk_null)
  2573. assertIdTypeEqualsFkType()
  2574. # Test the database alteration
  2575. with connection.schema_editor() as editor:
  2576. operation.database_forwards(
  2577. "test_alflpkfk", editor, project_state, new_state
  2578. )
  2579. assertIdTypeEqualsFkType()
  2580. if connection.features.supports_foreign_keys:
  2581. self.assertFKExists(
  2582. "test_alflpkfk_pony_stables",
  2583. ["pony_id"],
  2584. ("test_alflpkfk_pony", "id"),
  2585. )
  2586. self.assertFKExists(
  2587. "test_alflpkfk_stable_ponies",
  2588. ["pony_id"],
  2589. ("test_alflpkfk_pony", "id"),
  2590. )
  2591. # And test reversal
  2592. with connection.schema_editor() as editor:
  2593. operation.database_backwards(
  2594. "test_alflpkfk", editor, new_state, project_state
  2595. )
  2596. assertIdTypeEqualsFkType()
  2597. if connection.features.supports_foreign_keys:
  2598. self.assertFKExists(
  2599. "test_alflpkfk_pony_stables",
  2600. ["pony_id"],
  2601. ("test_alflpkfk_pony", "id"),
  2602. )
  2603. self.assertFKExists(
  2604. "test_alflpkfk_stable_ponies",
  2605. ["pony_id"],
  2606. ("test_alflpkfk_pony", "id"),
  2607. )
  2608. @skipUnlessDBFeature("supports_collation_on_charfield", "supports_foreign_keys")
  2609. def test_alter_field_pk_fk_db_collation(self):
  2610. """
  2611. AlterField operation of db_collation on primary keys changes any FKs
  2612. pointing to it.
  2613. """
  2614. collation = connection.features.test_collations.get("non_default")
  2615. if not collation:
  2616. self.skipTest("Language collations are not supported.")
  2617. app_label = "test_alflpkfkdbc"
  2618. project_state = self.apply_operations(
  2619. app_label,
  2620. ProjectState(),
  2621. [
  2622. migrations.CreateModel(
  2623. "Pony",
  2624. [
  2625. ("id", models.CharField(primary_key=True, max_length=10)),
  2626. ],
  2627. ),
  2628. migrations.CreateModel(
  2629. "Rider",
  2630. [
  2631. ("pony", models.ForeignKey("Pony", models.CASCADE)),
  2632. ],
  2633. ),
  2634. migrations.CreateModel(
  2635. "Stable",
  2636. [
  2637. ("ponies", models.ManyToManyField("Pony")),
  2638. ],
  2639. ),
  2640. ],
  2641. )
  2642. # State alteration.
  2643. operation = migrations.AlterField(
  2644. "Pony",
  2645. "id",
  2646. models.CharField(
  2647. primary_key=True,
  2648. max_length=10,
  2649. db_collation=collation,
  2650. ),
  2651. )
  2652. new_state = project_state.clone()
  2653. operation.state_forwards(app_label, new_state)
  2654. # Database alteration.
  2655. with connection.schema_editor() as editor:
  2656. operation.database_forwards(app_label, editor, project_state, new_state)
  2657. self.assertColumnCollation(f"{app_label}_pony", "id", collation)
  2658. self.assertColumnCollation(f"{app_label}_rider", "pony_id", collation)
  2659. self.assertColumnCollation(f"{app_label}_stable_ponies", "pony_id", collation)
  2660. # Reversal.
  2661. with connection.schema_editor() as editor:
  2662. operation.database_backwards(app_label, editor, new_state, project_state)
  2663. def test_alter_field_pk_mti_fk(self):
  2664. app_label = "test_alflpkmtifk"
  2665. project_state = self.set_up_test_model(app_label, mti_model=True)
  2666. project_state = self.apply_operations(
  2667. app_label,
  2668. project_state,
  2669. [
  2670. migrations.CreateModel(
  2671. "ShetlandRider",
  2672. fields=[
  2673. (
  2674. "pony",
  2675. models.ForeignKey(
  2676. f"{app_label}.ShetlandPony", models.CASCADE
  2677. ),
  2678. ),
  2679. ],
  2680. ),
  2681. ],
  2682. )
  2683. operation = migrations.AlterField(
  2684. "Pony",
  2685. "id",
  2686. models.BigAutoField(primary_key=True),
  2687. )
  2688. new_state = project_state.clone()
  2689. operation.state_forwards(app_label, new_state)
  2690. self.assertIsInstance(
  2691. new_state.models[app_label, "pony"].fields["id"],
  2692. models.BigAutoField,
  2693. )
  2694. def _get_column_id_type(cursor, table, column):
  2695. return [
  2696. c.type_code
  2697. for c in connection.introspection.get_table_description(
  2698. cursor,
  2699. f"{app_label}_{table}",
  2700. )
  2701. if c.name == column
  2702. ][0]
  2703. def assertIdTypeEqualsMTIFkType():
  2704. with connection.cursor() as cursor:
  2705. parent_id_type = _get_column_id_type(cursor, "pony", "id")
  2706. child_id_type = _get_column_id_type(
  2707. cursor, "shetlandpony", "pony_ptr_id"
  2708. )
  2709. mti_id_type = _get_column_id_type(cursor, "shetlandrider", "pony_id")
  2710. self.assertEqual(parent_id_type, child_id_type)
  2711. self.assertEqual(parent_id_type, mti_id_type)
  2712. assertIdTypeEqualsMTIFkType()
  2713. # Alter primary key.
  2714. with connection.schema_editor() as editor:
  2715. operation.database_forwards(app_label, editor, project_state, new_state)
  2716. assertIdTypeEqualsMTIFkType()
  2717. if connection.features.supports_foreign_keys:
  2718. self.assertFKExists(
  2719. f"{app_label}_shetlandpony",
  2720. ["pony_ptr_id"],
  2721. (f"{app_label}_pony", "id"),
  2722. )
  2723. self.assertFKExists(
  2724. f"{app_label}_shetlandrider",
  2725. ["pony_id"],
  2726. (f"{app_label}_shetlandpony", "pony_ptr_id"),
  2727. )
  2728. # Reversal.
  2729. with connection.schema_editor() as editor:
  2730. operation.database_backwards(app_label, editor, new_state, project_state)
  2731. assertIdTypeEqualsMTIFkType()
  2732. if connection.features.supports_foreign_keys:
  2733. self.assertFKExists(
  2734. f"{app_label}_shetlandpony",
  2735. ["pony_ptr_id"],
  2736. (f"{app_label}_pony", "id"),
  2737. )
  2738. self.assertFKExists(
  2739. f"{app_label}_shetlandrider",
  2740. ["pony_id"],
  2741. (f"{app_label}_shetlandpony", "pony_ptr_id"),
  2742. )
  2743. def test_alter_field_pk_mti_and_fk_to_base(self):
  2744. app_label = "test_alflpkmtiftb"
  2745. project_state = self.set_up_test_model(
  2746. app_label,
  2747. mti_model=True,
  2748. related_model=True,
  2749. )
  2750. operation = migrations.AlterField(
  2751. "Pony",
  2752. "id",
  2753. models.BigAutoField(primary_key=True),
  2754. )
  2755. new_state = project_state.clone()
  2756. operation.state_forwards(app_label, new_state)
  2757. self.assertIsInstance(
  2758. new_state.models[app_label, "pony"].fields["id"],
  2759. models.BigAutoField,
  2760. )
  2761. def _get_column_id_type(cursor, table, column):
  2762. return [
  2763. c.type_code
  2764. for c in connection.introspection.get_table_description(
  2765. cursor,
  2766. f"{app_label}_{table}",
  2767. )
  2768. if c.name == column
  2769. ][0]
  2770. def assertIdTypeEqualsMTIFkType():
  2771. with connection.cursor() as cursor:
  2772. parent_id_type = _get_column_id_type(cursor, "pony", "id")
  2773. fk_id_type = _get_column_id_type(cursor, "rider", "pony_id")
  2774. child_id_type = _get_column_id_type(
  2775. cursor, "shetlandpony", "pony_ptr_id"
  2776. )
  2777. self.assertEqual(parent_id_type, child_id_type)
  2778. self.assertEqual(parent_id_type, fk_id_type)
  2779. assertIdTypeEqualsMTIFkType()
  2780. # Alter primary key.
  2781. with connection.schema_editor() as editor:
  2782. operation.database_forwards(app_label, editor, project_state, new_state)
  2783. assertIdTypeEqualsMTIFkType()
  2784. if connection.features.supports_foreign_keys:
  2785. self.assertFKExists(
  2786. f"{app_label}_shetlandpony",
  2787. ["pony_ptr_id"],
  2788. (f"{app_label}_pony", "id"),
  2789. )
  2790. self.assertFKExists(
  2791. f"{app_label}_rider",
  2792. ["pony_id"],
  2793. (f"{app_label}_pony", "id"),
  2794. )
  2795. # Reversal.
  2796. with connection.schema_editor() as editor:
  2797. operation.database_backwards(app_label, editor, new_state, project_state)
  2798. assertIdTypeEqualsMTIFkType()
  2799. if connection.features.supports_foreign_keys:
  2800. self.assertFKExists(
  2801. f"{app_label}_shetlandpony",
  2802. ["pony_ptr_id"],
  2803. (f"{app_label}_pony", "id"),
  2804. )
  2805. self.assertFKExists(
  2806. f"{app_label}_rider",
  2807. ["pony_id"],
  2808. (f"{app_label}_pony", "id"),
  2809. )
  2810. def test_alter_id_pk_to_uuid_pk(self):
  2811. app_label = "test_alidpktuuidpk"
  2812. project_state = self.set_up_test_model(app_label)
  2813. new_state = project_state.clone()
  2814. # Add UUID field.
  2815. operation = migrations.AddField("Pony", "uuid", models.UUIDField())
  2816. operation.state_forwards(app_label, new_state)
  2817. with connection.schema_editor() as editor:
  2818. operation.database_forwards(app_label, editor, project_state, new_state)
  2819. # Remove ID.
  2820. project_state = new_state
  2821. new_state = new_state.clone()
  2822. operation = migrations.RemoveField("Pony", "id")
  2823. operation.state_forwards(app_label, new_state)
  2824. with connection.schema_editor() as editor:
  2825. operation.database_forwards(app_label, editor, project_state, new_state)
  2826. self.assertColumnNotExists(f"{app_label}_pony", "id")
  2827. # Rename to ID.
  2828. project_state = new_state
  2829. new_state = new_state.clone()
  2830. operation = migrations.RenameField("Pony", "uuid", "id")
  2831. operation.state_forwards(app_label, new_state)
  2832. with connection.schema_editor() as editor:
  2833. operation.database_forwards(app_label, editor, project_state, new_state)
  2834. self.assertColumnNotExists(f"{app_label}_pony", "uuid")
  2835. self.assertColumnExists(f"{app_label}_pony", "id")
  2836. # Change to a primary key.
  2837. project_state = new_state
  2838. new_state = new_state.clone()
  2839. operation = migrations.AlterField(
  2840. "Pony", "id", models.UUIDField(primary_key=True)
  2841. )
  2842. operation.state_forwards(app_label, new_state)
  2843. with connection.schema_editor() as editor:
  2844. operation.database_forwards(app_label, editor, project_state, new_state)
  2845. @skipUnlessDBFeature("supports_foreign_keys")
  2846. def test_alter_field_reloads_state_on_fk_with_to_field_target_type_change(self):
  2847. app_label = "test_alflrsfkwtflttc"
  2848. project_state = self.apply_operations(
  2849. app_label,
  2850. ProjectState(),
  2851. operations=[
  2852. migrations.CreateModel(
  2853. "Rider",
  2854. fields=[
  2855. ("id", models.AutoField(primary_key=True)),
  2856. ("code", models.IntegerField(unique=True)),
  2857. ],
  2858. ),
  2859. migrations.CreateModel(
  2860. "Pony",
  2861. fields=[
  2862. ("id", models.AutoField(primary_key=True)),
  2863. (
  2864. "rider",
  2865. models.ForeignKey(
  2866. "%s.Rider" % app_label, models.CASCADE, to_field="code"
  2867. ),
  2868. ),
  2869. ],
  2870. ),
  2871. ],
  2872. )
  2873. operation = migrations.AlterField(
  2874. "Rider",
  2875. "code",
  2876. models.CharField(max_length=100, unique=True),
  2877. )
  2878. self.apply_operations(app_label, project_state, operations=[operation])
  2879. id_type, id_null = [
  2880. (c.type_code, c.null_ok)
  2881. for c in self.get_table_description("%s_rider" % app_label)
  2882. if c.name == "code"
  2883. ][0]
  2884. fk_type, fk_null = [
  2885. (c.type_code, c.null_ok)
  2886. for c in self.get_table_description("%s_pony" % app_label)
  2887. if c.name == "rider_id"
  2888. ][0]
  2889. self.assertEqual(id_type, fk_type)
  2890. self.assertEqual(id_null, fk_null)
  2891. @skipUnlessDBFeature("supports_foreign_keys")
  2892. def test_alter_field_reloads_state_fk_with_to_field_related_name_target_type_change(
  2893. self,
  2894. ):
  2895. app_label = "test_alflrsfkwtflrnttc"
  2896. project_state = self.apply_operations(
  2897. app_label,
  2898. ProjectState(),
  2899. operations=[
  2900. migrations.CreateModel(
  2901. "Rider",
  2902. fields=[
  2903. ("id", models.AutoField(primary_key=True)),
  2904. ("code", models.PositiveIntegerField(unique=True)),
  2905. ],
  2906. ),
  2907. migrations.CreateModel(
  2908. "Pony",
  2909. fields=[
  2910. ("id", models.AutoField(primary_key=True)),
  2911. (
  2912. "rider",
  2913. models.ForeignKey(
  2914. "%s.Rider" % app_label,
  2915. models.CASCADE,
  2916. to_field="code",
  2917. related_name="+",
  2918. ),
  2919. ),
  2920. ],
  2921. ),
  2922. ],
  2923. )
  2924. operation = migrations.AlterField(
  2925. "Rider",
  2926. "code",
  2927. models.CharField(max_length=100, unique=True),
  2928. )
  2929. self.apply_operations(app_label, project_state, operations=[operation])
  2930. def test_alter_field_reloads_state_on_fk_target_changes(self):
  2931. """
  2932. If AlterField doesn't reload state appropriately, the second AlterField
  2933. crashes on MySQL due to not dropping the PonyRider.pony foreign key
  2934. constraint before modifying the column.
  2935. """
  2936. app_label = "alter_alter_field_reloads_state_on_fk_target_changes"
  2937. project_state = self.apply_operations(
  2938. app_label,
  2939. ProjectState(),
  2940. operations=[
  2941. migrations.CreateModel(
  2942. "Rider",
  2943. fields=[
  2944. ("id", models.CharField(primary_key=True, max_length=100)),
  2945. ],
  2946. ),
  2947. migrations.CreateModel(
  2948. "Pony",
  2949. fields=[
  2950. ("id", models.CharField(primary_key=True, max_length=100)),
  2951. (
  2952. "rider",
  2953. models.ForeignKey("%s.Rider" % app_label, models.CASCADE),
  2954. ),
  2955. ],
  2956. ),
  2957. migrations.CreateModel(
  2958. "PonyRider",
  2959. fields=[
  2960. ("id", models.AutoField(primary_key=True)),
  2961. (
  2962. "pony",
  2963. models.ForeignKey("%s.Pony" % app_label, models.CASCADE),
  2964. ),
  2965. ],
  2966. ),
  2967. ],
  2968. )
  2969. project_state = self.apply_operations(
  2970. app_label,
  2971. project_state,
  2972. operations=[
  2973. migrations.AlterField(
  2974. "Rider", "id", models.CharField(primary_key=True, max_length=99)
  2975. ),
  2976. migrations.AlterField(
  2977. "Pony", "id", models.CharField(primary_key=True, max_length=99)
  2978. ),
  2979. ],
  2980. )
  2981. def test_alter_field_reloads_state_on_fk_with_to_field_target_changes(self):
  2982. """
  2983. If AlterField doesn't reload state appropriately, the second AlterField
  2984. crashes on MySQL due to not dropping the PonyRider.pony foreign key
  2985. constraint before modifying the column.
  2986. """
  2987. app_label = "alter_alter_field_reloads_state_on_fk_with_to_field_target_changes"
  2988. project_state = self.apply_operations(
  2989. app_label,
  2990. ProjectState(),
  2991. operations=[
  2992. migrations.CreateModel(
  2993. "Rider",
  2994. fields=[
  2995. ("id", models.CharField(primary_key=True, max_length=100)),
  2996. ("slug", models.CharField(unique=True, max_length=100)),
  2997. ],
  2998. ),
  2999. migrations.CreateModel(
  3000. "Pony",
  3001. fields=[
  3002. ("id", models.CharField(primary_key=True, max_length=100)),
  3003. (
  3004. "rider",
  3005. models.ForeignKey(
  3006. "%s.Rider" % app_label, models.CASCADE, to_field="slug"
  3007. ),
  3008. ),
  3009. ("slug", models.CharField(unique=True, max_length=100)),
  3010. ],
  3011. ),
  3012. migrations.CreateModel(
  3013. "PonyRider",
  3014. fields=[
  3015. ("id", models.AutoField(primary_key=True)),
  3016. (
  3017. "pony",
  3018. models.ForeignKey(
  3019. "%s.Pony" % app_label, models.CASCADE, to_field="slug"
  3020. ),
  3021. ),
  3022. ],
  3023. ),
  3024. ],
  3025. )
  3026. project_state = self.apply_operations(
  3027. app_label,
  3028. project_state,
  3029. operations=[
  3030. migrations.AlterField(
  3031. "Rider", "slug", models.CharField(unique=True, max_length=99)
  3032. ),
  3033. migrations.AlterField(
  3034. "Pony", "slug", models.CharField(unique=True, max_length=99)
  3035. ),
  3036. ],
  3037. )
  3038. def test_alter_field_pk_fk_char_to_int(self):
  3039. app_label = "alter_field_pk_fk_char_to_int"
  3040. project_state = self.apply_operations(
  3041. app_label,
  3042. ProjectState(),
  3043. operations=[
  3044. migrations.CreateModel(
  3045. name="Parent",
  3046. fields=[
  3047. ("id", models.CharField(max_length=255, primary_key=True)),
  3048. ],
  3049. ),
  3050. migrations.CreateModel(
  3051. name="Child",
  3052. fields=[
  3053. ("id", models.BigAutoField(primary_key=True)),
  3054. (
  3055. "parent",
  3056. models.ForeignKey(
  3057. f"{app_label}.Parent",
  3058. on_delete=models.CASCADE,
  3059. ),
  3060. ),
  3061. ],
  3062. ),
  3063. ],
  3064. )
  3065. self.apply_operations(
  3066. app_label,
  3067. project_state,
  3068. operations=[
  3069. migrations.AlterField(
  3070. model_name="parent",
  3071. name="id",
  3072. field=models.BigIntegerField(primary_key=True),
  3073. ),
  3074. ],
  3075. )
  3076. def test_rename_field_reloads_state_on_fk_target_changes(self):
  3077. """
  3078. If RenameField doesn't reload state appropriately, the AlterField
  3079. crashes on MySQL due to not dropping the PonyRider.pony foreign key
  3080. constraint before modifying the column.
  3081. """
  3082. app_label = "alter_rename_field_reloads_state_on_fk_target_changes"
  3083. project_state = self.apply_operations(
  3084. app_label,
  3085. ProjectState(),
  3086. operations=[
  3087. migrations.CreateModel(
  3088. "Rider",
  3089. fields=[
  3090. ("id", models.CharField(primary_key=True, max_length=100)),
  3091. ],
  3092. ),
  3093. migrations.CreateModel(
  3094. "Pony",
  3095. fields=[
  3096. ("id", models.CharField(primary_key=True, max_length=100)),
  3097. (
  3098. "rider",
  3099. models.ForeignKey("%s.Rider" % app_label, models.CASCADE),
  3100. ),
  3101. ],
  3102. ),
  3103. migrations.CreateModel(
  3104. "PonyRider",
  3105. fields=[
  3106. ("id", models.AutoField(primary_key=True)),
  3107. (
  3108. "pony",
  3109. models.ForeignKey("%s.Pony" % app_label, models.CASCADE),
  3110. ),
  3111. ],
  3112. ),
  3113. ],
  3114. )
  3115. project_state = self.apply_operations(
  3116. app_label,
  3117. project_state,
  3118. operations=[
  3119. migrations.RenameField("Rider", "id", "id2"),
  3120. migrations.AlterField(
  3121. "Pony", "id", models.CharField(primary_key=True, max_length=99)
  3122. ),
  3123. ],
  3124. )
  3125. def test_rename_field(self):
  3126. """
  3127. Tests the RenameField operation.
  3128. """
  3129. project_state = self.set_up_test_model("test_rnfl")
  3130. operation = migrations.RenameField("Pony", "pink", "blue")
  3131. self.assertEqual(operation.describe(), "Rename field pink on Pony to blue")
  3132. self.assertEqual(
  3133. operation.formatted_description(), "~ Rename field pink on Pony to blue"
  3134. )
  3135. self.assertEqual(operation.migration_name_fragment, "rename_pink_pony_blue")
  3136. new_state = project_state.clone()
  3137. operation.state_forwards("test_rnfl", new_state)
  3138. self.assertIn("blue", new_state.models["test_rnfl", "pony"].fields)
  3139. self.assertNotIn("pink", new_state.models["test_rnfl", "pony"].fields)
  3140. # Rename field.
  3141. self.assertColumnExists("test_rnfl_pony", "pink")
  3142. self.assertColumnNotExists("test_rnfl_pony", "blue")
  3143. with connection.schema_editor() as editor:
  3144. operation.database_forwards("test_rnfl", editor, project_state, new_state)
  3145. self.assertColumnExists("test_rnfl_pony", "blue")
  3146. self.assertColumnNotExists("test_rnfl_pony", "pink")
  3147. # Reversal.
  3148. with connection.schema_editor() as editor:
  3149. operation.database_backwards("test_rnfl", editor, new_state, project_state)
  3150. self.assertColumnExists("test_rnfl_pony", "pink")
  3151. self.assertColumnNotExists("test_rnfl_pony", "blue")
  3152. # Deconstruction.
  3153. definition = operation.deconstruct()
  3154. self.assertEqual(definition[0], "RenameField")
  3155. self.assertEqual(definition[1], [])
  3156. self.assertEqual(
  3157. definition[2],
  3158. {"model_name": "Pony", "old_name": "pink", "new_name": "blue"},
  3159. )
  3160. def test_rename_field_unique_together(self):
  3161. project_state = self.set_up_test_model("test_rnflut", unique_together=True)
  3162. operation = migrations.RenameField("Pony", "pink", "blue")
  3163. new_state = project_state.clone()
  3164. operation.state_forwards("test_rnflut", new_state)
  3165. # unique_together has the renamed column.
  3166. self.assertIn(
  3167. "blue",
  3168. new_state.models["test_rnflut", "pony"].options["unique_together"][0],
  3169. )
  3170. self.assertNotIn(
  3171. "pink",
  3172. new_state.models["test_rnflut", "pony"].options["unique_together"][0],
  3173. )
  3174. # Rename field.
  3175. self.assertColumnExists("test_rnflut_pony", "pink")
  3176. self.assertColumnNotExists("test_rnflut_pony", "blue")
  3177. with connection.schema_editor() as editor:
  3178. operation.database_forwards("test_rnflut", editor, project_state, new_state)
  3179. self.assertColumnExists("test_rnflut_pony", "blue")
  3180. self.assertColumnNotExists("test_rnflut_pony", "pink")
  3181. # The unique constraint has been ported over.
  3182. with connection.cursor() as cursor:
  3183. cursor.execute("INSERT INTO test_rnflut_pony (blue, weight) VALUES (1, 1)")
  3184. with self.assertRaises(IntegrityError):
  3185. with atomic():
  3186. cursor.execute(
  3187. "INSERT INTO test_rnflut_pony (blue, weight) VALUES (1, 1)"
  3188. )
  3189. cursor.execute("DELETE FROM test_rnflut_pony")
  3190. # Reversal.
  3191. with connection.schema_editor() as editor:
  3192. operation.database_backwards(
  3193. "test_rnflut", editor, new_state, project_state
  3194. )
  3195. self.assertColumnExists("test_rnflut_pony", "pink")
  3196. self.assertColumnNotExists("test_rnflut_pony", "blue")
  3197. def test_rename_field_with_db_column(self):
  3198. project_state = self.apply_operations(
  3199. "test_rfwdbc",
  3200. ProjectState(),
  3201. operations=[
  3202. migrations.CreateModel(
  3203. "Pony",
  3204. fields=[
  3205. ("id", models.AutoField(primary_key=True)),
  3206. ("field", models.IntegerField(db_column="db_field")),
  3207. (
  3208. "fk_field",
  3209. models.ForeignKey(
  3210. "Pony",
  3211. models.CASCADE,
  3212. db_column="db_fk_field",
  3213. ),
  3214. ),
  3215. ],
  3216. ),
  3217. ],
  3218. )
  3219. new_state = project_state.clone()
  3220. operation = migrations.RenameField("Pony", "field", "renamed_field")
  3221. operation.state_forwards("test_rfwdbc", new_state)
  3222. self.assertIn("renamed_field", new_state.models["test_rfwdbc", "pony"].fields)
  3223. self.assertNotIn("field", new_state.models["test_rfwdbc", "pony"].fields)
  3224. self.assertColumnExists("test_rfwdbc_pony", "db_field")
  3225. with connection.schema_editor() as editor:
  3226. with self.assertNumQueries(0):
  3227. operation.database_forwards(
  3228. "test_rfwdbc", editor, project_state, new_state
  3229. )
  3230. self.assertColumnExists("test_rfwdbc_pony", "db_field")
  3231. with connection.schema_editor() as editor:
  3232. with self.assertNumQueries(0):
  3233. operation.database_backwards(
  3234. "test_rfwdbc", editor, new_state, project_state
  3235. )
  3236. self.assertColumnExists("test_rfwdbc_pony", "db_field")
  3237. new_state = project_state.clone()
  3238. operation = migrations.RenameField("Pony", "fk_field", "renamed_fk_field")
  3239. operation.state_forwards("test_rfwdbc", new_state)
  3240. self.assertIn(
  3241. "renamed_fk_field", new_state.models["test_rfwdbc", "pony"].fields
  3242. )
  3243. self.assertNotIn("fk_field", new_state.models["test_rfwdbc", "pony"].fields)
  3244. self.assertColumnExists("test_rfwdbc_pony", "db_fk_field")
  3245. with connection.schema_editor() as editor:
  3246. with self.assertNumQueries(0):
  3247. operation.database_forwards(
  3248. "test_rfwdbc", editor, project_state, new_state
  3249. )
  3250. self.assertColumnExists("test_rfwdbc_pony", "db_fk_field")
  3251. with connection.schema_editor() as editor:
  3252. with self.assertNumQueries(0):
  3253. operation.database_backwards(
  3254. "test_rfwdbc", editor, new_state, project_state
  3255. )
  3256. self.assertColumnExists("test_rfwdbc_pony", "db_fk_field")
  3257. def test_rename_field_case(self):
  3258. project_state = self.apply_operations(
  3259. "test_rfmx",
  3260. ProjectState(),
  3261. operations=[
  3262. migrations.CreateModel(
  3263. "Pony",
  3264. fields=[
  3265. ("id", models.AutoField(primary_key=True)),
  3266. ("field", models.IntegerField()),
  3267. ],
  3268. ),
  3269. ],
  3270. )
  3271. new_state = project_state.clone()
  3272. operation = migrations.RenameField("Pony", "field", "FiElD")
  3273. operation.state_forwards("test_rfmx", new_state)
  3274. self.assertIn("FiElD", new_state.models["test_rfmx", "pony"].fields)
  3275. self.assertColumnExists("test_rfmx_pony", "field")
  3276. with connection.schema_editor() as editor:
  3277. operation.database_forwards("test_rfmx", editor, project_state, new_state)
  3278. self.assertColumnExists(
  3279. "test_rfmx_pony",
  3280. connection.introspection.identifier_converter("FiElD"),
  3281. )
  3282. with connection.schema_editor() as editor:
  3283. operation.database_backwards("test_rfmx", editor, new_state, project_state)
  3284. self.assertColumnExists("test_rfmx_pony", "field")
  3285. def test_rename_missing_field(self):
  3286. state = ProjectState()
  3287. state.add_model(ModelState("app", "model", []))
  3288. with self.assertRaisesMessage(
  3289. FieldDoesNotExist, "app.model has no field named 'field'"
  3290. ):
  3291. migrations.RenameField("model", "field", "new_field").state_forwards(
  3292. "app", state
  3293. )
  3294. def test_rename_referenced_field_state_forward(self):
  3295. state = ProjectState()
  3296. state.add_model(
  3297. ModelState(
  3298. "app",
  3299. "Model",
  3300. [
  3301. ("id", models.AutoField(primary_key=True)),
  3302. ("field", models.IntegerField(unique=True)),
  3303. ],
  3304. )
  3305. )
  3306. state.add_model(
  3307. ModelState(
  3308. "app",
  3309. "OtherModel",
  3310. [
  3311. ("id", models.AutoField(primary_key=True)),
  3312. (
  3313. "fk",
  3314. models.ForeignKey("Model", models.CASCADE, to_field="field"),
  3315. ),
  3316. (
  3317. "fo",
  3318. models.ForeignObject(
  3319. "Model",
  3320. models.CASCADE,
  3321. from_fields=("fk",),
  3322. to_fields=("field",),
  3323. ),
  3324. ),
  3325. ],
  3326. )
  3327. )
  3328. operation = migrations.RenameField("Model", "field", "renamed")
  3329. new_state = state.clone()
  3330. operation.state_forwards("app", new_state)
  3331. self.assertEqual(
  3332. new_state.models["app", "othermodel"].fields["fk"].remote_field.field_name,
  3333. "renamed",
  3334. )
  3335. self.assertEqual(
  3336. new_state.models["app", "othermodel"].fields["fk"].from_fields, ["self"]
  3337. )
  3338. self.assertEqual(
  3339. new_state.models["app", "othermodel"].fields["fk"].to_fields, ("renamed",)
  3340. )
  3341. self.assertEqual(
  3342. new_state.models["app", "othermodel"].fields["fo"].from_fields, ("fk",)
  3343. )
  3344. self.assertEqual(
  3345. new_state.models["app", "othermodel"].fields["fo"].to_fields, ("renamed",)
  3346. )
  3347. operation = migrations.RenameField("OtherModel", "fk", "renamed_fk")
  3348. new_state = state.clone()
  3349. operation.state_forwards("app", new_state)
  3350. self.assertEqual(
  3351. new_state.models["app", "othermodel"]
  3352. .fields["renamed_fk"]
  3353. .remote_field.field_name,
  3354. "renamed",
  3355. )
  3356. self.assertEqual(
  3357. new_state.models["app", "othermodel"].fields["renamed_fk"].from_fields,
  3358. ("self",),
  3359. )
  3360. self.assertEqual(
  3361. new_state.models["app", "othermodel"].fields["renamed_fk"].to_fields,
  3362. ("renamed",),
  3363. )
  3364. self.assertEqual(
  3365. new_state.models["app", "othermodel"].fields["fo"].from_fields,
  3366. ("renamed_fk",),
  3367. )
  3368. self.assertEqual(
  3369. new_state.models["app", "othermodel"].fields["fo"].to_fields, ("renamed",)
  3370. )
  3371. def test_alter_unique_together(self):
  3372. """
  3373. Tests the AlterUniqueTogether operation.
  3374. """
  3375. project_state = self.set_up_test_model("test_alunto")
  3376. # Test the state alteration
  3377. operation = migrations.AlterUniqueTogether("Pony", [("pink", "weight")])
  3378. self.assertEqual(
  3379. operation.describe(), "Alter unique_together for Pony (1 constraint(s))"
  3380. )
  3381. self.assertEqual(
  3382. operation.formatted_description(),
  3383. "~ Alter unique_together for Pony (1 constraint(s))",
  3384. )
  3385. self.assertEqual(
  3386. operation.migration_name_fragment,
  3387. "alter_pony_unique_together",
  3388. )
  3389. new_state = project_state.clone()
  3390. operation.state_forwards("test_alunto", new_state)
  3391. self.assertEqual(
  3392. len(
  3393. project_state.models["test_alunto", "pony"].options.get(
  3394. "unique_together", set()
  3395. )
  3396. ),
  3397. 0,
  3398. )
  3399. self.assertEqual(
  3400. len(
  3401. new_state.models["test_alunto", "pony"].options.get(
  3402. "unique_together", set()
  3403. )
  3404. ),
  3405. 1,
  3406. )
  3407. # Make sure we can insert duplicate rows
  3408. with connection.cursor() as cursor:
  3409. cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
  3410. cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
  3411. cursor.execute("DELETE FROM test_alunto_pony")
  3412. # Test the database alteration
  3413. with connection.schema_editor() as editor:
  3414. operation.database_forwards(
  3415. "test_alunto", editor, project_state, new_state
  3416. )
  3417. cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
  3418. with self.assertRaises(IntegrityError):
  3419. with atomic():
  3420. cursor.execute(
  3421. "INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)"
  3422. )
  3423. cursor.execute("DELETE FROM test_alunto_pony")
  3424. # And test reversal
  3425. with connection.schema_editor() as editor:
  3426. operation.database_backwards(
  3427. "test_alunto", editor, new_state, project_state
  3428. )
  3429. cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
  3430. cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
  3431. cursor.execute("DELETE FROM test_alunto_pony")
  3432. # Test flat unique_together
  3433. operation = migrations.AlterUniqueTogether("Pony", ("pink", "weight"))
  3434. operation.state_forwards("test_alunto", new_state)
  3435. self.assertEqual(
  3436. len(
  3437. new_state.models["test_alunto", "pony"].options.get(
  3438. "unique_together", set()
  3439. )
  3440. ),
  3441. 1,
  3442. )
  3443. # And deconstruction
  3444. definition = operation.deconstruct()
  3445. self.assertEqual(definition[0], "AlterUniqueTogether")
  3446. self.assertEqual(definition[1], [])
  3447. self.assertEqual(
  3448. definition[2], {"name": "Pony", "unique_together": {("pink", "weight")}}
  3449. )
  3450. def test_alter_unique_together_remove(self):
  3451. operation = migrations.AlterUniqueTogether("Pony", None)
  3452. self.assertEqual(
  3453. operation.describe(), "Alter unique_together for Pony (0 constraint(s))"
  3454. )
  3455. @skipUnlessDBFeature("allows_multiple_constraints_on_same_fields")
  3456. def test_remove_unique_together_on_pk_field(self):
  3457. app_label = "test_rutopkf"
  3458. project_state = self.apply_operations(
  3459. app_label,
  3460. ProjectState(),
  3461. operations=[
  3462. migrations.CreateModel(
  3463. "Pony",
  3464. fields=[("id", models.AutoField(primary_key=True))],
  3465. options={"unique_together": {("id",)}},
  3466. ),
  3467. ],
  3468. )
  3469. table_name = f"{app_label}_pony"
  3470. pk_constraint_name = f"{table_name}_pkey"
  3471. unique_together_constraint_name = f"{table_name}_id_fb61f881_uniq"
  3472. self.assertConstraintExists(table_name, pk_constraint_name, value=False)
  3473. self.assertConstraintExists(
  3474. table_name, unique_together_constraint_name, value=False
  3475. )
  3476. new_state = project_state.clone()
  3477. operation = migrations.AlterUniqueTogether("Pony", set())
  3478. operation.state_forwards(app_label, new_state)
  3479. with connection.schema_editor() as editor:
  3480. operation.database_forwards(app_label, editor, project_state, new_state)
  3481. self.assertConstraintExists(table_name, pk_constraint_name, value=False)
  3482. self.assertConstraintNotExists(table_name, unique_together_constraint_name)
  3483. @skipUnlessDBFeature("allows_multiple_constraints_on_same_fields")
  3484. def test_remove_unique_together_on_unique_field(self):
  3485. app_label = "test_rutouf"
  3486. project_state = self.apply_operations(
  3487. app_label,
  3488. ProjectState(),
  3489. operations=[
  3490. migrations.CreateModel(
  3491. "Pony",
  3492. fields=[
  3493. ("id", models.AutoField(primary_key=True)),
  3494. ("name", models.CharField(max_length=30, unique=True)),
  3495. ],
  3496. options={"unique_together": {("name",)}},
  3497. ),
  3498. ],
  3499. )
  3500. table_name = f"{app_label}_pony"
  3501. unique_constraint_name = f"{table_name}_name_key"
  3502. unique_together_constraint_name = f"{table_name}_name_694f3b9f_uniq"
  3503. self.assertConstraintExists(table_name, unique_constraint_name, value=False)
  3504. self.assertConstraintExists(
  3505. table_name, unique_together_constraint_name, value=False
  3506. )
  3507. new_state = project_state.clone()
  3508. operation = migrations.AlterUniqueTogether("Pony", set())
  3509. operation.state_forwards(app_label, new_state)
  3510. with connection.schema_editor() as editor:
  3511. operation.database_forwards(app_label, editor, project_state, new_state)
  3512. self.assertConstraintExists(table_name, unique_constraint_name, value=False)
  3513. self.assertConstraintNotExists(table_name, unique_together_constraint_name)
  3514. def test_add_index(self):
  3515. """
  3516. Test the AddIndex operation.
  3517. """
  3518. project_state = self.set_up_test_model("test_adin")
  3519. msg = (
  3520. "Indexes passed to AddIndex operations require a name argument. "
  3521. "<Index: fields=['pink']> doesn't have one."
  3522. )
  3523. with self.assertRaisesMessage(ValueError, msg):
  3524. migrations.AddIndex("Pony", models.Index(fields=["pink"]))
  3525. index = models.Index(fields=["pink"], name="test_adin_pony_pink_idx")
  3526. operation = migrations.AddIndex("Pony", index)
  3527. self.assertEqual(
  3528. operation.describe(),
  3529. "Create index test_adin_pony_pink_idx on field(s) pink of model Pony",
  3530. )
  3531. self.assertEqual(
  3532. operation.formatted_description(),
  3533. "+ Create index test_adin_pony_pink_idx on field(s) pink of model Pony",
  3534. )
  3535. self.assertEqual(
  3536. operation.migration_name_fragment,
  3537. "pony_test_adin_pony_pink_idx",
  3538. )
  3539. new_state = project_state.clone()
  3540. operation.state_forwards("test_adin", new_state)
  3541. # Test the database alteration
  3542. self.assertEqual(
  3543. len(new_state.models["test_adin", "pony"].options["indexes"]), 1
  3544. )
  3545. self.assertIndexNotExists("test_adin_pony", ["pink"])
  3546. with connection.schema_editor() as editor:
  3547. operation.database_forwards("test_adin", editor, project_state, new_state)
  3548. self.assertIndexExists("test_adin_pony", ["pink"])
  3549. # And test reversal
  3550. with connection.schema_editor() as editor:
  3551. operation.database_backwards("test_adin", editor, new_state, project_state)
  3552. self.assertIndexNotExists("test_adin_pony", ["pink"])
  3553. # And deconstruction
  3554. definition = operation.deconstruct()
  3555. self.assertEqual(definition[0], "AddIndex")
  3556. self.assertEqual(definition[1], [])
  3557. self.assertEqual(definition[2], {"model_name": "Pony", "index": index})
  3558. def test_remove_index(self):
  3559. """
  3560. Test the RemoveIndex operation.
  3561. """
  3562. project_state = self.set_up_test_model("test_rmin", multicol_index=True)
  3563. self.assertTableExists("test_rmin_pony")
  3564. self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
  3565. operation = migrations.RemoveIndex("Pony", "pony_test_idx")
  3566. self.assertEqual(operation.describe(), "Remove index pony_test_idx from Pony")
  3567. self.assertEqual(
  3568. operation.formatted_description(), "- Remove index pony_test_idx from Pony"
  3569. )
  3570. self.assertEqual(
  3571. operation.migration_name_fragment,
  3572. "remove_pony_pony_test_idx",
  3573. )
  3574. new_state = project_state.clone()
  3575. operation.state_forwards("test_rmin", new_state)
  3576. # Test the state alteration
  3577. self.assertEqual(
  3578. len(new_state.models["test_rmin", "pony"].options["indexes"]), 0
  3579. )
  3580. self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
  3581. # Test the database alteration
  3582. with connection.schema_editor() as editor:
  3583. operation.database_forwards("test_rmin", editor, project_state, new_state)
  3584. self.assertIndexNotExists("test_rmin_pony", ["pink", "weight"])
  3585. # And test reversal
  3586. with connection.schema_editor() as editor:
  3587. operation.database_backwards("test_rmin", editor, new_state, project_state)
  3588. self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
  3589. # And deconstruction
  3590. definition = operation.deconstruct()
  3591. self.assertEqual(definition[0], "RemoveIndex")
  3592. self.assertEqual(definition[1], [])
  3593. self.assertEqual(definition[2], {"model_name": "Pony", "name": "pony_test_idx"})
  3594. # Also test a field dropped with index - sqlite remake issue
  3595. operations = [
  3596. migrations.RemoveIndex("Pony", "pony_test_idx"),
  3597. migrations.RemoveField("Pony", "pink"),
  3598. ]
  3599. self.assertColumnExists("test_rmin_pony", "pink")
  3600. self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
  3601. # Test database alteration
  3602. new_state = project_state.clone()
  3603. self.apply_operations("test_rmin", new_state, operations=operations)
  3604. self.assertColumnNotExists("test_rmin_pony", "pink")
  3605. self.assertIndexNotExists("test_rmin_pony", ["pink", "weight"])
  3606. # And test reversal
  3607. self.unapply_operations("test_rmin", project_state, operations=operations)
  3608. self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
  3609. def test_rename_index(self):
  3610. app_label = "test_rnin"
  3611. project_state = self.set_up_test_model(app_label, index=True)
  3612. table_name = app_label + "_pony"
  3613. self.assertIndexNameExists(table_name, "pony_pink_idx")
  3614. self.assertIndexNameNotExists(table_name, "new_pony_test_idx")
  3615. operation = migrations.RenameIndex(
  3616. "Pony", new_name="new_pony_test_idx", old_name="pony_pink_idx"
  3617. )
  3618. self.assertEqual(
  3619. operation.describe(),
  3620. "Rename index pony_pink_idx on Pony to new_pony_test_idx",
  3621. )
  3622. self.assertEqual(
  3623. operation.formatted_description(),
  3624. "~ Rename index pony_pink_idx on Pony to new_pony_test_idx",
  3625. )
  3626. self.assertEqual(
  3627. operation.migration_name_fragment,
  3628. "rename_pony_pink_idx_new_pony_test_idx",
  3629. )
  3630. new_state = project_state.clone()
  3631. operation.state_forwards(app_label, new_state)
  3632. # Rename index.
  3633. expected_queries = 1 if connection.features.can_rename_index else 2
  3634. with (
  3635. connection.schema_editor() as editor,
  3636. self.assertNumQueries(expected_queries),
  3637. ):
  3638. operation.database_forwards(app_label, editor, project_state, new_state)
  3639. self.assertIndexNameNotExists(table_name, "pony_pink_idx")
  3640. self.assertIndexNameExists(table_name, "new_pony_test_idx")
  3641. # Reversal.
  3642. with (
  3643. connection.schema_editor() as editor,
  3644. self.assertNumQueries(expected_queries),
  3645. ):
  3646. operation.database_backwards(app_label, editor, new_state, project_state)
  3647. self.assertIndexNameExists(table_name, "pony_pink_idx")
  3648. self.assertIndexNameNotExists(table_name, "new_pony_test_idx")
  3649. # Deconstruction.
  3650. definition = operation.deconstruct()
  3651. self.assertEqual(definition[0], "RenameIndex")
  3652. self.assertEqual(definition[1], [])
  3653. self.assertEqual(
  3654. definition[2],
  3655. {
  3656. "model_name": "Pony",
  3657. "old_name": "pony_pink_idx",
  3658. "new_name": "new_pony_test_idx",
  3659. },
  3660. )
  3661. def test_rename_index_arguments(self):
  3662. msg = "RenameIndex.old_name and old_fields are mutually exclusive."
  3663. with self.assertRaisesMessage(ValueError, msg):
  3664. migrations.RenameIndex(
  3665. "Pony",
  3666. new_name="new_idx_name",
  3667. old_name="old_idx_name",
  3668. old_fields=("weight", "pink"),
  3669. )
  3670. msg = "RenameIndex requires one of old_name and old_fields arguments to be set."
  3671. with self.assertRaisesMessage(ValueError, msg):
  3672. migrations.RenameIndex("Pony", new_name="new_idx_name")
  3673. def test_rename_index_unknown_unnamed_index(self):
  3674. app_label = "test_rninuui"
  3675. project_state = self.set_up_test_model(app_label)
  3676. operation = migrations.RenameIndex(
  3677. "Pony", new_name="new_pony_test_idx", old_fields=("weight", "pink")
  3678. )
  3679. new_state = project_state.clone()
  3680. operation.state_forwards(app_label, new_state)
  3681. msg = "Found wrong number (0) of indexes for test_rninuui_pony(weight, pink)."
  3682. with connection.schema_editor() as editor:
  3683. with self.assertRaisesMessage(ValueError, msg):
  3684. operation.database_forwards(app_label, editor, project_state, new_state)
  3685. @skipUnlessDBFeature("allows_multiple_constraints_on_same_fields")
  3686. def test_rename_index_unnamed_index_with_unique_index(self):
  3687. app_label = "test_rninuniwui"
  3688. project_state = self.set_up_test_model(
  3689. app_label,
  3690. multicol_index=True,
  3691. unique_together=True,
  3692. )
  3693. table_name = app_label + "_pony"
  3694. self.assertIndexNotExists(table_name, "new_pony_test_idx")
  3695. operation = migrations.RenameIndex(
  3696. "Pony", new_name="new_pony_test_idx", old_fields=["pink", "weight"]
  3697. )
  3698. new_state = project_state.clone()
  3699. operation.state_forwards(app_label, new_state)
  3700. # Rename index.
  3701. with connection.schema_editor() as editor:
  3702. operation.database_forwards(app_label, editor, project_state, new_state)
  3703. self.assertIndexNameExists(table_name, "new_pony_test_idx")
  3704. def test_add_index_state_forwards(self):
  3705. project_state = self.set_up_test_model("test_adinsf")
  3706. index = models.Index(fields=["pink"], name="test_adinsf_pony_pink_idx")
  3707. old_model = project_state.apps.get_model("test_adinsf", "Pony")
  3708. new_state = project_state.clone()
  3709. operation = migrations.AddIndex("Pony", index)
  3710. operation.state_forwards("test_adinsf", new_state)
  3711. new_model = new_state.apps.get_model("test_adinsf", "Pony")
  3712. self.assertIsNot(old_model, new_model)
  3713. def test_remove_index_state_forwards(self):
  3714. project_state = self.set_up_test_model("test_rminsf")
  3715. index = models.Index(fields=["pink"], name="test_rminsf_pony_pink_idx")
  3716. migrations.AddIndex("Pony", index).state_forwards("test_rminsf", project_state)
  3717. old_model = project_state.apps.get_model("test_rminsf", "Pony")
  3718. new_state = project_state.clone()
  3719. operation = migrations.RemoveIndex("Pony", "test_rminsf_pony_pink_idx")
  3720. operation.state_forwards("test_rminsf", new_state)
  3721. new_model = new_state.apps.get_model("test_rminsf", "Pony")
  3722. self.assertIsNot(old_model, new_model)
  3723. def test_rename_index_state_forwards(self):
  3724. app_label = "test_rnidsf"
  3725. project_state = self.set_up_test_model(app_label, index=True)
  3726. old_model = project_state.apps.get_model(app_label, "Pony")
  3727. new_state = project_state.clone()
  3728. operation = migrations.RenameIndex(
  3729. "Pony", new_name="new_pony_pink_idx", old_name="pony_pink_idx"
  3730. )
  3731. operation.state_forwards(app_label, new_state)
  3732. new_model = new_state.apps.get_model(app_label, "Pony")
  3733. self.assertIsNot(old_model, new_model)
  3734. self.assertEqual(new_model._meta.indexes[0].name, "new_pony_pink_idx")
  3735. @skipUnlessDBFeature("supports_expression_indexes")
  3736. def test_add_func_index(self):
  3737. app_label = "test_addfuncin"
  3738. index_name = f"{app_label}_pony_abs_idx"
  3739. table_name = f"{app_label}_pony"
  3740. project_state = self.set_up_test_model(app_label)
  3741. index = models.Index(Abs("weight"), name=index_name)
  3742. operation = migrations.AddIndex("Pony", index)
  3743. self.assertEqual(
  3744. operation.describe(),
  3745. "Create index test_addfuncin_pony_abs_idx on Abs(F(weight)) on model Pony",
  3746. )
  3747. self.assertEqual(
  3748. operation.migration_name_fragment,
  3749. "pony_test_addfuncin_pony_abs_idx",
  3750. )
  3751. new_state = project_state.clone()
  3752. operation.state_forwards(app_label, new_state)
  3753. self.assertEqual(len(new_state.models[app_label, "pony"].options["indexes"]), 1)
  3754. self.assertIndexNameNotExists(table_name, index_name)
  3755. # Add index.
  3756. with connection.schema_editor() as editor:
  3757. operation.database_forwards(app_label, editor, project_state, new_state)
  3758. self.assertIndexNameExists(table_name, index_name)
  3759. # Reversal.
  3760. with connection.schema_editor() as editor:
  3761. operation.database_backwards(app_label, editor, new_state, project_state)
  3762. self.assertIndexNameNotExists(table_name, index_name)
  3763. # Deconstruction.
  3764. definition = operation.deconstruct()
  3765. self.assertEqual(definition[0], "AddIndex")
  3766. self.assertEqual(definition[1], [])
  3767. self.assertEqual(definition[2], {"model_name": "Pony", "index": index})
  3768. @skipUnlessDBFeature("supports_expression_indexes")
  3769. def test_remove_func_index(self):
  3770. app_label = "test_rmfuncin"
  3771. index_name = f"{app_label}_pony_abs_idx"
  3772. table_name = f"{app_label}_pony"
  3773. project_state = self.set_up_test_model(
  3774. app_label,
  3775. indexes=[
  3776. models.Index(Abs("weight"), name=index_name),
  3777. ],
  3778. )
  3779. self.assertTableExists(table_name)
  3780. self.assertIndexNameExists(table_name, index_name)
  3781. operation = migrations.RemoveIndex("Pony", index_name)
  3782. self.assertEqual(
  3783. operation.describe(),
  3784. "Remove index test_rmfuncin_pony_abs_idx from Pony",
  3785. )
  3786. self.assertEqual(
  3787. operation.migration_name_fragment,
  3788. "remove_pony_test_rmfuncin_pony_abs_idx",
  3789. )
  3790. new_state = project_state.clone()
  3791. operation.state_forwards(app_label, new_state)
  3792. self.assertEqual(len(new_state.models[app_label, "pony"].options["indexes"]), 0)
  3793. # Remove index.
  3794. with connection.schema_editor() as editor:
  3795. operation.database_forwards(app_label, editor, project_state, new_state)
  3796. self.assertIndexNameNotExists(table_name, index_name)
  3797. # Reversal.
  3798. with connection.schema_editor() as editor:
  3799. operation.database_backwards(app_label, editor, new_state, project_state)
  3800. self.assertIndexNameExists(table_name, index_name)
  3801. # Deconstruction.
  3802. definition = operation.deconstruct()
  3803. self.assertEqual(definition[0], "RemoveIndex")
  3804. self.assertEqual(definition[1], [])
  3805. self.assertEqual(definition[2], {"model_name": "Pony", "name": index_name})
  3806. @skipUnlessDBFeature("supports_expression_indexes")
  3807. def test_alter_field_with_func_index(self):
  3808. app_label = "test_alfuncin"
  3809. index_name = f"{app_label}_pony_idx"
  3810. table_name = f"{app_label}_pony"
  3811. project_state = self.set_up_test_model(
  3812. app_label,
  3813. indexes=[models.Index(Abs("pink"), name=index_name)],
  3814. )
  3815. operation = migrations.AlterField(
  3816. "Pony", "pink", models.IntegerField(null=True)
  3817. )
  3818. new_state = project_state.clone()
  3819. operation.state_forwards(app_label, new_state)
  3820. with connection.schema_editor() as editor:
  3821. operation.database_forwards(app_label, editor, project_state, new_state)
  3822. self.assertIndexNameExists(table_name, index_name)
  3823. with connection.schema_editor() as editor:
  3824. operation.database_backwards(app_label, editor, new_state, project_state)
  3825. self.assertIndexNameExists(table_name, index_name)
  3826. def test_alter_field_with_index(self):
  3827. """
  3828. Test AlterField operation with an index to ensure indexes created via
  3829. Meta.indexes don't get dropped with sqlite3 remake.
  3830. """
  3831. project_state = self.set_up_test_model("test_alflin", index=True)
  3832. operation = migrations.AlterField(
  3833. "Pony", "pink", models.IntegerField(null=True)
  3834. )
  3835. new_state = project_state.clone()
  3836. operation.state_forwards("test_alflin", new_state)
  3837. # Test the database alteration
  3838. self.assertColumnNotNull("test_alflin_pony", "pink")
  3839. with connection.schema_editor() as editor:
  3840. operation.database_forwards("test_alflin", editor, project_state, new_state)
  3841. # Index hasn't been dropped
  3842. self.assertIndexExists("test_alflin_pony", ["pink"])
  3843. # And test reversal
  3844. with connection.schema_editor() as editor:
  3845. operation.database_backwards(
  3846. "test_alflin", editor, new_state, project_state
  3847. )
  3848. # Ensure the index is still there
  3849. self.assertIndexExists("test_alflin_pony", ["pink"])
  3850. def test_alter_index_together_remove(self):
  3851. operation = migrations.AlterIndexTogether("Pony", None)
  3852. self.assertEqual(
  3853. operation.describe(), "Alter index_together for Pony (0 constraint(s))"
  3854. )
  3855. self.assertEqual(
  3856. operation.formatted_description(),
  3857. "~ Alter index_together for Pony (0 constraint(s))",
  3858. )
  3859. def test_add_constraint(self):
  3860. project_state = self.set_up_test_model("test_addconstraint")
  3861. gt_check = models.Q(pink__gt=2)
  3862. gt_constraint = models.CheckConstraint(
  3863. condition=gt_check, name="test_add_constraint_pony_pink_gt_2"
  3864. )
  3865. gt_operation = migrations.AddConstraint("Pony", gt_constraint)
  3866. self.assertEqual(
  3867. gt_operation.describe(),
  3868. "Create constraint test_add_constraint_pony_pink_gt_2 on model Pony",
  3869. )
  3870. self.assertEqual(
  3871. gt_operation.formatted_description(),
  3872. "+ Create constraint test_add_constraint_pony_pink_gt_2 on model Pony",
  3873. )
  3874. self.assertEqual(
  3875. gt_operation.migration_name_fragment,
  3876. "pony_test_add_constraint_pony_pink_gt_2",
  3877. )
  3878. # Test the state alteration
  3879. new_state = project_state.clone()
  3880. gt_operation.state_forwards("test_addconstraint", new_state)
  3881. self.assertEqual(
  3882. len(new_state.models["test_addconstraint", "pony"].options["constraints"]),
  3883. 1,
  3884. )
  3885. Pony = new_state.apps.get_model("test_addconstraint", "Pony")
  3886. self.assertEqual(len(Pony._meta.constraints), 1)
  3887. # Test the database alteration
  3888. with (
  3889. CaptureQueriesContext(connection) as ctx,
  3890. connection.schema_editor() as editor,
  3891. ):
  3892. gt_operation.database_forwards(
  3893. "test_addconstraint", editor, project_state, new_state
  3894. )
  3895. if connection.features.supports_table_check_constraints:
  3896. with self.assertRaises(IntegrityError), transaction.atomic():
  3897. Pony.objects.create(pink=1, weight=1.0)
  3898. else:
  3899. self.assertIs(
  3900. any("CHECK" in query["sql"] for query in ctx.captured_queries), False
  3901. )
  3902. Pony.objects.create(pink=1, weight=1.0)
  3903. # Add another one.
  3904. lt_check = models.Q(pink__lt=100)
  3905. lt_constraint = models.CheckConstraint(
  3906. condition=lt_check, name="test_add_constraint_pony_pink_lt_100"
  3907. )
  3908. lt_operation = migrations.AddConstraint("Pony", lt_constraint)
  3909. lt_operation.state_forwards("test_addconstraint", new_state)
  3910. self.assertEqual(
  3911. len(new_state.models["test_addconstraint", "pony"].options["constraints"]),
  3912. 2,
  3913. )
  3914. Pony = new_state.apps.get_model("test_addconstraint", "Pony")
  3915. self.assertEqual(len(Pony._meta.constraints), 2)
  3916. with (
  3917. CaptureQueriesContext(connection) as ctx,
  3918. connection.schema_editor() as editor,
  3919. ):
  3920. lt_operation.database_forwards(
  3921. "test_addconstraint", editor, project_state, new_state
  3922. )
  3923. if connection.features.supports_table_check_constraints:
  3924. with self.assertRaises(IntegrityError), transaction.atomic():
  3925. Pony.objects.create(pink=100, weight=1.0)
  3926. else:
  3927. self.assertIs(
  3928. any("CHECK" in query["sql"] for query in ctx.captured_queries), False
  3929. )
  3930. Pony.objects.create(pink=100, weight=1.0)
  3931. # Test reversal
  3932. with connection.schema_editor() as editor:
  3933. gt_operation.database_backwards(
  3934. "test_addconstraint", editor, new_state, project_state
  3935. )
  3936. Pony.objects.create(pink=1, weight=1.0)
  3937. # Test deconstruction
  3938. definition = gt_operation.deconstruct()
  3939. self.assertEqual(definition[0], "AddConstraint")
  3940. self.assertEqual(definition[1], [])
  3941. self.assertEqual(
  3942. definition[2], {"model_name": "Pony", "constraint": gt_constraint}
  3943. )
  3944. @skipUnlessDBFeature("supports_table_check_constraints")
  3945. def test_create_model_constraint_percent_escaping(self):
  3946. app_label = "add_constraint_string_quoting"
  3947. from_state = ProjectState()
  3948. checks = [
  3949. # "%" generated in startswith lookup should be escaped in a way
  3950. # that is considered a leading wildcard.
  3951. (
  3952. models.Q(name__startswith="Albert"),
  3953. {"name": "Alberta"},
  3954. {"name": "Artur"},
  3955. ),
  3956. # Literal "%" should be escaped in a way that is not a considered a
  3957. # wildcard.
  3958. (models.Q(rebate__endswith="%"), {"rebate": "10%"}, {"rebate": "10%$"}),
  3959. # Right-hand-side baked "%" literals should not be used for
  3960. # parameters interpolation.
  3961. (
  3962. ~models.Q(surname__startswith=models.F("name")),
  3963. {"name": "Albert"},
  3964. {"name": "Albert", "surname": "Alberto"},
  3965. ),
  3966. # Exact matches against "%" literals should also be supported.
  3967. (
  3968. models.Q(name="%"),
  3969. {"name": "%"},
  3970. {"name": "Albert"},
  3971. ),
  3972. ]
  3973. for check, valid, invalid in checks:
  3974. with self.subTest(condition=check, valid=valid, invalid=invalid):
  3975. constraint = models.CheckConstraint(condition=check, name="constraint")
  3976. operation = migrations.CreateModel(
  3977. "Author",
  3978. fields=[
  3979. ("id", models.AutoField(primary_key=True)),
  3980. ("name", models.CharField(max_length=100)),
  3981. ("surname", models.CharField(max_length=100, db_default="")),
  3982. ("rebate", models.CharField(max_length=100)),
  3983. ],
  3984. options={"constraints": [constraint]},
  3985. )
  3986. to_state = from_state.clone()
  3987. operation.state_forwards(app_label, to_state)
  3988. with connection.schema_editor() as editor:
  3989. operation.database_forwards(app_label, editor, from_state, to_state)
  3990. Author = to_state.apps.get_model(app_label, "Author")
  3991. try:
  3992. with transaction.atomic():
  3993. Author.objects.create(**valid).delete()
  3994. with self.assertRaises(IntegrityError), transaction.atomic():
  3995. Author.objects.create(**invalid)
  3996. finally:
  3997. with connection.schema_editor() as editor:
  3998. migrations.DeleteModel("Author").database_forwards(
  3999. app_label, editor, to_state, from_state
  4000. )
  4001. @skipUnlessDBFeature("supports_table_check_constraints")
  4002. def test_add_constraint_percent_escaping(self):
  4003. app_label = "add_constraint_string_quoting"
  4004. operations = [
  4005. migrations.CreateModel(
  4006. "Author",
  4007. fields=[
  4008. ("id", models.AutoField(primary_key=True)),
  4009. ("name", models.CharField(max_length=100)),
  4010. ("surname", models.CharField(max_length=100, default="")),
  4011. ("rebate", models.CharField(max_length=100)),
  4012. ],
  4013. ),
  4014. ]
  4015. from_state = self.apply_operations(app_label, ProjectState(), operations)
  4016. checks = [
  4017. # "%" generated in startswith lookup should be escaped in a way
  4018. # that is considered a leading wildcard.
  4019. (
  4020. models.Q(name__startswith="Albert"),
  4021. {"name": "Alberta"},
  4022. {"name": "Artur"},
  4023. ),
  4024. # Literal "%" should be escaped in a way that is not a considered a
  4025. # wildcard.
  4026. (models.Q(rebate__endswith="%"), {"rebate": "10%"}, {"rebate": "10%$"}),
  4027. # Right-hand-side baked "%" literals should not be used for
  4028. # parameters interpolation.
  4029. (
  4030. ~models.Q(surname__startswith=models.F("name")),
  4031. {"name": "Albert"},
  4032. {"name": "Albert", "surname": "Alberto"},
  4033. ),
  4034. # Exact matches against "%" literals should also be supported.
  4035. (
  4036. models.Q(name="%"),
  4037. {"name": "%"},
  4038. {"name": "Albert"},
  4039. ),
  4040. ]
  4041. for check, valid, invalid in checks:
  4042. with self.subTest(condition=check, valid=valid, invalid=invalid):
  4043. constraint = models.CheckConstraint(condition=check, name="constraint")
  4044. operation = migrations.AddConstraint("Author", constraint)
  4045. to_state = from_state.clone()
  4046. operation.state_forwards(app_label, to_state)
  4047. with connection.schema_editor() as editor:
  4048. operation.database_forwards(app_label, editor, from_state, to_state)
  4049. Author = to_state.apps.get_model(app_label, "Author")
  4050. try:
  4051. with transaction.atomic():
  4052. Author.objects.create(**valid).delete()
  4053. with self.assertRaises(IntegrityError), transaction.atomic():
  4054. Author.objects.create(**invalid)
  4055. finally:
  4056. with connection.schema_editor() as editor:
  4057. operation.database_backwards(
  4058. app_label, editor, from_state, to_state
  4059. )
  4060. @skipUnlessDBFeature("supports_table_check_constraints")
  4061. def test_add_or_constraint(self):
  4062. app_label = "test_addorconstraint"
  4063. constraint_name = "add_constraint_or"
  4064. from_state = self.set_up_test_model(app_label)
  4065. check = models.Q(pink__gt=2, weight__gt=2) | models.Q(weight__lt=0)
  4066. constraint = models.CheckConstraint(condition=check, name=constraint_name)
  4067. operation = migrations.AddConstraint("Pony", constraint)
  4068. to_state = from_state.clone()
  4069. operation.state_forwards(app_label, to_state)
  4070. with connection.schema_editor() as editor:
  4071. operation.database_forwards(app_label, editor, from_state, to_state)
  4072. Pony = to_state.apps.get_model(app_label, "Pony")
  4073. with self.assertRaises(IntegrityError), transaction.atomic():
  4074. Pony.objects.create(pink=2, weight=3.0)
  4075. with self.assertRaises(IntegrityError), transaction.atomic():
  4076. Pony.objects.create(pink=3, weight=1.0)
  4077. Pony.objects.bulk_create(
  4078. [
  4079. Pony(pink=3, weight=-1.0),
  4080. Pony(pink=1, weight=-1.0),
  4081. Pony(pink=3, weight=3.0),
  4082. ]
  4083. )
  4084. @skipUnlessDBFeature("supports_table_check_constraints")
  4085. def test_add_constraint_combinable(self):
  4086. app_label = "test_addconstraint_combinable"
  4087. operations = [
  4088. migrations.CreateModel(
  4089. "Book",
  4090. fields=[
  4091. ("id", models.AutoField(primary_key=True)),
  4092. ("read", models.PositiveIntegerField()),
  4093. ("unread", models.PositiveIntegerField()),
  4094. ],
  4095. ),
  4096. ]
  4097. from_state = self.apply_operations(app_label, ProjectState(), operations)
  4098. constraint = models.CheckConstraint(
  4099. condition=models.Q(read=(100 - models.F("unread"))),
  4100. name="test_addconstraint_combinable_sum_100",
  4101. )
  4102. operation = migrations.AddConstraint("Book", constraint)
  4103. to_state = from_state.clone()
  4104. operation.state_forwards(app_label, to_state)
  4105. with connection.schema_editor() as editor:
  4106. operation.database_forwards(app_label, editor, from_state, to_state)
  4107. Book = to_state.apps.get_model(app_label, "Book")
  4108. with self.assertRaises(IntegrityError), transaction.atomic():
  4109. Book.objects.create(read=70, unread=10)
  4110. Book.objects.create(read=70, unread=30)
  4111. def test_remove_constraint(self):
  4112. project_state = self.set_up_test_model(
  4113. "test_removeconstraint",
  4114. constraints=[
  4115. models.CheckConstraint(
  4116. condition=models.Q(pink__gt=2),
  4117. name="test_remove_constraint_pony_pink_gt_2",
  4118. ),
  4119. models.CheckConstraint(
  4120. condition=models.Q(pink__lt=100),
  4121. name="test_remove_constraint_pony_pink_lt_100",
  4122. ),
  4123. ],
  4124. )
  4125. gt_operation = migrations.RemoveConstraint(
  4126. "Pony", "test_remove_constraint_pony_pink_gt_2"
  4127. )
  4128. self.assertEqual(
  4129. gt_operation.describe(),
  4130. "Remove constraint test_remove_constraint_pony_pink_gt_2 from model Pony",
  4131. )
  4132. self.assertEqual(
  4133. gt_operation.formatted_description(),
  4134. "- Remove constraint test_remove_constraint_pony_pink_gt_2 from model Pony",
  4135. )
  4136. self.assertEqual(
  4137. gt_operation.migration_name_fragment,
  4138. "remove_pony_test_remove_constraint_pony_pink_gt_2",
  4139. )
  4140. # Test state alteration
  4141. new_state = project_state.clone()
  4142. gt_operation.state_forwards("test_removeconstraint", new_state)
  4143. self.assertEqual(
  4144. len(
  4145. new_state.models["test_removeconstraint", "pony"].options["constraints"]
  4146. ),
  4147. 1,
  4148. )
  4149. Pony = new_state.apps.get_model("test_removeconstraint", "Pony")
  4150. self.assertEqual(len(Pony._meta.constraints), 1)
  4151. # Test database alteration
  4152. with connection.schema_editor() as editor:
  4153. gt_operation.database_forwards(
  4154. "test_removeconstraint", editor, project_state, new_state
  4155. )
  4156. Pony.objects.create(pink=1, weight=1.0).delete()
  4157. if connection.features.supports_table_check_constraints:
  4158. with self.assertRaises(IntegrityError), transaction.atomic():
  4159. Pony.objects.create(pink=100, weight=1.0)
  4160. else:
  4161. Pony.objects.create(pink=100, weight=1.0)
  4162. # Remove the other one.
  4163. lt_operation = migrations.RemoveConstraint(
  4164. "Pony", "test_remove_constraint_pony_pink_lt_100"
  4165. )
  4166. lt_operation.state_forwards("test_removeconstraint", new_state)
  4167. self.assertEqual(
  4168. len(
  4169. new_state.models["test_removeconstraint", "pony"].options["constraints"]
  4170. ),
  4171. 0,
  4172. )
  4173. Pony = new_state.apps.get_model("test_removeconstraint", "Pony")
  4174. self.assertEqual(len(Pony._meta.constraints), 0)
  4175. with connection.schema_editor() as editor:
  4176. lt_operation.database_forwards(
  4177. "test_removeconstraint", editor, project_state, new_state
  4178. )
  4179. Pony.objects.create(pink=100, weight=1.0).delete()
  4180. # Test reversal
  4181. with connection.schema_editor() as editor:
  4182. gt_operation.database_backwards(
  4183. "test_removeconstraint", editor, new_state, project_state
  4184. )
  4185. if connection.features.supports_table_check_constraints:
  4186. with self.assertRaises(IntegrityError), transaction.atomic():
  4187. Pony.objects.create(pink=1, weight=1.0)
  4188. else:
  4189. Pony.objects.create(pink=1, weight=1.0)
  4190. # Test deconstruction
  4191. definition = gt_operation.deconstruct()
  4192. self.assertEqual(definition[0], "RemoveConstraint")
  4193. self.assertEqual(definition[1], [])
  4194. self.assertEqual(
  4195. definition[2],
  4196. {"model_name": "Pony", "name": "test_remove_constraint_pony_pink_gt_2"},
  4197. )
  4198. def test_alter_constraint(self):
  4199. constraint = models.UniqueConstraint(
  4200. fields=["pink"], name="test_alter_constraint_pony_fields_uq"
  4201. )
  4202. project_state = self.set_up_test_model(
  4203. "test_alterconstraint", constraints=[constraint]
  4204. )
  4205. new_state = project_state.clone()
  4206. violation_error_message = "Pink isn't unique"
  4207. uq_constraint = models.UniqueConstraint(
  4208. fields=["pink"],
  4209. name="test_alter_constraint_pony_fields_uq",
  4210. violation_error_message=violation_error_message,
  4211. )
  4212. uq_operation = migrations.AlterConstraint(
  4213. "Pony", "test_alter_constraint_pony_fields_uq", uq_constraint
  4214. )
  4215. self.assertEqual(
  4216. uq_operation.describe(),
  4217. "Alter constraint test_alter_constraint_pony_fields_uq on Pony",
  4218. )
  4219. self.assertEqual(
  4220. uq_operation.formatted_description(),
  4221. "~ Alter constraint test_alter_constraint_pony_fields_uq on Pony",
  4222. )
  4223. self.assertEqual(
  4224. uq_operation.migration_name_fragment,
  4225. "alter_pony_test_alter_constraint_pony_fields_uq",
  4226. )
  4227. uq_operation.state_forwards("test_alterconstraint", new_state)
  4228. self.assertEqual(
  4229. project_state.models["test_alterconstraint", "pony"]
  4230. .options["constraints"][0]
  4231. .violation_error_message,
  4232. "Constraint “%(name)s” is violated.",
  4233. )
  4234. self.assertEqual(
  4235. new_state.models["test_alterconstraint", "pony"]
  4236. .options["constraints"][0]
  4237. .violation_error_message,
  4238. violation_error_message,
  4239. )
  4240. with connection.schema_editor() as editor, self.assertNumQueries(0):
  4241. uq_operation.database_forwards(
  4242. "test_alterconstraint", editor, project_state, new_state
  4243. )
  4244. self.assertConstraintExists(
  4245. "test_alterconstraint_pony",
  4246. "test_alter_constraint_pony_fields_uq",
  4247. value=False,
  4248. )
  4249. with connection.schema_editor() as editor, self.assertNumQueries(0):
  4250. uq_operation.database_backwards(
  4251. "test_alterconstraint", editor, project_state, new_state
  4252. )
  4253. self.assertConstraintExists(
  4254. "test_alterconstraint_pony",
  4255. "test_alter_constraint_pony_fields_uq",
  4256. value=False,
  4257. )
  4258. definition = uq_operation.deconstruct()
  4259. self.assertEqual(definition[0], "AlterConstraint")
  4260. self.assertEqual(definition[1], [])
  4261. self.assertEqual(
  4262. definition[2],
  4263. {
  4264. "model_name": "Pony",
  4265. "name": "test_alter_constraint_pony_fields_uq",
  4266. "constraint": uq_constraint,
  4267. },
  4268. )
  4269. def test_add_partial_unique_constraint(self):
  4270. project_state = self.set_up_test_model("test_addpartialuniqueconstraint")
  4271. partial_unique_constraint = models.UniqueConstraint(
  4272. fields=["pink"],
  4273. condition=models.Q(weight__gt=5),
  4274. name="test_constraint_pony_pink_for_weight_gt_5_uniq",
  4275. )
  4276. operation = migrations.AddConstraint("Pony", partial_unique_constraint)
  4277. self.assertEqual(
  4278. operation.describe(),
  4279. "Create constraint test_constraint_pony_pink_for_weight_gt_5_uniq "
  4280. "on model Pony",
  4281. )
  4282. # Test the state alteration
  4283. new_state = project_state.clone()
  4284. operation.state_forwards("test_addpartialuniqueconstraint", new_state)
  4285. self.assertEqual(
  4286. len(
  4287. new_state.models["test_addpartialuniqueconstraint", "pony"].options[
  4288. "constraints"
  4289. ]
  4290. ),
  4291. 1,
  4292. )
  4293. Pony = new_state.apps.get_model("test_addpartialuniqueconstraint", "Pony")
  4294. self.assertEqual(len(Pony._meta.constraints), 1)
  4295. # Test the database alteration
  4296. with connection.schema_editor() as editor:
  4297. operation.database_forwards(
  4298. "test_addpartialuniqueconstraint", editor, project_state, new_state
  4299. )
  4300. # Test constraint works
  4301. Pony.objects.create(pink=1, weight=4.0)
  4302. Pony.objects.create(pink=1, weight=4.0)
  4303. Pony.objects.create(pink=1, weight=6.0)
  4304. if connection.features.supports_partial_indexes:
  4305. with self.assertRaises(IntegrityError), transaction.atomic():
  4306. Pony.objects.create(pink=1, weight=7.0)
  4307. else:
  4308. Pony.objects.create(pink=1, weight=7.0)
  4309. # Test reversal
  4310. with connection.schema_editor() as editor:
  4311. operation.database_backwards(
  4312. "test_addpartialuniqueconstraint", editor, new_state, project_state
  4313. )
  4314. # Test constraint doesn't work
  4315. Pony.objects.create(pink=1, weight=7.0)
  4316. # Test deconstruction
  4317. definition = operation.deconstruct()
  4318. self.assertEqual(definition[0], "AddConstraint")
  4319. self.assertEqual(definition[1], [])
  4320. self.assertEqual(
  4321. definition[2],
  4322. {"model_name": "Pony", "constraint": partial_unique_constraint},
  4323. )
  4324. def test_remove_partial_unique_constraint(self):
  4325. project_state = self.set_up_test_model(
  4326. "test_removepartialuniqueconstraint",
  4327. constraints=[
  4328. models.UniqueConstraint(
  4329. fields=["pink"],
  4330. condition=models.Q(weight__gt=5),
  4331. name="test_constraint_pony_pink_for_weight_gt_5_uniq",
  4332. ),
  4333. ],
  4334. )
  4335. gt_operation = migrations.RemoveConstraint(
  4336. "Pony", "test_constraint_pony_pink_for_weight_gt_5_uniq"
  4337. )
  4338. self.assertEqual(
  4339. gt_operation.describe(),
  4340. "Remove constraint test_constraint_pony_pink_for_weight_gt_5_uniq from "
  4341. "model Pony",
  4342. )
  4343. # Test state alteration
  4344. new_state = project_state.clone()
  4345. gt_operation.state_forwards("test_removepartialuniqueconstraint", new_state)
  4346. self.assertEqual(
  4347. len(
  4348. new_state.models["test_removepartialuniqueconstraint", "pony"].options[
  4349. "constraints"
  4350. ]
  4351. ),
  4352. 0,
  4353. )
  4354. Pony = new_state.apps.get_model("test_removepartialuniqueconstraint", "Pony")
  4355. self.assertEqual(len(Pony._meta.constraints), 0)
  4356. # Test database alteration
  4357. with connection.schema_editor() as editor:
  4358. gt_operation.database_forwards(
  4359. "test_removepartialuniqueconstraint", editor, project_state, new_state
  4360. )
  4361. # Test constraint doesn't work
  4362. Pony.objects.create(pink=1, weight=4.0)
  4363. Pony.objects.create(pink=1, weight=4.0)
  4364. Pony.objects.create(pink=1, weight=6.0)
  4365. Pony.objects.create(pink=1, weight=7.0).delete()
  4366. # Test reversal
  4367. with connection.schema_editor() as editor:
  4368. gt_operation.database_backwards(
  4369. "test_removepartialuniqueconstraint", editor, new_state, project_state
  4370. )
  4371. # Test constraint works
  4372. if connection.features.supports_partial_indexes:
  4373. with self.assertRaises(IntegrityError), transaction.atomic():
  4374. Pony.objects.create(pink=1, weight=7.0)
  4375. else:
  4376. Pony.objects.create(pink=1, weight=7.0)
  4377. # Test deconstruction
  4378. definition = gt_operation.deconstruct()
  4379. self.assertEqual(definition[0], "RemoveConstraint")
  4380. self.assertEqual(definition[1], [])
  4381. self.assertEqual(
  4382. definition[2],
  4383. {
  4384. "model_name": "Pony",
  4385. "name": "test_constraint_pony_pink_for_weight_gt_5_uniq",
  4386. },
  4387. )
  4388. def test_add_deferred_unique_constraint(self):
  4389. app_label = "test_adddeferred_uc"
  4390. project_state = self.set_up_test_model(app_label)
  4391. deferred_unique_constraint = models.UniqueConstraint(
  4392. fields=["pink"],
  4393. name="deferred_pink_constraint_add",
  4394. deferrable=models.Deferrable.DEFERRED,
  4395. )
  4396. operation = migrations.AddConstraint("Pony", deferred_unique_constraint)
  4397. self.assertEqual(
  4398. operation.describe(),
  4399. "Create constraint deferred_pink_constraint_add on model Pony",
  4400. )
  4401. # Add constraint.
  4402. new_state = project_state.clone()
  4403. operation.state_forwards(app_label, new_state)
  4404. self.assertEqual(
  4405. len(new_state.models[app_label, "pony"].options["constraints"]), 1
  4406. )
  4407. Pony = new_state.apps.get_model(app_label, "Pony")
  4408. self.assertEqual(len(Pony._meta.constraints), 1)
  4409. with (
  4410. connection.schema_editor() as editor,
  4411. CaptureQueriesContext(connection) as ctx,
  4412. ):
  4413. operation.database_forwards(app_label, editor, project_state, new_state)
  4414. Pony.objects.create(pink=1, weight=4.0)
  4415. if connection.features.supports_deferrable_unique_constraints:
  4416. # Unique constraint is deferred.
  4417. with transaction.atomic():
  4418. obj = Pony.objects.create(pink=1, weight=4.0)
  4419. obj.pink = 2
  4420. obj.save()
  4421. # Constraint behavior can be changed with SET CONSTRAINTS.
  4422. with self.assertRaises(IntegrityError):
  4423. with transaction.atomic(), connection.cursor() as cursor:
  4424. quoted_name = connection.ops.quote_name(
  4425. deferred_unique_constraint.name
  4426. )
  4427. cursor.execute("SET CONSTRAINTS %s IMMEDIATE" % quoted_name)
  4428. obj = Pony.objects.create(pink=1, weight=4.0)
  4429. obj.pink = 3
  4430. obj.save()
  4431. else:
  4432. self.assertEqual(len(ctx), 0)
  4433. Pony.objects.create(pink=1, weight=4.0)
  4434. # Reversal.
  4435. with connection.schema_editor() as editor:
  4436. operation.database_backwards(app_label, editor, new_state, project_state)
  4437. # Constraint doesn't work.
  4438. Pony.objects.create(pink=1, weight=4.0)
  4439. # Deconstruction.
  4440. definition = operation.deconstruct()
  4441. self.assertEqual(definition[0], "AddConstraint")
  4442. self.assertEqual(definition[1], [])
  4443. self.assertEqual(
  4444. definition[2],
  4445. {"model_name": "Pony", "constraint": deferred_unique_constraint},
  4446. )
  4447. def test_remove_deferred_unique_constraint(self):
  4448. app_label = "test_removedeferred_uc"
  4449. deferred_unique_constraint = models.UniqueConstraint(
  4450. fields=["pink"],
  4451. name="deferred_pink_constraint_rm",
  4452. deferrable=models.Deferrable.DEFERRED,
  4453. )
  4454. project_state = self.set_up_test_model(
  4455. app_label, constraints=[deferred_unique_constraint]
  4456. )
  4457. operation = migrations.RemoveConstraint("Pony", deferred_unique_constraint.name)
  4458. self.assertEqual(
  4459. operation.describe(),
  4460. "Remove constraint deferred_pink_constraint_rm from model Pony",
  4461. )
  4462. # Remove constraint.
  4463. new_state = project_state.clone()
  4464. operation.state_forwards(app_label, new_state)
  4465. self.assertEqual(
  4466. len(new_state.models[app_label, "pony"].options["constraints"]), 0
  4467. )
  4468. Pony = new_state.apps.get_model(app_label, "Pony")
  4469. self.assertEqual(len(Pony._meta.constraints), 0)
  4470. with (
  4471. connection.schema_editor() as editor,
  4472. CaptureQueriesContext(connection) as ctx,
  4473. ):
  4474. operation.database_forwards(app_label, editor, project_state, new_state)
  4475. # Constraint doesn't work.
  4476. Pony.objects.create(pink=1, weight=4.0)
  4477. Pony.objects.create(pink=1, weight=4.0).delete()
  4478. if not connection.features.supports_deferrable_unique_constraints:
  4479. self.assertEqual(len(ctx), 0)
  4480. # Reversal.
  4481. with connection.schema_editor() as editor:
  4482. operation.database_backwards(app_label, editor, new_state, project_state)
  4483. if connection.features.supports_deferrable_unique_constraints:
  4484. # Unique constraint is deferred.
  4485. with transaction.atomic():
  4486. obj = Pony.objects.create(pink=1, weight=4.0)
  4487. obj.pink = 2
  4488. obj.save()
  4489. # Constraint behavior can be changed with SET CONSTRAINTS.
  4490. with self.assertRaises(IntegrityError):
  4491. with transaction.atomic(), connection.cursor() as cursor:
  4492. quoted_name = connection.ops.quote_name(
  4493. deferred_unique_constraint.name
  4494. )
  4495. cursor.execute("SET CONSTRAINTS %s IMMEDIATE" % quoted_name)
  4496. obj = Pony.objects.create(pink=1, weight=4.0)
  4497. obj.pink = 3
  4498. obj.save()
  4499. else:
  4500. Pony.objects.create(pink=1, weight=4.0)
  4501. # Deconstruction.
  4502. definition = operation.deconstruct()
  4503. self.assertEqual(definition[0], "RemoveConstraint")
  4504. self.assertEqual(definition[1], [])
  4505. self.assertEqual(
  4506. definition[2],
  4507. {
  4508. "model_name": "Pony",
  4509. "name": "deferred_pink_constraint_rm",
  4510. },
  4511. )
  4512. def test_add_covering_unique_constraint(self):
  4513. app_label = "test_addcovering_uc"
  4514. project_state = self.set_up_test_model(app_label)
  4515. covering_unique_constraint = models.UniqueConstraint(
  4516. fields=["pink"],
  4517. name="covering_pink_constraint_add",
  4518. include=["weight"],
  4519. )
  4520. operation = migrations.AddConstraint("Pony", covering_unique_constraint)
  4521. self.assertEqual(
  4522. operation.describe(),
  4523. "Create constraint covering_pink_constraint_add on model Pony",
  4524. )
  4525. # Add constraint.
  4526. new_state = project_state.clone()
  4527. operation.state_forwards(app_label, new_state)
  4528. self.assertEqual(
  4529. len(new_state.models[app_label, "pony"].options["constraints"]), 1
  4530. )
  4531. Pony = new_state.apps.get_model(app_label, "Pony")
  4532. self.assertEqual(len(Pony._meta.constraints), 1)
  4533. with (
  4534. connection.schema_editor() as editor,
  4535. CaptureQueriesContext(connection) as ctx,
  4536. ):
  4537. operation.database_forwards(app_label, editor, project_state, new_state)
  4538. Pony.objects.create(pink=1, weight=4.0)
  4539. if connection.features.supports_covering_indexes:
  4540. with self.assertRaises(IntegrityError):
  4541. Pony.objects.create(pink=1, weight=4.0)
  4542. else:
  4543. self.assertEqual(len(ctx), 0)
  4544. Pony.objects.create(pink=1, weight=4.0)
  4545. # Reversal.
  4546. with connection.schema_editor() as editor:
  4547. operation.database_backwards(app_label, editor, new_state, project_state)
  4548. # Constraint doesn't work.
  4549. Pony.objects.create(pink=1, weight=4.0)
  4550. # Deconstruction.
  4551. definition = operation.deconstruct()
  4552. self.assertEqual(definition[0], "AddConstraint")
  4553. self.assertEqual(definition[1], [])
  4554. self.assertEqual(
  4555. definition[2],
  4556. {"model_name": "Pony", "constraint": covering_unique_constraint},
  4557. )
  4558. def test_remove_covering_unique_constraint(self):
  4559. app_label = "test_removecovering_uc"
  4560. covering_unique_constraint = models.UniqueConstraint(
  4561. fields=["pink"],
  4562. name="covering_pink_constraint_rm",
  4563. include=["weight"],
  4564. )
  4565. project_state = self.set_up_test_model(
  4566. app_label, constraints=[covering_unique_constraint]
  4567. )
  4568. operation = migrations.RemoveConstraint("Pony", covering_unique_constraint.name)
  4569. self.assertEqual(
  4570. operation.describe(),
  4571. "Remove constraint covering_pink_constraint_rm from model Pony",
  4572. )
  4573. # Remove constraint.
  4574. new_state = project_state.clone()
  4575. operation.state_forwards(app_label, new_state)
  4576. self.assertEqual(
  4577. len(new_state.models[app_label, "pony"].options["constraints"]), 0
  4578. )
  4579. Pony = new_state.apps.get_model(app_label, "Pony")
  4580. self.assertEqual(len(Pony._meta.constraints), 0)
  4581. with (
  4582. connection.schema_editor() as editor,
  4583. CaptureQueriesContext(connection) as ctx,
  4584. ):
  4585. operation.database_forwards(app_label, editor, project_state, new_state)
  4586. # Constraint doesn't work.
  4587. Pony.objects.create(pink=1, weight=4.0)
  4588. Pony.objects.create(pink=1, weight=4.0).delete()
  4589. if not connection.features.supports_covering_indexes:
  4590. self.assertEqual(len(ctx), 0)
  4591. # Reversal.
  4592. with connection.schema_editor() as editor:
  4593. operation.database_backwards(app_label, editor, new_state, project_state)
  4594. if connection.features.supports_covering_indexes:
  4595. with self.assertRaises(IntegrityError):
  4596. Pony.objects.create(pink=1, weight=4.0)
  4597. else:
  4598. Pony.objects.create(pink=1, weight=4.0)
  4599. # Deconstruction.
  4600. definition = operation.deconstruct()
  4601. self.assertEqual(definition[0], "RemoveConstraint")
  4602. self.assertEqual(definition[1], [])
  4603. self.assertEqual(
  4604. definition[2],
  4605. {
  4606. "model_name": "Pony",
  4607. "name": "covering_pink_constraint_rm",
  4608. },
  4609. )
  4610. def test_alter_field_with_func_unique_constraint(self):
  4611. app_label = "test_alfuncuc"
  4612. constraint_name = f"{app_label}_pony_uq"
  4613. table_name = f"{app_label}_pony"
  4614. project_state = self.set_up_test_model(
  4615. app_label,
  4616. constraints=[
  4617. models.UniqueConstraint("pink", "weight", name=constraint_name)
  4618. ],
  4619. )
  4620. operation = migrations.AlterField(
  4621. "Pony", "pink", models.IntegerField(null=True)
  4622. )
  4623. new_state = project_state.clone()
  4624. operation.state_forwards(app_label, new_state)
  4625. with connection.schema_editor() as editor:
  4626. operation.database_forwards(app_label, editor, project_state, new_state)
  4627. if connection.features.supports_expression_indexes:
  4628. self.assertIndexNameExists(table_name, constraint_name)
  4629. with connection.schema_editor() as editor:
  4630. operation.database_backwards(app_label, editor, new_state, project_state)
  4631. if connection.features.supports_expression_indexes:
  4632. self.assertIndexNameExists(table_name, constraint_name)
  4633. def test_add_func_unique_constraint(self):
  4634. app_label = "test_adfuncuc"
  4635. constraint_name = f"{app_label}_pony_abs_uq"
  4636. table_name = f"{app_label}_pony"
  4637. project_state = self.set_up_test_model(app_label)
  4638. constraint = models.UniqueConstraint(Abs("weight"), name=constraint_name)
  4639. operation = migrations.AddConstraint("Pony", constraint)
  4640. self.assertEqual(
  4641. operation.describe(),
  4642. "Create constraint test_adfuncuc_pony_abs_uq on model Pony",
  4643. )
  4644. self.assertEqual(
  4645. operation.migration_name_fragment,
  4646. "pony_test_adfuncuc_pony_abs_uq",
  4647. )
  4648. new_state = project_state.clone()
  4649. operation.state_forwards(app_label, new_state)
  4650. self.assertEqual(
  4651. len(new_state.models[app_label, "pony"].options["constraints"]), 1
  4652. )
  4653. self.assertIndexNameNotExists(table_name, constraint_name)
  4654. # Add constraint.
  4655. with connection.schema_editor() as editor:
  4656. operation.database_forwards(app_label, editor, project_state, new_state)
  4657. Pony = new_state.apps.get_model(app_label, "Pony")
  4658. Pony.objects.create(weight=4.0)
  4659. if connection.features.supports_expression_indexes:
  4660. self.assertIndexNameExists(table_name, constraint_name)
  4661. with self.assertRaises(IntegrityError):
  4662. Pony.objects.create(weight=-4.0)
  4663. else:
  4664. self.assertIndexNameNotExists(table_name, constraint_name)
  4665. Pony.objects.create(weight=-4.0)
  4666. # Reversal.
  4667. with connection.schema_editor() as editor:
  4668. operation.database_backwards(app_label, editor, new_state, project_state)
  4669. self.assertIndexNameNotExists(table_name, constraint_name)
  4670. # Constraint doesn't work.
  4671. Pony.objects.create(weight=-4.0)
  4672. # Deconstruction.
  4673. definition = operation.deconstruct()
  4674. self.assertEqual(definition[0], "AddConstraint")
  4675. self.assertEqual(definition[1], [])
  4676. self.assertEqual(
  4677. definition[2],
  4678. {"model_name": "Pony", "constraint": constraint},
  4679. )
  4680. def test_remove_func_unique_constraint(self):
  4681. app_label = "test_rmfuncuc"
  4682. constraint_name = f"{app_label}_pony_abs_uq"
  4683. table_name = f"{app_label}_pony"
  4684. project_state = self.set_up_test_model(
  4685. app_label,
  4686. constraints=[
  4687. models.UniqueConstraint(Abs("weight"), name=constraint_name),
  4688. ],
  4689. )
  4690. self.assertTableExists(table_name)
  4691. if connection.features.supports_expression_indexes:
  4692. self.assertIndexNameExists(table_name, constraint_name)
  4693. operation = migrations.RemoveConstraint("Pony", constraint_name)
  4694. self.assertEqual(
  4695. operation.describe(),
  4696. "Remove constraint test_rmfuncuc_pony_abs_uq from model Pony",
  4697. )
  4698. self.assertEqual(
  4699. operation.migration_name_fragment,
  4700. "remove_pony_test_rmfuncuc_pony_abs_uq",
  4701. )
  4702. new_state = project_state.clone()
  4703. operation.state_forwards(app_label, new_state)
  4704. self.assertEqual(
  4705. len(new_state.models[app_label, "pony"].options["constraints"]), 0
  4706. )
  4707. Pony = new_state.apps.get_model(app_label, "Pony")
  4708. self.assertEqual(len(Pony._meta.constraints), 0)
  4709. # Remove constraint.
  4710. with connection.schema_editor() as editor:
  4711. operation.database_forwards(app_label, editor, project_state, new_state)
  4712. self.assertIndexNameNotExists(table_name, constraint_name)
  4713. # Constraint doesn't work.
  4714. Pony.objects.create(pink=1, weight=4.0)
  4715. Pony.objects.create(pink=1, weight=-4.0).delete()
  4716. # Reversal.
  4717. with connection.schema_editor() as editor:
  4718. operation.database_backwards(app_label, editor, new_state, project_state)
  4719. if connection.features.supports_expression_indexes:
  4720. self.assertIndexNameExists(table_name, constraint_name)
  4721. with self.assertRaises(IntegrityError):
  4722. Pony.objects.create(weight=-4.0)
  4723. else:
  4724. self.assertIndexNameNotExists(table_name, constraint_name)
  4725. Pony.objects.create(weight=-4.0)
  4726. # Deconstruction.
  4727. definition = operation.deconstruct()
  4728. self.assertEqual(definition[0], "RemoveConstraint")
  4729. self.assertEqual(definition[1], [])
  4730. self.assertEqual(definition[2], {"model_name": "Pony", "name": constraint_name})
  4731. def test_alter_model_options(self):
  4732. """
  4733. Tests the AlterModelOptions operation.
  4734. """
  4735. project_state = self.set_up_test_model("test_almoop")
  4736. # Test the state alteration (no DB alteration to test)
  4737. operation = migrations.AlterModelOptions(
  4738. "Pony", {"permissions": [("can_groom", "Can groom")]}
  4739. )
  4740. self.assertEqual(operation.describe(), "Change Meta options on Pony")
  4741. self.assertEqual(
  4742. operation.formatted_description(), "~ Change Meta options on Pony"
  4743. )
  4744. self.assertEqual(operation.migration_name_fragment, "alter_pony_options")
  4745. new_state = project_state.clone()
  4746. operation.state_forwards("test_almoop", new_state)
  4747. self.assertEqual(
  4748. len(
  4749. project_state.models["test_almoop", "pony"].options.get(
  4750. "permissions", []
  4751. )
  4752. ),
  4753. 0,
  4754. )
  4755. self.assertEqual(
  4756. len(new_state.models["test_almoop", "pony"].options.get("permissions", [])),
  4757. 1,
  4758. )
  4759. self.assertEqual(
  4760. new_state.models["test_almoop", "pony"].options["permissions"][0][0],
  4761. "can_groom",
  4762. )
  4763. # And deconstruction
  4764. definition = operation.deconstruct()
  4765. self.assertEqual(definition[0], "AlterModelOptions")
  4766. self.assertEqual(definition[1], [])
  4767. self.assertEqual(
  4768. definition[2],
  4769. {"name": "Pony", "options": {"permissions": [("can_groom", "Can groom")]}},
  4770. )
  4771. def test_alter_model_options_emptying(self):
  4772. """
  4773. The AlterModelOptions operation removes keys from the dict (#23121)
  4774. """
  4775. project_state = self.set_up_test_model("test_almoop", options=True)
  4776. # Test the state alteration (no DB alteration to test)
  4777. operation = migrations.AlterModelOptions("Pony", {})
  4778. self.assertEqual(operation.describe(), "Change Meta options on Pony")
  4779. new_state = project_state.clone()
  4780. operation.state_forwards("test_almoop", new_state)
  4781. self.assertEqual(
  4782. len(
  4783. project_state.models["test_almoop", "pony"].options.get(
  4784. "permissions", []
  4785. )
  4786. ),
  4787. 1,
  4788. )
  4789. self.assertEqual(
  4790. len(new_state.models["test_almoop", "pony"].options.get("permissions", [])),
  4791. 0,
  4792. )
  4793. # And deconstruction
  4794. definition = operation.deconstruct()
  4795. self.assertEqual(definition[0], "AlterModelOptions")
  4796. self.assertEqual(definition[1], [])
  4797. self.assertEqual(definition[2], {"name": "Pony", "options": {}})
  4798. def test_alter_order_with_respect_to(self):
  4799. """
  4800. Tests the AlterOrderWithRespectTo operation.
  4801. """
  4802. project_state = self.set_up_test_model("test_alorwrtto", related_model=True)
  4803. # Test the state alteration
  4804. operation = migrations.AlterOrderWithRespectTo("Rider", "pony")
  4805. self.assertEqual(
  4806. operation.describe(), "Set order_with_respect_to on Rider to pony"
  4807. )
  4808. self.assertEqual(
  4809. operation.formatted_description(),
  4810. "~ Set order_with_respect_to on Rider to pony",
  4811. )
  4812. self.assertEqual(
  4813. operation.migration_name_fragment,
  4814. "alter_rider_order_with_respect_to",
  4815. )
  4816. new_state = project_state.clone()
  4817. operation.state_forwards("test_alorwrtto", new_state)
  4818. self.assertIsNone(
  4819. project_state.models["test_alorwrtto", "rider"].options.get(
  4820. "order_with_respect_to", None
  4821. )
  4822. )
  4823. self.assertEqual(
  4824. new_state.models["test_alorwrtto", "rider"].options.get(
  4825. "order_with_respect_to", None
  4826. ),
  4827. "pony",
  4828. )
  4829. # Make sure there's no matching index
  4830. self.assertColumnNotExists("test_alorwrtto_rider", "_order")
  4831. # Create some rows before alteration
  4832. rendered_state = project_state.apps
  4833. pony = rendered_state.get_model("test_alorwrtto", "Pony").objects.create(
  4834. weight=50
  4835. )
  4836. rider1 = rendered_state.get_model("test_alorwrtto", "Rider").objects.create(
  4837. pony=pony
  4838. )
  4839. rider1.friend = rider1
  4840. rider1.save()
  4841. rider2 = rendered_state.get_model("test_alorwrtto", "Rider").objects.create(
  4842. pony=pony
  4843. )
  4844. rider2.friend = rider2
  4845. rider2.save()
  4846. # Test the database alteration
  4847. with connection.schema_editor() as editor:
  4848. operation.database_forwards(
  4849. "test_alorwrtto", editor, project_state, new_state
  4850. )
  4851. self.assertColumnExists("test_alorwrtto_rider", "_order")
  4852. # Check for correct value in rows
  4853. updated_riders = new_state.apps.get_model(
  4854. "test_alorwrtto", "Rider"
  4855. ).objects.all()
  4856. self.assertEqual(updated_riders[0]._order, 0)
  4857. self.assertEqual(updated_riders[1]._order, 0)
  4858. # And test reversal
  4859. with connection.schema_editor() as editor:
  4860. operation.database_backwards(
  4861. "test_alorwrtto", editor, new_state, project_state
  4862. )
  4863. self.assertColumnNotExists("test_alorwrtto_rider", "_order")
  4864. # And deconstruction
  4865. definition = operation.deconstruct()
  4866. self.assertEqual(definition[0], "AlterOrderWithRespectTo")
  4867. self.assertEqual(definition[1], [])
  4868. self.assertEqual(
  4869. definition[2], {"name": "Rider", "order_with_respect_to": "pony"}
  4870. )
  4871. def test_alter_model_managers(self):
  4872. """
  4873. The managers on a model are set.
  4874. """
  4875. project_state = self.set_up_test_model("test_almoma")
  4876. # Test the state alteration
  4877. operation = migrations.AlterModelManagers(
  4878. "Pony",
  4879. managers=[
  4880. ("food_qs", FoodQuerySet.as_manager()),
  4881. ("food_mgr", FoodManager("a", "b")),
  4882. ("food_mgr_kwargs", FoodManager("x", "y", 3, 4)),
  4883. ],
  4884. )
  4885. self.assertEqual(operation.describe(), "Change managers on Pony")
  4886. self.assertEqual(operation.formatted_description(), "~ Change managers on Pony")
  4887. self.assertEqual(operation.migration_name_fragment, "alter_pony_managers")
  4888. managers = project_state.models["test_almoma", "pony"].managers
  4889. self.assertEqual(managers, [])
  4890. new_state = project_state.clone()
  4891. operation.state_forwards("test_almoma", new_state)
  4892. self.assertIn(("test_almoma", "pony"), new_state.models)
  4893. managers = new_state.models["test_almoma", "pony"].managers
  4894. self.assertEqual(managers[0][0], "food_qs")
  4895. self.assertIsInstance(managers[0][1], models.Manager)
  4896. self.assertEqual(managers[1][0], "food_mgr")
  4897. self.assertIsInstance(managers[1][1], FoodManager)
  4898. self.assertEqual(managers[1][1].args, ("a", "b", 1, 2))
  4899. self.assertEqual(managers[2][0], "food_mgr_kwargs")
  4900. self.assertIsInstance(managers[2][1], FoodManager)
  4901. self.assertEqual(managers[2][1].args, ("x", "y", 3, 4))
  4902. rendered_state = new_state.apps
  4903. model = rendered_state.get_model("test_almoma", "pony")
  4904. self.assertIsInstance(model.food_qs, models.Manager)
  4905. self.assertIsInstance(model.food_mgr, FoodManager)
  4906. self.assertIsInstance(model.food_mgr_kwargs, FoodManager)
  4907. def test_alter_model_managers_emptying(self):
  4908. """
  4909. The managers on a model are set.
  4910. """
  4911. project_state = self.set_up_test_model("test_almomae", manager_model=True)
  4912. # Test the state alteration
  4913. operation = migrations.AlterModelManagers("Food", managers=[])
  4914. self.assertEqual(operation.describe(), "Change managers on Food")
  4915. self.assertIn(("test_almomae", "food"), project_state.models)
  4916. managers = project_state.models["test_almomae", "food"].managers
  4917. self.assertEqual(managers[0][0], "food_qs")
  4918. self.assertIsInstance(managers[0][1], models.Manager)
  4919. self.assertEqual(managers[1][0], "food_mgr")
  4920. self.assertIsInstance(managers[1][1], FoodManager)
  4921. self.assertEqual(managers[1][1].args, ("a", "b", 1, 2))
  4922. self.assertEqual(managers[2][0], "food_mgr_kwargs")
  4923. self.assertIsInstance(managers[2][1], FoodManager)
  4924. self.assertEqual(managers[2][1].args, ("x", "y", 3, 4))
  4925. new_state = project_state.clone()
  4926. operation.state_forwards("test_almomae", new_state)
  4927. managers = new_state.models["test_almomae", "food"].managers
  4928. self.assertEqual(managers, [])
  4929. def test_alter_fk(self):
  4930. """
  4931. Creating and then altering an FK works correctly
  4932. and deals with the pending SQL (#23091)
  4933. """
  4934. project_state = self.set_up_test_model("test_alfk")
  4935. # Test adding and then altering the FK in one go
  4936. create_operation = migrations.CreateModel(
  4937. name="Rider",
  4938. fields=[
  4939. ("id", models.AutoField(primary_key=True)),
  4940. ("pony", models.ForeignKey("Pony", models.CASCADE)),
  4941. ],
  4942. )
  4943. create_state = project_state.clone()
  4944. create_operation.state_forwards("test_alfk", create_state)
  4945. alter_operation = migrations.AlterField(
  4946. model_name="Rider",
  4947. name="pony",
  4948. field=models.ForeignKey("Pony", models.CASCADE, editable=False),
  4949. )
  4950. alter_state = create_state.clone()
  4951. alter_operation.state_forwards("test_alfk", alter_state)
  4952. with connection.schema_editor() as editor:
  4953. create_operation.database_forwards(
  4954. "test_alfk", editor, project_state, create_state
  4955. )
  4956. alter_operation.database_forwards(
  4957. "test_alfk", editor, create_state, alter_state
  4958. )
  4959. def test_alter_fk_non_fk(self):
  4960. """
  4961. Altering an FK to a non-FK works (#23244)
  4962. """
  4963. # Test the state alteration
  4964. operation = migrations.AlterField(
  4965. model_name="Rider",
  4966. name="pony",
  4967. field=models.FloatField(),
  4968. )
  4969. project_state, new_state = self.make_test_state(
  4970. "test_afknfk", operation, related_model=True
  4971. )
  4972. # Test the database alteration
  4973. self.assertColumnExists("test_afknfk_rider", "pony_id")
  4974. self.assertColumnNotExists("test_afknfk_rider", "pony")
  4975. with connection.schema_editor() as editor:
  4976. operation.database_forwards("test_afknfk", editor, project_state, new_state)
  4977. self.assertColumnExists("test_afknfk_rider", "pony")
  4978. self.assertColumnNotExists("test_afknfk_rider", "pony_id")
  4979. # And test reversal
  4980. with connection.schema_editor() as editor:
  4981. operation.database_backwards(
  4982. "test_afknfk", editor, new_state, project_state
  4983. )
  4984. self.assertColumnExists("test_afknfk_rider", "pony_id")
  4985. self.assertColumnNotExists("test_afknfk_rider", "pony")
  4986. def test_run_sql(self):
  4987. """
  4988. Tests the RunSQL operation.
  4989. """
  4990. project_state = self.set_up_test_model("test_runsql")
  4991. # Create the operation
  4992. operation = migrations.RunSQL(
  4993. # Use a multi-line string with a comment to test splitting on
  4994. # SQLite and MySQL respectively.
  4995. "CREATE TABLE i_love_ponies (id int, special_thing varchar(15));\n"
  4996. "INSERT INTO i_love_ponies (id, special_thing) "
  4997. "VALUES (1, 'i love ponies'); -- this is magic!\n"
  4998. "INSERT INTO i_love_ponies (id, special_thing) "
  4999. "VALUES (2, 'i love django');\n"
  5000. "UPDATE i_love_ponies SET special_thing = 'Ponies' "
  5001. "WHERE special_thing LIKE '%%ponies';"
  5002. "UPDATE i_love_ponies SET special_thing = 'Django' "
  5003. "WHERE special_thing LIKE '%django';",
  5004. # Run delete queries to test for parameter substitution failure
  5005. # reported in #23426
  5006. "DELETE FROM i_love_ponies WHERE special_thing LIKE '%Django%';"
  5007. "DELETE FROM i_love_ponies WHERE special_thing LIKE '%%Ponies%%';"
  5008. "DROP TABLE i_love_ponies",
  5009. state_operations=[
  5010. migrations.CreateModel(
  5011. "SomethingElse", [("id", models.AutoField(primary_key=True))]
  5012. )
  5013. ],
  5014. )
  5015. self.assertEqual(operation.describe(), "Raw SQL operation")
  5016. self.assertEqual(operation.formatted_description(), "s Raw SQL operation")
  5017. # Test the state alteration
  5018. new_state = project_state.clone()
  5019. operation.state_forwards("test_runsql", new_state)
  5020. self.assertEqual(
  5021. len(new_state.models["test_runsql", "somethingelse"].fields), 1
  5022. )
  5023. # Make sure there's no table
  5024. self.assertTableNotExists("i_love_ponies")
  5025. # Test SQL collection
  5026. with connection.schema_editor(collect_sql=True) as editor:
  5027. operation.database_forwards("test_runsql", editor, project_state, new_state)
  5028. self.assertIn("LIKE '%%ponies';", "\n".join(editor.collected_sql))
  5029. operation.database_backwards(
  5030. "test_runsql", editor, project_state, new_state
  5031. )
  5032. self.assertIn("LIKE '%%Ponies%%';", "\n".join(editor.collected_sql))
  5033. # Test the database alteration
  5034. with connection.schema_editor() as editor:
  5035. operation.database_forwards("test_runsql", editor, project_state, new_state)
  5036. self.assertTableExists("i_love_ponies")
  5037. # Make sure all the SQL was processed
  5038. with connection.cursor() as cursor:
  5039. cursor.execute("SELECT COUNT(*) FROM i_love_ponies")
  5040. self.assertEqual(cursor.fetchall()[0][0], 2)
  5041. cursor.execute(
  5042. "SELECT COUNT(*) FROM i_love_ponies WHERE special_thing = 'Django'"
  5043. )
  5044. self.assertEqual(cursor.fetchall()[0][0], 1)
  5045. cursor.execute(
  5046. "SELECT COUNT(*) FROM i_love_ponies WHERE special_thing = 'Ponies'"
  5047. )
  5048. self.assertEqual(cursor.fetchall()[0][0], 1)
  5049. # And test reversal
  5050. self.assertTrue(operation.reversible)
  5051. with connection.schema_editor() as editor:
  5052. operation.database_backwards(
  5053. "test_runsql", editor, new_state, project_state
  5054. )
  5055. self.assertTableNotExists("i_love_ponies")
  5056. # And deconstruction
  5057. definition = operation.deconstruct()
  5058. self.assertEqual(definition[0], "RunSQL")
  5059. self.assertEqual(definition[1], [])
  5060. self.assertEqual(
  5061. sorted(definition[2]), ["reverse_sql", "sql", "state_operations"]
  5062. )
  5063. # And elidable reduction
  5064. self.assertIs(False, operation.reduce(operation, []))
  5065. elidable_operation = migrations.RunSQL("SELECT 1 FROM void;", elidable=True)
  5066. self.assertEqual(elidable_operation.reduce(operation, []), [operation])
  5067. def test_run_sql_params(self):
  5068. """
  5069. #23426 - RunSQL should accept parameters.
  5070. """
  5071. project_state = self.set_up_test_model("test_runsql")
  5072. # Create the operation
  5073. operation = migrations.RunSQL(
  5074. ["CREATE TABLE i_love_ponies (id int, special_thing varchar(15));"],
  5075. ["DROP TABLE i_love_ponies"],
  5076. )
  5077. param_operation = migrations.RunSQL(
  5078. # forwards
  5079. (
  5080. "INSERT INTO i_love_ponies (id, special_thing) VALUES (1, 'Django');",
  5081. [
  5082. "INSERT INTO i_love_ponies (id, special_thing) VALUES (2, %s);",
  5083. ["Ponies"],
  5084. ],
  5085. (
  5086. "INSERT INTO i_love_ponies (id, special_thing) VALUES (%s, %s);",
  5087. (
  5088. 3,
  5089. "Python",
  5090. ),
  5091. ),
  5092. ),
  5093. # backwards
  5094. [
  5095. "DELETE FROM i_love_ponies WHERE special_thing = 'Django';",
  5096. ["DELETE FROM i_love_ponies WHERE special_thing = 'Ponies';", None],
  5097. (
  5098. "DELETE FROM i_love_ponies WHERE id = %s OR special_thing = %s;",
  5099. [3, "Python"],
  5100. ),
  5101. ],
  5102. )
  5103. # Make sure there's no table
  5104. self.assertTableNotExists("i_love_ponies")
  5105. new_state = project_state.clone()
  5106. # Test the database alteration
  5107. with connection.schema_editor() as editor:
  5108. operation.database_forwards("test_runsql", editor, project_state, new_state)
  5109. # Test parameter passing
  5110. with connection.schema_editor() as editor:
  5111. param_operation.database_forwards(
  5112. "test_runsql", editor, project_state, new_state
  5113. )
  5114. # Make sure all the SQL was processed
  5115. with connection.cursor() as cursor:
  5116. cursor.execute("SELECT COUNT(*) FROM i_love_ponies")
  5117. self.assertEqual(cursor.fetchall()[0][0], 3)
  5118. with connection.schema_editor() as editor:
  5119. param_operation.database_backwards(
  5120. "test_runsql", editor, new_state, project_state
  5121. )
  5122. with connection.cursor() as cursor:
  5123. cursor.execute("SELECT COUNT(*) FROM i_love_ponies")
  5124. self.assertEqual(cursor.fetchall()[0][0], 0)
  5125. # And test reversal
  5126. with connection.schema_editor() as editor:
  5127. operation.database_backwards(
  5128. "test_runsql", editor, new_state, project_state
  5129. )
  5130. self.assertTableNotExists("i_love_ponies")
  5131. def test_run_sql_params_invalid(self):
  5132. """
  5133. #23426 - RunSQL should fail when a list of statements with an incorrect
  5134. number of tuples is given.
  5135. """
  5136. project_state = self.set_up_test_model("test_runsql")
  5137. new_state = project_state.clone()
  5138. operation = migrations.RunSQL(
  5139. # forwards
  5140. [["INSERT INTO foo (bar) VALUES ('buz');"]],
  5141. # backwards
  5142. (("DELETE FROM foo WHERE bar = 'buz';", "invalid", "parameter count"),),
  5143. )
  5144. with connection.schema_editor() as editor:
  5145. with self.assertRaisesMessage(ValueError, "Expected a 2-tuple but got 1"):
  5146. operation.database_forwards(
  5147. "test_runsql", editor, project_state, new_state
  5148. )
  5149. with connection.schema_editor() as editor:
  5150. with self.assertRaisesMessage(ValueError, "Expected a 2-tuple but got 3"):
  5151. operation.database_backwards(
  5152. "test_runsql", editor, new_state, project_state
  5153. )
  5154. def test_run_sql_noop(self):
  5155. """
  5156. #24098 - Tests no-op RunSQL operations.
  5157. """
  5158. operation = migrations.RunSQL(migrations.RunSQL.noop, migrations.RunSQL.noop)
  5159. with connection.schema_editor() as editor:
  5160. operation.database_forwards("test_runsql", editor, None, None)
  5161. operation.database_backwards("test_runsql", editor, None, None)
  5162. def test_run_sql_add_missing_semicolon_on_collect_sql(self):
  5163. project_state = self.set_up_test_model("test_runsql")
  5164. new_state = project_state.clone()
  5165. tests = [
  5166. "INSERT INTO test_runsql_pony (pink, weight) VALUES (1, 1);\n",
  5167. "INSERT INTO test_runsql_pony (pink, weight) VALUES (1, 1)\n",
  5168. ]
  5169. for sql in tests:
  5170. with self.subTest(sql=sql):
  5171. operation = migrations.RunSQL(sql, migrations.RunPython.noop)
  5172. with connection.schema_editor(collect_sql=True) as editor:
  5173. operation.database_forwards(
  5174. "test_runsql", editor, project_state, new_state
  5175. )
  5176. collected_sql = "\n".join(editor.collected_sql)
  5177. self.assertEqual(collected_sql.count(";"), 1)
  5178. def test_run_sql_backward_reverse_sql_required(self):
  5179. operation = migrations.RunSQL(sql=migrations.RunSQL.noop)
  5180. msg = "You cannot reverse this operation"
  5181. with (
  5182. connection.schema_editor() as editor,
  5183. self.assertRaisesMessage(NotImplementedError, msg),
  5184. ):
  5185. operation.database_backwards("test_runsql", editor, None, None)
  5186. def test_run_python(self):
  5187. """
  5188. Tests the RunPython operation
  5189. """
  5190. project_state = self.set_up_test_model("test_runpython", mti_model=True)
  5191. # Create the operation
  5192. def inner_method(models, schema_editor):
  5193. Pony = models.get_model("test_runpython", "Pony")
  5194. Pony.objects.create(pink=1, weight=3.55)
  5195. Pony.objects.create(weight=5)
  5196. def inner_method_reverse(models, schema_editor):
  5197. Pony = models.get_model("test_runpython", "Pony")
  5198. Pony.objects.filter(pink=1, weight=3.55).delete()
  5199. Pony.objects.filter(weight=5).delete()
  5200. operation = migrations.RunPython(
  5201. inner_method, reverse_code=inner_method_reverse
  5202. )
  5203. self.assertEqual(operation.describe(), "Raw Python operation")
  5204. self.assertEqual(operation.formatted_description(), "p Raw Python operation")
  5205. # Test the state alteration does nothing
  5206. new_state = project_state.clone()
  5207. operation.state_forwards("test_runpython", new_state)
  5208. self.assertEqual(new_state, project_state)
  5209. # Test the database alteration
  5210. self.assertEqual(
  5211. project_state.apps.get_model("test_runpython", "Pony").objects.count(), 0
  5212. )
  5213. with connection.schema_editor() as editor:
  5214. operation.database_forwards(
  5215. "test_runpython", editor, project_state, new_state
  5216. )
  5217. self.assertEqual(
  5218. project_state.apps.get_model("test_runpython", "Pony").objects.count(), 2
  5219. )
  5220. # Now test reversal
  5221. self.assertTrue(operation.reversible)
  5222. with connection.schema_editor() as editor:
  5223. operation.database_backwards(
  5224. "test_runpython", editor, project_state, new_state
  5225. )
  5226. self.assertEqual(
  5227. project_state.apps.get_model("test_runpython", "Pony").objects.count(), 0
  5228. )
  5229. # Now test we can't use a string
  5230. with self.assertRaisesMessage(
  5231. ValueError, "RunPython must be supplied with a callable"
  5232. ):
  5233. migrations.RunPython("print 'ahahaha'")
  5234. # And deconstruction
  5235. definition = operation.deconstruct()
  5236. self.assertEqual(definition[0], "RunPython")
  5237. self.assertEqual(definition[1], [])
  5238. self.assertEqual(sorted(definition[2]), ["code", "reverse_code"])
  5239. # Also test reversal fails, with an operation identical to above but
  5240. # without reverse_code set.
  5241. no_reverse_operation = migrations.RunPython(inner_method)
  5242. self.assertFalse(no_reverse_operation.reversible)
  5243. with connection.schema_editor() as editor:
  5244. no_reverse_operation.database_forwards(
  5245. "test_runpython", editor, project_state, new_state
  5246. )
  5247. with self.assertRaises(NotImplementedError):
  5248. no_reverse_operation.database_backwards(
  5249. "test_runpython", editor, new_state, project_state
  5250. )
  5251. self.assertEqual(
  5252. project_state.apps.get_model("test_runpython", "Pony").objects.count(), 2
  5253. )
  5254. def create_ponies(models, schema_editor):
  5255. Pony = models.get_model("test_runpython", "Pony")
  5256. pony1 = Pony.objects.create(pink=1, weight=3.55)
  5257. self.assertIsNot(pony1.pk, None)
  5258. pony2 = Pony.objects.create(weight=5)
  5259. self.assertIsNot(pony2.pk, None)
  5260. self.assertNotEqual(pony1.pk, pony2.pk)
  5261. operation = migrations.RunPython(create_ponies)
  5262. with connection.schema_editor() as editor:
  5263. operation.database_forwards(
  5264. "test_runpython", editor, project_state, new_state
  5265. )
  5266. self.assertEqual(
  5267. project_state.apps.get_model("test_runpython", "Pony").objects.count(), 4
  5268. )
  5269. # And deconstruction
  5270. definition = operation.deconstruct()
  5271. self.assertEqual(definition[0], "RunPython")
  5272. self.assertEqual(definition[1], [])
  5273. self.assertEqual(sorted(definition[2]), ["code"])
  5274. def create_shetlandponies(models, schema_editor):
  5275. ShetlandPony = models.get_model("test_runpython", "ShetlandPony")
  5276. pony1 = ShetlandPony.objects.create(weight=4.0)
  5277. self.assertIsNot(pony1.pk, None)
  5278. pony2 = ShetlandPony.objects.create(weight=5.0)
  5279. self.assertIsNot(pony2.pk, None)
  5280. self.assertNotEqual(pony1.pk, pony2.pk)
  5281. operation = migrations.RunPython(create_shetlandponies)
  5282. with connection.schema_editor() as editor:
  5283. operation.database_forwards(
  5284. "test_runpython", editor, project_state, new_state
  5285. )
  5286. self.assertEqual(
  5287. project_state.apps.get_model("test_runpython", "Pony").objects.count(), 6
  5288. )
  5289. self.assertEqual(
  5290. project_state.apps.get_model(
  5291. "test_runpython", "ShetlandPony"
  5292. ).objects.count(),
  5293. 2,
  5294. )
  5295. # And elidable reduction
  5296. self.assertIs(False, operation.reduce(operation, []))
  5297. elidable_operation = migrations.RunPython(inner_method, elidable=True)
  5298. self.assertEqual(elidable_operation.reduce(operation, []), [operation])
  5299. def test_run_python_invalid_reverse_code(self):
  5300. msg = "RunPython must be supplied with callable arguments"
  5301. with self.assertRaisesMessage(ValueError, msg):
  5302. migrations.RunPython(code=migrations.RunPython.noop, reverse_code="invalid")
  5303. def test_run_python_atomic(self):
  5304. """
  5305. Tests the RunPython operation correctly handles the "atomic" keyword
  5306. """
  5307. project_state = self.set_up_test_model("test_runpythonatomic", mti_model=True)
  5308. def inner_method(models, schema_editor):
  5309. Pony = models.get_model("test_runpythonatomic", "Pony")
  5310. Pony.objects.create(pink=1, weight=3.55)
  5311. raise ValueError("Adrian hates ponies.")
  5312. # Verify atomicity when applying.
  5313. atomic_migration = Migration("test", "test_runpythonatomic")
  5314. atomic_migration.operations = [
  5315. migrations.RunPython(inner_method, reverse_code=inner_method)
  5316. ]
  5317. non_atomic_migration = Migration("test", "test_runpythonatomic")
  5318. non_atomic_migration.operations = [
  5319. migrations.RunPython(inner_method, reverse_code=inner_method, atomic=False)
  5320. ]
  5321. # If we're a fully-transactional database, both versions should rollback
  5322. if connection.features.can_rollback_ddl:
  5323. self.assertEqual(
  5324. project_state.apps.get_model(
  5325. "test_runpythonatomic", "Pony"
  5326. ).objects.count(),
  5327. 0,
  5328. )
  5329. with self.assertRaises(ValueError):
  5330. with connection.schema_editor() as editor:
  5331. atomic_migration.apply(project_state, editor)
  5332. self.assertEqual(
  5333. project_state.apps.get_model(
  5334. "test_runpythonatomic", "Pony"
  5335. ).objects.count(),
  5336. 0,
  5337. )
  5338. with self.assertRaises(ValueError):
  5339. with connection.schema_editor() as editor:
  5340. non_atomic_migration.apply(project_state, editor)
  5341. self.assertEqual(
  5342. project_state.apps.get_model(
  5343. "test_runpythonatomic", "Pony"
  5344. ).objects.count(),
  5345. 0,
  5346. )
  5347. # Otherwise, the non-atomic operation should leave a row there
  5348. else:
  5349. self.assertEqual(
  5350. project_state.apps.get_model(
  5351. "test_runpythonatomic", "Pony"
  5352. ).objects.count(),
  5353. 0,
  5354. )
  5355. with self.assertRaises(ValueError):
  5356. with connection.schema_editor() as editor:
  5357. atomic_migration.apply(project_state, editor)
  5358. self.assertEqual(
  5359. project_state.apps.get_model(
  5360. "test_runpythonatomic", "Pony"
  5361. ).objects.count(),
  5362. 0,
  5363. )
  5364. with self.assertRaises(ValueError):
  5365. with connection.schema_editor() as editor:
  5366. non_atomic_migration.apply(project_state, editor)
  5367. self.assertEqual(
  5368. project_state.apps.get_model(
  5369. "test_runpythonatomic", "Pony"
  5370. ).objects.count(),
  5371. 1,
  5372. )
  5373. # Reset object count to zero and verify atomicity when unapplying.
  5374. project_state.apps.get_model(
  5375. "test_runpythonatomic", "Pony"
  5376. ).objects.all().delete()
  5377. # On a fully-transactional database, both versions rollback.
  5378. if connection.features.can_rollback_ddl:
  5379. self.assertEqual(
  5380. project_state.apps.get_model(
  5381. "test_runpythonatomic", "Pony"
  5382. ).objects.count(),
  5383. 0,
  5384. )
  5385. with self.assertRaises(ValueError):
  5386. with connection.schema_editor() as editor:
  5387. atomic_migration.unapply(project_state, editor)
  5388. self.assertEqual(
  5389. project_state.apps.get_model(
  5390. "test_runpythonatomic", "Pony"
  5391. ).objects.count(),
  5392. 0,
  5393. )
  5394. with self.assertRaises(ValueError):
  5395. with connection.schema_editor() as editor:
  5396. non_atomic_migration.unapply(project_state, editor)
  5397. self.assertEqual(
  5398. project_state.apps.get_model(
  5399. "test_runpythonatomic", "Pony"
  5400. ).objects.count(),
  5401. 0,
  5402. )
  5403. # Otherwise, the non-atomic operation leaves a row there.
  5404. else:
  5405. self.assertEqual(
  5406. project_state.apps.get_model(
  5407. "test_runpythonatomic", "Pony"
  5408. ).objects.count(),
  5409. 0,
  5410. )
  5411. with self.assertRaises(ValueError):
  5412. with connection.schema_editor() as editor:
  5413. atomic_migration.unapply(project_state, editor)
  5414. self.assertEqual(
  5415. project_state.apps.get_model(
  5416. "test_runpythonatomic", "Pony"
  5417. ).objects.count(),
  5418. 0,
  5419. )
  5420. with self.assertRaises(ValueError):
  5421. with connection.schema_editor() as editor:
  5422. non_atomic_migration.unapply(project_state, editor)
  5423. self.assertEqual(
  5424. project_state.apps.get_model(
  5425. "test_runpythonatomic", "Pony"
  5426. ).objects.count(),
  5427. 1,
  5428. )
  5429. # Verify deconstruction.
  5430. definition = non_atomic_migration.operations[0].deconstruct()
  5431. self.assertEqual(definition[0], "RunPython")
  5432. self.assertEqual(definition[1], [])
  5433. self.assertEqual(sorted(definition[2]), ["atomic", "code", "reverse_code"])
  5434. def test_run_python_related_assignment(self):
  5435. """
  5436. #24282 - Model changes to a FK reverse side update the model
  5437. on the FK side as well.
  5438. """
  5439. def inner_method(models, schema_editor):
  5440. Author = models.get_model("test_authors", "Author")
  5441. Book = models.get_model("test_books", "Book")
  5442. author = Author.objects.create(name="Hemingway")
  5443. Book.objects.create(title="Old Man and The Sea", author=author)
  5444. create_author = migrations.CreateModel(
  5445. "Author",
  5446. [
  5447. ("id", models.AutoField(primary_key=True)),
  5448. ("name", models.CharField(max_length=100)),
  5449. ],
  5450. options={},
  5451. )
  5452. create_book = migrations.CreateModel(
  5453. "Book",
  5454. [
  5455. ("id", models.AutoField(primary_key=True)),
  5456. ("title", models.CharField(max_length=100)),
  5457. ("author", models.ForeignKey("test_authors.Author", models.CASCADE)),
  5458. ],
  5459. options={},
  5460. )
  5461. add_hometown = migrations.AddField(
  5462. "Author",
  5463. "hometown",
  5464. models.CharField(max_length=100),
  5465. )
  5466. create_old_man = migrations.RunPython(inner_method, inner_method)
  5467. project_state = ProjectState()
  5468. new_state = project_state.clone()
  5469. with connection.schema_editor() as editor:
  5470. create_author.state_forwards("test_authors", new_state)
  5471. create_author.database_forwards(
  5472. "test_authors", editor, project_state, new_state
  5473. )
  5474. project_state = new_state
  5475. new_state = new_state.clone()
  5476. with connection.schema_editor() as editor:
  5477. create_book.state_forwards("test_books", new_state)
  5478. create_book.database_forwards(
  5479. "test_books", editor, project_state, new_state
  5480. )
  5481. project_state = new_state
  5482. new_state = new_state.clone()
  5483. with connection.schema_editor() as editor:
  5484. add_hometown.state_forwards("test_authors", new_state)
  5485. add_hometown.database_forwards(
  5486. "test_authors", editor, project_state, new_state
  5487. )
  5488. project_state = new_state
  5489. new_state = new_state.clone()
  5490. with connection.schema_editor() as editor:
  5491. create_old_man.state_forwards("test_books", new_state)
  5492. create_old_man.database_forwards(
  5493. "test_books", editor, project_state, new_state
  5494. )
  5495. def test_model_with_bigautofield(self):
  5496. """
  5497. A model with BigAutoField can be created.
  5498. """
  5499. def create_data(models, schema_editor):
  5500. Author = models.get_model("test_author", "Author")
  5501. Book = models.get_model("test_book", "Book")
  5502. author1 = Author.objects.create(name="Hemingway")
  5503. Book.objects.create(title="Old Man and The Sea", author=author1)
  5504. Book.objects.create(id=2**33, title="A farewell to arms", author=author1)
  5505. author2 = Author.objects.create(id=2**33, name="Remarque")
  5506. Book.objects.create(title="All quiet on the western front", author=author2)
  5507. Book.objects.create(title="Arc de Triomphe", author=author2)
  5508. create_author = migrations.CreateModel(
  5509. "Author",
  5510. [
  5511. ("id", models.BigAutoField(primary_key=True)),
  5512. ("name", models.CharField(max_length=100)),
  5513. ],
  5514. options={},
  5515. )
  5516. create_book = migrations.CreateModel(
  5517. "Book",
  5518. [
  5519. ("id", models.BigAutoField(primary_key=True)),
  5520. ("title", models.CharField(max_length=100)),
  5521. (
  5522. "author",
  5523. models.ForeignKey(
  5524. to="test_author.Author", on_delete=models.CASCADE
  5525. ),
  5526. ),
  5527. ],
  5528. options={},
  5529. )
  5530. fill_data = migrations.RunPython(create_data)
  5531. project_state = ProjectState()
  5532. new_state = project_state.clone()
  5533. with connection.schema_editor() as editor:
  5534. create_author.state_forwards("test_author", new_state)
  5535. create_author.database_forwards(
  5536. "test_author", editor, project_state, new_state
  5537. )
  5538. project_state = new_state
  5539. new_state = new_state.clone()
  5540. with connection.schema_editor() as editor:
  5541. create_book.state_forwards("test_book", new_state)
  5542. create_book.database_forwards("test_book", editor, project_state, new_state)
  5543. project_state = new_state
  5544. new_state = new_state.clone()
  5545. with connection.schema_editor() as editor:
  5546. fill_data.state_forwards("fill_data", new_state)
  5547. fill_data.database_forwards("fill_data", editor, project_state, new_state)
  5548. def _test_autofield_foreignfield_growth(
  5549. self, source_field, target_field, target_value
  5550. ):
  5551. """
  5552. A field may be migrated in the following ways:
  5553. - AutoField to BigAutoField
  5554. - SmallAutoField to AutoField
  5555. - SmallAutoField to BigAutoField
  5556. """
  5557. def create_initial_data(models, schema_editor):
  5558. Article = models.get_model("test_article", "Article")
  5559. Blog = models.get_model("test_blog", "Blog")
  5560. blog = Blog.objects.create(name="web development done right")
  5561. Article.objects.create(name="Frameworks", blog=blog)
  5562. Article.objects.create(name="Programming Languages", blog=blog)
  5563. def create_big_data(models, schema_editor):
  5564. Article = models.get_model("test_article", "Article")
  5565. Blog = models.get_model("test_blog", "Blog")
  5566. blog2 = Blog.objects.create(name="Frameworks", id=target_value)
  5567. Article.objects.create(name="Django", blog=blog2)
  5568. Article.objects.create(id=target_value, name="Django2", blog=blog2)
  5569. create_blog = migrations.CreateModel(
  5570. "Blog",
  5571. [
  5572. ("id", source_field(primary_key=True)),
  5573. ("name", models.CharField(max_length=100)),
  5574. ],
  5575. options={},
  5576. )
  5577. create_article = migrations.CreateModel(
  5578. "Article",
  5579. [
  5580. ("id", source_field(primary_key=True)),
  5581. (
  5582. "blog",
  5583. models.ForeignKey(to="test_blog.Blog", on_delete=models.CASCADE),
  5584. ),
  5585. ("name", models.CharField(max_length=100)),
  5586. ("data", models.TextField(default="")),
  5587. ],
  5588. options={},
  5589. )
  5590. fill_initial_data = migrations.RunPython(
  5591. create_initial_data, create_initial_data
  5592. )
  5593. fill_big_data = migrations.RunPython(create_big_data, create_big_data)
  5594. grow_article_id = migrations.AlterField(
  5595. "Article", "id", target_field(primary_key=True)
  5596. )
  5597. grow_blog_id = migrations.AlterField(
  5598. "Blog", "id", target_field(primary_key=True)
  5599. )
  5600. project_state = ProjectState()
  5601. new_state = project_state.clone()
  5602. with connection.schema_editor() as editor:
  5603. create_blog.state_forwards("test_blog", new_state)
  5604. create_blog.database_forwards("test_blog", editor, project_state, new_state)
  5605. project_state = new_state
  5606. new_state = new_state.clone()
  5607. with connection.schema_editor() as editor:
  5608. create_article.state_forwards("test_article", new_state)
  5609. create_article.database_forwards(
  5610. "test_article", editor, project_state, new_state
  5611. )
  5612. project_state = new_state
  5613. new_state = new_state.clone()
  5614. with connection.schema_editor() as editor:
  5615. fill_initial_data.state_forwards("fill_initial_data", new_state)
  5616. fill_initial_data.database_forwards(
  5617. "fill_initial_data", editor, project_state, new_state
  5618. )
  5619. project_state = new_state
  5620. new_state = new_state.clone()
  5621. with connection.schema_editor() as editor:
  5622. grow_article_id.state_forwards("test_article", new_state)
  5623. grow_article_id.database_forwards(
  5624. "test_article", editor, project_state, new_state
  5625. )
  5626. state = new_state.clone()
  5627. article = state.apps.get_model("test_article.Article")
  5628. self.assertIsInstance(article._meta.pk, target_field)
  5629. project_state = new_state
  5630. new_state = new_state.clone()
  5631. with connection.schema_editor() as editor:
  5632. grow_blog_id.state_forwards("test_blog", new_state)
  5633. grow_blog_id.database_forwards(
  5634. "test_blog", editor, project_state, new_state
  5635. )
  5636. state = new_state.clone()
  5637. blog = state.apps.get_model("test_blog.Blog")
  5638. self.assertIsInstance(blog._meta.pk, target_field)
  5639. project_state = new_state
  5640. new_state = new_state.clone()
  5641. with connection.schema_editor() as editor:
  5642. fill_big_data.state_forwards("fill_big_data", new_state)
  5643. fill_big_data.database_forwards(
  5644. "fill_big_data", editor, project_state, new_state
  5645. )
  5646. def test_autofield__bigautofield_foreignfield_growth(self):
  5647. """A field may be migrated from AutoField to BigAutoField."""
  5648. self._test_autofield_foreignfield_growth(
  5649. models.AutoField,
  5650. models.BigAutoField,
  5651. 2**33,
  5652. )
  5653. def test_smallfield_autofield_foreignfield_growth(self):
  5654. """A field may be migrated from SmallAutoField to AutoField."""
  5655. self._test_autofield_foreignfield_growth(
  5656. models.SmallAutoField,
  5657. models.AutoField,
  5658. 2**22,
  5659. )
  5660. def test_smallfield_bigautofield_foreignfield_growth(self):
  5661. """A field may be migrated from SmallAutoField to BigAutoField."""
  5662. self._test_autofield_foreignfield_growth(
  5663. models.SmallAutoField,
  5664. models.BigAutoField,
  5665. 2**33,
  5666. )
  5667. def test_run_python_noop(self):
  5668. """
  5669. #24098 - Tests no-op RunPython operations.
  5670. """
  5671. project_state = ProjectState()
  5672. new_state = project_state.clone()
  5673. operation = migrations.RunPython(
  5674. migrations.RunPython.noop, migrations.RunPython.noop
  5675. )
  5676. with connection.schema_editor() as editor:
  5677. operation.database_forwards(
  5678. "test_runpython", editor, project_state, new_state
  5679. )
  5680. operation.database_backwards(
  5681. "test_runpython", editor, new_state, project_state
  5682. )
  5683. def test_separate_database_and_state(self):
  5684. """
  5685. Tests the SeparateDatabaseAndState operation.
  5686. """
  5687. project_state = self.set_up_test_model("test_separatedatabaseandstate")
  5688. # Create the operation
  5689. database_operation = migrations.RunSQL(
  5690. "CREATE TABLE i_love_ponies (id int, special_thing int);",
  5691. "DROP TABLE i_love_ponies;",
  5692. )
  5693. state_operation = migrations.CreateModel(
  5694. "SomethingElse", [("id", models.AutoField(primary_key=True))]
  5695. )
  5696. operation = migrations.SeparateDatabaseAndState(
  5697. state_operations=[state_operation], database_operations=[database_operation]
  5698. )
  5699. self.assertEqual(
  5700. operation.describe(), "Custom state/database change combination"
  5701. )
  5702. self.assertEqual(
  5703. operation.formatted_description(),
  5704. "? Custom state/database change combination",
  5705. )
  5706. # Test the state alteration
  5707. new_state = project_state.clone()
  5708. operation.state_forwards("test_separatedatabaseandstate", new_state)
  5709. self.assertEqual(
  5710. len(
  5711. new_state.models[
  5712. "test_separatedatabaseandstate", "somethingelse"
  5713. ].fields
  5714. ),
  5715. 1,
  5716. )
  5717. # Make sure there's no table
  5718. self.assertTableNotExists("i_love_ponies")
  5719. # Test the database alteration
  5720. with connection.schema_editor() as editor:
  5721. operation.database_forwards(
  5722. "test_separatedatabaseandstate", editor, project_state, new_state
  5723. )
  5724. self.assertTableExists("i_love_ponies")
  5725. # And test reversal
  5726. self.assertTrue(operation.reversible)
  5727. with connection.schema_editor() as editor:
  5728. operation.database_backwards(
  5729. "test_separatedatabaseandstate", editor, new_state, project_state
  5730. )
  5731. self.assertTableNotExists("i_love_ponies")
  5732. # And deconstruction
  5733. definition = operation.deconstruct()
  5734. self.assertEqual(definition[0], "SeparateDatabaseAndState")
  5735. self.assertEqual(definition[1], [])
  5736. self.assertEqual(
  5737. sorted(definition[2]), ["database_operations", "state_operations"]
  5738. )
  5739. def test_separate_database_and_state2(self):
  5740. """
  5741. A complex SeparateDatabaseAndState operation: Multiple operations both
  5742. for state and database. Verify the state dependencies within each list
  5743. and that state ops don't affect the database.
  5744. """
  5745. app_label = "test_separatedatabaseandstate2"
  5746. project_state = self.set_up_test_model(app_label)
  5747. # Create the operation
  5748. database_operations = [
  5749. migrations.CreateModel(
  5750. "ILovePonies",
  5751. [("id", models.AutoField(primary_key=True))],
  5752. options={"db_table": "iloveponies"},
  5753. ),
  5754. migrations.CreateModel(
  5755. "ILoveMorePonies",
  5756. # We use IntegerField and not AutoField because
  5757. # the model is going to be deleted immediately
  5758. # and with an AutoField this fails on Oracle
  5759. [("id", models.IntegerField(primary_key=True))],
  5760. options={"db_table": "ilovemoreponies"},
  5761. ),
  5762. migrations.DeleteModel("ILoveMorePonies"),
  5763. migrations.CreateModel(
  5764. "ILoveEvenMorePonies",
  5765. [("id", models.AutoField(primary_key=True))],
  5766. options={"db_table": "iloveevenmoreponies"},
  5767. ),
  5768. ]
  5769. state_operations = [
  5770. migrations.CreateModel(
  5771. "SomethingElse",
  5772. [("id", models.AutoField(primary_key=True))],
  5773. options={"db_table": "somethingelse"},
  5774. ),
  5775. migrations.DeleteModel("SomethingElse"),
  5776. migrations.CreateModel(
  5777. "SomethingCompletelyDifferent",
  5778. [("id", models.AutoField(primary_key=True))],
  5779. options={"db_table": "somethingcompletelydifferent"},
  5780. ),
  5781. ]
  5782. operation = migrations.SeparateDatabaseAndState(
  5783. state_operations=state_operations,
  5784. database_operations=database_operations,
  5785. )
  5786. # Test the state alteration
  5787. new_state = project_state.clone()
  5788. operation.state_forwards(app_label, new_state)
  5789. def assertModelsAndTables(after_db):
  5790. # Tables and models exist, or don't, as they should:
  5791. self.assertNotIn((app_label, "somethingelse"), new_state.models)
  5792. self.assertEqual(
  5793. len(new_state.models[app_label, "somethingcompletelydifferent"].fields),
  5794. 1,
  5795. )
  5796. self.assertNotIn((app_label, "iloveponiesonies"), new_state.models)
  5797. self.assertNotIn((app_label, "ilovemoreponies"), new_state.models)
  5798. self.assertNotIn((app_label, "iloveevenmoreponies"), new_state.models)
  5799. self.assertTableNotExists("somethingelse")
  5800. self.assertTableNotExists("somethingcompletelydifferent")
  5801. self.assertTableNotExists("ilovemoreponies")
  5802. if after_db:
  5803. self.assertTableExists("iloveponies")
  5804. self.assertTableExists("iloveevenmoreponies")
  5805. else:
  5806. self.assertTableNotExists("iloveponies")
  5807. self.assertTableNotExists("iloveevenmoreponies")
  5808. assertModelsAndTables(after_db=False)
  5809. # Test the database alteration
  5810. with connection.schema_editor() as editor:
  5811. operation.database_forwards(app_label, editor, project_state, new_state)
  5812. assertModelsAndTables(after_db=True)
  5813. # And test reversal
  5814. self.assertTrue(operation.reversible)
  5815. with connection.schema_editor() as editor:
  5816. operation.database_backwards(app_label, editor, new_state, project_state)
  5817. assertModelsAndTables(after_db=False)
  5818. def _test_invalid_generated_field_changes(self, db_persist):
  5819. regular = models.IntegerField(default=1)
  5820. generated_1 = models.GeneratedField(
  5821. expression=F("pink") + F("pink"),
  5822. output_field=models.IntegerField(),
  5823. db_persist=db_persist,
  5824. )
  5825. generated_2 = models.GeneratedField(
  5826. expression=F("pink") + F("pink") + F("pink"),
  5827. output_field=models.IntegerField(),
  5828. db_persist=db_persist,
  5829. )
  5830. tests = [
  5831. ("test_igfc_1", regular, generated_1),
  5832. ("test_igfc_2", generated_1, regular),
  5833. ("test_igfc_3", generated_1, generated_2),
  5834. ]
  5835. for app_label, add_field, alter_field in tests:
  5836. project_state = self.set_up_test_model(app_label)
  5837. operations = [
  5838. migrations.AddField("Pony", "modified_pink", add_field),
  5839. migrations.AlterField("Pony", "modified_pink", alter_field),
  5840. ]
  5841. msg = (
  5842. "Modifying GeneratedFields is not supported - the field "
  5843. f"{app_label}.Pony.modified_pink must be removed and re-added with the "
  5844. "new definition."
  5845. )
  5846. with self.assertRaisesMessage(ValueError, msg):
  5847. self.apply_operations(app_label, project_state, operations)
  5848. @skipUnlessDBFeature("supports_stored_generated_columns")
  5849. def test_invalid_generated_field_changes_stored(self):
  5850. self._test_invalid_generated_field_changes(db_persist=True)
  5851. @skipUnlessDBFeature("supports_virtual_generated_columns")
  5852. def test_invalid_generated_field_changes_virtual(self):
  5853. self._test_invalid_generated_field_changes(db_persist=False)
  5854. def _test_invalid_generated_field_changes_on_rename(self, db_persist):
  5855. app_label = "test_igfcor"
  5856. operation = migrations.AddField(
  5857. "Pony",
  5858. "modified_pink",
  5859. models.GeneratedField(
  5860. expression=F("pink") + F("pink"),
  5861. output_field=models.IntegerField(),
  5862. db_persist=db_persist,
  5863. ),
  5864. )
  5865. project_state, new_state = self.make_test_state(app_label, operation)
  5866. # Add generated column.
  5867. with connection.schema_editor() as editor:
  5868. operation.database_forwards(app_label, editor, project_state, new_state)
  5869. # Rename field used in the generated field.
  5870. operations = [
  5871. migrations.RenameField("Pony", "pink", "renamed_pink"),
  5872. migrations.AlterField(
  5873. "Pony",
  5874. "modified_pink",
  5875. models.GeneratedField(
  5876. expression=F("renamed_pink"),
  5877. output_field=models.IntegerField(),
  5878. db_persist=db_persist,
  5879. ),
  5880. ),
  5881. ]
  5882. msg = (
  5883. "Modifying GeneratedFields is not supported - the field "
  5884. f"{app_label}.Pony.modified_pink must be removed and re-added with the "
  5885. "new definition."
  5886. )
  5887. with self.assertRaisesMessage(ValueError, msg):
  5888. self.apply_operations(app_label, new_state, operations)
  5889. @skipUnlessDBFeature("supports_stored_generated_columns")
  5890. def test_invalid_generated_field_changes_on_rename_stored(self):
  5891. self._test_invalid_generated_field_changes_on_rename(db_persist=True)
  5892. @skipUnlessDBFeature("supports_virtual_generated_columns")
  5893. def test_invalid_generated_field_changes_on_rename_virtual(self):
  5894. self._test_invalid_generated_field_changes_on_rename(db_persist=False)
  5895. @skipUnlessDBFeature(
  5896. "supports_stored_generated_columns",
  5897. "supports_virtual_generated_columns",
  5898. )
  5899. def test_invalid_generated_field_persistency_change(self):
  5900. app_label = "test_igfpc"
  5901. project_state = self.set_up_test_model(app_label)
  5902. operations = [
  5903. migrations.AddField(
  5904. "Pony",
  5905. "modified_pink",
  5906. models.GeneratedField(
  5907. expression=F("pink"),
  5908. output_field=models.IntegerField(),
  5909. db_persist=True,
  5910. ),
  5911. ),
  5912. migrations.AlterField(
  5913. "Pony",
  5914. "modified_pink",
  5915. models.GeneratedField(
  5916. expression=F("pink"),
  5917. output_field=models.IntegerField(),
  5918. db_persist=False,
  5919. ),
  5920. ),
  5921. ]
  5922. msg = (
  5923. "Modifying GeneratedFields is not supported - the field "
  5924. f"{app_label}.Pony.modified_pink must be removed and re-added with the "
  5925. "new definition."
  5926. )
  5927. with self.assertRaisesMessage(ValueError, msg):
  5928. self.apply_operations(app_label, project_state, operations)
  5929. def _test_add_generated_field(self, db_persist):
  5930. app_label = "test_agf"
  5931. operation = migrations.AddField(
  5932. "Pony",
  5933. "modified_pink",
  5934. models.GeneratedField(
  5935. expression=F("pink") + F("pink"),
  5936. output_field=models.IntegerField(),
  5937. db_persist=db_persist,
  5938. ),
  5939. )
  5940. project_state, new_state = self.make_test_state(app_label, operation)
  5941. self.assertEqual(len(new_state.models[app_label, "pony"].fields), 6)
  5942. # Add generated column.
  5943. with connection.schema_editor() as editor:
  5944. operation.database_forwards(app_label, editor, project_state, new_state)
  5945. self.assertColumnExists(f"{app_label}_pony", "modified_pink")
  5946. Pony = new_state.apps.get_model(app_label, "Pony")
  5947. obj = Pony.objects.create(pink=5, weight=3.23)
  5948. self.assertEqual(obj.modified_pink, 10)
  5949. # Reversal.
  5950. with connection.schema_editor() as editor:
  5951. operation.database_backwards(app_label, editor, new_state, project_state)
  5952. self.assertColumnNotExists(f"{app_label}_pony", "modified_pink")
  5953. @skipUnlessDBFeature("supports_stored_generated_columns")
  5954. def test_add_generated_field_stored(self):
  5955. self._test_add_generated_field(db_persist=True)
  5956. @skipUnlessDBFeature("supports_virtual_generated_columns")
  5957. def test_add_generated_field_virtual(self):
  5958. self._test_add_generated_field(db_persist=False)
  5959. def _test_remove_generated_field(self, db_persist):
  5960. app_label = "test_rgf"
  5961. operation = migrations.AddField(
  5962. "Pony",
  5963. "modified_pink",
  5964. models.GeneratedField(
  5965. expression=F("pink") + F("pink"),
  5966. output_field=models.IntegerField(),
  5967. db_persist=db_persist,
  5968. ),
  5969. )
  5970. project_state, new_state = self.make_test_state(app_label, operation)
  5971. self.assertEqual(len(new_state.models[app_label, "pony"].fields), 6)
  5972. # Add generated column.
  5973. with connection.schema_editor() as editor:
  5974. operation.database_forwards(app_label, editor, project_state, new_state)
  5975. project_state = new_state
  5976. new_state = project_state.clone()
  5977. operation = migrations.RemoveField("Pony", "modified_pink")
  5978. operation.state_forwards(app_label, new_state)
  5979. # Remove generated column.
  5980. with connection.schema_editor() as editor:
  5981. operation.database_forwards(app_label, editor, project_state, new_state)
  5982. self.assertColumnNotExists(f"{app_label}_pony", "modified_pink")
  5983. @skipUnlessDBFeature("supports_stored_generated_columns")
  5984. def test_remove_generated_field_stored(self):
  5985. self._test_remove_generated_field(db_persist=True)
  5986. @skipUnlessDBFeature("supports_virtual_generated_columns")
  5987. def test_remove_generated_field_virtual(self):
  5988. self._test_remove_generated_field(db_persist=False)
  5989. @skipUnlessDBFeature("supports_stored_generated_columns")
  5990. def test_add_field_after_generated_field(self):
  5991. app_label = "test_adfagf"
  5992. project_state = self.set_up_test_model(app_label)
  5993. operation_1 = migrations.AddField(
  5994. "Pony",
  5995. "generated",
  5996. models.GeneratedField(
  5997. expression=Value(1),
  5998. output_field=models.IntegerField(),
  5999. db_persist=True,
  6000. ),
  6001. )
  6002. operation_2 = migrations.AddField(
  6003. "Pony",
  6004. "static",
  6005. models.IntegerField(default=2),
  6006. )
  6007. new_state = project_state.clone()
  6008. operation_1.state_forwards(app_label, new_state)
  6009. with connection.schema_editor() as editor:
  6010. operation_1.database_forwards(app_label, editor, project_state, new_state)
  6011. project_state, new_state = new_state, new_state.clone()
  6012. pony_old = new_state.apps.get_model(app_label, "Pony").objects.create(weight=20)
  6013. self.assertEqual(pony_old.generated, 1)
  6014. operation_2.state_forwards(app_label, new_state)
  6015. with connection.schema_editor() as editor:
  6016. operation_2.database_forwards(app_label, editor, project_state, new_state)
  6017. Pony = new_state.apps.get_model(app_label, "Pony")
  6018. pony_old = Pony.objects.get(pk=pony_old.pk)
  6019. self.assertEqual(pony_old.generated, 1)
  6020. self.assertEqual(pony_old.static, 2)
  6021. pony_new = Pony.objects.create(weight=20)
  6022. self.assertEqual(pony_new.generated, 1)
  6023. self.assertEqual(pony_new.static, 2)
  6024. def test_composite_pk_operations(self):
  6025. app_label = "test_d8d90af6"
  6026. project_state = self.set_up_test_model(app_label)
  6027. operation_1 = migrations.AddField(
  6028. "Pony", "pk", models.CompositePrimaryKey("id", "pink")
  6029. )
  6030. operation_2 = migrations.AlterField("Pony", "id", models.IntegerField())
  6031. operation_3 = migrations.RemoveField("Pony", "pk")
  6032. table_name = f"{app_label}_pony"
  6033. # 1. Add field (pk).
  6034. new_state = project_state.clone()
  6035. operation_1.state_forwards(app_label, new_state)
  6036. with connection.schema_editor() as editor:
  6037. operation_1.database_forwards(app_label, editor, project_state, new_state)
  6038. self.assertColumnNotExists(table_name, "pk")
  6039. Pony = new_state.apps.get_model(app_label, "pony")
  6040. obj_1 = Pony.objects.create(weight=1)
  6041. msg = (
  6042. f"obj_1={obj_1}, "
  6043. f"obj_1.id={obj_1.id}, "
  6044. f"obj_1.pink={obj_1.pink}, "
  6045. f"obj_1.pk={obj_1.pk}, "
  6046. f"Pony._meta.pk={repr(Pony._meta.pk)}, "
  6047. f"Pony._meta.get_field('id')={repr(Pony._meta.get_field('id'))}"
  6048. )
  6049. self.assertEqual(obj_1.pink, 3, msg)
  6050. self.assertEqual(obj_1.pk, (obj_1.id, obj_1.pink), msg)
  6051. # 2. Alter field (id -> IntegerField()).
  6052. project_state, new_state = new_state, new_state.clone()
  6053. operation_2.state_forwards(app_label, new_state)
  6054. with connection.schema_editor() as editor:
  6055. operation_2.database_forwards(app_label, editor, project_state, new_state)
  6056. Pony = new_state.apps.get_model(app_label, "pony")
  6057. obj_1 = Pony.objects.get(id=obj_1.id)
  6058. self.assertEqual(obj_1.pink, 3)
  6059. self.assertEqual(obj_1.pk, (obj_1.id, obj_1.pink))
  6060. obj_2 = Pony.objects.create(id=2, weight=2)
  6061. self.assertEqual(obj_2.id, 2)
  6062. self.assertEqual(obj_2.pink, 3)
  6063. self.assertEqual(obj_2.pk, (obj_2.id, obj_2.pink))
  6064. # 3. Remove field (pk).
  6065. project_state, new_state = new_state, new_state.clone()
  6066. operation_3.state_forwards(app_label, new_state)
  6067. with connection.schema_editor() as editor:
  6068. operation_3.database_forwards(app_label, editor, project_state, new_state)
  6069. Pony = new_state.apps.get_model(app_label, "pony")
  6070. obj_1 = Pony.objects.get(id=obj_1.id)
  6071. self.assertEqual(obj_1.pk, obj_1.id)
  6072. obj_2 = Pony.objects.get(id=obj_2.id)
  6073. self.assertEqual(obj_2.id, 2)
  6074. self.assertEqual(obj_2.pk, obj_2.id)
  6075. class SwappableOperationTests(OperationTestBase):
  6076. """
  6077. Key operations ignore swappable models
  6078. (we don't want to replicate all of them here, as the functionality
  6079. is in a common base class anyway)
  6080. """
  6081. available_apps = ["migrations"]
  6082. @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
  6083. def test_create_ignore_swapped(self):
  6084. """
  6085. The CreateTable operation ignores swapped models.
  6086. """
  6087. operation = migrations.CreateModel(
  6088. "Pony",
  6089. [
  6090. ("id", models.AutoField(primary_key=True)),
  6091. ("pink", models.IntegerField(default=1)),
  6092. ],
  6093. options={
  6094. "swappable": "TEST_SWAP_MODEL",
  6095. },
  6096. )
  6097. # Test the state alteration (it should still be there!)
  6098. project_state = ProjectState()
  6099. new_state = project_state.clone()
  6100. operation.state_forwards("test_crigsw", new_state)
  6101. self.assertEqual(new_state.models["test_crigsw", "pony"].name, "Pony")
  6102. self.assertEqual(len(new_state.models["test_crigsw", "pony"].fields), 2)
  6103. # Test the database alteration
  6104. self.assertTableNotExists("test_crigsw_pony")
  6105. with connection.schema_editor() as editor:
  6106. operation.database_forwards("test_crigsw", editor, project_state, new_state)
  6107. self.assertTableNotExists("test_crigsw_pony")
  6108. # And test reversal
  6109. with connection.schema_editor() as editor:
  6110. operation.database_backwards(
  6111. "test_crigsw", editor, new_state, project_state
  6112. )
  6113. self.assertTableNotExists("test_crigsw_pony")
  6114. @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
  6115. def test_delete_ignore_swapped(self):
  6116. """
  6117. Tests the DeleteModel operation ignores swapped models.
  6118. """
  6119. operation = migrations.DeleteModel("Pony")
  6120. project_state, new_state = self.make_test_state("test_dligsw", operation)
  6121. # Test the database alteration
  6122. self.assertTableNotExists("test_dligsw_pony")
  6123. with connection.schema_editor() as editor:
  6124. operation.database_forwards("test_dligsw", editor, project_state, new_state)
  6125. self.assertTableNotExists("test_dligsw_pony")
  6126. # And test reversal
  6127. with connection.schema_editor() as editor:
  6128. operation.database_backwards(
  6129. "test_dligsw", editor, new_state, project_state
  6130. )
  6131. self.assertTableNotExists("test_dligsw_pony")
  6132. @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
  6133. def test_add_field_ignore_swapped(self):
  6134. """
  6135. Tests the AddField operation.
  6136. """
  6137. # Test the state alteration
  6138. operation = migrations.AddField(
  6139. "Pony",
  6140. "height",
  6141. models.FloatField(null=True, default=5),
  6142. )
  6143. project_state, new_state = self.make_test_state("test_adfligsw", operation)
  6144. # Test the database alteration
  6145. self.assertTableNotExists("test_adfligsw_pony")
  6146. with connection.schema_editor() as editor:
  6147. operation.database_forwards(
  6148. "test_adfligsw", editor, project_state, new_state
  6149. )
  6150. self.assertTableNotExists("test_adfligsw_pony")
  6151. # And test reversal
  6152. with connection.schema_editor() as editor:
  6153. operation.database_backwards(
  6154. "test_adfligsw", editor, new_state, project_state
  6155. )
  6156. self.assertTableNotExists("test_adfligsw_pony")
  6157. @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
  6158. def test_indexes_ignore_swapped(self):
  6159. """
  6160. Add/RemoveIndex operations ignore swapped models.
  6161. """
  6162. operation = migrations.AddIndex(
  6163. "Pony", models.Index(fields=["pink"], name="my_name_idx")
  6164. )
  6165. project_state, new_state = self.make_test_state("test_adinigsw", operation)
  6166. with connection.schema_editor() as editor:
  6167. # No database queries should be run for swapped models
  6168. operation.database_forwards(
  6169. "test_adinigsw", editor, project_state, new_state
  6170. )
  6171. operation.database_backwards(
  6172. "test_adinigsw", editor, new_state, project_state
  6173. )
  6174. operation = migrations.RemoveIndex(
  6175. "Pony", models.Index(fields=["pink"], name="my_name_idx")
  6176. )
  6177. project_state, new_state = self.make_test_state("test_rminigsw", operation)
  6178. with connection.schema_editor() as editor:
  6179. operation.database_forwards(
  6180. "test_rminigsw", editor, project_state, new_state
  6181. )
  6182. operation.database_backwards(
  6183. "test_rminigsw", editor, new_state, project_state
  6184. )
  6185. class TestCreateModel(SimpleTestCase):
  6186. def test_references_model_mixin(self):
  6187. migrations.CreateModel(
  6188. "name",
  6189. fields=[],
  6190. bases=(Mixin, models.Model),
  6191. ).references_model("other_model", "migrations")
  6192. class FieldOperationTests(SimpleTestCase):
  6193. def test_references_model(self):
  6194. operation = FieldOperation(
  6195. "MoDel", "field", models.ForeignKey("Other", models.CASCADE)
  6196. )
  6197. # Model name match.
  6198. self.assertIs(operation.references_model("mOdEl", "migrations"), True)
  6199. # Referenced field.
  6200. self.assertIs(operation.references_model("oTher", "migrations"), True)
  6201. # Doesn't reference.
  6202. self.assertIs(operation.references_model("Whatever", "migrations"), False)
  6203. def test_references_field_by_name(self):
  6204. operation = FieldOperation("MoDel", "field", models.BooleanField(default=False))
  6205. self.assertIs(operation.references_field("model", "field", "migrations"), True)
  6206. def test_references_field_by_remote_field_model(self):
  6207. operation = FieldOperation(
  6208. "Model", "field", models.ForeignKey("Other", models.CASCADE)
  6209. )
  6210. self.assertIs(
  6211. operation.references_field("Other", "whatever", "migrations"), True
  6212. )
  6213. self.assertIs(
  6214. operation.references_field("Missing", "whatever", "migrations"), False
  6215. )
  6216. def test_references_field_by_from_fields(self):
  6217. operation = FieldOperation(
  6218. "Model",
  6219. "field",
  6220. models.fields.related.ForeignObject(
  6221. "Other", models.CASCADE, ["from"], ["to"]
  6222. ),
  6223. )
  6224. self.assertIs(operation.references_field("Model", "from", "migrations"), True)
  6225. self.assertIs(operation.references_field("Model", "to", "migrations"), False)
  6226. self.assertIs(operation.references_field("Other", "from", "migrations"), False)
  6227. self.assertIs(operation.references_field("Model", "to", "migrations"), False)
  6228. def test_references_field_by_to_fields(self):
  6229. operation = FieldOperation(
  6230. "Model",
  6231. "field",
  6232. models.ForeignKey("Other", models.CASCADE, to_field="field"),
  6233. )
  6234. self.assertIs(operation.references_field("Other", "field", "migrations"), True)
  6235. self.assertIs(
  6236. operation.references_field("Other", "whatever", "migrations"), False
  6237. )
  6238. self.assertIs(
  6239. operation.references_field("Missing", "whatever", "migrations"), False
  6240. )
  6241. def test_references_field_by_through(self):
  6242. operation = FieldOperation(
  6243. "Model", "field", models.ManyToManyField("Other", through="Through")
  6244. )
  6245. self.assertIs(
  6246. operation.references_field("Other", "whatever", "migrations"), True
  6247. )
  6248. self.assertIs(
  6249. operation.references_field("Through", "whatever", "migrations"), True
  6250. )
  6251. self.assertIs(
  6252. operation.references_field("Missing", "whatever", "migrations"), False
  6253. )
  6254. def test_reference_field_by_through_fields(self):
  6255. operation = FieldOperation(
  6256. "Model",
  6257. "field",
  6258. models.ManyToManyField(
  6259. "Other", through="Through", through_fields=("first", "second")
  6260. ),
  6261. )
  6262. self.assertIs(
  6263. operation.references_field("Other", "whatever", "migrations"), True
  6264. )
  6265. self.assertIs(
  6266. operation.references_field("Through", "whatever", "migrations"), False
  6267. )
  6268. self.assertIs(
  6269. operation.references_field("Through", "first", "migrations"), True
  6270. )
  6271. self.assertIs(
  6272. operation.references_field("Through", "second", "migrations"), True
  6273. )
  6274. class BaseOperationTests(SimpleTestCase):
  6275. def test_formatted_description_no_category(self):
  6276. operation = Operation()
  6277. self.assertEqual(operation.formatted_description(), "? Operation: ((), {})")