| 1 |
import compileall |
| 2 |
import os |
| 3 |
from importlib import import_module |
| 4 |
|
| 5 |
from django.db import connection, connections |
| 6 |
from django.db.migrations.exceptions import ( |
| 7 |
AmbiguityError, |
| 8 |
InconsistentMigrationHistory, |
| 9 |
NodeNotFoundError, |
| 10 |
) |
| 11 |
from django.db.migrations.loader import MigrationLoader |
| 12 |
from django.db.migrations.recorder import MigrationRecorder |
| 13 |
from django.test import TestCase, modify_settings, override_settings |
| 14 |
|
| 15 |
from .test_base import MigrationTestBase |
| 16 |
|
| 17 |
|
| 18 |
class RecorderTests(TestCase): |
| 19 |
""" |
| 20 |
Tests recording migrations as applied or not. |
| 21 |
""" |
| 22 |
|
| 23 |
databases = {"default", "other"} |
| 24 |
|
| 25 |
def test_apply(self): |
| 26 |
""" |
| 27 |
Tests marking migrations as applied/unapplied. |
| 28 |
""" |
| 29 |
recorder = MigrationRecorder(connection) |
| 30 |
self.assertEqual( |
| 31 |
{(x, y) for (x, y) in recorder.applied_migrations() if x == "myapp"}, |
| 32 |
set(), |
| 33 |
) |
| 34 |
recorder.record_applied("myapp", "0432_ponies") |
| 35 |
self.assertEqual( |
| 36 |
{(x, y) for (x, y) in recorder.applied_migrations() if x == "myapp"}, |
| 37 |
{("myapp", "0432_ponies")}, |
| 38 |
) |
| 39 |
# That should not affect records of another database |
| 40 |
recorder_other = MigrationRecorder(connections["other"]) |
| 41 |
self.assertEqual( |
| 42 |
{(x, y) for (x, y) in recorder_other.applied_migrations() if x == "myapp"}, |
| 43 |
set(), |
| 44 |
) |
| 45 |
recorder.record_unapplied("myapp", "0432_ponies") |
| 46 |
self.assertEqual( |
| 47 |
{(x, y) for (x, y) in recorder.applied_migrations() if x == "myapp"}, |
| 48 |
set(), |
| 49 |
) |
| 50 |
|
| 51 |
def test_has_table_cached(self): |
| 52 |
""" |
| 53 |
The has_table() method caches a positive result and not continually |
| 54 |
query for the existence of the migrations table. |
| 55 |
""" |
| 56 |
recorder = MigrationRecorder(connection) |
| 57 |
self.assertIs(recorder.has_table(), True) |
| 58 |
with self.assertNumQueries(0): |
| 59 |
self.assertIs(recorder.has_table(), True) |
| 60 |
|
| 61 |
|
| 62 |
class LoaderTests(TestCase): |
| 63 |
""" |
| 64 |
Tests the disk and database loader, and running through migrations |
| 65 |
in memory. |
| 66 |
""" |
| 67 |
|
| 68 |
def setUp(self): |
| 69 |
self.applied_records = [] |
| 70 |
|
| 71 |
def tearDown(self): |
| 72 |
# Unapply records on databases that don't roll back changes after each |
| 73 |
# test method. |
| 74 |
if not connection.features.supports_transactions: |
| 75 |
for recorder, app, name in self.applied_records: |
| 76 |
recorder.record_unapplied(app, name) |
| 77 |
|
| 78 |
def record_applied(self, recorder, app, name): |
| 79 |
recorder.record_applied(app, name) |
| 80 |
self.applied_records.append((recorder, app, name)) |
| 81 |
|
| 82 |
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) |
| 83 |
@modify_settings(INSTALLED_APPS={"append": "basic"}) |
| 84 |
def test_load(self): |
| 85 |
""" |
| 86 |
Makes sure the loader can load the migrations for the test apps, |
| 87 |
and then render them out to a new Apps. |
| 88 |
""" |
| 89 |
# Load and test the plan |
| 90 |
migration_loader = MigrationLoader(connection) |
| 91 |
self.assertEqual( |
| 92 |
migration_loader.graph.forwards_plan(("migrations", "0002_second")), |
| 93 |
[ |
| 94 |
("migrations", "0001_initial"), |
| 95 |
("migrations", "0002_second"), |
| 96 |
], |
| 97 |
) |
| 98 |
# Now render it out! |
| 99 |
project_state = migration_loader.project_state(("migrations", "0002_second")) |
| 100 |
self.assertEqual(len(project_state.models), 2) |
| 101 |
|
| 102 |
author_state = project_state.models["migrations", "author"] |
| 103 |
self.assertEqual( |
| 104 |
list(author_state.fields), ["id", "name", "slug", "age", "rating"] |
| 105 |
) |
| 106 |
|
| 107 |
book_state = project_state.models["migrations", "book"] |
| 108 |
self.assertEqual(list(book_state.fields), ["id", "author"]) |
| 109 |
|
| 110 |
# Ensure we've included unmigrated apps in there too |
| 111 |
self.assertIn("basic", project_state.real_apps) |
| 112 |
|
| 113 |
@override_settings( |
| 114 |
MIGRATION_MODULES={ |
| 115 |
"migrations": "migrations.test_migrations", |
| 116 |
"migrations2": "migrations2.test_migrations_2", |
| 117 |
} |
| 118 |
) |
| 119 |
@modify_settings(INSTALLED_APPS={"append": "migrations2"}) |
| 120 |
def test_plan_handles_repeated_migrations(self): |
| 121 |
""" |
| 122 |
_generate_plan() doesn't readd migrations already in the plan (#29180). |
| 123 |
""" |
| 124 |
migration_loader = MigrationLoader(connection) |
| 125 |
nodes = [("migrations", "0002_second"), ("migrations2", "0001_initial")] |
| 126 |
self.assertEqual( |
| 127 |
migration_loader.graph._generate_plan(nodes, at_end=True), |
| 128 |
[ |
| 129 |
("migrations", "0001_initial"), |
| 130 |
("migrations", "0002_second"), |
| 131 |
("migrations2", "0001_initial"), |
| 132 |
], |
| 133 |
) |
| 134 |
|
| 135 |
@override_settings( |
| 136 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_unmigdep"} |
| 137 |
) |
| 138 |
def test_load_unmigrated_dependency(self): |
| 139 |
""" |
| 140 |
The loader can load migrations with a dependency on an unmigrated app. |
| 141 |
""" |
| 142 |
# Load and test the plan |
| 143 |
migration_loader = MigrationLoader(connection) |
| 144 |
self.assertEqual( |
| 145 |
migration_loader.graph.forwards_plan(("migrations", "0001_initial")), |
| 146 |
[ |
| 147 |
("contenttypes", "0001_initial"), |
| 148 |
("auth", "0001_initial"), |
| 149 |
("migrations", "0001_initial"), |
| 150 |
], |
| 151 |
) |
| 152 |
# Now render it out! |
| 153 |
project_state = migration_loader.project_state(("migrations", "0001_initial")) |
| 154 |
self.assertEqual( |
| 155 |
len([m for a, m in project_state.models if a == "migrations"]), 1 |
| 156 |
) |
| 157 |
|
| 158 |
book_state = project_state.models["migrations", "book"] |
| 159 |
self.assertEqual(list(book_state.fields), ["id", "user"]) |
| 160 |
|
| 161 |
@override_settings( |
| 162 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_run_before"} |
| 163 |
) |
| 164 |
def test_run_before(self): |
| 165 |
""" |
| 166 |
Makes sure the loader uses Migration.run_before. |
| 167 |
""" |
| 168 |
# Load and test the plan |
| 169 |
migration_loader = MigrationLoader(connection) |
| 170 |
self.assertEqual( |
| 171 |
migration_loader.graph.forwards_plan(("migrations", "0002_second")), |
| 172 |
[ |
| 173 |
("migrations", "0001_initial"), |
| 174 |
("migrations", "0003_third"), |
| 175 |
("migrations", "0002_second"), |
| 176 |
], |
| 177 |
) |
| 178 |
|
| 179 |
@override_settings( |
| 180 |
MIGRATION_MODULES={ |
| 181 |
"migrations": "migrations.test_migrations_first", |
| 182 |
"migrations2": "migrations2.test_migrations_2_first", |
| 183 |
} |
| 184 |
) |
| 185 |
@modify_settings(INSTALLED_APPS={"append": "migrations2"}) |
| 186 |
def test_first(self): |
| 187 |
""" |
| 188 |
Makes sure the '__first__' migrations build correctly. |
| 189 |
""" |
| 190 |
migration_loader = MigrationLoader(connection) |
| 191 |
self.assertEqual( |
| 192 |
migration_loader.graph.forwards_plan(("migrations", "second")), |
| 193 |
[ |
| 194 |
("migrations", "thefirst"), |
| 195 |
("migrations2", "0001_initial"), |
| 196 |
("migrations2", "0002_second"), |
| 197 |
("migrations", "second"), |
| 198 |
], |
| 199 |
) |
| 200 |
|
| 201 |
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) |
| 202 |
def test_name_match(self): |
| 203 |
"Tests prefix name matching" |
| 204 |
migration_loader = MigrationLoader(connection) |
| 205 |
self.assertEqual( |
| 206 |
migration_loader.get_migration_by_prefix("migrations", "0001").name, |
| 207 |
"0001_initial", |
| 208 |
) |
| 209 |
msg = "There is more than one migration for 'migrations' with the prefix '0'" |
| 210 |
with self.assertRaisesMessage(AmbiguityError, msg): |
| 211 |
migration_loader.get_migration_by_prefix("migrations", "0") |
| 212 |
msg = "There is no migration for 'migrations' with the prefix 'blarg'" |
| 213 |
with self.assertRaisesMessage(KeyError, msg): |
| 214 |
migration_loader.get_migration_by_prefix("migrations", "blarg") |
| 215 |
|
| 216 |
def test_load_import_error(self): |
| 217 |
with override_settings( |
| 218 |
MIGRATION_MODULES={"migrations": "import_error_package"} |
| 219 |
): |
| 220 |
with self.assertRaises(ImportError): |
| 221 |
MigrationLoader(connection) |
| 222 |
|
| 223 |
def test_load_module_file(self): |
| 224 |
with override_settings( |
| 225 |
MIGRATION_MODULES={"migrations": "migrations.faulty_migrations.file"} |
| 226 |
): |
| 227 |
loader = MigrationLoader(connection) |
| 228 |
self.assertIn( |
| 229 |
"migrations", |
| 230 |
loader.unmigrated_apps, |
| 231 |
"App with migrations module file not in unmigrated apps.", |
| 232 |
) |
| 233 |
|
| 234 |
def test_load_empty_dir(self): |
| 235 |
with override_settings( |
| 236 |
MIGRATION_MODULES={"migrations": "migrations.faulty_migrations.namespace"} |
| 237 |
): |
| 238 |
loader = MigrationLoader(connection) |
| 239 |
self.assertIn( |
| 240 |
"migrations", |
| 241 |
loader.unmigrated_apps, |
| 242 |
"App missing __init__.py in migrations module not in unmigrated apps.", |
| 243 |
) |
| 244 |
|
| 245 |
@override_settings( |
| 246 |
INSTALLED_APPS=["migrations.migrations_test_apps.migrated_app"], |
| 247 |
) |
| 248 |
def test_marked_as_migrated(self): |
| 249 |
""" |
| 250 |
Undefined MIGRATION_MODULES implies default migration module. |
| 251 |
""" |
| 252 |
migration_loader = MigrationLoader(connection) |
| 253 |
self.assertEqual(migration_loader.migrated_apps, {"migrated_app"}) |
| 254 |
self.assertEqual(migration_loader.unmigrated_apps, set()) |
| 255 |
|
| 256 |
@override_settings( |
| 257 |
INSTALLED_APPS=["migrations.migrations_test_apps.migrated_app"], |
| 258 |
MIGRATION_MODULES={"migrated_app": None}, |
| 259 |
) |
| 260 |
def test_marked_as_unmigrated(self): |
| 261 |
""" |
| 262 |
MIGRATION_MODULES allows disabling of migrations for a particular app. |
| 263 |
""" |
| 264 |
migration_loader = MigrationLoader(connection) |
| 265 |
self.assertEqual(migration_loader.migrated_apps, set()) |
| 266 |
self.assertEqual(migration_loader.unmigrated_apps, {"migrated_app"}) |
| 267 |
|
| 268 |
@override_settings( |
| 269 |
INSTALLED_APPS=["migrations.migrations_test_apps.migrated_app"], |
| 270 |
MIGRATION_MODULES={"migrated_app": "missing-module"}, |
| 271 |
) |
| 272 |
def test_explicit_missing_module(self): |
| 273 |
""" |
| 274 |
If a MIGRATION_MODULES override points to a missing module, the error |
| 275 |
raised during the importation attempt should be propagated unless |
| 276 |
`ignore_no_migrations=True`. |
| 277 |
""" |
| 278 |
with self.assertRaisesMessage(ImportError, "missing-module"): |
| 279 |
migration_loader = MigrationLoader(connection) |
| 280 |
migration_loader = MigrationLoader(connection, ignore_no_migrations=True) |
| 281 |
self.assertEqual(migration_loader.migrated_apps, set()) |
| 282 |
self.assertEqual(migration_loader.unmigrated_apps, {"migrated_app"}) |
| 283 |
|
| 284 |
@override_settings( |
| 285 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"} |
| 286 |
) |
| 287 |
def test_loading_squashed(self): |
| 288 |
"Tests loading a squashed migration" |
| 289 |
migration_loader = MigrationLoader(connection) |
| 290 |
recorder = MigrationRecorder(connection) |
| 291 |
self.addCleanup(recorder.flush) |
| 292 |
# Loading with nothing applied should just give us the one node |
| 293 |
self.assertEqual( |
| 294 |
len([x for x in migration_loader.graph.nodes if x[0] == "migrations"]), |
| 295 |
1, |
| 296 |
) |
| 297 |
# However, fake-apply one migration and it should now use the old two |
| 298 |
self.record_applied(recorder, "migrations", "0001_initial") |
| 299 |
migration_loader.build_graph() |
| 300 |
self.assertEqual( |
| 301 |
len([x for x in migration_loader.graph.nodes if x[0] == "migrations"]), |
| 302 |
2, |
| 303 |
) |
| 304 |
|
| 305 |
@override_settings( |
| 306 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed_complex"} |
| 307 |
) |
| 308 |
def test_loading_squashed_complex(self): |
| 309 |
"Tests loading a complex set of squashed migrations" |
| 310 |
|
| 311 |
loader = MigrationLoader(connection) |
| 312 |
recorder = MigrationRecorder(connection) |
| 313 |
self.addCleanup(recorder.flush) |
| 314 |
|
| 315 |
def num_nodes(): |
| 316 |
plan = set(loader.graph.forwards_plan(("migrations", "7_auto"))) |
| 317 |
return len(plan - loader.applied_migrations.keys()) |
| 318 |
|
| 319 |
# Empty database: use squashed migration |
| 320 |
loader.build_graph() |
| 321 |
self.assertEqual(num_nodes(), 5) |
| 322 |
|
| 323 |
# Starting at 1 or 2 should use the squashed migration too |
| 324 |
self.record_applied(recorder, "migrations", "1_auto") |
| 325 |
loader.build_graph() |
| 326 |
self.assertEqual(num_nodes(), 4) |
| 327 |
|
| 328 |
self.record_applied(recorder, "migrations", "2_auto") |
| 329 |
loader.build_graph() |
| 330 |
self.assertEqual(num_nodes(), 3) |
| 331 |
|
| 332 |
# However, starting at 3 to 5 cannot use the squashed migration |
| 333 |
self.record_applied(recorder, "migrations", "3_auto") |
| 334 |
loader.build_graph() |
| 335 |
self.assertEqual(num_nodes(), 4) |
| 336 |
|
| 337 |
self.record_applied(recorder, "migrations", "4_auto") |
| 338 |
loader.build_graph() |
| 339 |
self.assertEqual(num_nodes(), 3) |
| 340 |
|
| 341 |
# Starting at 5 to 7 we are past the squashed migrations. |
| 342 |
self.record_applied(recorder, "migrations", "5_auto") |
| 343 |
loader.build_graph() |
| 344 |
self.assertEqual(num_nodes(), 2) |
| 345 |
|
| 346 |
self.record_applied(recorder, "migrations", "6_auto") |
| 347 |
loader.build_graph() |
| 348 |
self.assertEqual(num_nodes(), 1) |
| 349 |
|
| 350 |
self.record_applied(recorder, "migrations", "7_auto") |
| 351 |
loader.build_graph() |
| 352 |
self.assertEqual(num_nodes(), 0) |
| 353 |
|
| 354 |
@override_settings( |
| 355 |
MIGRATION_MODULES={ |
| 356 |
"app1": "migrations.test_migrations_squashed_complex_multi_apps.app1", |
| 357 |
"app2": "migrations.test_migrations_squashed_complex_multi_apps.app2", |
| 358 |
} |
| 359 |
) |
| 360 |
@modify_settings( |
| 361 |
INSTALLED_APPS={ |
| 362 |
"append": [ |
| 363 |
"migrations.test_migrations_squashed_complex_multi_apps.app1", |
| 364 |
"migrations.test_migrations_squashed_complex_multi_apps.app2", |
| 365 |
] |
| 366 |
} |
| 367 |
) |
| 368 |
def test_loading_squashed_complex_multi_apps(self): |
| 369 |
loader = MigrationLoader(connection) |
| 370 |
loader.build_graph() |
| 371 |
|
| 372 |
plan = set(loader.graph.forwards_plan(("app1", "4_auto"))) |
| 373 |
expected_plan = { |
| 374 |
("app1", "1_auto"), |
| 375 |
("app2", "1_squashed_2"), |
| 376 |
("app1", "2_squashed_3"), |
| 377 |
("app1", "4_auto"), |
| 378 |
} |
| 379 |
self.assertEqual(plan, expected_plan) |
| 380 |
|
| 381 |
@override_settings( |
| 382 |
MIGRATION_MODULES={ |
| 383 |
"app1": "migrations.test_migrations_squashed_complex_multi_apps.app1", |
| 384 |
"app2": "migrations.test_migrations_squashed_complex_multi_apps.app2", |
| 385 |
} |
| 386 |
) |
| 387 |
@modify_settings( |
| 388 |
INSTALLED_APPS={ |
| 389 |
"append": [ |
| 390 |
"migrations.test_migrations_squashed_complex_multi_apps.app1", |
| 391 |
"migrations.test_migrations_squashed_complex_multi_apps.app2", |
| 392 |
] |
| 393 |
} |
| 394 |
) |
| 395 |
def test_loading_squashed_complex_multi_apps_partially_applied(self): |
| 396 |
loader = MigrationLoader(connection) |
| 397 |
recorder = MigrationRecorder(connection) |
| 398 |
self.record_applied(recorder, "app1", "1_auto") |
| 399 |
self.record_applied(recorder, "app1", "2_auto") |
| 400 |
loader.build_graph() |
| 401 |
|
| 402 |
plan = set(loader.graph.forwards_plan(("app1", "4_auto"))) |
| 403 |
plan -= loader.applied_migrations.keys() |
| 404 |
expected_plan = { |
| 405 |
("app2", "1_squashed_2"), |
| 406 |
("app1", "3_auto"), |
| 407 |
("app1", "4_auto"), |
| 408 |
} |
| 409 |
|
| 410 |
self.assertEqual(plan, expected_plan) |
| 411 |
|
| 412 |
@override_settings( |
| 413 |
MIGRATION_MODULES={ |
| 414 |
"migrations": "migrations.test_migrations_squashed_erroneous" |
| 415 |
} |
| 416 |
) |
| 417 |
def test_loading_squashed_erroneous(self): |
| 418 |
"Tests loading a complex but erroneous set of squashed migrations" |
| 419 |
|
| 420 |
loader = MigrationLoader(connection) |
| 421 |
recorder = MigrationRecorder(connection) |
| 422 |
self.addCleanup(recorder.flush) |
| 423 |
|
| 424 |
def num_nodes(): |
| 425 |
plan = set(loader.graph.forwards_plan(("migrations", "7_auto"))) |
| 426 |
return len(plan - loader.applied_migrations.keys()) |
| 427 |
|
| 428 |
# Empty database: use squashed migration |
| 429 |
loader.build_graph() |
| 430 |
self.assertEqual(num_nodes(), 5) |
| 431 |
|
| 432 |
# Starting at 1 or 2 should use the squashed migration too |
| 433 |
self.record_applied(recorder, "migrations", "1_auto") |
| 434 |
loader.build_graph() |
| 435 |
self.assertEqual(num_nodes(), 4) |
| 436 |
|
| 437 |
self.record_applied(recorder, "migrations", "2_auto") |
| 438 |
loader.build_graph() |
| 439 |
self.assertEqual(num_nodes(), 3) |
| 440 |
|
| 441 |
# However, starting at 3 or 4, nonexistent migrations would be needed. |
| 442 |
msg = ( |
| 443 |
"Migration migrations.6_auto depends on nonexistent node " |
| 444 |
"('migrations', '5_auto'). Django tried to replace migration " |
| 445 |
"migrations.5_auto with any of [migrations.3_squashed_5] but wasn't able " |
| 446 |
"to because some of the replaced migrations are already applied." |
| 447 |
) |
| 448 |
|
| 449 |
self.record_applied(recorder, "migrations", "3_auto") |
| 450 |
with self.assertRaisesMessage(NodeNotFoundError, msg): |
| 451 |
loader.build_graph() |
| 452 |
|
| 453 |
self.record_applied(recorder, "migrations", "4_auto") |
| 454 |
with self.assertRaisesMessage(NodeNotFoundError, msg): |
| 455 |
loader.build_graph() |
| 456 |
|
| 457 |
# Starting at 5 to 7 we are passed the squashed migrations |
| 458 |
self.record_applied(recorder, "migrations", "5_auto") |
| 459 |
loader.build_graph() |
| 460 |
self.assertEqual(num_nodes(), 2) |
| 461 |
|
| 462 |
self.record_applied(recorder, "migrations", "6_auto") |
| 463 |
loader.build_graph() |
| 464 |
self.assertEqual(num_nodes(), 1) |
| 465 |
|
| 466 |
self.record_applied(recorder, "migrations", "7_auto") |
| 467 |
loader.build_graph() |
| 468 |
self.assertEqual(num_nodes(), 0) |
| 469 |
|
| 470 |
@override_settings( |
| 471 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations"}, |
| 472 |
INSTALLED_APPS=["migrations"], |
| 473 |
) |
| 474 |
def test_check_consistent_history(self): |
| 475 |
loader = MigrationLoader(connection=None) |
| 476 |
loader.check_consistent_history(connection) |
| 477 |
recorder = MigrationRecorder(connection) |
| 478 |
self.record_applied(recorder, "migrations", "0002_second") |
| 479 |
msg = ( |
| 480 |
"Migration migrations.0002_second is applied before its dependency " |
| 481 |
"migrations.0001_initial on database 'default'." |
| 482 |
) |
| 483 |
with self.assertRaisesMessage(InconsistentMigrationHistory, msg): |
| 484 |
loader.check_consistent_history(connection) |
| 485 |
|
| 486 |
@override_settings( |
| 487 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed_extra"}, |
| 488 |
INSTALLED_APPS=["migrations"], |
| 489 |
) |
| 490 |
def test_check_consistent_history_squashed(self): |
| 491 |
""" |
| 492 |
MigrationLoader.check_consistent_history() should ignore unapplied |
| 493 |
squashed migrations that have all of their `replaces` applied. |
| 494 |
""" |
| 495 |
loader = MigrationLoader(connection=None) |
| 496 |
recorder = MigrationRecorder(connection) |
| 497 |
self.record_applied(recorder, "migrations", "0001_initial") |
| 498 |
self.record_applied(recorder, "migrations", "0002_second") |
| 499 |
loader.check_consistent_history(connection) |
| 500 |
self.record_applied(recorder, "migrations", "0003_third") |
| 501 |
loader.check_consistent_history(connection) |
| 502 |
|
| 503 |
@override_settings( |
| 504 |
MIGRATION_MODULES={ |
| 505 |
"app1": "migrations.test_migrations_squashed_ref_squashed.app1", |
| 506 |
"app2": "migrations.test_migrations_squashed_ref_squashed.app2", |
| 507 |
} |
| 508 |
) |
| 509 |
@modify_settings( |
| 510 |
INSTALLED_APPS={ |
| 511 |
"append": [ |
| 512 |
"migrations.test_migrations_squashed_ref_squashed.app1", |
| 513 |
"migrations.test_migrations_squashed_ref_squashed.app2", |
| 514 |
] |
| 515 |
} |
| 516 |
) |
| 517 |
def test_loading_squashed_ref_squashed(self): |
| 518 |
"Tests loading a squashed migration with a new migration referencing it" |
| 519 |
r""" |
| 520 |
The sample migrations are structured like this: |
| 521 |
|
| 522 |
app_1 1 --> 2 ---------------------*--> 3 *--> 4 |
| 523 |
\ / / |
| 524 |
*-------------------*----/--> 2_sq_3 --* |
| 525 |
\ / / |
| 526 |
=============== \ ============= / == / ====================== |
| 527 |
app_2 *--> 1_sq_2 --* / |
| 528 |
\ / |
| 529 |
*--> 1 --> 2 --* |
| 530 |
|
| 531 |
Where 2_sq_3 is a replacing migration for 2 and 3 in app_1, |
| 532 |
as 1_sq_2 is a replacing migration for 1 and 2 in app_2. |
| 533 |
""" |
| 534 |
|
| 535 |
loader = MigrationLoader(connection) |
| 536 |
recorder = MigrationRecorder(connection) |
| 537 |
self.addCleanup(recorder.flush) |
| 538 |
|
| 539 |
# Load with nothing applied: both migrations squashed. |
| 540 |
loader.build_graph() |
| 541 |
plan = set(loader.graph.forwards_plan(("app1", "4_auto"))) |
| 542 |
plan -= loader.applied_migrations.keys() |
| 543 |
expected_plan = { |
| 544 |
("app1", "1_auto"), |
| 545 |
("app2", "1_squashed_2"), |
| 546 |
("app1", "2_squashed_3"), |
| 547 |
("app1", "4_auto"), |
| 548 |
} |
| 549 |
self.assertEqual(plan, expected_plan) |
| 550 |
|
| 551 |
# Load with nothing applied and migrate to a replaced migration. |
| 552 |
# Not possible if loader.replace_migrations is True (default). |
| 553 |
loader.build_graph() |
| 554 |
msg = "Node ('app1', '3_auto') not a valid node" |
| 555 |
with self.assertRaisesMessage(NodeNotFoundError, msg): |
| 556 |
loader.graph.forwards_plan(("app1", "3_auto")) |
| 557 |
# Possible if loader.replace_migrations is False. |
| 558 |
loader.replace_migrations = False |
| 559 |
loader.build_graph() |
| 560 |
plan = set(loader.graph.forwards_plan(("app1", "3_auto"))) |
| 561 |
plan -= loader.applied_migrations.keys() |
| 562 |
expected_plan = { |
| 563 |
("app1", "1_auto"), |
| 564 |
("app2", "1_auto"), |
| 565 |
("app2", "2_auto"), |
| 566 |
("app1", "2_auto"), |
| 567 |
("app1", "3_auto"), |
| 568 |
} |
| 569 |
self.assertEqual(plan, expected_plan) |
| 570 |
loader.replace_migrations = True |
| 571 |
|
| 572 |
# Fake-apply a few from app1: unsquashes migration in app1. |
| 573 |
self.record_applied(recorder, "app1", "1_auto") |
| 574 |
self.record_applied(recorder, "app1", "2_auto") |
| 575 |
loader.build_graph() |
| 576 |
plan = set(loader.graph.forwards_plan(("app1", "4_auto"))) |
| 577 |
plan -= loader.applied_migrations.keys() |
| 578 |
expected_plan = { |
| 579 |
("app2", "1_squashed_2"), |
| 580 |
("app1", "3_auto"), |
| 581 |
("app1", "4_auto"), |
| 582 |
} |
| 583 |
self.assertEqual(plan, expected_plan) |
| 584 |
|
| 585 |
# Fake-apply one from app2: unsquashes migration in app2 too. |
| 586 |
self.record_applied(recorder, "app2", "1_auto") |
| 587 |
loader.build_graph() |
| 588 |
plan = set(loader.graph.forwards_plan(("app1", "4_auto"))) |
| 589 |
plan -= loader.applied_migrations.keys() |
| 590 |
expected_plan = { |
| 591 |
("app2", "2_auto"), |
| 592 |
("app1", "3_auto"), |
| 593 |
("app1", "4_auto"), |
| 594 |
} |
| 595 |
self.assertEqual(plan, expected_plan) |
| 596 |
|
| 597 |
@override_settings( |
| 598 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_private"} |
| 599 |
) |
| 600 |
def test_ignore_files(self): |
| 601 |
"""Files prefixed with underscore, tilde, or dot aren't loaded.""" |
| 602 |
loader = MigrationLoader(connection) |
| 603 |
loader.load_disk() |
| 604 |
migrations = [ |
| 605 |
name for app, name in loader.disk_migrations if app == "migrations" |
| 606 |
] |
| 607 |
self.assertEqual(migrations, ["0001_initial"]) |
| 608 |
|
| 609 |
@override_settings( |
| 610 |
MIGRATION_MODULES={ |
| 611 |
"migrations": "migrations.test_migrations_namespace_package" |
| 612 |
}, |
| 613 |
) |
| 614 |
def test_loading_namespace_package(self): |
| 615 |
"""Migration directories without an __init__.py file are ignored.""" |
| 616 |
loader = MigrationLoader(connection) |
| 617 |
loader.load_disk() |
| 618 |
migrations = [ |
| 619 |
name for app, name in loader.disk_migrations if app == "migrations" |
| 620 |
] |
| 621 |
self.assertEqual(migrations, []) |
| 622 |
|
| 623 |
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) |
| 624 |
def test_loading_package_without__file__(self): |
| 625 |
""" |
| 626 |
To support frozen environments, MigrationLoader loads migrations from |
| 627 |
regular packages with no __file__ attribute. |
| 628 |
""" |
| 629 |
test_module = import_module("migrations.test_migrations") |
| 630 |
loader = MigrationLoader(connection) |
| 631 |
# __file__ == __spec__.origin or the latter is None and former is |
| 632 |
# undefined. |
| 633 |
module_file = test_module.__file__ |
| 634 |
module_origin = test_module.__spec__.origin |
| 635 |
module_has_location = test_module.__spec__.has_location |
| 636 |
try: |
| 637 |
del test_module.__file__ |
| 638 |
test_module.__spec__.origin = None |
| 639 |
test_module.__spec__.has_location = False |
| 640 |
loader.load_disk() |
| 641 |
migrations = [ |
| 642 |
name for app, name in loader.disk_migrations if app == "migrations" |
| 643 |
] |
| 644 |
self.assertCountEqual(migrations, ["0001_initial", "0002_second"]) |
| 645 |
finally: |
| 646 |
test_module.__file__ = module_file |
| 647 |
test_module.__spec__.origin = module_origin |
| 648 |
test_module.__spec__.has_location = module_has_location |
| 649 |
|
| 650 |
|
| 651 |
class PycLoaderTests(MigrationTestBase): |
| 652 |
def test_valid(self): |
| 653 |
""" |
| 654 |
To support frozen environments, MigrationLoader loads .pyc migrations. |
| 655 |
""" |
| 656 |
with self.temporary_migration_module( |
| 657 |
module="migrations.test_migrations" |
| 658 |
) as migration_dir: |
| 659 |
# Compile .py files to .pyc files and delete .py files. |
| 660 |
compileall.compile_dir(migration_dir, force=True, quiet=1, legacy=True) |
| 661 |
for name in os.listdir(migration_dir): |
| 662 |
if name.endswith(".py"): |
| 663 |
os.remove(os.path.join(migration_dir, name)) |
| 664 |
loader = MigrationLoader(connection) |
| 665 |
self.assertIn(("migrations", "0001_initial"), loader.disk_migrations) |
| 666 |
|
| 667 |
def test_invalid(self): |
| 668 |
""" |
| 669 |
MigrationLoader reraises ImportErrors caused by "bad magic number" pyc |
| 670 |
files with a more helpful message. |
| 671 |
""" |
| 672 |
with self.temporary_migration_module( |
| 673 |
module="migrations.test_migrations_bad_pyc" |
| 674 |
) as migration_dir: |
| 675 |
# The -tpl suffix is to avoid the pyc exclusion in MANIFEST.in. |
| 676 |
os.rename( |
| 677 |
os.path.join(migration_dir, "0001_initial.pyc-tpl"), |
| 678 |
os.path.join(migration_dir, "0001_initial.pyc"), |
| 679 |
) |
| 680 |
msg = ( |
| 681 |
r"Couldn't import '\w+.migrations.0001_initial' as it appears " |
| 682 |
"to be a stale .pyc file." |
| 683 |
) |
| 684 |
with self.assertRaisesRegex(ImportError, msg): |
| 685 |
MigrationLoader(connection) |