123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256225722582259226022612262226322642265226622672268226922702271227222732274227522762277227822792280228122822283228422852286228722882289229022912292229322942295229622972298229923002301230223032304230523062307230823092310231123122313231423152316231723182319232023212322232323242325232623272328232923302331233223332334233523362337233823392340234123422343234423452346234723482349235023512352235323542355235623572358235923602361236223632364236523662367236823692370237123722373237423752376237723782379238023812382238323842385238623872388238923902391239223932394239523962397239823992400240124022403240424052406240724082409241024112412241324142415241624172418241924202421242224232424242524262427242824292430243124322433243424352436243724382439244024412442244324442445244624472448244924502451245224532454245524562457245824592460246124622463246424652466246724682469247024712472247324742475247624772478247924802481248224832484248524862487248824892490249124922493249424952496249724982499250025012502250325042505250625072508250925102511251225132514251525162517251825192520252125222523252425252526252725282529253025312532253325342535253625372538253925402541254225432544254525462547254825492550255125522553255425552556255725582559256025612562256325642565256625672568256925702571257225732574257525762577257825792580258125822583258425852586258725882589259025912592259325942595259625972598259926002601260226032604260526062607260826092610261126122613261426152616261726182619262026212622262326242625262626272628262926302631263226332634263526362637263826392640264126422643264426452646264726482649265026512652265326542655265626572658265926602661266226632664266526662667266826692670267126722673267426752676267726782679268026812682268326842685268626872688268926902691269226932694269526962697269826992700270127022703270427052706270727082709271027112712271327142715271627172718271927202721272227232724272527262727272827292730273127322733273427352736273727382739274027412742274327442745274627472748274927502751275227532754275527562757275827592760276127622763276427652766276727682769277027712772277327742775277627772778277927802781278227832784278527862787278827892790279127922793279427952796279727982799280028012802280328042805280628072808280928102811281228132814281528162817281828192820282128222823282428252826282728282829 |
- import unittest
- from django.core.exceptions import FieldDoesNotExist
- from django.db import connection, migrations, models, transaction
- from django.db.migrations.migration import Migration
- from django.db.migrations.operations import CreateModel
- from django.db.migrations.operations.fields import FieldOperation
- from django.db.migrations.state import ModelState, ProjectState
- from django.db.models.fields import NOT_PROVIDED
- from django.db.transaction import atomic
- from django.db.utils import IntegrityError
- from django.test import SimpleTestCase, override_settings, skipUnlessDBFeature
- from .models import FoodManager, FoodQuerySet, UnicodeModel
- from .test_base import MigrationTestBase
- try:
- import sqlparse
- except ImportError:
- sqlparse = None
- class Mixin:
- pass
- class OperationTestBase(MigrationTestBase):
- """
- Common functions to help test operations.
- """
- def apply_operations(self, app_label, project_state, operations, atomic=True):
- migration = Migration('name', app_label)
- migration.operations = operations
- with connection.schema_editor(atomic=atomic) as editor:
- return migration.apply(project_state, editor)
- def unapply_operations(self, app_label, project_state, operations, atomic=True):
- migration = Migration('name', app_label)
- migration.operations = operations
- with connection.schema_editor(atomic=atomic) as editor:
- return migration.unapply(project_state, editor)
- def make_test_state(self, app_label, operation, **kwargs):
- """
- Makes a test state using set_up_test_model and returns the
- original state and the state after the migration is applied.
- """
- project_state = self.set_up_test_model(app_label, **kwargs)
- new_state = project_state.clone()
- operation.state_forwards(app_label, new_state)
- return project_state, new_state
- def set_up_test_model(
- self, app_label, second_model=False, third_model=False, index=False, multicol_index=False,
- related_model=False, mti_model=False, proxy_model=False, manager_model=False,
- unique_together=False, options=False, db_table=None, index_together=False, check_constraint=False):
- """
- Creates a test model state and database table.
- """
-
- table_names = [
-
- '_pony_stables', '_pony_vans',
-
- '_pony', '_stable', '_van',
- ]
- tables = [(app_label + table_name) for table_name in table_names]
- with connection.cursor() as cursor:
- table_names = connection.introspection.table_names(cursor)
- connection.disable_constraint_checking()
- sql_delete_table = connection.schema_editor().sql_delete_table
- with transaction.atomic():
- for table in tables:
- if table in table_names:
- cursor.execute(sql_delete_table % {
- "table": connection.ops.quote_name(table),
- })
- connection.enable_constraint_checking()
-
- model_options = {
- "swappable": "TEST_SWAP_MODEL",
- "index_together": [["weight", "pink"]] if index_together else [],
- "unique_together": [["pink", "weight"]] if unique_together else [],
- }
- if options:
- model_options["permissions"] = [("can_groom", "Can groom")]
- if db_table:
- model_options["db_table"] = db_table
- operations = [migrations.CreateModel(
- "Pony",
- [
- ("id", models.AutoField(primary_key=True)),
- ("pink", models.IntegerField(default=3)),
- ("weight", models.FloatField()),
- ],
- options=model_options,
- )]
- if index:
- operations.append(migrations.AddIndex(
- "Pony",
- models.Index(fields=["pink"], name="pony_pink_idx")
- ))
- if multicol_index:
- operations.append(migrations.AddIndex(
- "Pony",
- models.Index(fields=["pink", "weight"], name="pony_test_idx")
- ))
- if check_constraint:
- operations.append(migrations.AddConstraint(
- "Pony",
- models.CheckConstraint(check=models.Q(pink__gt=2), name="pony_test_constraint")
- ))
- if second_model:
- operations.append(migrations.CreateModel(
- "Stable",
- [
- ("id", models.AutoField(primary_key=True)),
- ]
- ))
- if third_model:
- operations.append(migrations.CreateModel(
- "Van",
- [
- ("id", models.AutoField(primary_key=True)),
- ]
- ))
- if related_model:
- operations.append(migrations.CreateModel(
- "Rider",
- [
- ("id", models.AutoField(primary_key=True)),
- ("pony", models.ForeignKey("Pony", models.CASCADE)),
- ("friend", models.ForeignKey("self", models.CASCADE))
- ],
- ))
- if mti_model:
- operations.append(migrations.CreateModel(
- "ShetlandPony",
- fields=[
- ('pony_ptr', models.OneToOneField(
- 'Pony',
- models.CASCADE,
- auto_created=True,
- parent_link=True,
- primary_key=True,
- to_field='id',
- serialize=False,
- )),
- ("cuteness", models.IntegerField(default=1)),
- ],
- bases=['%s.Pony' % app_label],
- ))
- if proxy_model:
- operations.append(migrations.CreateModel(
- "ProxyPony",
- fields=[],
- options={"proxy": True},
- bases=['%s.Pony' % app_label],
- ))
- if manager_model:
- operations.append(migrations.CreateModel(
- "Food",
- fields=[
- ("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)),
- ]
- ))
- return self.apply_operations(app_label, ProjectState(), operations)
- class OperationTests(OperationTestBase):
- """
- Tests running the operations and making sure they do what they say they do.
- Each test looks at their state changing, and then their database operation -
- both forwards and backwards.
- """
- def test_create_model(self):
- """
- Tests the CreateModel operation.
- Most other tests use this operation as part of setup, so check failures here first.
- """
- operation = migrations.CreateModel(
- "Pony",
- [
- ("id", models.AutoField(primary_key=True)),
- ("pink", models.IntegerField(default=1)),
- ],
- )
- self.assertEqual(operation.describe(), "Create model Pony")
-
- project_state = ProjectState()
- new_state = project_state.clone()
- operation.state_forwards("test_crmo", new_state)
- self.assertEqual(new_state.models["test_crmo", "pony"].name, "Pony")
- self.assertEqual(len(new_state.models["test_crmo", "pony"].fields), 2)
-
- self.assertTableNotExists("test_crmo_pony")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_crmo", editor, project_state, new_state)
- self.assertTableExists("test_crmo_pony")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_crmo", editor, new_state, project_state)
- self.assertTableNotExists("test_crmo_pony")
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "CreateModel")
- self.assertEqual(definition[1], [])
- self.assertEqual(sorted(definition[2]), ["fields", "name"])
-
- operation = migrations.CreateModel("Foo", fields=[], managers=[("objects", models.Manager())])
- definition = operation.deconstruct()
- self.assertNotIn('managers', definition[2])
- def test_create_model_with_duplicate_field_name(self):
- with self.assertRaisesMessage(ValueError, 'Found duplicate value pink in CreateModel fields argument.'):
- migrations.CreateModel(
- "Pony",
- [
- ("id", models.AutoField(primary_key=True)),
- ("pink", models.TextField()),
- ("pink", models.IntegerField(default=1)),
- ],
- )
- def test_create_model_with_duplicate_base(self):
- message = 'Found duplicate value test_crmo.pony in CreateModel bases argument.'
- with self.assertRaisesMessage(ValueError, message):
- migrations.CreateModel(
- "Pony",
- fields=[],
- bases=("test_crmo.Pony", "test_crmo.Pony",),
- )
- with self.assertRaisesMessage(ValueError, message):
- migrations.CreateModel(
- "Pony",
- fields=[],
- bases=("test_crmo.Pony", "test_crmo.pony",),
- )
- message = 'Found duplicate value migrations.unicodemodel in CreateModel bases argument.'
- with self.assertRaisesMessage(ValueError, message):
- migrations.CreateModel(
- "Pony",
- fields=[],
- bases=(UnicodeModel, UnicodeModel,),
- )
- with self.assertRaisesMessage(ValueError, message):
- migrations.CreateModel(
- "Pony",
- fields=[],
- bases=(UnicodeModel, 'migrations.unicodemodel',),
- )
- with self.assertRaisesMessage(ValueError, message):
- migrations.CreateModel(
- "Pony",
- fields=[],
- bases=(UnicodeModel, 'migrations.UnicodeModel',),
- )
- message = "Found duplicate value <class 'django.db.models.base.Model'> in CreateModel bases argument."
- with self.assertRaisesMessage(ValueError, message):
- migrations.CreateModel(
- "Pony",
- fields=[],
- bases=(models.Model, models.Model,),
- )
- message = "Found duplicate value <class 'migrations.test_operations.Mixin'> in CreateModel bases argument."
- with self.assertRaisesMessage(ValueError, message):
- migrations.CreateModel(
- "Pony",
- fields=[],
- bases=(Mixin, Mixin,),
- )
- def test_create_model_with_duplicate_manager_name(self):
- with self.assertRaisesMessage(ValueError, 'Found duplicate value objects in CreateModel managers argument.'):
- migrations.CreateModel(
- "Pony",
- fields=[],
- managers=[
- ("objects", models.Manager()),
- ("objects", models.Manager()),
- ],
- )
- def test_create_model_with_unique_after(self):
- """
- Tests the CreateModel operation directly followed by an
- AlterUniqueTogether (bug #22844 - sqlite remake issues)
- """
- operation1 = migrations.CreateModel(
- "Pony",
- [
- ("id", models.AutoField(primary_key=True)),
- ("pink", models.IntegerField(default=1)),
- ],
- )
- operation2 = migrations.CreateModel(
- "Rider",
- [
- ("id", models.AutoField(primary_key=True)),
- ("number", models.IntegerField(default=1)),
- ("pony", models.ForeignKey("test_crmoua.Pony", models.CASCADE)),
- ],
- )
- operation3 = migrations.AlterUniqueTogether(
- "Rider",
- [
- ("number", "pony"),
- ],
- )
-
- project_state = ProjectState()
- self.assertTableNotExists("test_crmoua_pony")
- self.assertTableNotExists("test_crmoua_rider")
- with connection.schema_editor() as editor:
- new_state = project_state.clone()
- operation1.state_forwards("test_crmoua", new_state)
- operation1.database_forwards("test_crmoua", editor, project_state, new_state)
- project_state, new_state = new_state, new_state.clone()
- operation2.state_forwards("test_crmoua", new_state)
- operation2.database_forwards("test_crmoua", editor, project_state, new_state)
- project_state, new_state = new_state, new_state.clone()
- operation3.state_forwards("test_crmoua", new_state)
- operation3.database_forwards("test_crmoua", editor, project_state, new_state)
- self.assertTableExists("test_crmoua_pony")
- self.assertTableExists("test_crmoua_rider")
- def test_create_model_m2m(self):
- """
- Test the creation of a model with a ManyToMany field and the
- auto-created "through" model.
- """
- project_state = self.set_up_test_model("test_crmomm")
- operation = migrations.CreateModel(
- "Stable",
- [
- ("id", models.AutoField(primary_key=True)),
- ("ponies", models.ManyToManyField("Pony", related_name="stables"))
- ]
- )
-
- new_state = project_state.clone()
- operation.state_forwards("test_crmomm", new_state)
-
- self.assertTableNotExists("test_crmomm_stable_ponies")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_crmomm", editor, project_state, new_state)
- self.assertTableExists("test_crmomm_stable")
- self.assertTableExists("test_crmomm_stable_ponies")
- self.assertColumnNotExists("test_crmomm_stable", "ponies")
-
- with atomic():
- Pony = new_state.apps.get_model("test_crmomm", "Pony")
- Stable = new_state.apps.get_model("test_crmomm", "Stable")
- stable = Stable.objects.create()
- p1 = Pony.objects.create(pink=False, weight=4.55)
- p2 = Pony.objects.create(pink=True, weight=5.43)
- stable.ponies.add(p1, p2)
- self.assertEqual(stable.ponies.count(), 2)
- stable.ponies.all().delete()
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_crmomm", editor, new_state, project_state)
- self.assertTableNotExists("test_crmomm_stable")
- self.assertTableNotExists("test_crmomm_stable_ponies")
- def test_create_model_inheritance(self):
- """
- Tests the CreateModel operation on a multi-table inheritance setup.
- """
- project_state = self.set_up_test_model("test_crmoih")
-
- operation = migrations.CreateModel(
- "ShetlandPony",
- [
- ('pony_ptr', models.OneToOneField(
- 'test_crmoih.Pony',
- models.CASCADE,
- auto_created=True,
- primary_key=True,
- to_field='id',
- serialize=False,
- )),
- ("cuteness", models.IntegerField(default=1)),
- ],
- )
- new_state = project_state.clone()
- operation.state_forwards("test_crmoih", new_state)
- self.assertIn(("test_crmoih", "shetlandpony"), new_state.models)
-
- self.assertTableNotExists("test_crmoih_shetlandpony")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_crmoih", editor, project_state, new_state)
- self.assertTableExists("test_crmoih_shetlandpony")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_crmoih", editor, new_state, project_state)
- self.assertTableNotExists("test_crmoih_shetlandpony")
- def test_create_proxy_model(self):
- """
- CreateModel ignores proxy models.
- """
- project_state = self.set_up_test_model("test_crprmo")
-
- operation = migrations.CreateModel(
- "ProxyPony",
- [],
- options={"proxy": True},
- bases=("test_crprmo.Pony",),
- )
- self.assertEqual(operation.describe(), "Create proxy model ProxyPony")
- new_state = project_state.clone()
- operation.state_forwards("test_crprmo", new_state)
- self.assertIn(("test_crprmo", "proxypony"), new_state.models)
-
- self.assertTableNotExists("test_crprmo_proxypony")
- self.assertTableExists("test_crprmo_pony")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_crprmo", editor, project_state, new_state)
- self.assertTableNotExists("test_crprmo_proxypony")
- self.assertTableExists("test_crprmo_pony")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_crprmo", editor, new_state, project_state)
- self.assertTableNotExists("test_crprmo_proxypony")
- self.assertTableExists("test_crprmo_pony")
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "CreateModel")
- self.assertEqual(definition[1], [])
- self.assertEqual(sorted(definition[2]), ["bases", "fields", "name", "options"])
- def test_create_unmanaged_model(self):
- """
- CreateModel ignores unmanaged models.
- """
- project_state = self.set_up_test_model("test_crummo")
-
- operation = migrations.CreateModel(
- "UnmanagedPony",
- [],
- options={"proxy": True},
- bases=("test_crummo.Pony",),
- )
- self.assertEqual(operation.describe(), "Create proxy model UnmanagedPony")
- new_state = project_state.clone()
- operation.state_forwards("test_crummo", new_state)
- self.assertIn(("test_crummo", "unmanagedpony"), new_state.models)
-
- self.assertTableNotExists("test_crummo_unmanagedpony")
- self.assertTableExists("test_crummo_pony")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_crummo", editor, project_state, new_state)
- self.assertTableNotExists("test_crummo_unmanagedpony")
- self.assertTableExists("test_crummo_pony")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_crummo", editor, new_state, project_state)
- self.assertTableNotExists("test_crummo_unmanagedpony")
- self.assertTableExists("test_crummo_pony")
- @skipUnlessDBFeature('supports_table_check_constraints')
- def test_create_model_with_constraint(self):
- where = models.Q(pink__gt=2)
- check_constraint = models.CheckConstraint(check=where, name='test_constraint_pony_pink_gt_2')
- operation = migrations.CreateModel(
- "Pony",
- [
- ("id", models.AutoField(primary_key=True)),
- ("pink", models.IntegerField(default=3)),
- ],
- options={'constraints': [check_constraint]},
- )
-
- project_state = ProjectState()
- new_state = project_state.clone()
- operation.state_forwards("test_crmo", new_state)
- self.assertEqual(len(new_state.models['test_crmo', 'pony'].options['constraints']), 1)
-
- self.assertTableNotExists("test_crmo_pony")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_crmo", editor, project_state, new_state)
- self.assertTableExists("test_crmo_pony")
- with connection.cursor() as cursor:
- with self.assertRaises(IntegrityError):
- cursor.execute("INSERT INTO test_crmo_pony (id, pink) VALUES (1, 1)")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_crmo", editor, new_state, project_state)
- self.assertTableNotExists("test_crmo_pony")
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "CreateModel")
- self.assertEqual(definition[1], [])
- self.assertEqual(definition[2]['options']['constraints'], [check_constraint])
- def test_create_model_managers(self):
- """
- The managers on a model are set.
- """
- project_state = self.set_up_test_model("test_cmoma")
-
- operation = migrations.CreateModel(
- "Food",
- fields=[
- ("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)),
- ]
- )
- self.assertEqual(operation.describe(), "Create model Food")
- new_state = project_state.clone()
- operation.state_forwards("test_cmoma", new_state)
- self.assertIn(("test_cmoma", "food"), new_state.models)
- managers = new_state.models["test_cmoma", "food"].managers
- self.assertEqual(managers[0][0], "food_qs")
- self.assertIsInstance(managers[0][1], models.Manager)
- self.assertEqual(managers[1][0], "food_mgr")
- self.assertIsInstance(managers[1][1], FoodManager)
- self.assertEqual(managers[1][1].args, ("a", "b", 1, 2))
- self.assertEqual(managers[2][0], "food_mgr_kwargs")
- self.assertIsInstance(managers[2][1], FoodManager)
- self.assertEqual(managers[2][1].args, ("x", "y", 3, 4))
- def test_delete_model(self):
- """
- Tests the DeleteModel operation.
- """
- project_state = self.set_up_test_model("test_dlmo")
-
- operation = migrations.DeleteModel("Pony")
- self.assertEqual(operation.describe(), "Delete model Pony")
- new_state = project_state.clone()
- operation.state_forwards("test_dlmo", new_state)
- self.assertNotIn(("test_dlmo", "pony"), new_state.models)
-
- self.assertTableExists("test_dlmo_pony")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_dlmo", editor, project_state, new_state)
- self.assertTableNotExists("test_dlmo_pony")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_dlmo", editor, new_state, project_state)
- self.assertTableExists("test_dlmo_pony")
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "DeleteModel")
- self.assertEqual(definition[1], [])
- self.assertEqual(list(definition[2]), ["name"])
- def test_delete_proxy_model(self):
- """
- Tests the DeleteModel operation ignores proxy models.
- """
- project_state = self.set_up_test_model("test_dlprmo", proxy_model=True)
-
- operation = migrations.DeleteModel("ProxyPony")
- new_state = project_state.clone()
- operation.state_forwards("test_dlprmo", new_state)
- self.assertIn(("test_dlprmo", "proxypony"), project_state.models)
- self.assertNotIn(("test_dlprmo", "proxypony"), new_state.models)
-
- self.assertTableExists("test_dlprmo_pony")
- self.assertTableNotExists("test_dlprmo_proxypony")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_dlprmo", editor, project_state, new_state)
- self.assertTableExists("test_dlprmo_pony")
- self.assertTableNotExists("test_dlprmo_proxypony")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_dlprmo", editor, new_state, project_state)
- self.assertTableExists("test_dlprmo_pony")
- self.assertTableNotExists("test_dlprmo_proxypony")
- def test_delete_mti_model(self):
- project_state = self.set_up_test_model('test_dlmtimo', mti_model=True)
-
- operation = migrations.DeleteModel('ShetlandPony')
- new_state = project_state.clone()
- operation.state_forwards('test_dlmtimo', new_state)
- self.assertIn(('test_dlmtimo', 'shetlandpony'), project_state.models)
- self.assertNotIn(('test_dlmtimo', 'shetlandpony'), new_state.models)
-
- self.assertTableExists('test_dlmtimo_pony')
- self.assertTableExists('test_dlmtimo_shetlandpony')
- self.assertColumnExists('test_dlmtimo_shetlandpony', 'pony_ptr_id')
- with connection.schema_editor() as editor:
- operation.database_forwards('test_dlmtimo', editor, project_state, new_state)
- self.assertTableExists('test_dlmtimo_pony')
- self.assertTableNotExists('test_dlmtimo_shetlandpony')
-
- with connection.schema_editor() as editor:
- operation.database_backwards('test_dlmtimo', editor, new_state, project_state)
- self.assertTableExists('test_dlmtimo_pony')
- self.assertTableExists('test_dlmtimo_shetlandpony')
- self.assertColumnExists('test_dlmtimo_shetlandpony', 'pony_ptr_id')
- def test_rename_model(self):
- """
- Tests the RenameModel operation.
- """
- project_state = self.set_up_test_model("test_rnmo", related_model=True)
-
- operation = migrations.RenameModel("Pony", "Horse")
- self.assertEqual(operation.describe(), "Rename model Pony to Horse")
-
- self.assertIn(("test_rnmo", "pony"), project_state.models)
- self.assertNotIn(("test_rnmo", "horse"), project_state.models)
- self.assertTableExists("test_rnmo_pony")
- self.assertTableNotExists("test_rnmo_horse")
- if connection.features.supports_foreign_keys:
- self.assertFKExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id"))
- self.assertFKNotExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id"))
-
- new_state = project_state.clone()
- atomic_rename = connection.features.supports_atomic_references_rename
- new_state = self.apply_operations("test_rnmo", new_state, [operation], atomic=atomic_rename)
-
- self.assertNotIn(("test_rnmo", "pony"), new_state.models)
- self.assertIn(("test_rnmo", "horse"), new_state.models)
-
- self.assertEqual("test_rnmo.Horse", new_state.models["test_rnmo", "rider"].fields[1][1].remote_field.model)
- self.assertTableNotExists("test_rnmo_pony")
- self.assertTableExists("test_rnmo_horse")
- if connection.features.supports_foreign_keys:
- self.assertFKNotExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id"))
- self.assertFKExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id"))
-
- original_state = self.unapply_operations("test_rnmo", project_state, [operation], atomic=atomic_rename)
-
- self.assertIn(("test_rnmo", "pony"), original_state.models)
- self.assertNotIn(("test_rnmo", "horse"), original_state.models)
- self.assertEqual("Pony", original_state.models["test_rnmo", "rider"].fields[1][1].remote_field.model)
- self.assertTableExists("test_rnmo_pony")
- self.assertTableNotExists("test_rnmo_horse")
- if connection.features.supports_foreign_keys:
- self.assertFKExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_pony", "id"))
- self.assertFKNotExists("test_rnmo_rider", ["pony_id"], ("test_rnmo_horse", "id"))
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "RenameModel")
- self.assertEqual(definition[1], [])
- self.assertEqual(definition[2], {'old_name': "Pony", 'new_name': "Horse"})
- def test_rename_model_state_forwards(self):
- """
- RenameModel operations shouldn't trigger the caching of rendered apps
- on state without prior apps.
- """
- state = ProjectState()
- state.add_model(ModelState('migrations', 'Foo', []))
- operation = migrations.RenameModel('Foo', 'Bar')
- operation.state_forwards('migrations', state)
- self.assertNotIn('apps', state.__dict__)
- self.assertNotIn(('migrations', 'foo'), state.models)
- self.assertIn(('migrations', 'bar'), state.models)
-
- apps = state.apps
- operation = migrations.RenameModel('Bar', 'Foo')
- operation.state_forwards('migrations', state)
- self.assertIs(state.apps, apps)
- self.assertNotIn(('migrations', 'bar'), state.models)
- self.assertIn(('migrations', 'foo'), state.models)
- def test_rename_model_with_self_referential_fk(self):
- """
- Tests the RenameModel operation on model with self referential FK.
- """
- project_state = self.set_up_test_model("test_rmwsrf", related_model=True)
-
- operation = migrations.RenameModel("Rider", "HorseRider")
- self.assertEqual(operation.describe(), "Rename model Rider to HorseRider")
- new_state = project_state.clone()
- operation.state_forwards("test_rmwsrf", new_state)
- self.assertNotIn(("test_rmwsrf", "rider"), new_state.models)
- self.assertIn(("test_rmwsrf", "horserider"), new_state.models)
-
- self.assertEqual(
- 'self',
- new_state.models["test_rmwsrf", "horserider"].fields[2][1].remote_field.model
- )
- HorseRider = new_state.apps.get_model('test_rmwsrf', 'horserider')
- self.assertIs(HorseRider._meta.get_field('horserider').remote_field.model, HorseRider)
-
- self.assertTableExists("test_rmwsrf_rider")
- self.assertTableNotExists("test_rmwsrf_horserider")
- if connection.features.supports_foreign_keys:
- self.assertFKExists("test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_rider", "id"))
- self.assertFKNotExists("test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_horserider", "id"))
- atomic_rename = connection.features.supports_atomic_references_rename
- with connection.schema_editor(atomic=atomic_rename) as editor:
- operation.database_forwards("test_rmwsrf", editor, project_state, new_state)
- self.assertTableNotExists("test_rmwsrf_rider")
- self.assertTableExists("test_rmwsrf_horserider")
- if connection.features.supports_foreign_keys:
- self.assertFKNotExists("test_rmwsrf_horserider", ["friend_id"], ("test_rmwsrf_rider", "id"))
- self.assertFKExists("test_rmwsrf_horserider", ["friend_id"], ("test_rmwsrf_horserider", "id"))
-
- with connection.schema_editor(atomic=atomic_rename) as editor:
- operation.database_backwards("test_rmwsrf", editor, new_state, project_state)
- self.assertTableExists("test_rmwsrf_rider")
- self.assertTableNotExists("test_rmwsrf_horserider")
- if connection.features.supports_foreign_keys:
- self.assertFKExists("test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_rider", "id"))
- self.assertFKNotExists("test_rmwsrf_rider", ["friend_id"], ("test_rmwsrf_horserider", "id"))
- def test_rename_model_with_superclass_fk(self):
- """
- Tests the RenameModel operation on a model which has a superclass that
- has a foreign key.
- """
- project_state = self.set_up_test_model("test_rmwsc", related_model=True, mti_model=True)
-
- operation = migrations.RenameModel("ShetlandPony", "LittleHorse")
- self.assertEqual(operation.describe(), "Rename model ShetlandPony to LittleHorse")
- new_state = project_state.clone()
- operation.state_forwards("test_rmwsc", new_state)
- self.assertNotIn(("test_rmwsc", "shetlandpony"), new_state.models)
- self.assertIn(("test_rmwsc", "littlehorse"), new_state.models)
-
- self.assertEqual(
- project_state.models["test_rmwsc", "rider"].fields[1][1].remote_field.model,
- new_state.models["test_rmwsc", "rider"].fields[1][1].remote_field.model
- )
-
- self.assertTableExists("test_rmwsc_shetlandpony")
- self.assertTableNotExists("test_rmwsc_littlehorse")
- if connection.features.supports_foreign_keys:
-
- self.assertFKExists("test_rmwsc_rider", ["pony_id"], ("test_rmwsc_pony", "id"))
- self.assertFKNotExists("test_rmwsc_rider", ["pony_id"], ("test_rmwsc_shetlandpony", "id"))
- with connection.schema_editor(atomic=connection.features.supports_atomic_references_rename) as editor:
- operation.database_forwards("test_rmwsc", editor, project_state, new_state)
-
- self.assertTableNotExists("test_rmwsc_shetlandpony")
- self.assertTableExists("test_rmwsc_littlehorse")
- if connection.features.supports_foreign_keys:
-
- self.assertFKExists("test_rmwsc_rider", ["pony_id"], ("test_rmwsc_pony", "id"))
- self.assertFKNotExists("test_rmwsc_rider", ["pony_id"], ("test_rmwsc_littlehorse", "id"))
- def test_rename_model_with_self_referential_m2m(self):
- app_label = "test_rename_model_with_self_referential_m2m"
- project_state = self.apply_operations(app_label, ProjectState(), operations=[
- migrations.CreateModel("ReflexivePony", fields=[
- ("id", models.AutoField(primary_key=True)),
- ("ponies", models.ManyToManyField("self")),
- ]),
- ])
- project_state = self.apply_operations(app_label, project_state, operations=[
- migrations.RenameModel("ReflexivePony", "ReflexivePony2"),
- ], atomic=connection.features.supports_atomic_references_rename)
- Pony = project_state.apps.get_model(app_label, "ReflexivePony2")
- pony = Pony.objects.create()
- pony.ponies.add(pony)
- def test_rename_model_with_m2m(self):
- app_label = "test_rename_model_with_m2m"
- project_state = self.apply_operations(app_label, ProjectState(), operations=[
- migrations.CreateModel("Rider", fields=[
- ("id", models.AutoField(primary_key=True)),
- ]),
- migrations.CreateModel("Pony", fields=[
- ("id", models.AutoField(primary_key=True)),
- ("riders", models.ManyToManyField("Rider")),
- ]),
- ])
- Pony = project_state.apps.get_model(app_label, "Pony")
- Rider = project_state.apps.get_model(app_label, "Rider")
- pony = Pony.objects.create()
- rider = Rider.objects.create()
- pony.riders.add(rider)
- project_state = self.apply_operations(app_label, project_state, operations=[
- migrations.RenameModel("Pony", "Pony2"),
- ], atomic=connection.features.supports_atomic_references_rename)
- Pony = project_state.apps.get_model(app_label, "Pony2")
- Rider = project_state.apps.get_model(app_label, "Rider")
- pony = Pony.objects.create()
- rider = Rider.objects.create()
- pony.riders.add(rider)
- self.assertEqual(Pony.objects.count(), 2)
- self.assertEqual(Rider.objects.count(), 2)
- self.assertEqual(Pony._meta.get_field('riders').remote_field.through.objects.count(), 2)
- def test_rename_m2m_target_model(self):
- app_label = "test_rename_m2m_target_model"
- project_state = self.apply_operations(app_label, ProjectState(), operations=[
- migrations.CreateModel("Rider", fields=[
- ("id", models.AutoField(primary_key=True)),
- ]),
- migrations.CreateModel("Pony", fields=[
- ("id", models.AutoField(primary_key=True)),
- ("riders", models.ManyToManyField("Rider")),
- ]),
- ])
- Pony = project_state.apps.get_model(app_label, "Pony")
- Rider = project_state.apps.get_model(app_label, "Rider")
- pony = Pony.objects.create()
- rider = Rider.objects.create()
- pony.riders.add(rider)
- project_state = self.apply_operations(app_label, project_state, operations=[
- migrations.RenameModel("Rider", "Rider2"),
- ], atomic=connection.features.supports_atomic_references_rename)
- Pony = project_state.apps.get_model(app_label, "Pony")
- Rider = project_state.apps.get_model(app_label, "Rider2")
- pony = Pony.objects.create()
- rider = Rider.objects.create()
- pony.riders.add(rider)
- self.assertEqual(Pony.objects.count(), 2)
- self.assertEqual(Rider.objects.count(), 2)
- self.assertEqual(Pony._meta.get_field('riders').remote_field.through.objects.count(), 2)
- def test_rename_m2m_through_model(self):
- app_label = "test_rename_through"
- project_state = self.apply_operations(app_label, ProjectState(), operations=[
- migrations.CreateModel("Rider", fields=[
- ("id", models.AutoField(primary_key=True)),
- ]),
- migrations.CreateModel("Pony", fields=[
- ("id", models.AutoField(primary_key=True)),
- ]),
- migrations.CreateModel("PonyRider", fields=[
- ("id", models.AutoField(primary_key=True)),
- ("rider", models.ForeignKey("test_rename_through.Rider", models.CASCADE)),
- ("pony", models.ForeignKey("test_rename_through.Pony", models.CASCADE)),
- ]),
- migrations.AddField(
- "Pony",
- "riders",
- models.ManyToManyField("test_rename_through.Rider", through="test_rename_through.PonyRider"),
- ),
- ])
- Pony = project_state.apps.get_model(app_label, "Pony")
- Rider = project_state.apps.get_model(app_label, "Rider")
- PonyRider = project_state.apps.get_model(app_label, "PonyRider")
- pony = Pony.objects.create()
- rider = Rider.objects.create()
- PonyRider.objects.create(pony=pony, rider=rider)
- project_state = self.apply_operations(app_label, project_state, operations=[
- migrations.RenameModel("PonyRider", "PonyRider2"),
- ])
- Pony = project_state.apps.get_model(app_label, "Pony")
- Rider = project_state.apps.get_model(app_label, "Rider")
- PonyRider = project_state.apps.get_model(app_label, "PonyRider2")
- pony = Pony.objects.first()
- rider = Rider.objects.create()
- PonyRider.objects.create(pony=pony, rider=rider)
- self.assertEqual(Pony.objects.count(), 1)
- self.assertEqual(Rider.objects.count(), 2)
- self.assertEqual(PonyRider.objects.count(), 2)
- self.assertEqual(pony.riders.count(), 2)
- def test_rename_m2m_model_after_rename_field(self):
- """RenameModel renames a many-to-many column after a RenameField."""
- app_label = 'test_rename_multiple'
- project_state = self.apply_operations(app_label, ProjectState(), operations=[
- migrations.CreateModel('Pony', fields=[
- ('id', models.AutoField(primary_key=True)),
- ('name', models.CharField(max_length=20)),
- ]),
- migrations.CreateModel('Rider', fields=[
- ('id', models.AutoField(primary_key=True)),
- ('pony', models.ForeignKey('test_rename_multiple.Pony', models.CASCADE)),
- ]),
- migrations.CreateModel('PonyRider', fields=[
- ('id', models.AutoField(primary_key=True)),
- ('riders', models.ManyToManyField('Rider')),
- ]),
- migrations.RenameField(model_name='pony', old_name='name', new_name='fancy_name'),
- migrations.RenameModel(old_name='Rider', new_name='Jockey'),
- ], atomic=connection.features.supports_atomic_references_rename)
- Pony = project_state.apps.get_model(app_label, 'Pony')
- Jockey = project_state.apps.get_model(app_label, 'Jockey')
- PonyRider = project_state.apps.get_model(app_label, 'PonyRider')
-
- pony = Pony.objects.create(fancy_name='a good name')
- jockey = Jockey.objects.create(pony=pony)
- ponyrider = PonyRider.objects.create()
- ponyrider.riders.add(jockey)
- def test_add_field(self):
- """
- Tests the AddField operation.
- """
-
- operation = migrations.AddField(
- "Pony",
- "height",
- models.FloatField(null=True, default=5),
- )
- self.assertEqual(operation.describe(), "Add field height to Pony")
- project_state, new_state = self.make_test_state("test_adfl", operation)
- self.assertEqual(len(new_state.models["test_adfl", "pony"].fields), 4)
- field = [
- f for n, f in new_state.models["test_adfl", "pony"].fields
- if n == "height"
- ][0]
- self.assertEqual(field.default, 5)
-
- self.assertColumnNotExists("test_adfl_pony", "height")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_adfl", editor, project_state, new_state)
- self.assertColumnExists("test_adfl_pony", "height")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_adfl", editor, new_state, project_state)
- self.assertColumnNotExists("test_adfl_pony", "height")
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "AddField")
- self.assertEqual(definition[1], [])
- self.assertEqual(sorted(definition[2]), ["field", "model_name", "name"])
- def test_add_charfield(self):
- """
- Tests the AddField operation on TextField.
- """
- project_state = self.set_up_test_model("test_adchfl")
- Pony = project_state.apps.get_model("test_adchfl", "Pony")
- pony = Pony.objects.create(weight=42)
- new_state = self.apply_operations("test_adchfl", project_state, [
- migrations.AddField(
- "Pony",
- "text",
- models.CharField(max_length=10, default="some text"),
- ),
- migrations.AddField(
- "Pony",
- "empty",
- models.CharField(max_length=10, default=""),
- ),
-
- migrations.AddField(
- "Pony",
- "digits",
- models.CharField(max_length=10, default="42"),
- ),
-
- migrations.AddField(
- "Pony",
- "quotes",
- models.CharField(max_length=10, default='"\'"'),
- ),
- ])
- Pony = new_state.apps.get_model("test_adchfl", "Pony")
- pony = Pony.objects.get(pk=pony.pk)
- self.assertEqual(pony.text, "some text")
- self.assertEqual(pony.empty, "")
- self.assertEqual(pony.digits, "42")
- self.assertEqual(pony.quotes, '"\'"')
- def test_add_textfield(self):
- """
- Tests the AddField operation on TextField.
- """
- project_state = self.set_up_test_model("test_adtxtfl")
- Pony = project_state.apps.get_model("test_adtxtfl", "Pony")
- pony = Pony.objects.create(weight=42)
- new_state = self.apply_operations("test_adtxtfl", project_state, [
- migrations.AddField(
- "Pony",
- "text",
- models.TextField(default="some text"),
- ),
- migrations.AddField(
- "Pony",
- "empty",
- models.TextField(default=""),
- ),
-
- migrations.AddField(
- "Pony",
- "digits",
- models.TextField(default="42"),
- ),
-
- migrations.AddField(
- "Pony",
- "quotes",
- models.TextField(default='"\'"'),
- ),
- ])
- Pony = new_state.apps.get_model("test_adtxtfl", "Pony")
- pony = Pony.objects.get(pk=pony.pk)
- self.assertEqual(pony.text, "some text")
- self.assertEqual(pony.empty, "")
- self.assertEqual(pony.digits, "42")
- self.assertEqual(pony.quotes, '"\'"')
- def test_add_binaryfield(self):
- """
- Tests the AddField operation on TextField/BinaryField.
- """
- project_state = self.set_up_test_model("test_adbinfl")
- Pony = project_state.apps.get_model("test_adbinfl", "Pony")
- pony = Pony.objects.create(weight=42)
- new_state = self.apply_operations("test_adbinfl", project_state, [
- migrations.AddField(
- "Pony",
- "blob",
- models.BinaryField(default=b"some text"),
- ),
- migrations.AddField(
- "Pony",
- "empty",
- models.BinaryField(default=b""),
- ),
-
- migrations.AddField(
- "Pony",
- "digits",
- models.BinaryField(default=b"42"),
- ),
-
- migrations.AddField(
- "Pony",
- "quotes",
- models.BinaryField(default=b'"\'"'),
- ),
- ])
- Pony = new_state.apps.get_model("test_adbinfl", "Pony")
- pony = Pony.objects.get(pk=pony.pk)
-
- self.assertEqual(bytes(pony.blob), b"some text")
- self.assertEqual(bytes(pony.empty), b"")
- self.assertEqual(bytes(pony.digits), b"42")
- self.assertEqual(bytes(pony.quotes), b'"\'"')
- def test_column_name_quoting(self):
- """
- Column names that are SQL keywords shouldn't cause problems when used
- in migrations (#22168).
- """
- project_state = self.set_up_test_model("test_regr22168")
- operation = migrations.AddField(
- "Pony",
- "order",
- models.IntegerField(default=0),
- )
- new_state = project_state.clone()
- operation.state_forwards("test_regr22168", new_state)
- with connection.schema_editor() as editor:
- operation.database_forwards("test_regr22168", editor, project_state, new_state)
- self.assertColumnExists("test_regr22168_pony", "order")
- def test_add_field_preserve_default(self):
- """
- Tests the AddField operation's state alteration
- when preserve_default = False.
- """
- project_state = self.set_up_test_model("test_adflpd")
-
- operation = migrations.AddField(
- "Pony",
- "height",
- models.FloatField(null=True, default=4),
- preserve_default=False,
- )
- new_state = project_state.clone()
- operation.state_forwards("test_adflpd", new_state)
- self.assertEqual(len(new_state.models["test_adflpd", "pony"].fields), 4)
- field = [
- f for n, f in new_state.models["test_adflpd", "pony"].fields
- if n == "height"
- ][0]
- self.assertEqual(field.default, NOT_PROVIDED)
-
- project_state.apps.get_model("test_adflpd", "pony").objects.create(
- weight=4,
- )
- self.assertColumnNotExists("test_adflpd_pony", "height")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_adflpd", editor, project_state, new_state)
- self.assertColumnExists("test_adflpd_pony", "height")
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "AddField")
- self.assertEqual(definition[1], [])
- self.assertEqual(sorted(definition[2]), ["field", "model_name", "name", "preserve_default"])
- def test_add_field_m2m(self):
- """
- Tests the AddField operation with a ManyToManyField.
- """
- project_state = self.set_up_test_model("test_adflmm", second_model=True)
-
- operation = migrations.AddField("Pony", "stables", models.ManyToManyField("Stable", related_name="ponies"))
- new_state = project_state.clone()
- operation.state_forwards("test_adflmm", new_state)
- self.assertEqual(len(new_state.models["test_adflmm", "pony"].fields), 4)
-
- self.assertTableNotExists("test_adflmm_pony_stables")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_adflmm", editor, project_state, new_state)
- self.assertTableExists("test_adflmm_pony_stables")
- self.assertColumnNotExists("test_adflmm_pony", "stables")
-
- with atomic():
- Pony = new_state.apps.get_model("test_adflmm", "Pony")
- p = Pony.objects.create(pink=False, weight=4.55)
- p.stables.create()
- self.assertEqual(p.stables.count(), 1)
- p.stables.all().delete()
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_adflmm", editor, new_state, project_state)
- self.assertTableNotExists("test_adflmm_pony_stables")
- def test_alter_field_m2m(self):
- project_state = self.set_up_test_model("test_alflmm", second_model=True)
- project_state = self.apply_operations("test_alflmm", project_state, operations=[
- migrations.AddField("Pony", "stables", models.ManyToManyField("Stable", related_name="ponies"))
- ])
- Pony = project_state.apps.get_model("test_alflmm", "Pony")
- self.assertFalse(Pony._meta.get_field('stables').blank)
- project_state = self.apply_operations("test_alflmm", project_state, operations=[
- migrations.AlterField(
- "Pony", "stables", models.ManyToManyField(to="Stable", related_name="ponies", blank=True)
- )
- ])
- Pony = project_state.apps.get_model("test_alflmm", "Pony")
- self.assertTrue(Pony._meta.get_field('stables').blank)
- def test_repoint_field_m2m(self):
- project_state = self.set_up_test_model("test_alflmm", second_model=True, third_model=True)
- project_state = self.apply_operations("test_alflmm", project_state, operations=[
- migrations.AddField("Pony", "places", models.ManyToManyField("Stable", related_name="ponies"))
- ])
- Pony = project_state.apps.get_model("test_alflmm", "Pony")
- project_state = self.apply_operations("test_alflmm", project_state, operations=[
- migrations.AlterField("Pony", "places", models.ManyToManyField(to="Van", related_name="ponies"))
- ])
-
- Pony = project_state.apps.get_model("test_alflmm", "Pony")
- p = Pony.objects.create(pink=False, weight=4.55)
- p.places.create()
- self.assertEqual(p.places.count(), 1)
- p.places.all().delete()
- def test_remove_field_m2m(self):
- project_state = self.set_up_test_model("test_rmflmm", second_model=True)
- project_state = self.apply_operations("test_rmflmm", project_state, operations=[
- migrations.AddField("Pony", "stables", models.ManyToManyField("Stable", related_name="ponies"))
- ])
- self.assertTableExists("test_rmflmm_pony_stables")
- with_field_state = project_state.clone()
- operations = [migrations.RemoveField("Pony", "stables")]
- project_state = self.apply_operations("test_rmflmm", project_state, operations=operations)
- self.assertTableNotExists("test_rmflmm_pony_stables")
-
- self.unapply_operations("test_rmflmm", with_field_state, operations=operations)
- self.assertTableExists("test_rmflmm_pony_stables")
- def test_remove_field_m2m_with_through(self):
- project_state = self.set_up_test_model("test_rmflmmwt", second_model=True)
- self.assertTableNotExists("test_rmflmmwt_ponystables")
- project_state = self.apply_operations("test_rmflmmwt", project_state, operations=[
- migrations.CreateModel("PonyStables", fields=[
- ("pony", models.ForeignKey('test_rmflmmwt.Pony', models.CASCADE)),
- ("stable", models.ForeignKey('test_rmflmmwt.Stable', models.CASCADE)),
- ]),
- migrations.AddField(
- "Pony", "stables",
- models.ManyToManyField("Stable", related_name="ponies", through='test_rmflmmwt.PonyStables')
- )
- ])
- self.assertTableExists("test_rmflmmwt_ponystables")
- operations = [migrations.RemoveField("Pony", "stables"), migrations.DeleteModel("PonyStables")]
- self.apply_operations("test_rmflmmwt", project_state, operations=operations)
- def test_remove_field(self):
- """
- Tests the RemoveField operation.
- """
- project_state = self.set_up_test_model("test_rmfl")
-
- operation = migrations.RemoveField("Pony", "pink")
- self.assertEqual(operation.describe(), "Remove field pink from Pony")
- new_state = project_state.clone()
- operation.state_forwards("test_rmfl", new_state)
- self.assertEqual(len(new_state.models["test_rmfl", "pony"].fields), 2)
-
- self.assertColumnExists("test_rmfl_pony", "pink")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_rmfl", editor, project_state, new_state)
- self.assertColumnNotExists("test_rmfl_pony", "pink")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_rmfl", editor, new_state, project_state)
- self.assertColumnExists("test_rmfl_pony", "pink")
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "RemoveField")
- self.assertEqual(definition[1], [])
- self.assertEqual(definition[2], {'model_name': "Pony", 'name': 'pink'})
- def test_remove_fk(self):
- """
- Tests the RemoveField operation on a foreign key.
- """
- project_state = self.set_up_test_model("test_rfk", related_model=True)
- self.assertColumnExists("test_rfk_rider", "pony_id")
- operation = migrations.RemoveField("Rider", "pony")
- new_state = project_state.clone()
- operation.state_forwards("test_rfk", new_state)
- with connection.schema_editor() as editor:
- operation.database_forwards("test_rfk", editor, project_state, new_state)
- self.assertColumnNotExists("test_rfk_rider", "pony_id")
- with connection.schema_editor() as editor:
- operation.database_backwards("test_rfk", editor, new_state, project_state)
- self.assertColumnExists("test_rfk_rider", "pony_id")
- def test_alter_model_table(self):
- """
- Tests the AlterModelTable operation.
- """
- project_state = self.set_up_test_model("test_almota")
-
- operation = migrations.AlterModelTable("Pony", "test_almota_pony_2")
- self.assertEqual(operation.describe(), "Rename table for Pony to test_almota_pony_2")
- new_state = project_state.clone()
- operation.state_forwards("test_almota", new_state)
- self.assertEqual(new_state.models["test_almota", "pony"].options["db_table"], "test_almota_pony_2")
-
- self.assertTableExists("test_almota_pony")
- self.assertTableNotExists("test_almota_pony_2")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_almota", editor, project_state, new_state)
- self.assertTableNotExists("test_almota_pony")
- self.assertTableExists("test_almota_pony_2")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_almota", editor, new_state, project_state)
- self.assertTableExists("test_almota_pony")
- self.assertTableNotExists("test_almota_pony_2")
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "AlterModelTable")
- self.assertEqual(definition[1], [])
- self.assertEqual(definition[2], {'name': "Pony", 'table': "test_almota_pony_2"})
- def test_alter_model_table_none(self):
- """
- Tests the AlterModelTable operation if the table name is set to None.
- """
- operation = migrations.AlterModelTable("Pony", None)
- self.assertEqual(operation.describe(), "Rename table for Pony to (default)")
- def test_alter_model_table_noop(self):
- """
- Tests the AlterModelTable operation if the table name is not changed.
- """
- project_state = self.set_up_test_model("test_almota")
-
- operation = migrations.AlterModelTable("Pony", "test_almota_pony")
- new_state = project_state.clone()
- operation.state_forwards("test_almota", new_state)
- self.assertEqual(new_state.models["test_almota", "pony"].options["db_table"], "test_almota_pony")
-
- self.assertTableExists("test_almota_pony")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_almota", editor, project_state, new_state)
- self.assertTableExists("test_almota_pony")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_almota", editor, new_state, project_state)
- self.assertTableExists("test_almota_pony")
- def test_alter_model_table_m2m(self):
- """
- AlterModelTable should rename auto-generated M2M tables.
- """
- app_label = "test_talflmltlm2m"
- pony_db_table = 'pony_foo'
- project_state = self.set_up_test_model(app_label, second_model=True, db_table=pony_db_table)
-
- first_state = project_state.clone()
- operation = migrations.AddField("Pony", "stables", models.ManyToManyField("Stable"))
- operation.state_forwards(app_label, first_state)
- with connection.schema_editor() as editor:
- operation.database_forwards(app_label, editor, project_state, first_state)
- original_m2m_table = "%s_%s" % (pony_db_table, "stables")
- new_m2m_table = "%s_%s" % (app_label, "pony_stables")
- self.assertTableExists(original_m2m_table)
- self.assertTableNotExists(new_m2m_table)
-
- second_state = first_state.clone()
- operation = migrations.AlterModelTable(name='pony', table=None)
- operation.state_forwards(app_label, second_state)
- atomic_rename = connection.features.supports_atomic_references_rename
- with connection.schema_editor(atomic=atomic_rename) as editor:
- operation.database_forwards(app_label, editor, first_state, second_state)
- self.assertTableExists(new_m2m_table)
- self.assertTableNotExists(original_m2m_table)
-
- with connection.schema_editor(atomic=atomic_rename) as editor:
- operation.database_backwards(app_label, editor, second_state, first_state)
- self.assertTableExists(original_m2m_table)
- self.assertTableNotExists(new_m2m_table)
- def test_alter_field(self):
- """
- Tests the AlterField operation.
- """
- project_state = self.set_up_test_model("test_alfl")
-
- operation = migrations.AlterField("Pony", "pink", models.IntegerField(null=True))
- self.assertEqual(operation.describe(), "Alter field pink on Pony")
- new_state = project_state.clone()
- operation.state_forwards("test_alfl", new_state)
- self.assertIs(project_state.models["test_alfl", "pony"].get_field_by_name("pink").null, False)
- self.assertIs(new_state.models["test_alfl", "pony"].get_field_by_name("pink").null, True)
-
- self.assertColumnNotNull("test_alfl_pony", "pink")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_alfl", editor, project_state, new_state)
- self.assertColumnNull("test_alfl_pony", "pink")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_alfl", editor, new_state, project_state)
- self.assertColumnNotNull("test_alfl_pony", "pink")
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "AlterField")
- self.assertEqual(definition[1], [])
- self.assertEqual(sorted(definition[2]), ["field", "model_name", "name"])
- def test_alter_field_pk(self):
- """
- Tests the AlterField operation on primary keys (for things like PostgreSQL's SERIAL weirdness)
- """
- project_state = self.set_up_test_model("test_alflpk")
-
- operation = migrations.AlterField("Pony", "id", models.IntegerField(primary_key=True))
- new_state = project_state.clone()
- operation.state_forwards("test_alflpk", new_state)
- self.assertIsInstance(project_state.models["test_alflpk", "pony"].get_field_by_name("id"), models.AutoField)
- self.assertIsInstance(new_state.models["test_alflpk", "pony"].get_field_by_name("id"), models.IntegerField)
-
- with connection.schema_editor() as editor:
- operation.database_forwards("test_alflpk", editor, project_state, new_state)
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_alflpk", editor, new_state, project_state)
- @skipUnlessDBFeature('supports_foreign_keys')
- def test_alter_field_pk_fk(self):
- """
- Tests the AlterField operation on primary keys changes any FKs pointing to it.
- """
- project_state = self.set_up_test_model("test_alflpkfk", related_model=True)
-
- operation = migrations.AlterField("Pony", "id", models.FloatField(primary_key=True))
- new_state = project_state.clone()
- operation.state_forwards("test_alflpkfk", new_state)
- self.assertIsInstance(project_state.models["test_alflpkfk", "pony"].get_field_by_name("id"), models.AutoField)
- self.assertIsInstance(new_state.models["test_alflpkfk", "pony"].get_field_by_name("id"), models.FloatField)
- def assertIdTypeEqualsFkType():
- with connection.cursor() as cursor:
- id_type, id_null = [
- (c.type_code, c.null_ok)
- for c in connection.introspection.get_table_description(cursor, "test_alflpkfk_pony")
- if c.name == "id"
- ][0]
- fk_type, fk_null = [
- (c.type_code, c.null_ok)
- for c in connection.introspection.get_table_description(cursor, "test_alflpkfk_rider")
- if c.name == "pony_id"
- ][0]
- self.assertEqual(id_type, fk_type)
- self.assertEqual(id_null, fk_null)
- assertIdTypeEqualsFkType()
-
- with connection.schema_editor() as editor:
- operation.database_forwards("test_alflpkfk", editor, project_state, new_state)
- assertIdTypeEqualsFkType()
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_alflpkfk", editor, new_state, project_state)
- assertIdTypeEqualsFkType()
- def test_alter_field_reloads_state_on_fk_target_changes(self):
- """
- If AlterField doesn't reload state appropriately, the second AlterField
- crashes on MySQL due to not dropping the PonyRider.pony foreign key
- constraint before modifying the column.
- """
- app_label = 'alter_alter_field_reloads_state_on_fk_target_changes'
- project_state = self.apply_operations(app_label, ProjectState(), operations=[
- migrations.CreateModel('Rider', fields=[
- ('id', models.CharField(primary_key=True, max_length=100)),
- ]),
- migrations.CreateModel('Pony', fields=[
- ('id', models.CharField(primary_key=True, max_length=100)),
- ('rider', models.ForeignKey('%s.Rider' % app_label, models.CASCADE)),
- ]),
- migrations.CreateModel('PonyRider', fields=[
- ('id', models.AutoField(primary_key=True)),
- ('pony', models.ForeignKey('%s.Pony' % app_label, models.CASCADE)),
- ]),
- ])
- project_state = self.apply_operations(app_label, project_state, operations=[
- migrations.AlterField('Rider', 'id', models.CharField(primary_key=True, max_length=99)),
- migrations.AlterField('Pony', 'id', models.CharField(primary_key=True, max_length=99)),
- ])
- def test_alter_field_reloads_state_on_fk_with_to_field_target_changes(self):
- """
- If AlterField doesn't reload state appropriately, the second AlterField
- crashes on MySQL due to not dropping the PonyRider.pony foreign key
- constraint before modifying the column.
- """
- app_label = 'alter_alter_field_reloads_state_on_fk_with_to_field_target_changes'
- project_state = self.apply_operations(app_label, ProjectState(), operations=[
- migrations.CreateModel('Rider', fields=[
- ('id', models.CharField(primary_key=True, max_length=100)),
- ('slug', models.CharField(unique=True, max_length=100)),
- ]),
- migrations.CreateModel('Pony', fields=[
- ('id', models.CharField(primary_key=True, max_length=100)),
- ('rider', models.ForeignKey('%s.Rider' % app_label, models.CASCADE, to_field='slug')),
- ('slug', models.CharField(unique=True, max_length=100)),
- ]),
- migrations.CreateModel('PonyRider', fields=[
- ('id', models.AutoField(primary_key=True)),
- ('pony', models.ForeignKey('%s.Pony' % app_label, models.CASCADE, to_field='slug')),
- ]),
- ])
- project_state = self.apply_operations(app_label, project_state, operations=[
- migrations.AlterField('Rider', 'slug', models.CharField(unique=True, max_length=99)),
- migrations.AlterField('Pony', 'slug', models.CharField(unique=True, max_length=99)),
- ])
- def test_rename_field_reloads_state_on_fk_target_changes(self):
- """
- If RenameField doesn't reload state appropriately, the AlterField
- crashes on MySQL due to not dropping the PonyRider.pony foreign key
- constraint before modifying the column.
- """
- app_label = 'alter_rename_field_reloads_state_on_fk_target_changes'
- project_state = self.apply_operations(app_label, ProjectState(), operations=[
- migrations.CreateModel('Rider', fields=[
- ('id', models.CharField(primary_key=True, max_length=100)),
- ]),
- migrations.CreateModel('Pony', fields=[
- ('id', models.CharField(primary_key=True, max_length=100)),
- ('rider', models.ForeignKey('%s.Rider' % app_label, models.CASCADE)),
- ]),
- migrations.CreateModel('PonyRider', fields=[
- ('id', models.AutoField(primary_key=True)),
- ('pony', models.ForeignKey('%s.Pony' % app_label, models.CASCADE)),
- ]),
- ])
- project_state = self.apply_operations(app_label, project_state, operations=[
- migrations.RenameField('Rider', 'id', 'id2'),
- migrations.AlterField('Pony', 'id', models.CharField(primary_key=True, max_length=99)),
- ], atomic=connection.features.supports_atomic_references_rename)
- def test_rename_field(self):
- """
- Tests the RenameField operation.
- """
- project_state = self.set_up_test_model("test_rnfl", unique_together=True, index_together=True)
-
- operation = migrations.RenameField("Pony", "pink", "blue")
- self.assertEqual(operation.describe(), "Rename field pink on Pony to blue")
- new_state = project_state.clone()
- operation.state_forwards("test_rnfl", new_state)
- self.assertIn("blue", [n for n, f in new_state.models["test_rnfl", "pony"].fields])
- self.assertNotIn("pink", [n for n, f in new_state.models["test_rnfl", "pony"].fields])
-
- self.assertIn("blue", new_state.models["test_rnfl", "pony"].options['unique_together'][0])
- self.assertNotIn("pink", new_state.models["test_rnfl", "pony"].options['unique_together'][0])
-
- self.assertIn("blue", new_state.models["test_rnfl", "pony"].options['index_together'][0])
- self.assertNotIn("pink", new_state.models["test_rnfl", "pony"].options['index_together'][0])
-
- self.assertColumnExists("test_rnfl_pony", "pink")
- self.assertColumnNotExists("test_rnfl_pony", "blue")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_rnfl", editor, project_state, new_state)
- self.assertColumnExists("test_rnfl_pony", "blue")
- self.assertColumnNotExists("test_rnfl_pony", "pink")
-
- with connection.cursor() as cursor:
- cursor.execute("INSERT INTO test_rnfl_pony (blue, weight) VALUES (1, 1)")
- with self.assertRaises(IntegrityError):
- with atomic():
- cursor.execute("INSERT INTO test_rnfl_pony (blue, weight) VALUES (1, 1)")
- cursor.execute("DELETE FROM test_rnfl_pony")
-
- self.assertIndexExists("test_rnfl_pony", ["weight", "blue"])
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_rnfl", editor, new_state, project_state)
- self.assertColumnExists("test_rnfl_pony", "pink")
- self.assertColumnNotExists("test_rnfl_pony", "blue")
-
- self.assertIndexExists("test_rnfl_pony", ["weight", "pink"])
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "RenameField")
- self.assertEqual(definition[1], [])
- self.assertEqual(definition[2], {'model_name': "Pony", 'old_name': "pink", 'new_name': "blue"})
- def test_rename_missing_field(self):
- state = ProjectState()
- state.add_model(ModelState('app', 'model', []))
- with self.assertRaisesMessage(FieldDoesNotExist, "app.model has no field named 'field'"):
- migrations.RenameField('model', 'field', 'new_field').state_forwards('app', state)
- def test_rename_referenced_field_state_forward(self):
- state = ProjectState()
- state.add_model(ModelState('app', 'Model', [
- ('id', models.AutoField(primary_key=True)),
- ('field', models.IntegerField(unique=True)),
- ]))
- state.add_model(ModelState('app', 'OtherModel', [
- ('id', models.AutoField(primary_key=True)),
- ('fk', models.ForeignKey('Model', models.CASCADE, to_field='field')),
- ('fo', models.ForeignObject('Model', models.CASCADE, from_fields=('fk',), to_fields=('field',))),
- ]))
- operation = migrations.RenameField('Model', 'field', 'renamed')
- new_state = state.clone()
- operation.state_forwards('app', new_state)
- self.assertEqual(new_state.models['app', 'othermodel'].fields[1][1].remote_field.field_name, 'renamed')
- self.assertEqual(new_state.models['app', 'othermodel'].fields[1][1].from_fields, ['self'])
- self.assertEqual(new_state.models['app', 'othermodel'].fields[1][1].to_fields, ('renamed',))
- self.assertEqual(new_state.models['app', 'othermodel'].fields[2][1].from_fields, ('fk',))
- self.assertEqual(new_state.models['app', 'othermodel'].fields[2][1].to_fields, ('renamed',))
- operation = migrations.RenameField('OtherModel', 'fk', 'renamed_fk')
- new_state = state.clone()
- operation.state_forwards('app', new_state)
- self.assertEqual(new_state.models['app', 'othermodel'].fields[1][1].remote_field.field_name, 'renamed')
- self.assertEqual(new_state.models['app', 'othermodel'].fields[1][1].from_fields, ('self',))
- self.assertEqual(new_state.models['app', 'othermodel'].fields[1][1].to_fields, ('renamed',))
- self.assertEqual(new_state.models['app', 'othermodel'].fields[2][1].from_fields, ('renamed_fk',))
- self.assertEqual(new_state.models['app', 'othermodel'].fields[2][1].to_fields, ('renamed',))
- def test_alter_unique_together(self):
- """
- Tests the AlterUniqueTogether operation.
- """
- project_state = self.set_up_test_model("test_alunto")
-
- operation = migrations.AlterUniqueTogether("Pony", [("pink", "weight")])
- self.assertEqual(operation.describe(), "Alter unique_together for Pony (1 constraint(s))")
- new_state = project_state.clone()
- operation.state_forwards("test_alunto", new_state)
- self.assertEqual(len(project_state.models["test_alunto", "pony"].options.get("unique_together", set())), 0)
- self.assertEqual(len(new_state.models["test_alunto", "pony"].options.get("unique_together", set())), 1)
-
- with connection.cursor() as cursor:
- cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
- cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
- cursor.execute("DELETE FROM test_alunto_pony")
-
- with connection.schema_editor() as editor:
- operation.database_forwards("test_alunto", editor, project_state, new_state)
- cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
- with self.assertRaises(IntegrityError):
- with atomic():
- cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
- cursor.execute("DELETE FROM test_alunto_pony")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_alunto", editor, new_state, project_state)
- cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
- cursor.execute("INSERT INTO test_alunto_pony (pink, weight) VALUES (1, 1)")
- cursor.execute("DELETE FROM test_alunto_pony")
-
- operation = migrations.AlterUniqueTogether("Pony", ("pink", "weight"))
- operation.state_forwards("test_alunto", new_state)
- self.assertEqual(len(new_state.models["test_alunto", "pony"].options.get("unique_together", set())), 1)
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "AlterUniqueTogether")
- self.assertEqual(definition[1], [])
- self.assertEqual(definition[2], {'name': "Pony", 'unique_together': {("pink", "weight")}})
- def test_alter_unique_together_remove(self):
- operation = migrations.AlterUniqueTogether("Pony", None)
- self.assertEqual(operation.describe(), "Alter unique_together for Pony (0 constraint(s))")
- def test_add_index(self):
- """
- Test the AddIndex operation.
- """
- project_state = self.set_up_test_model("test_adin")
- msg = (
- "Indexes passed to AddIndex operations require a name argument. "
- "<Index: fields='pink'> doesn't have one."
- )
- with self.assertRaisesMessage(ValueError, msg):
- migrations.AddIndex("Pony", models.Index(fields=["pink"]))
- index = models.Index(fields=["pink"], name="test_adin_pony_pink_idx")
- operation = migrations.AddIndex("Pony", index)
- self.assertEqual(operation.describe(), "Create index test_adin_pony_pink_idx on field(s) pink of model Pony")
- new_state = project_state.clone()
- operation.state_forwards("test_adin", new_state)
-
- self.assertEqual(len(new_state.models["test_adin", "pony"].options['indexes']), 1)
- self.assertIndexNotExists("test_adin_pony", ["pink"])
- with connection.schema_editor() as editor:
- operation.database_forwards("test_adin", editor, project_state, new_state)
- self.assertIndexExists("test_adin_pony", ["pink"])
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_adin", editor, new_state, project_state)
- self.assertIndexNotExists("test_adin_pony", ["pink"])
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "AddIndex")
- self.assertEqual(definition[1], [])
- self.assertEqual(definition[2], {'model_name': "Pony", 'index': index})
- def test_remove_index(self):
- """
- Test the RemoveIndex operation.
- """
- project_state = self.set_up_test_model("test_rmin", multicol_index=True)
- self.assertTableExists("test_rmin_pony")
- self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
- operation = migrations.RemoveIndex("Pony", "pony_test_idx")
- self.assertEqual(operation.describe(), "Remove index pony_test_idx from Pony")
- new_state = project_state.clone()
- operation.state_forwards("test_rmin", new_state)
-
- self.assertEqual(len(new_state.models["test_rmin", "pony"].options['indexes']), 0)
- self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
-
- with connection.schema_editor() as editor:
- operation.database_forwards("test_rmin", editor, project_state, new_state)
- self.assertIndexNotExists("test_rmin_pony", ["pink", "weight"])
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_rmin", editor, new_state, project_state)
- self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "RemoveIndex")
- self.assertEqual(definition[1], [])
- self.assertEqual(definition[2], {'model_name': "Pony", 'name': "pony_test_idx"})
-
- operations = [
- migrations.RemoveIndex("Pony", "pony_test_idx"),
- migrations.RemoveField("Pony", "pink"),
- ]
- self.assertColumnExists("test_rmin_pony", "pink")
- self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
-
- new_state = project_state.clone()
- self.apply_operations('test_rmin', new_state, operations=operations)
- self.assertColumnNotExists("test_rmin_pony", "pink")
- self.assertIndexNotExists("test_rmin_pony", ["pink", "weight"])
-
- self.unapply_operations("test_rmin", project_state, operations=operations)
- self.assertIndexExists("test_rmin_pony", ["pink", "weight"])
- def test_add_index_state_forwards(self):
- project_state = self.set_up_test_model('test_adinsf')
- index = models.Index(fields=['pink'], name='test_adinsf_pony_pink_idx')
- old_model = project_state.apps.get_model('test_adinsf', 'Pony')
- new_state = project_state.clone()
- operation = migrations.AddIndex('Pony', index)
- operation.state_forwards('test_adinsf', new_state)
- new_model = new_state.apps.get_model('test_adinsf', 'Pony')
- self.assertIsNot(old_model, new_model)
- def test_remove_index_state_forwards(self):
- project_state = self.set_up_test_model('test_rminsf')
- index = models.Index(fields=['pink'], name='test_rminsf_pony_pink_idx')
- migrations.AddIndex('Pony', index).state_forwards('test_rminsf', project_state)
- old_model = project_state.apps.get_model('test_rminsf', 'Pony')
- new_state = project_state.clone()
- operation = migrations.RemoveIndex('Pony', 'test_rminsf_pony_pink_idx')
- operation.state_forwards('test_rminsf', new_state)
- new_model = new_state.apps.get_model('test_rminsf', 'Pony')
- self.assertIsNot(old_model, new_model)
- def test_alter_field_with_index(self):
- """
- Test AlterField operation with an index to ensure indexes created via
- Meta.indexes don't get dropped with sqlite3 remake.
- """
- project_state = self.set_up_test_model("test_alflin", index=True)
- operation = migrations.AlterField("Pony", "pink", models.IntegerField(null=True))
- new_state = project_state.clone()
- operation.state_forwards("test_alflin", new_state)
-
- self.assertColumnNotNull("test_alflin_pony", "pink")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_alflin", editor, project_state, new_state)
-
- self.assertIndexExists("test_alflin_pony", ["pink"])
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_alflin", editor, new_state, project_state)
-
- self.assertIndexExists("test_alflin_pony", ["pink"])
- def test_alter_index_together(self):
- """
- Tests the AlterIndexTogether operation.
- """
- project_state = self.set_up_test_model("test_alinto")
-
- operation = migrations.AlterIndexTogether("Pony", [("pink", "weight")])
- self.assertEqual(operation.describe(), "Alter index_together for Pony (1 constraint(s))")
- new_state = project_state.clone()
- operation.state_forwards("test_alinto", new_state)
- self.assertEqual(len(project_state.models["test_alinto", "pony"].options.get("index_together", set())), 0)
- self.assertEqual(len(new_state.models["test_alinto", "pony"].options.get("index_together", set())), 1)
-
- self.assertIndexNotExists("test_alinto_pony", ["pink", "weight"])
-
- with connection.schema_editor() as editor:
- operation.database_forwards("test_alinto", editor, project_state, new_state)
- self.assertIndexExists("test_alinto_pony", ["pink", "weight"])
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_alinto", editor, new_state, project_state)
- self.assertIndexNotExists("test_alinto_pony", ["pink", "weight"])
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "AlterIndexTogether")
- self.assertEqual(definition[1], [])
- self.assertEqual(definition[2], {'name': "Pony", 'index_together': {("pink", "weight")}})
- def test_alter_index_together_remove(self):
- operation = migrations.AlterIndexTogether("Pony", None)
- self.assertEqual(operation.describe(), "Alter index_together for Pony (0 constraint(s))")
- @skipUnlessDBFeature('supports_table_check_constraints')
- def test_add_constraint(self):
- """Test the AddConstraint operation."""
- project_state = self.set_up_test_model('test_addconstraint')
- where = models.Q(pink__gt=2)
- check_constraint = models.CheckConstraint(check=where, name='test_constraint_pony_pink_gt_2')
- operation = migrations.AddConstraint('Pony', check_constraint)
- self.assertEqual(operation.describe(), 'Create constraint test_constraint_pony_pink_gt_2 on model Pony')
- new_state = project_state.clone()
- operation.state_forwards('test_addconstraint', new_state)
- self.assertEqual(len(new_state.models['test_addconstraint', 'pony'].options['constraints']), 1)
-
- with connection.cursor() as cursor:
- with atomic():
- cursor.execute("INSERT INTO test_addconstraint_pony (id, pink, weight) VALUES (1, 1, 1.0)")
- cursor.execute("DELETE FROM test_addconstraint_pony")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_addconstraint", editor, project_state, new_state)
- with connection.cursor() as cursor:
- with self.assertRaises(IntegrityError):
- cursor.execute("INSERT INTO test_addconstraint_pony (id, pink, weight) VALUES (1, 1, 1.0)")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_addconstraint", editor, new_state, project_state)
- with connection.cursor() as cursor:
- with atomic():
- cursor.execute("INSERT INTO test_addconstraint_pony (id, pink, weight) VALUES (1, 1, 1.0)")
- cursor.execute("DELETE FROM test_addconstraint_pony")
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "AddConstraint")
- self.assertEqual(definition[1], [])
- self.assertEqual(definition[2], {'model_name': "Pony", 'constraint': check_constraint})
- @skipUnlessDBFeature('supports_table_check_constraints')
- def test_remove_constraint(self):
- """Test the RemoveConstraint operation."""
- project_state = self.set_up_test_model("test_removeconstraint", check_constraint=True)
- self.assertTableExists("test_removeconstraint_pony")
- operation = migrations.RemoveConstraint("Pony", "pony_test_constraint")
- self.assertEqual(operation.describe(), "Remove constraint pony_test_constraint from model Pony")
- new_state = project_state.clone()
- operation.state_forwards("test_removeconstraint", new_state)
-
- self.assertEqual(len(new_state.models["test_removeconstraint", "pony"].options['constraints']), 0)
- with connection.cursor() as cursor:
- with self.assertRaises(IntegrityError):
- cursor.execute("INSERT INTO test_removeconstraint_pony (id, pink, weight) VALUES (1, 1, 1.0)")
-
- with connection.schema_editor() as editor:
- operation.database_forwards("test_removeconstraint", editor, project_state, new_state)
- with connection.cursor() as cursor:
- with atomic():
- cursor.execute("INSERT INTO test_removeconstraint_pony (id, pink, weight) VALUES (1, 1, 1.0)")
- cursor.execute("DELETE FROM test_removeconstraint_pony")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_removeconstraint", editor, new_state, project_state)
- with connection.cursor() as cursor:
- with self.assertRaises(IntegrityError):
- cursor.execute("INSERT INTO test_removeconstraint_pony (id, pink, weight) VALUES (1, 1, 1.0)")
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "RemoveConstraint")
- self.assertEqual(definition[1], [])
- self.assertEqual(definition[2], {'model_name': "Pony", 'name': "pony_test_constraint"})
- def test_alter_model_options(self):
- """
- Tests the AlterModelOptions operation.
- """
- project_state = self.set_up_test_model("test_almoop")
-
- operation = migrations.AlterModelOptions("Pony", {"permissions": [("can_groom", "Can groom")]})
- self.assertEqual(operation.describe(), "Change Meta options on Pony")
- new_state = project_state.clone()
- operation.state_forwards("test_almoop", new_state)
- self.assertEqual(len(project_state.models["test_almoop", "pony"].options.get("permissions", [])), 0)
- self.assertEqual(len(new_state.models["test_almoop", "pony"].options.get("permissions", [])), 1)
- self.assertEqual(new_state.models["test_almoop", "pony"].options["permissions"][0][0], "can_groom")
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "AlterModelOptions")
- self.assertEqual(definition[1], [])
- self.assertEqual(definition[2], {'name': "Pony", 'options': {"permissions": [("can_groom", "Can groom")]}})
- def test_alter_model_options_emptying(self):
- """
- The AlterModelOptions operation removes keys from the dict (#23121)
- """
- project_state = self.set_up_test_model("test_almoop", options=True)
-
- operation = migrations.AlterModelOptions("Pony", {})
- self.assertEqual(operation.describe(), "Change Meta options on Pony")
- new_state = project_state.clone()
- operation.state_forwards("test_almoop", new_state)
- self.assertEqual(len(project_state.models["test_almoop", "pony"].options.get("permissions", [])), 1)
- self.assertEqual(len(new_state.models["test_almoop", "pony"].options.get("permissions", [])), 0)
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "AlterModelOptions")
- self.assertEqual(definition[1], [])
- self.assertEqual(definition[2], {'name': "Pony", 'options': {}})
- def test_alter_order_with_respect_to(self):
- """
- Tests the AlterOrderWithRespectTo operation.
- """
- project_state = self.set_up_test_model("test_alorwrtto", related_model=True)
-
- operation = migrations.AlterOrderWithRespectTo("Rider", "pony")
- self.assertEqual(operation.describe(), "Set order_with_respect_to on Rider to pony")
- new_state = project_state.clone()
- operation.state_forwards("test_alorwrtto", new_state)
- self.assertIsNone(
- project_state.models["test_alorwrtto", "rider"].options.get("order_with_respect_to", None)
- )
- self.assertEqual(
- new_state.models["test_alorwrtto", "rider"].options.get("order_with_respect_to", None),
- "pony"
- )
-
- self.assertColumnNotExists("test_alorwrtto_rider", "_order")
-
- rendered_state = project_state.apps
- pony = rendered_state.get_model("test_alorwrtto", "Pony").objects.create(weight=50)
- rendered_state.get_model("test_alorwrtto", "Rider").objects.create(pony=pony, friend_id=1)
- rendered_state.get_model("test_alorwrtto", "Rider").objects.create(pony=pony, friend_id=2)
-
- with connection.schema_editor() as editor:
- operation.database_forwards("test_alorwrtto", editor, project_state, new_state)
- self.assertColumnExists("test_alorwrtto_rider", "_order")
-
- updated_riders = new_state.apps.get_model("test_alorwrtto", "Rider").objects.all()
- self.assertEqual(updated_riders[0]._order, 0)
- self.assertEqual(updated_riders[1]._order, 0)
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_alorwrtto", editor, new_state, project_state)
- self.assertColumnNotExists("test_alorwrtto_rider", "_order")
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "AlterOrderWithRespectTo")
- self.assertEqual(definition[1], [])
- self.assertEqual(definition[2], {'name': "Rider", 'order_with_respect_to': "pony"})
- def test_alter_model_managers(self):
- """
- The managers on a model are set.
- """
- project_state = self.set_up_test_model("test_almoma")
-
- operation = migrations.AlterModelManagers(
- "Pony",
- managers=[
- ("food_qs", FoodQuerySet.as_manager()),
- ("food_mgr", FoodManager("a", "b")),
- ("food_mgr_kwargs", FoodManager("x", "y", 3, 4)),
- ]
- )
- self.assertEqual(operation.describe(), "Change managers on Pony")
- managers = project_state.models["test_almoma", "pony"].managers
- self.assertEqual(managers, [])
- new_state = project_state.clone()
- operation.state_forwards("test_almoma", new_state)
- self.assertIn(("test_almoma", "pony"), new_state.models)
- managers = new_state.models["test_almoma", "pony"].managers
- self.assertEqual(managers[0][0], "food_qs")
- self.assertIsInstance(managers[0][1], models.Manager)
- self.assertEqual(managers[1][0], "food_mgr")
- self.assertIsInstance(managers[1][1], FoodManager)
- self.assertEqual(managers[1][1].args, ("a", "b", 1, 2))
- self.assertEqual(managers[2][0], "food_mgr_kwargs")
- self.assertIsInstance(managers[2][1], FoodManager)
- self.assertEqual(managers[2][1].args, ("x", "y", 3, 4))
- rendered_state = new_state.apps
- model = rendered_state.get_model('test_almoma', 'pony')
- self.assertIsInstance(model.food_qs, models.Manager)
- self.assertIsInstance(model.food_mgr, FoodManager)
- self.assertIsInstance(model.food_mgr_kwargs, FoodManager)
- def test_alter_model_managers_emptying(self):
- """
- The managers on a model are set.
- """
- project_state = self.set_up_test_model("test_almomae", manager_model=True)
-
- operation = migrations.AlterModelManagers("Food", managers=[])
- self.assertEqual(operation.describe(), "Change managers on Food")
- self.assertIn(("test_almomae", "food"), project_state.models)
- managers = project_state.models["test_almomae", "food"].managers
- self.assertEqual(managers[0][0], "food_qs")
- self.assertIsInstance(managers[0][1], models.Manager)
- self.assertEqual(managers[1][0], "food_mgr")
- self.assertIsInstance(managers[1][1], FoodManager)
- self.assertEqual(managers[1][1].args, ("a", "b", 1, 2))
- self.assertEqual(managers[2][0], "food_mgr_kwargs")
- self.assertIsInstance(managers[2][1], FoodManager)
- self.assertEqual(managers[2][1].args, ("x", "y", 3, 4))
- new_state = project_state.clone()
- operation.state_forwards("test_almomae", new_state)
- managers = new_state.models["test_almomae", "food"].managers
- self.assertEqual(managers, [])
- def test_alter_fk(self):
- """
- Creating and then altering an FK works correctly
- and deals with the pending SQL (#23091)
- """
- project_state = self.set_up_test_model("test_alfk")
-
- create_operation = migrations.CreateModel(
- name="Rider",
- fields=[
- ("id", models.AutoField(primary_key=True)),
- ("pony", models.ForeignKey("Pony", models.CASCADE)),
- ],
- )
- create_state = project_state.clone()
- create_operation.state_forwards("test_alfk", create_state)
- alter_operation = migrations.AlterField(
- model_name='Rider',
- name='pony',
- field=models.ForeignKey("Pony", models.CASCADE, editable=False),
- )
- alter_state = create_state.clone()
- alter_operation.state_forwards("test_alfk", alter_state)
- with connection.schema_editor() as editor:
- create_operation.database_forwards("test_alfk", editor, project_state, create_state)
- alter_operation.database_forwards("test_alfk", editor, create_state, alter_state)
- def test_alter_fk_non_fk(self):
- """
- Altering an FK to a non-FK works (#23244)
- """
-
- operation = migrations.AlterField(
- model_name="Rider",
- name="pony",
- field=models.FloatField(),
- )
- project_state, new_state = self.make_test_state("test_afknfk", operation, related_model=True)
-
- self.assertColumnExists("test_afknfk_rider", "pony_id")
- self.assertColumnNotExists("test_afknfk_rider", "pony")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_afknfk", editor, project_state, new_state)
- self.assertColumnExists("test_afknfk_rider", "pony")
- self.assertColumnNotExists("test_afknfk_rider", "pony_id")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_afknfk", editor, new_state, project_state)
- self.assertColumnExists("test_afknfk_rider", "pony_id")
- self.assertColumnNotExists("test_afknfk_rider", "pony")
- @unittest.skipIf(sqlparse is None and connection.features.requires_sqlparse_for_splitting, "Missing sqlparse")
- def test_run_sql(self):
- """
- Tests the RunSQL operation.
- """
- project_state = self.set_up_test_model("test_runsql")
-
- operation = migrations.RunSQL(
-
- "CREATE TABLE i_love_ponies (id int, special_thing varchar(15));\n"
- "INSERT INTO i_love_ponies (id, special_thing) VALUES (1, 'i love ponies'); -- this is magic!\n"
- "INSERT INTO i_love_ponies (id, special_thing) VALUES (2, 'i love django');\n"
- "UPDATE i_love_ponies SET special_thing = 'Ponies' WHERE special_thing LIKE '%%ponies';"
- "UPDATE i_love_ponies SET special_thing = 'Django' WHERE special_thing LIKE '%django';",
-
-
- "DELETE FROM i_love_ponies WHERE special_thing LIKE '%Django%';"
- "DELETE FROM i_love_ponies WHERE special_thing LIKE '%%Ponies%%';"
- "DROP TABLE i_love_ponies",
- state_operations=[migrations.CreateModel("SomethingElse", [("id", models.AutoField(primary_key=True))])],
- )
- self.assertEqual(operation.describe(), "Raw SQL operation")
-
- new_state = project_state.clone()
- operation.state_forwards("test_runsql", new_state)
- self.assertEqual(len(new_state.models["test_runsql", "somethingelse"].fields), 1)
-
- self.assertTableNotExists("i_love_ponies")
-
- with connection.schema_editor(collect_sql=True) as editor:
- operation.database_forwards("test_runsql", editor, project_state, new_state)
- self.assertIn("LIKE '%%ponies';", "\n".join(editor.collected_sql))
- operation.database_backwards("test_runsql", editor, project_state, new_state)
- self.assertIn("LIKE '%%Ponies%%';", "\n".join(editor.collected_sql))
-
- with connection.schema_editor() as editor:
- operation.database_forwards("test_runsql", editor, project_state, new_state)
- self.assertTableExists("i_love_ponies")
-
- with connection.cursor() as cursor:
- cursor.execute("SELECT COUNT(*) FROM i_love_ponies")
- self.assertEqual(cursor.fetchall()[0][0], 2)
- cursor.execute("SELECT COUNT(*) FROM i_love_ponies WHERE special_thing = 'Django'")
- self.assertEqual(cursor.fetchall()[0][0], 1)
- cursor.execute("SELECT COUNT(*) FROM i_love_ponies WHERE special_thing = 'Ponies'")
- self.assertEqual(cursor.fetchall()[0][0], 1)
-
- self.assertTrue(operation.reversible)
- with connection.schema_editor() as editor:
- operation.database_backwards("test_runsql", editor, new_state, project_state)
- self.assertTableNotExists("i_love_ponies")
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "RunSQL")
- self.assertEqual(definition[1], [])
- self.assertEqual(sorted(definition[2]), ["reverse_sql", "sql", "state_operations"])
-
- self.assertIs(False, operation.reduce(operation, []))
- elidable_operation = migrations.RunSQL('SELECT 1 FROM void;', elidable=True)
- self.assertEqual(elidable_operation.reduce(operation, []), [operation])
- def test_run_sql_params(self):
- """
- #23426 - RunSQL should accept parameters.
- """
- project_state = self.set_up_test_model("test_runsql")
-
- operation = migrations.RunSQL(
- ["CREATE TABLE i_love_ponies (id int, special_thing varchar(15));"],
- ["DROP TABLE i_love_ponies"],
- )
- param_operation = migrations.RunSQL(
-
- (
- "INSERT INTO i_love_ponies (id, special_thing) VALUES (1, 'Django');",
- ["INSERT INTO i_love_ponies (id, special_thing) VALUES (2, %s);", ['Ponies']],
- ("INSERT INTO i_love_ponies (id, special_thing) VALUES (%s, %s);", (3, 'Python',)),
- ),
-
- [
- "DELETE FROM i_love_ponies WHERE special_thing = 'Django';",
- ["DELETE FROM i_love_ponies WHERE special_thing = 'Ponies';", None],
- ("DELETE FROM i_love_ponies WHERE id = %s OR special_thing = %s;", [3, 'Python']),
- ]
- )
-
- self.assertTableNotExists("i_love_ponies")
- new_state = project_state.clone()
-
- with connection.schema_editor() as editor:
- operation.database_forwards("test_runsql", editor, project_state, new_state)
-
- with connection.schema_editor() as editor:
- param_operation.database_forwards("test_runsql", editor, project_state, new_state)
-
- with connection.cursor() as cursor:
- cursor.execute("SELECT COUNT(*) FROM i_love_ponies")
- self.assertEqual(cursor.fetchall()[0][0], 3)
- with connection.schema_editor() as editor:
- param_operation.database_backwards("test_runsql", editor, new_state, project_state)
- with connection.cursor() as cursor:
- cursor.execute("SELECT COUNT(*) FROM i_love_ponies")
- self.assertEqual(cursor.fetchall()[0][0], 0)
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_runsql", editor, new_state, project_state)
- self.assertTableNotExists("i_love_ponies")
- def test_run_sql_params_invalid(self):
- """
- #23426 - RunSQL should fail when a list of statements with an incorrect
- number of tuples is given.
- """
- project_state = self.set_up_test_model("test_runsql")
- new_state = project_state.clone()
- operation = migrations.RunSQL(
-
- [
- ["INSERT INTO foo (bar) VALUES ('buz');"]
- ],
-
- (
- ("DELETE FROM foo WHERE bar = 'buz';", 'invalid', 'parameter count'),
- ),
- )
- with connection.schema_editor() as editor:
- with self.assertRaisesMessage(ValueError, "Expected a 2-tuple but got 1"):
- operation.database_forwards("test_runsql", editor, project_state, new_state)
- with connection.schema_editor() as editor:
- with self.assertRaisesMessage(ValueError, "Expected a 2-tuple but got 3"):
- operation.database_backwards("test_runsql", editor, new_state, project_state)
- def test_run_sql_noop(self):
- """
- #24098 - Tests no-op RunSQL operations.
- """
- operation = migrations.RunSQL(migrations.RunSQL.noop, migrations.RunSQL.noop)
- with connection.schema_editor() as editor:
- operation.database_forwards("test_runsql", editor, None, None)
- operation.database_backwards("test_runsql", editor, None, None)
- def test_run_python(self):
- """
- Tests the RunPython operation
- """
- project_state = self.set_up_test_model("test_runpython", mti_model=True)
-
- def inner_method(models, schema_editor):
- Pony = models.get_model("test_runpython", "Pony")
- Pony.objects.create(pink=1, weight=3.55)
- Pony.objects.create(weight=5)
- def inner_method_reverse(models, schema_editor):
- Pony = models.get_model("test_runpython", "Pony")
- Pony.objects.filter(pink=1, weight=3.55).delete()
- Pony.objects.filter(weight=5).delete()
- operation = migrations.RunPython(inner_method, reverse_code=inner_method_reverse)
- self.assertEqual(operation.describe(), "Raw Python operation")
-
- new_state = project_state.clone()
- operation.state_forwards("test_runpython", new_state)
- self.assertEqual(new_state, project_state)
-
- self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 0)
- with connection.schema_editor() as editor:
- operation.database_forwards("test_runpython", editor, project_state, new_state)
- self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 2)
-
- self.assertTrue(operation.reversible)
- with connection.schema_editor() as editor:
- operation.database_backwards("test_runpython", editor, project_state, new_state)
- self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 0)
-
- with self.assertRaisesMessage(ValueError, 'RunPython must be supplied with a callable'):
- migrations.RunPython("print 'ahahaha'")
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "RunPython")
- self.assertEqual(definition[1], [])
- self.assertEqual(sorted(definition[2]), ["code", "reverse_code"])
-
- no_reverse_operation = migrations.RunPython(inner_method)
- self.assertFalse(no_reverse_operation.reversible)
- with connection.schema_editor() as editor:
- no_reverse_operation.database_forwards("test_runpython", editor, project_state, new_state)
- with self.assertRaises(NotImplementedError):
- no_reverse_operation.database_backwards("test_runpython", editor, new_state, project_state)
- self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 2)
- def create_ponies(models, schema_editor):
- Pony = models.get_model("test_runpython", "Pony")
- pony1 = Pony.objects.create(pink=1, weight=3.55)
- self.assertIsNot(pony1.pk, None)
- pony2 = Pony.objects.create(weight=5)
- self.assertIsNot(pony2.pk, None)
- self.assertNotEqual(pony1.pk, pony2.pk)
- operation = migrations.RunPython(create_ponies)
- with connection.schema_editor() as editor:
- operation.database_forwards("test_runpython", editor, project_state, new_state)
- self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 4)
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "RunPython")
- self.assertEqual(definition[1], [])
- self.assertEqual(sorted(definition[2]), ["code"])
- def create_shetlandponies(models, schema_editor):
- ShetlandPony = models.get_model("test_runpython", "ShetlandPony")
- pony1 = ShetlandPony.objects.create(weight=4.0)
- self.assertIsNot(pony1.pk, None)
- pony2 = ShetlandPony.objects.create(weight=5.0)
- self.assertIsNot(pony2.pk, None)
- self.assertNotEqual(pony1.pk, pony2.pk)
- operation = migrations.RunPython(create_shetlandponies)
- with connection.schema_editor() as editor:
- operation.database_forwards("test_runpython", editor, project_state, new_state)
- self.assertEqual(project_state.apps.get_model("test_runpython", "Pony").objects.count(), 6)
- self.assertEqual(project_state.apps.get_model("test_runpython", "ShetlandPony").objects.count(), 2)
-
- self.assertIs(False, operation.reduce(operation, []))
- elidable_operation = migrations.RunPython(inner_method, elidable=True)
- self.assertEqual(elidable_operation.reduce(operation, []), [operation])
- def test_run_python_atomic(self):
- """
- Tests the RunPython operation correctly handles the "atomic" keyword
- """
- project_state = self.set_up_test_model("test_runpythonatomic", mti_model=True)
- def inner_method(models, schema_editor):
- Pony = models.get_model("test_runpythonatomic", "Pony")
- Pony.objects.create(pink=1, weight=3.55)
- raise ValueError("Adrian hates ponies.")
-
- atomic_migration = Migration("test", "test_runpythonatomic")
- atomic_migration.operations = [migrations.RunPython(inner_method, reverse_code=inner_method)]
- non_atomic_migration = Migration("test", "test_runpythonatomic")
- non_atomic_migration.operations = [migrations.RunPython(inner_method, reverse_code=inner_method, atomic=False)]
-
- if connection.features.can_rollback_ddl:
- self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
- with self.assertRaises(ValueError):
- with connection.schema_editor() as editor:
- atomic_migration.apply(project_state, editor)
- self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
- with self.assertRaises(ValueError):
- with connection.schema_editor() as editor:
- non_atomic_migration.apply(project_state, editor)
- self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
-
- else:
- self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
- with self.assertRaises(ValueError):
- with connection.schema_editor() as editor:
- atomic_migration.apply(project_state, editor)
- self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
- with self.assertRaises(ValueError):
- with connection.schema_editor() as editor:
- non_atomic_migration.apply(project_state, editor)
- self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 1)
-
- project_state.apps.get_model("test_runpythonatomic", "Pony").objects.all().delete()
-
- if connection.features.can_rollback_ddl:
- self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
- with self.assertRaises(ValueError):
- with connection.schema_editor() as editor:
- atomic_migration.unapply(project_state, editor)
- self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
- with self.assertRaises(ValueError):
- with connection.schema_editor() as editor:
- non_atomic_migration.unapply(project_state, editor)
- self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
-
- else:
- self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
- with self.assertRaises(ValueError):
- with connection.schema_editor() as editor:
- atomic_migration.unapply(project_state, editor)
- self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 0)
- with self.assertRaises(ValueError):
- with connection.schema_editor() as editor:
- non_atomic_migration.unapply(project_state, editor)
- self.assertEqual(project_state.apps.get_model("test_runpythonatomic", "Pony").objects.count(), 1)
-
- definition = non_atomic_migration.operations[0].deconstruct()
- self.assertEqual(definition[0], "RunPython")
- self.assertEqual(definition[1], [])
- self.assertEqual(sorted(definition[2]), ["atomic", "code", "reverse_code"])
- def test_run_python_related_assignment(self):
- """
- #24282 - Model changes to a FK reverse side update the model
- on the FK side as well.
- """
- def inner_method(models, schema_editor):
- Author = models.get_model("test_authors", "Author")
- Book = models.get_model("test_books", "Book")
- author = Author.objects.create(name="Hemingway")
- Book.objects.create(title="Old Man and The Sea", author=author)
- create_author = migrations.CreateModel(
- "Author",
- [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=100)),
- ],
- options={},
- )
- create_book = migrations.CreateModel(
- "Book",
- [
- ("id", models.AutoField(primary_key=True)),
- ("title", models.CharField(max_length=100)),
- ("author", models.ForeignKey("test_authors.Author", models.CASCADE))
- ],
- options={},
- )
- add_hometown = migrations.AddField(
- "Author",
- "hometown",
- models.CharField(max_length=100),
- )
- create_old_man = migrations.RunPython(inner_method, inner_method)
- project_state = ProjectState()
- new_state = project_state.clone()
- with connection.schema_editor() as editor:
- create_author.state_forwards("test_authors", new_state)
- create_author.database_forwards("test_authors", editor, project_state, new_state)
- project_state = new_state
- new_state = new_state.clone()
- with connection.schema_editor() as editor:
- create_book.state_forwards("test_books", new_state)
- create_book.database_forwards("test_books", editor, project_state, new_state)
- project_state = new_state
- new_state = new_state.clone()
- with connection.schema_editor() as editor:
- add_hometown.state_forwards("test_authors", new_state)
- add_hometown.database_forwards("test_authors", editor, project_state, new_state)
- project_state = new_state
- new_state = new_state.clone()
- with connection.schema_editor() as editor:
- create_old_man.state_forwards("test_books", new_state)
- create_old_man.database_forwards("test_books", editor, project_state, new_state)
- def test_model_with_bigautofield(self):
- """
- A model with BigAutoField can be created.
- """
- def create_data(models, schema_editor):
- Author = models.get_model("test_author", "Author")
- Book = models.get_model("test_book", "Book")
- author1 = Author.objects.create(name="Hemingway")
- Book.objects.create(title="Old Man and The Sea", author=author1)
- Book.objects.create(id=2 ** 33, title="A farewell to arms", author=author1)
- author2 = Author.objects.create(id=2 ** 33, name="Remarque")
- Book.objects.create(title="All quiet on the western front", author=author2)
- Book.objects.create(title="Arc de Triomphe", author=author2)
- create_author = migrations.CreateModel(
- "Author",
- [
- ("id", models.BigAutoField(primary_key=True)),
- ("name", models.CharField(max_length=100)),
- ],
- options={},
- )
- create_book = migrations.CreateModel(
- "Book",
- [
- ("id", models.BigAutoField(primary_key=True)),
- ("title", models.CharField(max_length=100)),
- ("author", models.ForeignKey(to="test_author.Author", on_delete=models.CASCADE))
- ],
- options={},
- )
- fill_data = migrations.RunPython(create_data)
- project_state = ProjectState()
- new_state = project_state.clone()
- with connection.schema_editor() as editor:
- create_author.state_forwards("test_author", new_state)
- create_author.database_forwards("test_author", editor, project_state, new_state)
- project_state = new_state
- new_state = new_state.clone()
- with connection.schema_editor() as editor:
- create_book.state_forwards("test_book", new_state)
- create_book.database_forwards("test_book", editor, project_state, new_state)
- project_state = new_state
- new_state = new_state.clone()
- with connection.schema_editor() as editor:
- fill_data.state_forwards("fill_data", new_state)
- fill_data.database_forwards("fill_data", editor, project_state, new_state)
- def test_autofield_foreignfield_growth(self):
- """
- A field may be migrated from AutoField to BigAutoField.
- """
- def create_initial_data(models, schema_editor):
- Article = models.get_model("test_article", "Article")
- Blog = models.get_model("test_blog", "Blog")
- blog = Blog.objects.create(name="web development done right")
- Article.objects.create(name="Frameworks", blog=blog)
- Article.objects.create(name="Programming Languages", blog=blog)
- def create_big_data(models, schema_editor):
- Article = models.get_model("test_article", "Article")
- Blog = models.get_model("test_blog", "Blog")
- blog2 = Blog.objects.create(name="Frameworks", id=2 ** 33)
- Article.objects.create(name="Django", blog=blog2)
- Article.objects.create(id=2 ** 33, name="Django2", blog=blog2)
- create_blog = migrations.CreateModel(
- "Blog",
- [
- ("id", models.AutoField(primary_key=True)),
- ("name", models.CharField(max_length=100)),
- ],
- options={},
- )
- create_article = migrations.CreateModel(
- "Article",
- [
- ("id", models.AutoField(primary_key=True)),
- ("blog", models.ForeignKey(to="test_blog.Blog", on_delete=models.CASCADE)),
- ("name", models.CharField(max_length=100)),
- ("data", models.TextField(default="")),
- ],
- options={},
- )
- fill_initial_data = migrations.RunPython(create_initial_data, create_initial_data)
- fill_big_data = migrations.RunPython(create_big_data, create_big_data)
- grow_article_id = migrations.AlterField("Article", "id", models.BigAutoField(primary_key=True))
- grow_blog_id = migrations.AlterField("Blog", "id", models.BigAutoField(primary_key=True))
- project_state = ProjectState()
- new_state = project_state.clone()
- with connection.schema_editor() as editor:
- create_blog.state_forwards("test_blog", new_state)
- create_blog.database_forwards("test_blog", editor, project_state, new_state)
- project_state = new_state
- new_state = new_state.clone()
- with connection.schema_editor() as editor:
- create_article.state_forwards("test_article", new_state)
- create_article.database_forwards("test_article", editor, project_state, new_state)
- project_state = new_state
- new_state = new_state.clone()
- with connection.schema_editor() as editor:
- fill_initial_data.state_forwards("fill_initial_data", new_state)
- fill_initial_data.database_forwards("fill_initial_data", editor, project_state, new_state)
- project_state = new_state
- new_state = new_state.clone()
- with connection.schema_editor() as editor:
- grow_article_id.state_forwards("test_article", new_state)
- grow_article_id.database_forwards("test_article", editor, project_state, new_state)
- state = new_state.clone()
- article = state.apps.get_model("test_article.Article")
- self.assertIsInstance(article._meta.pk, models.BigAutoField)
- project_state = new_state
- new_state = new_state.clone()
- with connection.schema_editor() as editor:
- grow_blog_id.state_forwards("test_blog", new_state)
- grow_blog_id.database_forwards("test_blog", editor, project_state, new_state)
- state = new_state.clone()
- blog = state.apps.get_model("test_blog.Blog")
- self.assertIsInstance(blog._meta.pk, models.BigAutoField)
- project_state = new_state
- new_state = new_state.clone()
- with connection.schema_editor() as editor:
- fill_big_data.state_forwards("fill_big_data", new_state)
- fill_big_data.database_forwards("fill_big_data", editor, project_state, new_state)
- def test_run_python_noop(self):
- """
- #24098 - Tests no-op RunPython operations.
- """
- project_state = ProjectState()
- new_state = project_state.clone()
- operation = migrations.RunPython(migrations.RunPython.noop, migrations.RunPython.noop)
- with connection.schema_editor() as editor:
- operation.database_forwards("test_runpython", editor, project_state, new_state)
- operation.database_backwards("test_runpython", editor, new_state, project_state)
- @unittest.skipIf(sqlparse is None and connection.features.requires_sqlparse_for_splitting, "Missing sqlparse")
- def test_separate_database_and_state(self):
- """
- Tests the SeparateDatabaseAndState operation.
- """
- project_state = self.set_up_test_model("test_separatedatabaseandstate")
-
- database_operation = migrations.RunSQL(
- "CREATE TABLE i_love_ponies (id int, special_thing int);",
- "DROP TABLE i_love_ponies;"
- )
- state_operation = migrations.CreateModel("SomethingElse", [("id", models.AutoField(primary_key=True))])
- operation = migrations.SeparateDatabaseAndState(
- state_operations=[state_operation],
- database_operations=[database_operation]
- )
- self.assertEqual(operation.describe(), "Custom state/database change combination")
-
- new_state = project_state.clone()
- operation.state_forwards("test_separatedatabaseandstate", new_state)
- self.assertEqual(len(new_state.models["test_separatedatabaseandstate", "somethingelse"].fields), 1)
-
- self.assertTableNotExists("i_love_ponies")
-
- with connection.schema_editor() as editor:
- operation.database_forwards("test_separatedatabaseandstate", editor, project_state, new_state)
- self.assertTableExists("i_love_ponies")
-
- self.assertTrue(operation.reversible)
- with connection.schema_editor() as editor:
- operation.database_backwards("test_separatedatabaseandstate", editor, new_state, project_state)
- self.assertTableNotExists("i_love_ponies")
-
- definition = operation.deconstruct()
- self.assertEqual(definition[0], "SeparateDatabaseAndState")
- self.assertEqual(definition[1], [])
- self.assertEqual(sorted(definition[2]), ["database_operations", "state_operations"])
- def test_separate_database_and_state2(self):
- """
- A complex SeparateDatabaseAndState operation: Multiple operations both
- for state and database. Verify the state dependencies within each list
- and that state ops don't affect the database.
- """
- app_label = "test_separatedatabaseandstate2"
- project_state = self.set_up_test_model(app_label)
-
- database_operations = [
- migrations.CreateModel(
- "ILovePonies",
- [("id", models.AutoField(primary_key=True))],
- options={"db_table": "iloveponies"},
- ),
- migrations.CreateModel(
- "ILoveMorePonies",
-
-
-
- [("id", models.IntegerField(primary_key=True))],
- options={"db_table": "ilovemoreponies"},
- ),
- migrations.DeleteModel("ILoveMorePonies"),
- migrations.CreateModel(
- "ILoveEvenMorePonies",
- [("id", models.AutoField(primary_key=True))],
- options={"db_table": "iloveevenmoreponies"},
- ),
- ]
- state_operations = [
- migrations.CreateModel(
- "SomethingElse",
- [("id", models.AutoField(primary_key=True))],
- options={"db_table": "somethingelse"},
- ),
- migrations.DeleteModel("SomethingElse"),
- migrations.CreateModel(
- "SomethingCompletelyDifferent",
- [("id", models.AutoField(primary_key=True))],
- options={"db_table": "somethingcompletelydifferent"},
- ),
- ]
- operation = migrations.SeparateDatabaseAndState(
- state_operations=state_operations,
- database_operations=database_operations,
- )
-
- new_state = project_state.clone()
- operation.state_forwards(app_label, new_state)
- def assertModelsAndTables(after_db):
-
- self.assertNotIn((app_label, "somethingelse"), new_state.models)
- self.assertEqual(len(new_state.models[app_label, "somethingcompletelydifferent"].fields), 1)
- self.assertNotIn((app_label, "iloveponiesonies"), new_state.models)
- self.assertNotIn((app_label, "ilovemoreponies"), new_state.models)
- self.assertNotIn((app_label, "iloveevenmoreponies"), new_state.models)
- self.assertTableNotExists("somethingelse")
- self.assertTableNotExists("somethingcompletelydifferent")
- self.assertTableNotExists("ilovemoreponies")
- if after_db:
- self.assertTableExists("iloveponies")
- self.assertTableExists("iloveevenmoreponies")
- else:
- self.assertTableNotExists("iloveponies")
- self.assertTableNotExists("iloveevenmoreponies")
- assertModelsAndTables(after_db=False)
-
- with connection.schema_editor() as editor:
- operation.database_forwards(app_label, editor, project_state, new_state)
- assertModelsAndTables(after_db=True)
-
- self.assertTrue(operation.reversible)
- with connection.schema_editor() as editor:
- operation.database_backwards(app_label, editor, new_state, project_state)
- assertModelsAndTables(after_db=False)
- class SwappableOperationTests(OperationTestBase):
- """
- Key operations ignore swappable models
- (we don't want to replicate all of them here, as the functionality
- is in a common base class anyway)
- """
- available_apps = ['migrations']
- @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
- def test_create_ignore_swapped(self):
- """
- The CreateTable operation ignores swapped models.
- """
- operation = migrations.CreateModel(
- "Pony",
- [
- ("id", models.AutoField(primary_key=True)),
- ("pink", models.IntegerField(default=1)),
- ],
- options={
- "swappable": "TEST_SWAP_MODEL",
- },
- )
-
- project_state = ProjectState()
- new_state = project_state.clone()
- operation.state_forwards("test_crigsw", new_state)
- self.assertEqual(new_state.models["test_crigsw", "pony"].name, "Pony")
- self.assertEqual(len(new_state.models["test_crigsw", "pony"].fields), 2)
-
- self.assertTableNotExists("test_crigsw_pony")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_crigsw", editor, project_state, new_state)
- self.assertTableNotExists("test_crigsw_pony")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_crigsw", editor, new_state, project_state)
- self.assertTableNotExists("test_crigsw_pony")
- @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
- def test_delete_ignore_swapped(self):
- """
- Tests the DeleteModel operation ignores swapped models.
- """
- operation = migrations.DeleteModel("Pony")
- project_state, new_state = self.make_test_state("test_dligsw", operation)
-
- self.assertTableNotExists("test_dligsw_pony")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_dligsw", editor, project_state, new_state)
- self.assertTableNotExists("test_dligsw_pony")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_dligsw", editor, new_state, project_state)
- self.assertTableNotExists("test_dligsw_pony")
- @override_settings(TEST_SWAP_MODEL="migrations.SomeFakeModel")
- def test_add_field_ignore_swapped(self):
- """
- Tests the AddField operation.
- """
-
- operation = migrations.AddField(
- "Pony",
- "height",
- models.FloatField(null=True, default=5),
- )
- project_state, new_state = self.make_test_state("test_adfligsw", operation)
-
- self.assertTableNotExists("test_adfligsw_pony")
- with connection.schema_editor() as editor:
- operation.database_forwards("test_adfligsw", editor, project_state, new_state)
- self.assertTableNotExists("test_adfligsw_pony")
-
- with connection.schema_editor() as editor:
- operation.database_backwards("test_adfligsw", editor, new_state, project_state)
- self.assertTableNotExists("test_adfligsw_pony")
- @override_settings(TEST_SWAP_MODEL='migrations.SomeFakeModel')
- def test_indexes_ignore_swapped(self):
- """
- Add/RemoveIndex operations ignore swapped models.
- """
- operation = migrations.AddIndex('Pony', models.Index(fields=['pink'], name='my_name_idx'))
- project_state, new_state = self.make_test_state('test_adinigsw', operation)
- with connection.schema_editor() as editor:
-
- operation.database_forwards('test_adinigsw', editor, project_state, new_state)
- operation.database_backwards('test_adinigsw', editor, new_state, project_state)
- operation = migrations.RemoveIndex('Pony', models.Index(fields=['pink'], name='my_name_idx'))
- project_state, new_state = self.make_test_state("test_rminigsw", operation)
- with connection.schema_editor() as editor:
- operation.database_forwards('test_rminigsw', editor, project_state, new_state)
- operation.database_backwards('test_rminigsw', editor, new_state, project_state)
- class TestCreateModel(SimpleTestCase):
- def test_references_model_mixin(self):
- CreateModel('name', [], bases=(Mixin, models.Model)).references_model('other_model')
- class FieldOperationTests(SimpleTestCase):
- def test_references_model(self):
- operation = FieldOperation('MoDel', 'field', models.ForeignKey('Other', models.CASCADE))
-
- self.assertIs(operation.references_model('mOdEl'), True)
-
- self.assertIs(operation.references_model('oTher'), True)
-
- self.assertIs(operation.references_model('Whatever'), False)
- def test_references_field_by_name(self):
- operation = FieldOperation('MoDel', 'field', models.BooleanField(default=False))
- self.assertIs(operation.references_field('model', 'field'), True)
- def test_references_field_by_remote_field_model(self):
- operation = FieldOperation('Model', 'field', models.ForeignKey('Other', models.CASCADE))
- self.assertIs(operation.references_field('Other', 'whatever'), True)
- self.assertIs(operation.references_field('Missing', 'whatever'), False)
- def test_references_field_by_from_fields(self):
- operation = FieldOperation(
- 'Model', 'field', models.fields.related.ForeignObject('Other', models.CASCADE, ['from'], ['to'])
- )
- self.assertIs(operation.references_field('Model', 'from'), True)
- self.assertIs(operation.references_field('Model', 'to'), False)
- self.assertIs(operation.references_field('Other', 'from'), False)
- self.assertIs(operation.references_field('Model', 'to'), False)
- def test_references_field_by_to_fields(self):
- operation = FieldOperation('Model', 'field', models.ForeignKey('Other', models.CASCADE, to_field='field'))
- self.assertIs(operation.references_field('Other', 'field'), True)
- self.assertIs(operation.references_field('Other', 'whatever'), False)
- self.assertIs(operation.references_field('Missing', 'whatever'), False)
- def test_references_field_by_through(self):
- operation = FieldOperation('Model', 'field', models.ManyToManyField('Other', through='Through'))
- self.assertIs(operation.references_field('Other', 'whatever'), True)
- self.assertIs(operation.references_field('Through', 'whatever'), True)
- self.assertIs(operation.references_field('Missing', 'whatever'), False)
- def test_reference_field_by_through_fields(self):
- operation = FieldOperation(
- 'Model', 'field', models.ManyToManyField('Other', through='Through', through_fields=('first', 'second'))
- )
- self.assertIs(operation.references_field('Other', 'whatever'), True)
- self.assertIs(operation.references_field('Through', 'whatever'), False)
- self.assertIs(operation.references_field('Through', 'first'), True)
- self.assertIs(operation.references_field('Through', 'second'), True)
|