123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163 |
- import functools
- import re
- from unittest import mock
- from django.apps import apps
- from django.conf import settings
- from django.contrib.auth.models import AbstractBaseUser
- from django.core.validators import RegexValidator, validate_slug
- from django.db import connection, models
- from django.db.migrations.autodetector import MigrationAutodetector
- from django.db.migrations.graph import MigrationGraph
- from django.db.migrations.loader import MigrationLoader
- from django.db.migrations.questioner import MigrationQuestioner
- from django.db.migrations.state import ModelState, ProjectState
- from django.test import TestCase, override_settings
- from django.test.utils import isolate_lru_cache
- from .models import FoodManager, FoodQuerySet
- class DeconstructibleObject:
- """
- A custom deconstructible object.
- """
- def __init__(self, *args, **kwargs):
- self.args = args
- self.kwargs = kwargs
- def deconstruct(self):
- return (
- self.__module__ + '.' + self.__class__.__name__,
- self.args,
- self.kwargs
- )
- class AutodetectorTests(TestCase):
- """
- Tests the migration autodetector.
- """
- author_empty = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True))])
- author_name = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200)),
- ])
- author_name_null = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, null=True)),
- ])
- author_name_longer = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=400)),
- ])
- author_name_renamed = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("names", models.CharField(max_length=200)),
- ])
- author_name_default = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default='Ada Lovelace')),
- ])
- author_dates_of_birth_auto_now = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("date_of_birth", models.DateField(auto_now=True)),
- ("date_time_of_birth", models.DateTimeField(auto_now=True)),
- ("time_of_birth", models.TimeField(auto_now=True)),
- ])
- author_dates_of_birth_auto_now_add = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("date_of_birth", models.DateField(auto_now_add=True)),
- ("date_time_of_birth", models.DateTimeField(auto_now_add=True)),
- ("time_of_birth", models.TimeField(auto_now_add=True)),
- ])
- author_name_deconstructible_1 = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default=DeconstructibleObject())),
- ])
- author_name_deconstructible_2 = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default=DeconstructibleObject())),
- ])
- author_name_deconstructible_3 = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default=models.IntegerField())),
- ])
- author_name_deconstructible_4 = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default=models.IntegerField())),
- ])
- author_name_deconstructible_list_1 = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default=[DeconstructibleObject(), 123])),
- ])
- author_name_deconstructible_list_2 = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default=[DeconstructibleObject(), 123])),
- ])
- author_name_deconstructible_list_3 = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default=[DeconstructibleObject(), 999])),
- ])
- author_name_deconstructible_tuple_1 = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default=(DeconstructibleObject(), 123))),
- ])
- author_name_deconstructible_tuple_2 = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default=(DeconstructibleObject(), 123))),
- ])
- author_name_deconstructible_tuple_3 = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default=(DeconstructibleObject(), 999))),
- ])
- author_name_deconstructible_dict_1 = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default={
- 'item': DeconstructibleObject(), 'otheritem': 123
- })),
- ])
- author_name_deconstructible_dict_2 = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default={
- 'item': DeconstructibleObject(), 'otheritem': 123
- })),
- ])
- author_name_deconstructible_dict_3 = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default={
- 'item': DeconstructibleObject(), 'otheritem': 999
- })),
- ])
- author_name_nested_deconstructible_1 = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default=DeconstructibleObject(
- DeconstructibleObject(1),
- (DeconstructibleObject('t1'), DeconstructibleObject('t2'),),
- a=DeconstructibleObject('A'),
- b=DeconstructibleObject(B=DeconstructibleObject('c')),
- ))),
- ])
- author_name_nested_deconstructible_2 = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default=DeconstructibleObject(
- DeconstructibleObject(1),
- (DeconstructibleObject('t1'), DeconstructibleObject('t2'),),
- a=DeconstructibleObject('A'),
- b=DeconstructibleObject(B=DeconstructibleObject('c')),
- ))),
- ])
- author_name_nested_deconstructible_changed_arg = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default=DeconstructibleObject(
- DeconstructibleObject(1),
- (DeconstructibleObject('t1'), DeconstructibleObject('t2-changed'),),
- a=DeconstructibleObject('A'),
- b=DeconstructibleObject(B=DeconstructibleObject('c')),
- ))),
- ])
- author_name_nested_deconstructible_extra_arg = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default=DeconstructibleObject(
- DeconstructibleObject(1),
- (DeconstructibleObject('t1'), DeconstructibleObject('t2'),),
- None,
- a=DeconstructibleObject('A'),
- b=DeconstructibleObject(B=DeconstructibleObject('c')),
- ))),
- ])
- author_name_nested_deconstructible_changed_kwarg = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default=DeconstructibleObject(
- DeconstructibleObject(1),
- (DeconstructibleObject('t1'), DeconstructibleObject('t2'),),
- a=DeconstructibleObject('A'),
- b=DeconstructibleObject(B=DeconstructibleObject('c-changed')),
- ))),
- ])
- author_name_nested_deconstructible_extra_kwarg = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200, default=DeconstructibleObject(
- DeconstructibleObject(1),
- (DeconstructibleObject('t1'), DeconstructibleObject('t2'),),
- a=DeconstructibleObject('A'),
- b=DeconstructibleObject(B=DeconstructibleObject('c')),
- c=None,
- ))),
- ])
- author_custom_pk = ModelState("testapp", "Author", [("pk_field", models.IntegerField(primary_key=True))])
- author_with_biography_non_blank = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField()),
- ("biography", models.TextField()),
- ])
- author_with_biography_blank = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(blank=True)),
- ("biography", models.TextField(blank=True)),
- ])
- author_with_book = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200)),
- ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
- ])
- author_with_book_order_wrt = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200)),
- ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
- ], options={"order_with_respect_to": "book"})
- author_renamed_with_book = ModelState("testapp", "Writer", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200)),
- ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
- ])
- author_with_publisher_string = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200)),
- ("publisher_name", models.CharField(max_length=200)),
- ])
- author_with_publisher = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200)),
- ("publisher", models.ForeignKey("testapp.Publisher", models.CASCADE)),
- ])
- author_with_user = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200)),
- ("user", models.ForeignKey("auth.User", models.CASCADE)),
- ])
- author_with_custom_user = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200)),
- ("user", models.ForeignKey("thirdapp.CustomUser", models.CASCADE)),
- ])
- author_proxy = ModelState("testapp", "AuthorProxy", [], {"proxy": True}, ("testapp.author",))
- author_proxy_options = ModelState("testapp", "AuthorProxy", [], {
- "proxy": True,
- "verbose_name": "Super Author",
- }, ("testapp.author", ))
- author_proxy_notproxy = ModelState("testapp", "AuthorProxy", [], {}, ("testapp.author", ))
- author_proxy_third = ModelState("thirdapp", "AuthorProxy", [], {"proxy": True}, ("testapp.author", ))
- author_proxy_third_notproxy = ModelState("thirdapp", "AuthorProxy", [], {}, ("testapp.author", ))
- author_proxy_proxy = ModelState("testapp", "AAuthorProxyProxy", [], {"proxy": True}, ("testapp.authorproxy", ))
- author_unmanaged = ModelState("testapp", "AuthorUnmanaged", [], {"managed": False}, ("testapp.author", ))
- author_unmanaged_managed = ModelState("testapp", "AuthorUnmanaged", [], {}, ("testapp.author", ))
- author_unmanaged_default_pk = ModelState("testapp", "Author", [("id", models.AutoField(primary_key=True))])
- author_unmanaged_custom_pk = ModelState("testapp", "Author", [
- ("pk_field", models.IntegerField(primary_key=True)),
- ])
- author_with_m2m = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("publishers", models.ManyToManyField("testapp.Publisher")),
- ])
- author_with_m2m_blank = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("publishers", models.ManyToManyField("testapp.Publisher", blank=True)),
- ])
- author_with_m2m_through = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("publishers", models.ManyToManyField("testapp.Publisher", through="testapp.Contract")),
- ])
- author_with_renamed_m2m_through = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("publishers", models.ManyToManyField("testapp.Publisher", through="testapp.Deal")),
- ])
- author_with_former_m2m = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("publishers", models.CharField(max_length=100)),
- ])
- author_with_options = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ], {
- "permissions": [('can_hire', 'Can hire')],
- "verbose_name": "Authi",
- })
- author_with_db_table_options = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ], {"db_table": "author_one"})
- author_with_new_db_table_options = ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ], {"db_table": "author_two"})
- author_renamed_with_db_table_options = ModelState("testapp", "NewAuthor", [
- ("id", models.AutoField(primary_key=True)),
- ], {"db_table": "author_one"})
- author_renamed_with_new_db_table_options = ModelState("testapp", "NewAuthor", [
- ("id", models.AutoField(primary_key=True)),
- ], {"db_table": "author_three"})
- contract = ModelState("testapp", "Contract", [
- ("id", models.AutoField(primary_key=True)),
- ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
- ("publisher", models.ForeignKey("testapp.Publisher", models.CASCADE)),
- ])
- contract_renamed = ModelState("testapp", "Deal", [
- ("id", models.AutoField(primary_key=True)),
- ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
- ("publisher", models.ForeignKey("testapp.Publisher", models.CASCADE)),
- ])
- publisher = ModelState("testapp", "Publisher", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=100)),
- ])
- publisher_with_author = ModelState("testapp", "Publisher", [
- ("id", models.AutoField(primary_key=True)),
- ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
- ("name", models.CharField(max_length=100)),
- ])
- publisher_with_aardvark_author = ModelState("testapp", "Publisher", [
- ("id", models.AutoField(primary_key=True)),
- ("author", models.ForeignKey("testapp.Aardvark", models.CASCADE)),
- ("name", models.CharField(max_length=100)),
- ])
- publisher_with_book = ModelState("testapp", "Publisher", [
- ("id", models.AutoField(primary_key=True)),
- ("author", models.ForeignKey("otherapp.Book", models.CASCADE)),
- ("name", models.CharField(max_length=100)),
- ])
- other_pony = ModelState("otherapp", "Pony", [
- ("id", models.AutoField(primary_key=True)),
- ])
- other_pony_food = ModelState("otherapp", "Pony", [
- ("id", models.AutoField(primary_key=True)),
- ], managers=[
- ('food_qs', FoodQuerySet.as_manager()),
- ('food_mgr', FoodManager('a', 'b')),
- ('food_mgr_kwargs', FoodManager('x', 'y', 3, 4)),
- ])
- other_stable = ModelState("otherapp", "Stable", [("id", models.AutoField(primary_key=True))])
- third_thing = ModelState("thirdapp", "Thing", [("id", models.AutoField(primary_key=True))])
- book = ModelState("otherapp", "Book", [
- ("id", models.AutoField(primary_key=True)),
- ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
- ("title", models.CharField(max_length=200)),
- ])
- book_proxy_fk = ModelState("otherapp", "Book", [
- ("id", models.AutoField(primary_key=True)),
- ("author", models.ForeignKey("thirdapp.AuthorProxy", models.CASCADE)),
- ("title", models.CharField(max_length=200)),
- ])
- book_proxy_proxy_fk = ModelState("otherapp", "Book", [
- ("id", models.AutoField(primary_key=True)),
- ("author", models.ForeignKey("testapp.AAuthorProxyProxy", models.CASCADE)),
- ])
- book_migrations_fk = ModelState("otherapp", "Book", [
- ("id", models.AutoField(primary_key=True)),
- ("author", models.ForeignKey("migrations.UnmigratedModel", models.CASCADE)),
- ("title", models.CharField(max_length=200)),
- ])
- book_with_no_author = ModelState("otherapp", "Book", [
- ("id", models.AutoField(primary_key=True)),
- ("title", models.CharField(max_length=200)),
- ])
- book_with_author_renamed = ModelState("otherapp", "Book", [
- ("id", models.AutoField(primary_key=True)),
- ("author", models.ForeignKey("testapp.Writer", models.CASCADE)),
- ("title", models.CharField(max_length=200)),
- ])
- book_with_field_and_author_renamed = ModelState("otherapp", "Book", [
- ("id", models.AutoField(primary_key=True)),
- ("writer", models.ForeignKey("testapp.Writer", models.CASCADE)),
- ("title", models.CharField(max_length=200)),
- ])
- book_with_multiple_authors = ModelState("otherapp", "Book", [
- ("id", models.AutoField(primary_key=True)),
- ("authors", models.ManyToManyField("testapp.Author")),
- ("title", models.CharField(max_length=200)),
- ])
- book_with_multiple_authors_through_attribution = ModelState("otherapp", "Book", [
- ("id", models.AutoField(primary_key=True)),
- ("authors", models.ManyToManyField("testapp.Author", through="otherapp.Attribution")),
- ("title", models.CharField(max_length=200)),
- ])
- book_indexes = ModelState("otherapp", "Book", [
- ("id", models.AutoField(primary_key=True)),
- ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
- ("title", models.CharField(max_length=200)),
- ], {
- "indexes": [models.Index(fields=["author", "title"], name="book_title_author_idx")],
- })
- book_unordered_indexes = ModelState("otherapp", "Book", [
- ("id", models.AutoField(primary_key=True)),
- ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
- ("title", models.CharField(max_length=200)),
- ], {
- "indexes": [models.Index(fields=["title", "author"], name="book_author_title_idx")],
- })
- book_foo_together = ModelState("otherapp", "Book", [
- ("id", models.AutoField(primary_key=True)),
- ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
- ("title", models.CharField(max_length=200)),
- ], {
- "index_together": {("author", "title")},
- "unique_together": {("author", "title")},
- })
- book_foo_together_2 = ModelState("otherapp", "Book", [
- ("id", models.AutoField(primary_key=True)),
- ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
- ("title", models.CharField(max_length=200)),
- ], {
- "index_together": {("title", "author")},
- "unique_together": {("title", "author")},
- })
- book_foo_together_3 = ModelState("otherapp", "Book", [
- ("id", models.AutoField(primary_key=True)),
- ("newfield", models.IntegerField()),
- ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
- ("title", models.CharField(max_length=200)),
- ], {
- "index_together": {("title", "newfield")},
- "unique_together": {("title", "newfield")},
- })
- book_foo_together_4 = ModelState("otherapp", "Book", [
- ("id", models.AutoField(primary_key=True)),
- ("newfield2", models.IntegerField()),
- ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
- ("title", models.CharField(max_length=200)),
- ], {
- "index_together": {("title", "newfield2")},
- "unique_together": {("title", "newfield2")},
- })
- attribution = ModelState("otherapp", "Attribution", [
- ("id", models.AutoField(primary_key=True)),
- ("author", models.ForeignKey("testapp.Author", models.CASCADE)),
- ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
- ])
- edition = ModelState("thirdapp", "Edition", [
- ("id", models.AutoField(primary_key=True)),
- ("book", models.ForeignKey("otherapp.Book", models.CASCADE)),
- ])
- custom_user = ModelState("thirdapp", "CustomUser", [
- ("id", models.AutoField(primary_key=True)),
- ("username", models.CharField(max_length=255)),
- ], bases=(AbstractBaseUser, ))
- custom_user_no_inherit = ModelState("thirdapp", "CustomUser", [
- ("id", models.AutoField(primary_key=True)),
- ("username", models.CharField(max_length=255)),
- ])
- aardvark = ModelState("thirdapp", "Aardvark", [("id", models.AutoField(primary_key=True))])
- aardvark_testapp = ModelState("testapp", "Aardvark", [("id", models.AutoField(primary_key=True))])
- aardvark_based_on_author = ModelState("testapp", "Aardvark", [], bases=("testapp.Author", ))
- aardvark_pk_fk_author = ModelState("testapp", "Aardvark", [
- ("id", models.OneToOneField("testapp.Author", models.CASCADE, primary_key=True)),
- ])
- knight = ModelState("eggs", "Knight", [("id", models.AutoField(primary_key=True))])
- rabbit = ModelState("eggs", "Rabbit", [
- ("id", models.AutoField(primary_key=True)),
- ("knight", models.ForeignKey("eggs.Knight", models.CASCADE)),
- ("parent", models.ForeignKey("eggs.Rabbit", models.CASCADE)),
- ], {
- "unique_together": {("parent", "knight")},
- "indexes": [models.Index(fields=["parent", "knight"], name='rabbit_circular_fk_index')],
- })
- def repr_changes(self, changes, include_dependencies=False):
- output = ""
- for app_label, migrations in sorted(changes.items()):
- output += " %s:\n" % app_label
- for migration in migrations:
- output += " %s\n" % migration.name
- for operation in migration.operations:
- output += " %s\n" % operation
- if include_dependencies:
- output += " Dependencies:\n"
- if migration.dependencies:
- for dep in migration.dependencies:
- output += " %s\n" % (dep,)
- else:
- output += " None\n"
- return output
- def assertNumberMigrations(self, changes, app_label, number):
- if len(changes.get(app_label, [])) != number:
- self.fail("Incorrect number of migrations (%s) for %s (expected %s)\n%s" % (
- len(changes.get(app_label, [])),
- app_label,
- number,
- self.repr_changes(changes),
- ))
- def assertMigrationDependencies(self, changes, app_label, position, dependencies):
- if not changes.get(app_label):
- self.fail("No migrations found for %s\n%s" % (app_label, self.repr_changes(changes)))
- if len(changes[app_label]) < position + 1:
- self.fail("No migration at index %s for %s\n%s" % (position, app_label, self.repr_changes(changes)))
- migration = changes[app_label][position]
- if set(migration.dependencies) != set(dependencies):
- self.fail("Migration dependencies mismatch for %s.%s (expected %s):\n%s" % (
- app_label,
- migration.name,
- dependencies,
- self.repr_changes(changes, include_dependencies=True),
- ))
- def assertOperationTypes(self, changes, app_label, position, types):
- if not changes.get(app_label):
- self.fail("No migrations found for %s\n%s" % (app_label, self.repr_changes(changes)))
- if len(changes[app_label]) < position + 1:
- self.fail("No migration at index %s for %s\n%s" % (position, app_label, self.repr_changes(changes)))
- migration = changes[app_label][position]
- real_types = [operation.__class__.__name__ for operation in migration.operations]
- if types != real_types:
- self.fail("Operation type mismatch for %s.%s (expected %s):\n%s" % (
- app_label,
- migration.name,
- types,
- self.repr_changes(changes),
- ))
- def assertOperationAttributes(self, changes, app_label, position, operation_position, **attrs):
- if not changes.get(app_label):
- self.fail("No migrations found for %s\n%s" % (app_label, self.repr_changes(changes)))
- if len(changes[app_label]) < position + 1:
- self.fail("No migration at index %s for %s\n%s" % (position, app_label, self.repr_changes(changes)))
- migration = changes[app_label][position]
- if len(changes[app_label]) < position + 1:
- self.fail("No operation at index %s for %s.%s\n%s" % (
- operation_position,
- app_label,
- migration.name,
- self.repr_changes(changes),
- ))
- operation = migration.operations[operation_position]
- for attr, value in attrs.items():
- if getattr(operation, attr, None) != value:
- self.fail("Attribute mismatch for %s.%s op #%s, %s (expected %r, got %r):\n%s" % (
- app_label,
- migration.name,
- operation_position,
- attr,
- value,
- getattr(operation, attr, None),
- self.repr_changes(changes),
- ))
- def assertOperationFieldAttributes(self, changes, app_label, position, operation_position, **attrs):
- if not changes.get(app_label):
- self.fail("No migrations found for %s\n%s" % (app_label, self.repr_changes(changes)))
- if len(changes[app_label]) < position + 1:
- self.fail("No migration at index %s for %s\n%s" % (position, app_label, self.repr_changes(changes)))
- migration = changes[app_label][position]
- if len(changes[app_label]) < position + 1:
- self.fail("No operation at index %s for %s.%s\n%s" % (
- operation_position,
- app_label,
- migration.name,
- self.repr_changes(changes),
- ))
- operation = migration.operations[operation_position]
- if not hasattr(operation, 'field'):
- self.fail("No field attribute for %s.%s op #%s." % (
- app_label,
- migration.name,
- operation_position,
- ))
- field = operation.field
- for attr, value in attrs.items():
- if getattr(field, attr, None) != value:
- self.fail("Field attribute mismatch for %s.%s op #%s, field.%s (expected %r, got %r):\n%s" % (
- app_label,
- migration.name,
- operation_position,
- attr,
- value,
- getattr(field, attr, None),
- self.repr_changes(changes),
- ))
- def make_project_state(self, model_states):
- "Shortcut to make ProjectStates from lists of predefined models"
- project_state = ProjectState()
- for model_state in model_states:
- project_state.add_model(model_state.clone())
- return project_state
- def get_changes(self, before_states, after_states, questioner=None):
- return MigrationAutodetector(
- self.make_project_state(before_states),
- self.make_project_state(after_states),
- questioner,
- )._detect_changes()
- def test_arrange_for_graph(self):
- """Tests auto-naming of migrations for graph matching."""
-
- graph = MigrationGraph()
- graph.add_node(("testapp", "0001_initial"), None)
- graph.add_node(("testapp", "0002_foobar"), None)
- graph.add_node(("otherapp", "0001_initial"), None)
- graph.add_dependency("testapp.0002_foobar", ("testapp", "0002_foobar"), ("testapp", "0001_initial"))
- graph.add_dependency("testapp.0002_foobar", ("testapp", "0002_foobar"), ("otherapp", "0001_initial"))
-
- before = self.make_project_state([])
- after = self.make_project_state([self.author_empty, self.other_pony, self.other_stable])
- autodetector = MigrationAutodetector(before, after)
- changes = autodetector._detect_changes()
-
- changes = autodetector.arrange_for_graph(changes, graph)
-
- self.assertEqual(changes["testapp"][0].name, "0003_author")
- self.assertEqual(changes["testapp"][0].dependencies, [("testapp", "0002_foobar")])
- self.assertEqual(changes["otherapp"][0].name, "0002_pony_stable")
- self.assertEqual(changes["otherapp"][0].dependencies, [("otherapp", "0001_initial")])
- def test_trim_apps(self):
- """
- Trim does not remove dependencies but does remove unwanted apps.
- """
-
- before = self.make_project_state([])
- after = self.make_project_state([self.author_empty, self.other_pony, self.other_stable, self.third_thing])
- autodetector = MigrationAutodetector(before, after, MigrationQuestioner({"ask_initial": True}))
- changes = autodetector._detect_changes()
-
- graph = MigrationGraph()
- changes = autodetector.arrange_for_graph(changes, graph)
- changes["testapp"][0].dependencies.append(("otherapp", "0001_initial"))
- changes = autodetector._trim_to_apps(changes, {"testapp"})
-
- self.assertEqual(changes["testapp"][0].name, "0001_initial")
- self.assertEqual(changes["otherapp"][0].name, "0001_initial")
- self.assertNotIn("thirdapp", changes)
- def test_custom_migration_name(self):
- """Tests custom naming of migrations for graph matching."""
-
- graph = MigrationGraph()
- graph.add_node(("testapp", "0001_initial"), None)
- graph.add_node(("testapp", "0002_foobar"), None)
- graph.add_node(("otherapp", "0001_initial"), None)
- graph.add_dependency("testapp.0002_foobar", ("testapp", "0002_foobar"), ("testapp", "0001_initial"))
-
- before = self.make_project_state([])
- after = self.make_project_state([self.author_empty, self.other_pony, self.other_stable])
- autodetector = MigrationAutodetector(before, after)
- changes = autodetector._detect_changes()
-
- migration_name = 'custom_name'
- changes = autodetector.arrange_for_graph(changes, graph, migration_name)
-
- self.assertEqual(changes["testapp"][0].name, "0003_%s" % migration_name)
- self.assertEqual(changes["testapp"][0].dependencies, [("testapp", "0002_foobar")])
- self.assertEqual(changes["otherapp"][0].name, "0002_%s" % migration_name)
- self.assertEqual(changes["otherapp"][0].dependencies, [("otherapp", "0001_initial")])
- def test_new_model(self):
- """Tests autodetection of new models."""
- changes = self.get_changes([], [self.other_pony_food])
-
- self.assertNumberMigrations(changes, 'otherapp', 1)
- self.assertOperationTypes(changes, 'otherapp', 0, ["CreateModel"])
- self.assertOperationAttributes(changes, "otherapp", 0, 0, name="Pony")
- self.assertEqual([name for name, mgr in changes['otherapp'][0].operations[0].managers],
- ['food_qs', 'food_mgr', 'food_mgr_kwargs'])
- def test_old_model(self):
- """Tests deletion of old models."""
- changes = self.get_changes([self.author_empty], [])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["DeleteModel"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author")
- def test_add_field(self):
- """Tests autodetection of new fields."""
- changes = self.get_changes([self.author_empty], [self.author_name])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AddField"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="name")
- @mock.patch('django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition',
- side_effect=AssertionError("Should not have prompted for not null addition"))
- def test_add_date_fields_with_auto_now_not_asking_for_default(self, mocked_ask_method):
- changes = self.get_changes([self.author_empty], [self.author_dates_of_birth_auto_now])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AddField", "AddField", "AddField"])
- self.assertOperationFieldAttributes(changes, "testapp", 0, 0, auto_now=True)
- self.assertOperationFieldAttributes(changes, "testapp", 0, 1, auto_now=True)
- self.assertOperationFieldAttributes(changes, "testapp", 0, 2, auto_now=True)
- @mock.patch('django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition',
- side_effect=AssertionError("Should not have prompted for not null addition"))
- def test_add_date_fields_with_auto_now_add_not_asking_for_null_addition(self, mocked_ask_method):
- changes = self.get_changes([self.author_empty], [self.author_dates_of_birth_auto_now_add])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AddField", "AddField", "AddField"])
- self.assertOperationFieldAttributes(changes, "testapp", 0, 0, auto_now_add=True)
- self.assertOperationFieldAttributes(changes, "testapp", 0, 1, auto_now_add=True)
- self.assertOperationFieldAttributes(changes, "testapp", 0, 2, auto_now_add=True)
- @mock.patch('django.db.migrations.questioner.MigrationQuestioner.ask_auto_now_add_addition')
- def test_add_date_fields_with_auto_now_add_asking_for_default(self, mocked_ask_method):
- changes = self.get_changes([self.author_empty], [self.author_dates_of_birth_auto_now_add])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AddField", "AddField", "AddField"])
- self.assertOperationFieldAttributes(changes, "testapp", 0, 0, auto_now_add=True)
- self.assertOperationFieldAttributes(changes, "testapp", 0, 1, auto_now_add=True)
- self.assertOperationFieldAttributes(changes, "testapp", 0, 2, auto_now_add=True)
- self.assertEqual(mocked_ask_method.call_count, 3)
- def test_remove_field(self):
- """Tests autodetection of removed fields."""
- changes = self.get_changes([self.author_name], [self.author_empty])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["RemoveField"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="name")
- def test_alter_field(self):
- """Tests autodetection of new fields."""
- changes = self.get_changes([self.author_name], [self.author_name_longer])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AlterField"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="name", preserve_default=True)
- def test_supports_functools_partial(self):
- def _content_file_name(instance, filename, key, **kwargs):
- return '{}/{}'.format(instance, filename)
- def content_file_name(key, **kwargs):
- return functools.partial(_content_file_name, key, **kwargs)
-
- before = [ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("file", models.FileField(max_length=200, upload_to=content_file_name('file'))),
- ])]
- after = [ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("file", models.FileField(max_length=200, upload_to=content_file_name('file'))),
- ])]
- changes = self.get_changes(before, after)
- self.assertNumberMigrations(changes, 'testapp', 0)
-
- args_changed = [ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("file", models.FileField(max_length=200, upload_to=content_file_name('other-file'))),
- ])]
- changes = self.get_changes(before, args_changed)
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ['AlterField'])
-
-
-
-
- value = changes['testapp'][0].operations[0].field.upload_to
- self.assertEqual(
- (_content_file_name, ('other-file',), {}),
- (value.func, value.args, value.keywords)
- )
- kwargs_changed = [ModelState("testapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("file", models.FileField(max_length=200, upload_to=content_file_name('file', spam='eggs'))),
- ])]
- changes = self.get_changes(before, kwargs_changed)
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ['AlterField'])
- value = changes['testapp'][0].operations[0].field.upload_to
- self.assertEqual(
- (_content_file_name, ('file',), {'spam': 'eggs'}),
- (value.func, value.args, value.keywords)
- )
- @mock.patch('django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration',
- side_effect=AssertionError("Should not have prompted for not null addition"))
- def test_alter_field_to_not_null_with_default(self, mocked_ask_method):
- """
- #23609 - Tests autodetection of nullable to non-nullable alterations.
- """
- changes = self.get_changes([self.author_name_null], [self.author_name_default])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AlterField"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="name", preserve_default=True)
- self.assertOperationFieldAttributes(changes, "testapp", 0, 0, default='Ada Lovelace')
- @mock.patch('django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration',
- return_value=models.NOT_PROVIDED)
- def test_alter_field_to_not_null_without_default(self, mocked_ask_method):
- """
- #23609 - Tests autodetection of nullable to non-nullable alterations.
- """
- changes = self.get_changes([self.author_name_null], [self.author_name])
- self.assertEqual(mocked_ask_method.call_count, 1)
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AlterField"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="name", preserve_default=True)
- self.assertOperationFieldAttributes(changes, "testapp", 0, 0, default=models.NOT_PROVIDED)
- @mock.patch('django.db.migrations.questioner.MigrationQuestioner.ask_not_null_alteration',
- return_value='Some Name')
- def test_alter_field_to_not_null_oneoff_default(self, mocked_ask_method):
- """
- #23609 - Tests autodetection of nullable to non-nullable alterations.
- """
- changes = self.get_changes([self.author_name_null], [self.author_name])
- self.assertEqual(mocked_ask_method.call_count, 1)
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AlterField"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="name", preserve_default=False)
- self.assertOperationFieldAttributes(changes, "testapp", 0, 0, default="Some Name")
- def test_rename_field(self):
- """Tests autodetection of renamed fields."""
- changes = self.get_changes(
- [self.author_name], [self.author_name_renamed], MigrationQuestioner({"ask_rename": True})
- )
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["RenameField"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, old_name="name", new_name="names")
- def test_rename_model(self):
- """Tests autodetection of renamed models."""
- changes = self.get_changes(
- [self.author_with_book, self.book],
- [self.author_renamed_with_book, self.book_with_author_renamed],
- MigrationQuestioner({"ask_rename_model": True}),
- )
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["RenameModel"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, old_name="Author", new_name="Writer")
-
-
- self.assertNumberMigrations(changes, 'otherapp', 0)
- def test_rename_m2m_through_model(self):
- """
- Tests autodetection of renamed models that are used in M2M relations as
- through models.
- """
- changes = self.get_changes(
- [self.author_with_m2m_through, self.publisher, self.contract],
- [self.author_with_renamed_m2m_through, self.publisher, self.contract_renamed],
- MigrationQuestioner({'ask_rename_model': True})
- )
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ['RenameModel'])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, old_name='Contract', new_name='Deal')
- def test_rename_model_with_renamed_rel_field(self):
- """
- Tests autodetection of renamed models while simultaneously renaming one
- of the fields that relate to the renamed model.
- """
- changes = self.get_changes(
- [self.author_with_book, self.book],
- [self.author_renamed_with_book, self.book_with_field_and_author_renamed],
- MigrationQuestioner({"ask_rename": True, "ask_rename_model": True}),
- )
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["RenameModel"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, old_name="Author", new_name="Writer")
-
-
- self.assertNumberMigrations(changes, 'otherapp', 1)
- self.assertOperationTypes(changes, 'otherapp', 0, ["RenameField"])
- self.assertOperationAttributes(changes, 'otherapp', 0, 0, old_name="author", new_name="writer")
- def test_rename_model_with_fks_in_different_position(self):
- """
- #24537 - The order of fields in a model does not influence
- the RenameModel detection.
- """
- before = [
- ModelState("testapp", "EntityA", [
- ("id", models.AutoField(primary_key=True)),
- ]),
- ModelState("testapp", "EntityB", [
- ("id", models.AutoField(primary_key=True)),
- ("some_label", models.CharField(max_length=255)),
- ("entity_a", models.ForeignKey("testapp.EntityA", models.CASCADE)),
- ]),
- ]
- after = [
- ModelState("testapp", "EntityA", [
- ("id", models.AutoField(primary_key=True)),
- ]),
- ModelState("testapp", "RenamedEntityB", [
- ("id", models.AutoField(primary_key=True)),
- ("entity_a", models.ForeignKey("testapp.EntityA", models.CASCADE)),
- ("some_label", models.CharField(max_length=255)),
- ]),
- ]
- changes = self.get_changes(before, after, MigrationQuestioner({"ask_rename_model": True}))
- self.assertNumberMigrations(changes, "testapp", 1)
- self.assertOperationTypes(changes, "testapp", 0, ["RenameModel"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, old_name="EntityB", new_name="RenamedEntityB")
- def test_rename_model_reverse_relation_dependencies(self):
- """
- The migration to rename a model pointed to by a foreign key in another
- app must run after the other app's migration that adds the foreign key
- with model's original name. Therefore, the renaming migration has a
- dependency on that other migration.
- """
- before = [
- ModelState('testapp', 'EntityA', [
- ('id', models.AutoField(primary_key=True)),
- ]),
- ModelState('otherapp', 'EntityB', [
- ('id', models.AutoField(primary_key=True)),
- ('entity_a', models.ForeignKey('testapp.EntityA', models.CASCADE)),
- ]),
- ]
- after = [
- ModelState('testapp', 'RenamedEntityA', [
- ('id', models.AutoField(primary_key=True)),
- ]),
- ModelState('otherapp', 'EntityB', [
- ('id', models.AutoField(primary_key=True)),
- ('entity_a', models.ForeignKey('testapp.RenamedEntityA', models.CASCADE)),
- ]),
- ]
- changes = self.get_changes(before, after, MigrationQuestioner({'ask_rename_model': True}))
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertMigrationDependencies(changes, 'testapp', 0, [('otherapp', '__first__')])
- self.assertOperationTypes(changes, 'testapp', 0, ['RenameModel'])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, old_name='EntityA', new_name='RenamedEntityA')
- def test_fk_dependency(self):
- """Having a ForeignKey automatically adds a dependency."""
-
-
-
- changes = self.get_changes([], [self.author_name, self.book, self.edition])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="Author")
- self.assertMigrationDependencies(changes, 'testapp', 0, [])
-
- self.assertNumberMigrations(changes, 'otherapp', 1)
- self.assertOperationTypes(changes, 'otherapp', 0, ["CreateModel"])
- self.assertOperationAttributes(changes, 'otherapp', 0, 0, name="Book")
- self.assertMigrationDependencies(changes, 'otherapp', 0, [("testapp", "auto_1")])
-
- self.assertNumberMigrations(changes, 'thirdapp', 1)
- self.assertOperationTypes(changes, 'thirdapp', 0, ["CreateModel"])
- self.assertOperationAttributes(changes, 'thirdapp', 0, 0, name="Edition")
- self.assertMigrationDependencies(changes, 'thirdapp', 0, [("otherapp", "auto_1")])
- def test_proxy_fk_dependency(self):
- """FK dependencies still work on proxy models."""
-
-
- changes = self.get_changes([], [self.author_empty, self.author_proxy_third, self.book_proxy_fk])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="Author")
- self.assertMigrationDependencies(changes, 'testapp', 0, [])
-
- self.assertNumberMigrations(changes, 'otherapp', 1)
- self.assertOperationTypes(changes, 'otherapp', 0, ["CreateModel"])
- self.assertOperationAttributes(changes, 'otherapp', 0, 0, name="Book")
- self.assertMigrationDependencies(changes, 'otherapp', 0, [("thirdapp", "auto_1")])
-
- self.assertNumberMigrations(changes, 'thirdapp', 1)
- self.assertOperationTypes(changes, 'thirdapp', 0, ["CreateModel"])
- self.assertOperationAttributes(changes, 'thirdapp', 0, 0, name="AuthorProxy")
- self.assertMigrationDependencies(changes, 'thirdapp', 0, [("testapp", "auto_1")])
- def test_same_app_no_fk_dependency(self):
- """
- A migration with a FK between two models of the same app
- does not have a dependency to itself.
- """
- changes = self.get_changes([], [self.author_with_publisher, self.publisher])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel", "CreateModel", "AddField"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author")
- self.assertOperationAttributes(changes, "testapp", 0, 1, name="Publisher")
- self.assertOperationAttributes(changes, "testapp", 0, 2, name="publisher")
- self.assertMigrationDependencies(changes, 'testapp', 0, [])
- def test_circular_fk_dependency(self):
- """
- Having a circular ForeignKey dependency automatically
- resolves the situation into 2 migrations on one side and 1 on the other.
- """
- changes = self.get_changes([], [self.author_with_book, self.book, self.publisher_with_book])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel", "CreateModel"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author")
- self.assertOperationAttributes(changes, "testapp", 0, 1, name="Publisher")
- self.assertMigrationDependencies(changes, 'testapp', 0, [("otherapp", "auto_1")])
-
- self.assertNumberMigrations(changes, 'otherapp', 2)
- self.assertOperationTypes(changes, 'otherapp', 0, ["CreateModel"])
- self.assertOperationTypes(changes, 'otherapp', 1, ["AddField"])
- self.assertMigrationDependencies(changes, 'otherapp', 0, [])
- self.assertMigrationDependencies(changes, 'otherapp', 1, [("otherapp", "auto_1"), ("testapp", "auto_1")])
-
- self.assertTrue(changes['otherapp'][0].initial)
- self.assertTrue(changes['otherapp'][1].initial)
- def test_same_app_circular_fk_dependency(self):
- """
- A migration with a FK between two models of the same app does
- not have a dependency to itself.
- """
- changes = self.get_changes([], [self.author_with_publisher, self.publisher_with_author])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel", "CreateModel", "AddField"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="Author")
- self.assertOperationAttributes(changes, "testapp", 0, 1, name="Publisher")
- self.assertOperationAttributes(changes, "testapp", 0, 2, name="publisher")
- self.assertMigrationDependencies(changes, 'testapp', 0, [])
- def test_same_app_circular_fk_dependency_with_unique_together_and_indexes(self):
- """
- #22275 - A migration with circular FK dependency does not try
- to create unique together constraint and indexes before creating all
- required fields first.
- """
- changes = self.get_changes([], [self.knight, self.rabbit])
-
- self.assertNumberMigrations(changes, 'eggs', 1)
- self.assertOperationTypes(
- changes, 'eggs', 0, ["CreateModel", "CreateModel", "AddIndex", "AlterUniqueTogether"]
- )
- self.assertNotIn("unique_together", changes['eggs'][0].operations[0].options)
- self.assertNotIn("unique_together", changes['eggs'][0].operations[1].options)
- self.assertMigrationDependencies(changes, 'eggs', 0, [])
- def test_alter_db_table_add(self):
- """Tests detection for adding db_table in model's options."""
- changes = self.get_changes([self.author_empty], [self.author_with_db_table_options])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AlterModelTable"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="author", table="author_one")
- def test_alter_db_table_change(self):
- """Tests detection for changing db_table in model's options'."""
- changes = self.get_changes([self.author_with_db_table_options], [self.author_with_new_db_table_options])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AlterModelTable"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="author", table="author_two")
- def test_alter_db_table_remove(self):
- """Tests detection for removing db_table in model's options."""
- changes = self.get_changes([self.author_with_db_table_options], [self.author_empty])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AlterModelTable"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="author", table=None)
- def test_alter_db_table_no_changes(self):
- """
- Alter_db_table doesn't generate a migration if no changes have been made.
- """
- changes = self.get_changes([self.author_with_db_table_options], [self.author_with_db_table_options])
-
- self.assertEqual(len(changes), 0)
- def test_keep_db_table_with_model_change(self):
- """
- Tests when model changes but db_table stays as-is, autodetector must not
- create more than one operation.
- """
- changes = self.get_changes(
- [self.author_with_db_table_options],
- [self.author_renamed_with_db_table_options],
- MigrationQuestioner({"ask_rename_model": True}),
- )
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["RenameModel"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, old_name="Author", new_name="NewAuthor")
- def test_alter_db_table_with_model_change(self):
- """
- Tests when model and db_table changes, autodetector must create two
- operations.
- """
- changes = self.get_changes(
- [self.author_with_db_table_options],
- [self.author_renamed_with_new_db_table_options],
- MigrationQuestioner({"ask_rename_model": True}),
- )
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["RenameModel", "AlterModelTable"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, old_name="Author", new_name="NewAuthor")
- self.assertOperationAttributes(changes, "testapp", 0, 1, name="newauthor", table="author_three")
- def test_identical_regex_doesnt_alter(self):
- from_state = ModelState(
- "testapp", "model", [("id", models.AutoField(primary_key=True, validators=[
- RegexValidator(
- re.compile('^[-a-zA-Z0-9_]+\\Z'),
- "Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens.",
- 'invalid'
- )
- ]))]
- )
- to_state = ModelState(
- "testapp", "model", [("id", models.AutoField(primary_key=True, validators=[validate_slug]))]
- )
- changes = self.get_changes([from_state], [to_state])
-
- self.assertNumberMigrations(changes, "testapp", 0)
- def test_different_regex_does_alter(self):
- from_state = ModelState(
- "testapp", "model", [("id", models.AutoField(primary_key=True, validators=[
- RegexValidator(
- re.compile('^[a-z]+\\Z', 32),
- "Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens.",
- 'invalid'
- )
- ]))]
- )
- to_state = ModelState(
- "testapp", "model", [("id", models.AutoField(primary_key=True, validators=[validate_slug]))]
- )
- changes = self.get_changes([from_state], [to_state])
- self.assertNumberMigrations(changes, "testapp", 1)
- self.assertOperationTypes(changes, "testapp", 0, ["AlterField"])
- def test_empty_foo_together(self):
- """
- #23452 - Empty unique/index_together shouldn't generate a migration.
- """
-
-
- model_state_not_specified = ModelState("a", "model", [("id", models.AutoField(primary_key=True))])
-
-
- model_state_none = ModelState("a", "model", [
- ("id", models.AutoField(primary_key=True))
- ], {
- "index_together": None,
- "unique_together": None,
- })
-
-
- model_state_empty = ModelState("a", "model", [
- ("id", models.AutoField(primary_key=True))
- ], {
- "index_together": set(),
- "unique_together": set(),
- })
- def test(from_state, to_state, msg):
- changes = self.get_changes([from_state], [to_state])
- if changes:
- ops = ', '.join(o.__class__.__name__ for o in changes['a'][0].operations)
- self.fail('Created operation(s) %s from %s' % (ops, msg))
- tests = (
- (model_state_not_specified, model_state_not_specified, '"not specified" to "not specified"'),
- (model_state_not_specified, model_state_none, '"not specified" to "None"'),
- (model_state_not_specified, model_state_empty, '"not specified" to "empty"'),
- (model_state_none, model_state_not_specified, '"None" to "not specified"'),
- (model_state_none, model_state_none, '"None" to "None"'),
- (model_state_none, model_state_empty, '"None" to "empty"'),
- (model_state_empty, model_state_not_specified, '"empty" to "not specified"'),
- (model_state_empty, model_state_none, '"empty" to "None"'),
- (model_state_empty, model_state_empty, '"empty" to "empty"'),
- )
- for t in tests:
- test(*t)
- def test_create_model_with_indexes(self):
- """Test creation of new model with indexes already defined."""
- author = ModelState('otherapp', 'Author', [
- ('id', models.AutoField(primary_key=True)),
- ('name', models.CharField(max_length=200)),
- ], {'indexes': [models.Index(fields=['name'], name='create_model_with_indexes_idx')]})
- changes = self.get_changes([], [author])
- added_index = models.Index(fields=['name'], name='create_model_with_indexes_idx')
-
- self.assertEqual(len(changes['otherapp']), 1)
-
- migration = changes['otherapp'][0]
- self.assertEqual(len(migration.operations), 2)
-
- self.assertOperationTypes(changes, 'otherapp', 0, ['CreateModel', 'AddIndex'])
- self.assertOperationAttributes(changes, 'otherapp', 0, 0, name='Author')
- self.assertOperationAttributes(changes, 'otherapp', 0, 1, model_name='author', index=added_index)
- def test_add_indexes(self):
- """Test change detection of new indexes."""
- changes = self.get_changes([self.author_empty, self.book], [self.author_empty, self.book_indexes])
- self.assertNumberMigrations(changes, 'otherapp', 1)
- self.assertOperationTypes(changes, 'otherapp', 0, ['AddIndex'])
- added_index = models.Index(fields=['author', 'title'], name='book_title_author_idx')
- self.assertOperationAttributes(changes, 'otherapp', 0, 0, model_name='book', index=added_index)
- def test_remove_indexes(self):
- """Test change detection of removed indexes."""
- changes = self.get_changes([self.author_empty, self.book_indexes], [self.author_empty, self.book])
-
- self.assertNumberMigrations(changes, 'otherapp', 1)
- self.assertOperationTypes(changes, 'otherapp', 0, ['RemoveIndex'])
- self.assertOperationAttributes(changes, 'otherapp', 0, 0, model_name='book', name='book_title_author_idx')
- def test_order_fields_indexes(self):
- """Test change detection of reordering of fields in indexes."""
- changes = self.get_changes(
- [self.author_empty, self.book_indexes], [self.author_empty, self.book_unordered_indexes]
- )
- self.assertNumberMigrations(changes, 'otherapp', 1)
- self.assertOperationTypes(changes, 'otherapp', 0, ['RemoveIndex', 'AddIndex'])
- self.assertOperationAttributes(changes, 'otherapp', 0, 0, model_name='book', name='book_title_author_idx')
- added_index = models.Index(fields=['title', 'author'], name='book_author_title_idx')
- self.assertOperationAttributes(changes, 'otherapp', 0, 1, model_name='book', index=added_index)
- def test_add_foo_together(self):
- """Tests index/unique_together detection."""
- changes = self.get_changes([self.author_empty, self.book], [self.author_empty, self.book_foo_together])
-
- self.assertNumberMigrations(changes, "otherapp", 1)
- self.assertOperationTypes(changes, "otherapp", 0, ["AlterUniqueTogether", "AlterIndexTogether"])
- self.assertOperationAttributes(changes, "otherapp", 0, 0, name="book", unique_together={("author", "title")})
- self.assertOperationAttributes(changes, "otherapp", 0, 1, name="book", index_together={("author", "title")})
- def test_remove_foo_together(self):
- """Tests index/unique_together detection."""
- changes = self.get_changes([self.author_empty, self.book_foo_together], [self.author_empty, self.book])
-
- self.assertNumberMigrations(changes, "otherapp", 1)
- self.assertOperationTypes(changes, "otherapp", 0, ["AlterUniqueTogether", "AlterIndexTogether"])
- self.assertOperationAttributes(changes, "otherapp", 0, 0, name="book", unique_together=set())
- self.assertOperationAttributes(changes, "otherapp", 0, 1, name="book", index_together=set())
- def test_foo_together_remove_fk(self):
- """Tests unique_together and field removal detection & ordering"""
- changes = self.get_changes(
- [self.author_empty, self.book_foo_together], [self.author_empty, self.book_with_no_author]
- )
-
- self.assertNumberMigrations(changes, "otherapp", 1)
- self.assertOperationTypes(changes, "otherapp", 0, [
- "AlterUniqueTogether", "AlterIndexTogether", "RemoveField"
- ])
- self.assertOperationAttributes(changes, "otherapp", 0, 0, name="book", unique_together=set())
- self.assertOperationAttributes(changes, "otherapp", 0, 1, name="book", index_together=set())
- self.assertOperationAttributes(changes, "otherapp", 0, 2, model_name="book", name="author")
- def test_foo_together_no_changes(self):
- """
- index/unique_together doesn't generate a migration if no
- changes have been made.
- """
- changes = self.get_changes(
- [self.author_empty, self.book_foo_together], [self.author_empty, self.book_foo_together]
- )
-
- self.assertEqual(len(changes), 0)
- def test_foo_together_ordering(self):
- """
- index/unique_together also triggers on ordering changes.
- """
- changes = self.get_changes(
- [self.author_empty, self.book_foo_together], [self.author_empty, self.book_foo_together_2]
- )
-
- self.assertNumberMigrations(changes, "otherapp", 1)
- self.assertOperationTypes(changes, "otherapp", 0, ["AlterUniqueTogether", "AlterIndexTogether"])
- self.assertOperationAttributes(changes, "otherapp", 0, 0, name="book", unique_together={("title", "author")})
- self.assertOperationAttributes(changes, "otherapp", 0, 1, name="book", index_together={("title", "author")})
- def test_add_field_and_foo_together(self):
- """
- Added fields will be created before using them in index/unique_together.
- """
- changes = self.get_changes([self.author_empty, self.book], [self.author_empty, self.book_foo_together_3])
-
- self.assertNumberMigrations(changes, "otherapp", 1)
- self.assertOperationTypes(changes, "otherapp", 0, ["AddField", "AlterUniqueTogether", "AlterIndexTogether"])
- self.assertOperationAttributes(changes, "otherapp", 0, 1, name="book", unique_together={("title", "newfield")})
- self.assertOperationAttributes(changes, "otherapp", 0, 2, name="book", index_together={("title", "newfield")})
- def test_create_model_and_unique_together(self):
- author = ModelState("otherapp", "Author", [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=200)),
- ])
- book_with_author = ModelState("otherapp", "Book", [
- ("id", models.AutoField(primary_key=True)),
- ("author", models.ForeignKey("otherapp.Author", models.CASCADE)),
- ("title", models.CharField(max_length=200)),
- ], {
- "index_together": {("title", "author")},
- "unique_together": {("title", "author")},
- })
- changes = self.get_changes([self.book_with_no_author], [author, book_with_author])
-
- self.assertEqual(len(changes['otherapp']), 1)
-
- migration = changes['otherapp'][0]
- self.assertEqual(len(migration.operations), 4)
-
- self.assertOperationTypes(
- changes, 'otherapp', 0,
- ['CreateModel', 'AddField', 'AlterUniqueTogether', 'AlterIndexTogether']
- )
- def test_remove_field_and_foo_together(self):
- """
- Removed fields will be removed after updating index/unique_together.
- """
- changes = self.get_changes(
- [self.author_empty, self.book_foo_together_3], [self.author_empty, self.book_foo_together]
- )
-
- self.assertNumberMigrations(changes, "otherapp", 1)
- self.assertOperationTypes(changes, "otherapp", 0, ["RemoveField", "AlterUniqueTogether", "AlterIndexTogether"])
- self.assertOperationAttributes(changes, "otherapp", 0, 0, model_name="book", name="newfield")
- self.assertOperationAttributes(changes, "otherapp", 0, 1, name="book", unique_together={("author", "title")})
- self.assertOperationAttributes(changes, "otherapp", 0, 2, name="book", index_together={("author", "title")})
- def test_rename_field_and_foo_together(self):
- """
- Removed fields will be removed after updating index/unique_together.
- """
- changes = self.get_changes(
- [self.author_empty, self.book_foo_together_3],
- [self.author_empty, self.book_foo_together_4],
- MigrationQuestioner({"ask_rename": True}),
- )
-
- self.assertNumberMigrations(changes, "otherapp", 1)
- self.assertOperationTypes(changes, "otherapp", 0, ["RenameField", "AlterUniqueTogether", "AlterIndexTogether"])
- self.assertOperationAttributes(changes, "otherapp", 0, 1, name="book", unique_together={
- ("title", "newfield2")
- })
- self.assertOperationAttributes(changes, "otherapp", 0, 2, name="book", index_together={("title", "newfield2")})
- def test_proxy(self):
- """The autodetector correctly deals with proxy models."""
-
- changes = self.get_changes([self.author_empty], [self.author_empty, self.author_proxy])
-
- self.assertNumberMigrations(changes, "testapp", 1)
- self.assertOperationTypes(changes, "testapp", 0, ["CreateModel"])
- self.assertOperationAttributes(
- changes, "testapp", 0, 0, name="AuthorProxy", options={"proxy": True, "indexes": []}
- )
-
-
- changes = self.get_changes(
- [self.author_empty, self.author_proxy], [self.author_empty, self.author_proxy_notproxy]
- )
-
- self.assertNumberMigrations(changes, "testapp", 1)
- self.assertOperationTypes(changes, "testapp", 0, ["DeleteModel", "CreateModel"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="AuthorProxy")
- self.assertOperationAttributes(changes, "testapp", 0, 1, name="AuthorProxy", options={})
- def test_proxy_custom_pk(self):
- """
- #23415 - The autodetector must correctly deal with custom FK on proxy
- models.
- """
-
- changes = self.get_changes([], [self.author_empty, self.author_proxy_third, self.book_proxy_fk])
-
- self.assertEqual(changes['otherapp'][0].operations[0].fields[2][1].remote_field.field_name, 'id')
-
- changes = self.get_changes([], [self.author_custom_pk, self.author_proxy_third, self.book_proxy_fk])
-
- self.assertEqual(changes['otherapp'][0].operations[0].fields[2][1].remote_field.field_name, 'pk_field')
- def test_proxy_to_mti_with_fk_to_proxy(self):
-
- changes = self.get_changes(
- [],
- [self.author_empty, self.author_proxy_third, self.book_proxy_fk],
- )
- self.assertEqual(
- changes['otherapp'][0].operations[0].fields[2][1].remote_field.model._meta.db_table,
- 'testapp_author',
- )
- self.assertEqual(changes['otherapp'][0].operations[0].fields[2][1].remote_field.field_name, 'id')
-
- changes = self.get_changes(
- [self.author_empty, self.author_proxy_third, self.book_proxy_fk],
- [self.author_empty, self.author_proxy_third_notproxy, self.book_proxy_fk],
- )
-
- self.assertNumberMigrations(changes, 'thirdapp', 1)
- self.assertOperationTypes(changes, 'thirdapp', 0, ['DeleteModel', 'CreateModel'])
-
-
- self.assertNumberMigrations(changes, 'otherapp', 1)
- self.assertOperationTypes(changes, 'otherapp', 0, ['AlterField'])
-
- self.assertMigrationDependencies(changes, 'otherapp', 0, [('thirdapp', 'auto_1')])
-
- self.assertEqual(
- changes['otherapp'][0].operations[0].field.remote_field.model._meta.db_table,
- 'thirdapp_authorproxy',
- )
- self.assertEqual(changes['otherapp'][0].operations[0].field.remote_field.field_name, 'author_ptr')
- def test_proxy_to_mti_with_fk_to_proxy_proxy(self):
-
- changes = self.get_changes(
- [],
- [self.author_empty, self.author_proxy, self.author_proxy_proxy, self.book_proxy_proxy_fk],
- )
- self.assertEqual(
- changes['otherapp'][0].operations[0].fields[1][1].remote_field.model._meta.db_table,
- 'testapp_author',
- )
- self.assertEqual(changes['otherapp'][0].operations[0].fields[1][1].remote_field.field_name, 'id')
-
-
- changes = self.get_changes(
- [self.author_empty, self.author_proxy, self.author_proxy_proxy, self.book_proxy_proxy_fk],
- [self.author_empty, self.author_proxy_notproxy, self.author_proxy_proxy, self.book_proxy_proxy_fk],
- )
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ['DeleteModel', 'CreateModel'])
-
-
- self.assertNumberMigrations(changes, 'otherapp', 1)
- self.assertOperationTypes(changes, 'otherapp', 0, ['AlterField'])
-
- self.assertMigrationDependencies(changes, 'otherapp', 0, [('testapp', 'auto_1')])
-
- self.assertEqual(
- changes['otherapp'][0].operations[0].field.remote_field.model._meta.db_table,
- 'testapp_authorproxy',
- )
- self.assertEqual(changes['otherapp'][0].operations[0].field.remote_field.field_name, 'author_ptr')
- def test_unmanaged_create(self):
- """The autodetector correctly deals with managed models."""
-
- changes = self.get_changes([self.author_empty], [self.author_empty, self.author_unmanaged])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="AuthorUnmanaged", options={"managed": False})
- def test_unmanaged_to_managed(self):
-
- changes = self.get_changes(
- [self.author_empty, self.author_unmanaged], [self.author_empty, self.author_unmanaged_managed]
- )
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AlterModelOptions"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="authorunmanaged", options={})
- def test_managed_to_unmanaged(self):
-
- changes = self.get_changes(
- [self.author_empty, self.author_unmanaged_managed], [self.author_empty, self.author_unmanaged]
- )
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, "testapp", 0, ["AlterModelOptions"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="authorunmanaged", options={"managed": False})
- def test_unmanaged_custom_pk(self):
- """
- #23415 - The autodetector must correctly deal with custom FK on
- unmanaged models.
- """
-
- changes = self.get_changes([], [self.author_unmanaged_default_pk, self.book])
-
- self.assertEqual(changes['otherapp'][0].operations[0].fields[2][1].remote_field.field_name, 'id')
-
- changes = self.get_changes([], [self.author_unmanaged_custom_pk, self.book])
-
- self.assertEqual(changes['otherapp'][0].operations[0].fields[2][1].remote_field.field_name, 'pk_field')
- @override_settings(AUTH_USER_MODEL="thirdapp.CustomUser")
- def test_swappable(self):
- with isolate_lru_cache(apps.get_swappable_settings_name):
- changes = self.get_changes([self.custom_user], [self.custom_user, self.author_with_custom_user])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="Author")
- self.assertMigrationDependencies(changes, 'testapp', 0, [("__setting__", "AUTH_USER_MODEL")])
- def test_swappable_changed(self):
- with isolate_lru_cache(apps.get_swappable_settings_name):
- before = self.make_project_state([self.custom_user, self.author_with_user])
- with override_settings(AUTH_USER_MODEL="thirdapp.CustomUser"):
- after = self.make_project_state([self.custom_user, self.author_with_custom_user])
- autodetector = MigrationAutodetector(before, after)
- changes = autodetector._detect_changes()
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AlterField"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, model_name="author", name='user')
- fk_field = changes['testapp'][0].operations[0].field
- to_model = '%s.%s' % (
- fk_field.remote_field.model._meta.app_label,
- fk_field.remote_field.model._meta.object_name,
- )
- self.assertEqual(to_model, 'thirdapp.CustomUser')
- def test_add_field_with_default(self):
- """#22030 - Adding a field with a default should work."""
- changes = self.get_changes([self.author_empty], [self.author_name_default])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AddField"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="name")
- def test_custom_deconstructible(self):
- """
- Two instances which deconstruct to the same value aren't considered a
- change.
- """
- changes = self.get_changes([self.author_name_deconstructible_1], [self.author_name_deconstructible_2])
-
- self.assertEqual(len(changes), 0)
- def test_deconstruct_field_kwarg(self):
- """Field instances are handled correctly by nested deconstruction."""
- changes = self.get_changes([self.author_name_deconstructible_3], [self.author_name_deconstructible_4])
- self.assertEqual(changes, {})
- def test_deconstructible_list(self):
- """Nested deconstruction descends into lists."""
-
-
-
- changes = self.get_changes(
- [self.author_name_deconstructible_list_1], [self.author_name_deconstructible_list_2]
- )
- self.assertEqual(changes, {})
-
-
- changes = self.get_changes(
- [self.author_name_deconstructible_list_1], [self.author_name_deconstructible_list_3]
- )
- self.assertEqual(len(changes), 1)
- def test_deconstructible_tuple(self):
- """Nested deconstruction descends into tuples."""
-
-
-
- changes = self.get_changes(
- [self.author_name_deconstructible_tuple_1], [self.author_name_deconstructible_tuple_2]
- )
- self.assertEqual(changes, {})
-
-
- changes = self.get_changes(
- [self.author_name_deconstructible_tuple_1], [self.author_name_deconstructible_tuple_3]
- )
- self.assertEqual(len(changes), 1)
- def test_deconstructible_dict(self):
- """Nested deconstruction descends into dict values."""
-
-
-
- changes = self.get_changes(
- [self.author_name_deconstructible_dict_1], [self.author_name_deconstructible_dict_2]
- )
- self.assertEqual(changes, {})
-
-
- changes = self.get_changes(
- [self.author_name_deconstructible_dict_1], [self.author_name_deconstructible_dict_3]
- )
- self.assertEqual(len(changes), 1)
- def test_nested_deconstructible_objects(self):
- """
- Nested deconstruction is applied recursively to the args/kwargs of
- deconstructed objects.
- """
-
-
-
- changes = self.get_changes(
- [self.author_name_nested_deconstructible_1], [self.author_name_nested_deconstructible_2]
- )
- self.assertEqual(changes, {})
-
-
- changes = self.get_changes(
- [self.author_name_nested_deconstructible_1], [self.author_name_nested_deconstructible_changed_arg]
- )
- self.assertEqual(len(changes), 1)
-
- changes = self.get_changes(
- [self.author_name_nested_deconstructible_1], [self.author_name_nested_deconstructible_extra_arg]
- )
- self.assertEqual(len(changes), 1)
-
-
- changes = self.get_changes(
- [self.author_name_nested_deconstructible_1], [self.author_name_nested_deconstructible_changed_kwarg]
- )
- self.assertEqual(len(changes), 1)
-
- changes = self.get_changes(
- [self.author_name_nested_deconstructible_1], [self.author_name_nested_deconstructible_extra_kwarg]
- )
- self.assertEqual(len(changes), 1)
- def test_deconstruct_type(self):
- """
- #22951 -- Uninstantiated classes with deconstruct are correctly returned
- by deep_deconstruct during serialization.
- """
- author = ModelState(
- "testapp",
- "Author",
- [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(
- max_length=200,
-
- default=models.IntegerField,
- ))
- ],
- )
- changes = self.get_changes([], [author])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel"])
- def test_replace_string_with_foreignkey(self):
- """
- #22300 - Adding an FK in the same "spot" as a deleted CharField should
- work.
- """
- changes = self.get_changes([self.author_with_publisher_string], [self.author_with_publisher, self.publisher])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel", "RemoveField", "AddField"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="Publisher")
- self.assertOperationAttributes(changes, 'testapp', 0, 1, name="publisher_name")
- self.assertOperationAttributes(changes, 'testapp', 0, 2, name="publisher")
- def test_foreign_key_removed_before_target_model(self):
- """
- Removing an FK and the model it targets in the same change must remove
- the FK field before the model to maintain consistency.
- """
- changes = self.get_changes(
- [self.author_with_publisher, self.publisher], [self.author_name]
- )
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["RemoveField", "DeleteModel"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="publisher")
- self.assertOperationAttributes(changes, 'testapp', 0, 1, name="Publisher")
- @mock.patch('django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition',
- side_effect=AssertionError("Should not have prompted for not null addition"))
- def test_add_many_to_many(self, mocked_ask_method):
- """#22435 - Adding a ManyToManyField should not prompt for a default."""
- changes = self.get_changes([self.author_empty, self.publisher], [self.author_with_m2m, self.publisher])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AddField"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="publishers")
- def test_alter_many_to_many(self):
- changes = self.get_changes(
- [self.author_with_m2m, self.publisher], [self.author_with_m2m_blank, self.publisher]
- )
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AlterField"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="publishers")
- def test_create_with_through_model(self):
- """
- Adding a m2m with a through model and the models that use it should be
- ordered correctly.
- """
- changes = self.get_changes([], [self.author_with_m2m_through, self.publisher, self.contract])
-
- self.assertNumberMigrations(changes, "testapp", 1)
- self.assertOperationTypes(changes, "testapp", 0, [
- "CreateModel", "CreateModel", "CreateModel", "AddField", "AddField"
- ])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="Author")
- self.assertOperationAttributes(changes, 'testapp', 0, 1, name="Contract")
- self.assertOperationAttributes(changes, 'testapp', 0, 2, name="Publisher")
- self.assertOperationAttributes(changes, 'testapp', 0, 3, model_name='contract', name='publisher')
- self.assertOperationAttributes(changes, 'testapp', 0, 4, model_name='author', name='publishers')
- def test_many_to_many_removed_before_through_model(self):
- """
- Removing a ManyToManyField and the "through" model in the same change
- must remove the field before the model to maintain consistency.
- """
- changes = self.get_changes(
- [self.book_with_multiple_authors_through_attribution, self.author_name, self.attribution],
- [self.book_with_no_author, self.author_name],
- )
-
-
- self.assertNumberMigrations(changes, "otherapp", 1)
- self.assertOperationTypes(changes, "otherapp", 0, ["RemoveField", "RemoveField", "RemoveField", "DeleteModel"])
- self.assertOperationAttributes(changes, 'otherapp', 0, 0, name="author", model_name='attribution')
- self.assertOperationAttributes(changes, 'otherapp', 0, 1, name="book", model_name='attribution')
- self.assertOperationAttributes(changes, 'otherapp', 0, 2, name="authors", model_name='book')
- self.assertOperationAttributes(changes, 'otherapp', 0, 3, name='Attribution')
- def test_many_to_many_removed_before_through_model_2(self):
- """
- Removing a model that contains a ManyToManyField and the "through" model
- in the same change must remove the field before the model to maintain
- consistency.
- """
- changes = self.get_changes(
- [self.book_with_multiple_authors_through_attribution, self.author_name, self.attribution],
- [self.author_name],
- )
-
-
- self.assertNumberMigrations(changes, "otherapp", 1)
- self.assertOperationTypes(changes, "otherapp", 0, [
- "RemoveField", "RemoveField", "RemoveField", "DeleteModel", "DeleteModel"
- ])
- self.assertOperationAttributes(changes, 'otherapp', 0, 0, name="author", model_name='attribution')
- self.assertOperationAttributes(changes, 'otherapp', 0, 1, name="book", model_name='attribution')
- self.assertOperationAttributes(changes, 'otherapp', 0, 2, name="authors", model_name='book')
- self.assertOperationAttributes(changes, 'otherapp', 0, 3, name='Attribution')
- self.assertOperationAttributes(changes, 'otherapp', 0, 4, name='Book')
- def test_m2m_w_through_multistep_remove(self):
- """
- A model with a m2m field that specifies a "through" model cannot be
- removed in the same migration as that through model as the schema will
- pass through an inconsistent state. The autodetector should produce two
- migrations to avoid this issue.
- """
- changes = self.get_changes([self.author_with_m2m_through, self.publisher, self.contract], [self.publisher])
-
- self.assertNumberMigrations(changes, "testapp", 1)
- self.assertOperationTypes(changes, "testapp", 0, [
- "RemoveField", "RemoveField", "RemoveField", "DeleteModel", "DeleteModel"
- ])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="publishers", model_name='author')
- self.assertOperationAttributes(changes, "testapp", 0, 1, name="author", model_name='contract')
- self.assertOperationAttributes(changes, "testapp", 0, 2, name="publisher", model_name='contract')
- self.assertOperationAttributes(changes, "testapp", 0, 3, name="Author")
- self.assertOperationAttributes(changes, "testapp", 0, 4, name="Contract")
- def test_concrete_field_changed_to_many_to_many(self):
- """
- #23938 - Changing a concrete field into a ManyToManyField
- first removes the concrete field and then adds the m2m field.
- """
- changes = self.get_changes([self.author_with_former_m2m], [self.author_with_m2m, self.publisher])
-
- self.assertNumberMigrations(changes, "testapp", 1)
- self.assertOperationTypes(changes, "testapp", 0, ["CreateModel", "RemoveField", "AddField"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name='Publisher')
- self.assertOperationAttributes(changes, 'testapp', 0, 1, name="publishers", model_name='author')
- self.assertOperationAttributes(changes, 'testapp', 0, 2, name="publishers", model_name='author')
- def test_many_to_many_changed_to_concrete_field(self):
- """
- #23938 - Changing a ManyToManyField into a concrete field
- first removes the m2m field and then adds the concrete field.
- """
- changes = self.get_changes([self.author_with_m2m, self.publisher], [self.author_with_former_m2m])
-
- self.assertNumberMigrations(changes, "testapp", 1)
- self.assertOperationTypes(changes, "testapp", 0, ["RemoveField", "AddField", "DeleteModel"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="publishers", model_name='author')
- self.assertOperationAttributes(changes, 'testapp', 0, 1, name="publishers", model_name='author')
- self.assertOperationAttributes(changes, 'testapp', 0, 2, name='Publisher')
- self.assertOperationFieldAttributes(changes, 'testapp', 0, 1, max_length=100)
- def test_non_circular_foreignkey_dependency_removal(self):
- """
- If two models with a ForeignKey from one to the other are removed at the
- same time, the autodetector should remove them in the correct order.
- """
- changes = self.get_changes([self.author_with_publisher, self.publisher_with_author], [])
-
- self.assertNumberMigrations(changes, "testapp", 1)
- self.assertOperationTypes(changes, "testapp", 0, ["RemoveField", "RemoveField", "DeleteModel", "DeleteModel"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="publisher", model_name='author')
- self.assertOperationAttributes(changes, "testapp", 0, 1, name="author", model_name='publisher')
- self.assertOperationAttributes(changes, "testapp", 0, 2, name="Author")
- self.assertOperationAttributes(changes, "testapp", 0, 3, name="Publisher")
- def test_alter_model_options(self):
- """Changing a model's options should make a change."""
- changes = self.get_changes([self.author_empty], [self.author_with_options])
-
- self.assertNumberMigrations(changes, "testapp", 1)
- self.assertOperationTypes(changes, "testapp", 0, ["AlterModelOptions"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, options={
- "permissions": [('can_hire', 'Can hire')],
- "verbose_name": "Authi",
- })
-
- changes = self.get_changes([self.author_with_options], [self.author_empty])
-
- self.assertNumberMigrations(changes, "testapp", 1)
- self.assertOperationTypes(changes, "testapp", 0, ["AlterModelOptions"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="author", options={})
- def test_alter_model_options_proxy(self):
- """Changing a proxy model's options should also make a change."""
- changes = self.get_changes(
- [self.author_proxy, self.author_empty], [self.author_proxy_options, self.author_empty]
- )
-
- self.assertNumberMigrations(changes, "testapp", 1)
- self.assertOperationTypes(changes, "testapp", 0, ["AlterModelOptions"])
- self.assertOperationAttributes(changes, "testapp", 0, 0, name="authorproxy", options={
- "verbose_name": "Super Author"
- })
- def test_set_alter_order_with_respect_to(self):
- """Setting order_with_respect_to adds a field."""
- changes = self.get_changes([self.book, self.author_with_book], [self.book, self.author_with_book_order_wrt])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AlterOrderWithRespectTo"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="author", order_with_respect_to="book")
- def test_add_alter_order_with_respect_to(self):
- """
- Setting order_with_respect_to when adding the FK too does
- things in the right order.
- """
- changes = self.get_changes([self.author_name], [self.book, self.author_with_book_order_wrt])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AddField", "AlterOrderWithRespectTo"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, model_name="author", name="book")
- self.assertOperationAttributes(changes, 'testapp', 0, 1, name="author", order_with_respect_to="book")
- def test_remove_alter_order_with_respect_to(self):
- """
- Removing order_with_respect_to when removing the FK too does
- things in the right order.
- """
- changes = self.get_changes([self.book, self.author_with_book_order_wrt], [self.author_name])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AlterOrderWithRespectTo", "RemoveField"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="author", order_with_respect_to=None)
- self.assertOperationAttributes(changes, 'testapp', 0, 1, model_name="author", name="book")
- def test_add_model_order_with_respect_to(self):
- """
- Setting order_with_respect_to when adding the whole model
- does things in the right order.
- """
- changes = self.get_changes([], [self.book, self.author_with_book_order_wrt])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel", "AlterOrderWithRespectTo"])
- self.assertOperationAttributes(changes, 'testapp', 0, 1, name="author", order_with_respect_to="book")
- self.assertNotIn("_order", [name for name, field in changes['testapp'][0].operations[0].fields])
- def test_alter_model_managers(self):
- """
- Changing the model managers adds a new operation.
- """
- changes = self.get_changes([self.other_pony], [self.other_pony_food])
-
- self.assertNumberMigrations(changes, 'otherapp', 1)
- self.assertOperationTypes(changes, 'otherapp', 0, ["AlterModelManagers"])
- self.assertOperationAttributes(changes, 'otherapp', 0, 0, name="pony")
- self.assertEqual([name for name, mgr in changes['otherapp'][0].operations[0].managers],
- ['food_qs', 'food_mgr', 'food_mgr_kwargs'])
- self.assertEqual(changes['otherapp'][0].operations[0].managers[1][1].args, ('a', 'b', 1, 2))
- self.assertEqual(changes['otherapp'][0].operations[0].managers[2][1].args, ('x', 'y', 3, 4))
- def test_swappable_first_inheritance(self):
- """Swappable models get their CreateModel first."""
- changes = self.get_changes([], [self.custom_user, self.aardvark])
-
- self.assertNumberMigrations(changes, 'thirdapp', 1)
- self.assertOperationTypes(changes, 'thirdapp', 0, ["CreateModel", "CreateModel"])
- self.assertOperationAttributes(changes, 'thirdapp', 0, 0, name="CustomUser")
- self.assertOperationAttributes(changes, 'thirdapp', 0, 1, name="Aardvark")
- @override_settings(AUTH_USER_MODEL="thirdapp.CustomUser")
- def test_swappable_first_setting(self):
- """Swappable models get their CreateModel first."""
- with isolate_lru_cache(apps.get_swappable_settings_name):
- changes = self.get_changes([], [self.custom_user_no_inherit, self.aardvark])
-
- self.assertNumberMigrations(changes, 'thirdapp', 1)
- self.assertOperationTypes(changes, 'thirdapp', 0, ["CreateModel", "CreateModel"])
- self.assertOperationAttributes(changes, 'thirdapp', 0, 0, name="CustomUser")
- self.assertOperationAttributes(changes, 'thirdapp', 0, 1, name="Aardvark")
- def test_bases_first(self):
- """Bases of other models come first."""
- changes = self.get_changes([], [self.aardvark_based_on_author, self.author_name])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel", "CreateModel"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="Author")
- self.assertOperationAttributes(changes, 'testapp', 0, 1, name="Aardvark")
- def test_multiple_bases(self):
- """#23956 - Inheriting models doesn't move *_ptr fields into AddField operations."""
- A = ModelState("app", "A", [("a_id", models.AutoField(primary_key=True))])
- B = ModelState("app", "B", [("b_id", models.AutoField(primary_key=True))])
- C = ModelState("app", "C", [], bases=("app.A", "app.B"))
- D = ModelState("app", "D", [], bases=("app.A", "app.B"))
- E = ModelState("app", "E", [], bases=("app.A", "app.B"))
- changes = self.get_changes([], [A, B, C, D, E])
-
- self.assertNumberMigrations(changes, "app", 1)
- self.assertOperationTypes(changes, "app", 0, [
- "CreateModel", "CreateModel", "CreateModel", "CreateModel", "CreateModel"
- ])
- self.assertOperationAttributes(changes, "app", 0, 0, name="A")
- self.assertOperationAttributes(changes, "app", 0, 1, name="B")
- self.assertOperationAttributes(changes, "app", 0, 2, name="C")
- self.assertOperationAttributes(changes, "app", 0, 3, name="D")
- self.assertOperationAttributes(changes, "app", 0, 4, name="E")
- def test_proxy_bases_first(self):
- """Bases of proxies come first."""
- changes = self.get_changes([], [self.author_empty, self.author_proxy, self.author_proxy_proxy])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel", "CreateModel", "CreateModel"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="Author")
- self.assertOperationAttributes(changes, 'testapp', 0, 1, name="AuthorProxy")
- self.assertOperationAttributes(changes, 'testapp', 0, 2, name="AAuthorProxyProxy")
- def test_pk_fk_included(self):
- """
- A relation used as the primary key is kept as part of CreateModel.
- """
- changes = self.get_changes([], [self.aardvark_pk_fk_author, self.author_name])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel", "CreateModel"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="Author")
- self.assertOperationAttributes(changes, 'testapp', 0, 1, name="Aardvark")
- def test_first_dependency(self):
- """
- A dependency to an app with no migrations uses __first__.
- """
-
- loader = MigrationLoader(connection)
- before = self.make_project_state([])
- after = self.make_project_state([self.book_migrations_fk])
- after.real_apps = ["migrations"]
- autodetector = MigrationAutodetector(before, after)
- changes = autodetector._detect_changes(graph=loader.graph)
-
- self.assertNumberMigrations(changes, 'otherapp', 1)
- self.assertOperationTypes(changes, 'otherapp', 0, ["CreateModel"])
- self.assertOperationAttributes(changes, 'otherapp', 0, 0, name="Book")
- self.assertMigrationDependencies(changes, 'otherapp', 0, [("migrations", "__first__")])
- @override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
- def test_last_dependency(self):
- """
- A dependency to an app with existing migrations uses the
- last migration of that app.
- """
-
- loader = MigrationLoader(connection)
- before = self.make_project_state([])
- after = self.make_project_state([self.book_migrations_fk])
- after.real_apps = ["migrations"]
- autodetector = MigrationAutodetector(before, after)
- changes = autodetector._detect_changes(graph=loader.graph)
-
- self.assertNumberMigrations(changes, 'otherapp', 1)
- self.assertOperationTypes(changes, 'otherapp', 0, ["CreateModel"])
- self.assertOperationAttributes(changes, 'otherapp', 0, 0, name="Book")
- self.assertMigrationDependencies(changes, 'otherapp', 0, [("migrations", "0002_second")])
- def test_alter_fk_before_model_deletion(self):
- """
- ForeignKeys are altered _before_ the model they used to
- refer to are deleted.
- """
- changes = self.get_changes(
- [self.author_name, self.publisher_with_author],
- [self.aardvark_testapp, self.publisher_with_aardvark_author]
- )
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["CreateModel", "AlterField", "DeleteModel"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="Aardvark")
- self.assertOperationAttributes(changes, 'testapp', 0, 1, name="author")
- self.assertOperationAttributes(changes, 'testapp', 0, 2, name="Author")
- def test_fk_dependency_other_app(self):
- """
- #23100 - ForeignKeys correctly depend on other apps' models.
- """
- changes = self.get_changes([self.author_name, self.book], [self.author_with_book, self.book])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AddField"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0, name="book")
- self.assertMigrationDependencies(changes, 'testapp', 0, [("otherapp", "__first__")])
- def test_circular_dependency_mixed_addcreate(self):
- """
- #23315 - The dependency resolver knows to put all CreateModel
- before AddField and not become unsolvable.
- """
- address = ModelState("a", "Address", [
- ("id", models.AutoField(primary_key=True)),
- ("country", models.ForeignKey("b.DeliveryCountry", models.CASCADE)),
- ])
- person = ModelState("a", "Person", [
- ("id", models.AutoField(primary_key=True)),
- ])
- apackage = ModelState("b", "APackage", [
- ("id", models.AutoField(primary_key=True)),
- ("person", models.ForeignKey("a.Person", models.CASCADE)),
- ])
- country = ModelState("b", "DeliveryCountry", [
- ("id", models.AutoField(primary_key=True)),
- ])
- changes = self.get_changes([], [address, person, apackage, country])
-
- self.assertNumberMigrations(changes, 'a', 2)
- self.assertNumberMigrations(changes, 'b', 1)
- self.assertOperationTypes(changes, 'a', 0, ["CreateModel", "CreateModel"])
- self.assertOperationTypes(changes, 'a', 1, ["AddField"])
- self.assertOperationTypes(changes, 'b', 0, ["CreateModel", "CreateModel"])
- @override_settings(AUTH_USER_MODEL="a.Tenant")
- def test_circular_dependency_swappable(self):
- """
- #23322 - The dependency resolver knows to explicitly resolve
- swappable models.
- """
- with isolate_lru_cache(apps.get_swappable_settings_name):
- tenant = ModelState("a", "Tenant", [
- ("id", models.AutoField(primary_key=True)),
- ("primary_address", models.ForeignKey("b.Address", models.CASCADE))],
- bases=(AbstractBaseUser, )
- )
- address = ModelState("b", "Address", [
- ("id", models.AutoField(primary_key=True)),
- ("tenant", models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE)),
- ])
- changes = self.get_changes([], [address, tenant])
-
- self.assertNumberMigrations(changes, 'a', 2)
- self.assertOperationTypes(changes, 'a', 0, ["CreateModel"])
- self.assertOperationTypes(changes, 'a', 1, ["AddField"])
- self.assertMigrationDependencies(changes, 'a', 0, [])
- self.assertMigrationDependencies(changes, 'a', 1, [('a', 'auto_1'), ('b', 'auto_1')])
-
- self.assertNumberMigrations(changes, 'b', 1)
- self.assertOperationTypes(changes, 'b', 0, ["CreateModel"])
- self.assertMigrationDependencies(changes, 'b', 0, [('__setting__', 'AUTH_USER_MODEL')])
- @override_settings(AUTH_USER_MODEL="b.Tenant")
- def test_circular_dependency_swappable2(self):
- """
- #23322 - The dependency resolver knows to explicitly resolve
- swappable models but with the swappable not being the first migrated
- model.
- """
- with isolate_lru_cache(apps.get_swappable_settings_name):
- address = ModelState("a", "Address", [
- ("id", models.AutoField(primary_key=True)),
- ("tenant", models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE)),
- ])
- tenant = ModelState("b", "Tenant", [
- ("id", models.AutoField(primary_key=True)),
- ("primary_address", models.ForeignKey("a.Address", models.CASCADE))],
- bases=(AbstractBaseUser, )
- )
- changes = self.get_changes([], [address, tenant])
-
- self.assertNumberMigrations(changes, 'a', 2)
- self.assertOperationTypes(changes, 'a', 0, ["CreateModel"])
- self.assertOperationTypes(changes, 'a', 1, ["AddField"])
- self.assertMigrationDependencies(changes, 'a', 0, [])
- self.assertMigrationDependencies(changes, 'a', 1, [('__setting__', 'AUTH_USER_MODEL'), ('a', 'auto_1')])
-
- self.assertNumberMigrations(changes, 'b', 1)
- self.assertOperationTypes(changes, 'b', 0, ["CreateModel"])
- self.assertMigrationDependencies(changes, 'b', 0, [('a', 'auto_1')])
- @override_settings(AUTH_USER_MODEL="a.Person")
- def test_circular_dependency_swappable_self(self):
- """
- #23322 - The dependency resolver knows to explicitly resolve
- swappable models.
- """
- with isolate_lru_cache(apps.get_swappable_settings_name):
- person = ModelState("a", "Person", [
- ("id", models.AutoField(primary_key=True)),
- ("parent1", models.ForeignKey(settings.AUTH_USER_MODEL, models.CASCADE, related_name='children'))
- ])
- changes = self.get_changes([], [person])
-
- self.assertNumberMigrations(changes, 'a', 1)
- self.assertOperationTypes(changes, 'a', 0, ["CreateModel"])
- self.assertMigrationDependencies(changes, 'a', 0, [])
- @mock.patch('django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition',
- side_effect=AssertionError("Should not have prompted for not null addition"))
- def test_add_blank_textfield_and_charfield(self, mocked_ask_method):
- """
- #23405 - Adding a NOT NULL and blank `CharField` or `TextField`
- without default should not prompt for a default.
- """
- changes = self.get_changes([self.author_empty], [self.author_with_biography_blank])
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AddField", "AddField"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0)
- @mock.patch('django.db.migrations.questioner.MigrationQuestioner.ask_not_null_addition')
- def test_add_non_blank_textfield_and_charfield(self, mocked_ask_method):
- """
- #23405 - Adding a NOT NULL and non-blank `CharField` or `TextField`
- without default should prompt for a default.
- """
- changes = self.get_changes([self.author_empty], [self.author_with_biography_non_blank])
- self.assertEqual(mocked_ask_method.call_count, 2)
-
- self.assertNumberMigrations(changes, 'testapp', 1)
- self.assertOperationTypes(changes, 'testapp', 0, ["AddField", "AddField"])
- self.assertOperationAttributes(changes, 'testapp', 0, 0)
|