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