| 1 |
import datetime |
| 2 |
import importlib |
| 3 |
import io |
| 4 |
import os |
| 5 |
import shutil |
| 6 |
import sys |
| 7 |
from pathlib import Path |
| 8 |
from unittest import mock |
| 9 |
|
| 10 |
from django.apps import apps |
| 11 |
from django.core.checks import Error, Tags, register |
| 12 |
from django.core.checks.registry import registry |
| 13 |
from django.core.management import CommandError, call_command |
| 14 |
from django.core.management.base import SystemCheckError |
| 15 |
from django.core.management.commands.makemigrations import ( |
| 16 |
Command as MakeMigrationsCommand, |
| 17 |
) |
| 18 |
from django.core.management.commands.migrate import Command as MigrateCommand |
| 19 |
from django.db import ( |
| 20 |
ConnectionHandler, |
| 21 |
DatabaseError, |
| 22 |
OperationalError, |
| 23 |
connection, |
| 24 |
connections, |
| 25 |
models, |
| 26 |
) |
| 27 |
from django.db.backends.base.schema import BaseDatabaseSchemaEditor |
| 28 |
from django.db.backends.utils import truncate_name |
| 29 |
from django.db.migrations.autodetector import MigrationAutodetector |
| 30 |
from django.db.migrations.exceptions import InconsistentMigrationHistory |
| 31 |
from django.db.migrations.recorder import MigrationRecorder |
| 32 |
from django.test import TestCase, override_settings, skipUnlessDBFeature |
| 33 |
from django.test.utils import captured_stdout, extend_sys_path, isolate_apps |
| 34 |
from django.utils import timezone |
| 35 |
from django.utils.version import get_docs_version |
| 36 |
|
| 37 |
from .models import UnicodeModel, UnserializableModel |
| 38 |
from .routers import TestRouter |
| 39 |
from .test_base import MigrationTestBase |
| 40 |
|
| 41 |
HAS_BLACK = shutil.which("black") |
| 42 |
|
| 43 |
|
| 44 |
class MigrateTests(MigrationTestBase): |
| 45 |
""" |
| 46 |
Tests running the migrate command. |
| 47 |
""" |
| 48 |
|
| 49 |
databases = {"default", "other"} |
| 50 |
|
| 51 |
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) |
| 52 |
def test_migrate(self): |
| 53 |
""" |
| 54 |
Tests basic usage of the migrate command. |
| 55 |
""" |
| 56 |
# No tables are created |
| 57 |
self.assertTableNotExists("migrations_author") |
| 58 |
self.assertTableNotExists("migrations_tribble") |
| 59 |
self.assertTableNotExists("migrations_book") |
| 60 |
# Run the migrations to 0001 only |
| 61 |
stdout = io.StringIO() |
| 62 |
call_command( |
| 63 |
"migrate", "migrations", "0001", verbosity=2, stdout=stdout, no_color=True |
| 64 |
) |
| 65 |
stdout = stdout.getvalue() |
| 66 |
self.assertIn( |
| 67 |
"Target specific migration: 0001_initial, from migrations", stdout |
| 68 |
) |
| 69 |
self.assertIn("Applying migrations.0001_initial... OK", stdout) |
| 70 |
self.assertIn("Running pre-migrate handlers for application migrations", stdout) |
| 71 |
self.assertIn( |
| 72 |
"Running post-migrate handlers for application migrations", stdout |
| 73 |
) |
| 74 |
# The correct tables exist |
| 75 |
self.assertTableExists("migrations_author") |
| 76 |
self.assertTableExists("migrations_tribble") |
| 77 |
self.assertTableNotExists("migrations_book") |
| 78 |
# Run migrations all the way |
| 79 |
call_command("migrate", verbosity=0) |
| 80 |
# The correct tables exist |
| 81 |
self.assertTableExists("migrations_author") |
| 82 |
self.assertTableNotExists("migrations_tribble") |
| 83 |
self.assertTableExists("migrations_book") |
| 84 |
# Unmigrate everything |
| 85 |
stdout = io.StringIO() |
| 86 |
call_command( |
| 87 |
"migrate", "migrations", "zero", verbosity=2, stdout=stdout, no_color=True |
| 88 |
) |
| 89 |
stdout = stdout.getvalue() |
| 90 |
self.assertIn("Unapply all migrations: migrations", stdout) |
| 91 |
self.assertIn("Unapplying migrations.0002_second... OK", stdout) |
| 92 |
self.assertIn("Running pre-migrate handlers for application migrations", stdout) |
| 93 |
self.assertIn( |
| 94 |
"Running post-migrate handlers for application migrations", stdout |
| 95 |
) |
| 96 |
# Tables are gone |
| 97 |
self.assertTableNotExists("migrations_author") |
| 98 |
self.assertTableNotExists("migrations_tribble") |
| 99 |
self.assertTableNotExists("migrations_book") |
| 100 |
|
| 101 |
@mock.patch("django.core.management.base.BaseCommand.check") |
| 102 |
@override_settings( |
| 103 |
INSTALLED_APPS=[ |
| 104 |
"django.contrib.auth", |
| 105 |
"django.contrib.contenttypes", |
| 106 |
"migrations.migrations_test_apps.migrated_app", |
| 107 |
] |
| 108 |
) |
| 109 |
def test_migrate_with_system_checks(self, mocked_check): |
| 110 |
out = io.StringIO() |
| 111 |
call_command("migrate", skip_checks=False, no_color=True, stdout=out) |
| 112 |
self.assertIn("Apply all migrations: migrated_app", out.getvalue()) |
| 113 |
mocked_check.assert_called_once() |
| 114 |
|
| 115 |
def test_migrate_with_custom_system_checks(self): |
| 116 |
original_checks = registry.registered_checks.copy() |
| 117 |
|
| 118 |
@register(Tags.signals) |
| 119 |
def my_check(app_configs, **kwargs): |
| 120 |
return [Error("my error")] |
| 121 |
|
| 122 |
self.addCleanup(setattr, registry, "registered_checks", original_checks) |
| 123 |
|
| 124 |
class CustomMigrateCommandWithSignalsChecks(MigrateCommand): |
| 125 |
requires_system_checks = [Tags.signals] |
| 126 |
|
| 127 |
command = CustomMigrateCommandWithSignalsChecks() |
| 128 |
with self.assertRaises(SystemCheckError): |
| 129 |
call_command(command, skip_checks=False, stderr=io.StringIO()) |
| 130 |
|
| 131 |
class CustomMigrateCommandWithSecurityChecks(MigrateCommand): |
| 132 |
requires_system_checks = [Tags.security] |
| 133 |
|
| 134 |
command = CustomMigrateCommandWithSecurityChecks() |
| 135 |
call_command(command, skip_checks=False, stdout=io.StringIO()) |
| 136 |
|
| 137 |
@override_settings( |
| 138 |
INSTALLED_APPS=[ |
| 139 |
"migrations", |
| 140 |
"migrations.migrations_test_apps.unmigrated_app_syncdb", |
| 141 |
] |
| 142 |
) |
| 143 |
def test_app_without_migrations(self): |
| 144 |
msg = "App 'unmigrated_app_syncdb' does not have migrations." |
| 145 |
with self.assertRaisesMessage(CommandError, msg): |
| 146 |
call_command("migrate", app_label="unmigrated_app_syncdb") |
| 147 |
|
| 148 |
@override_settings( |
| 149 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_clashing_prefix"} |
| 150 |
) |
| 151 |
def test_ambiguous_prefix(self): |
| 152 |
msg = ( |
| 153 |
"More than one migration matches 'a' in app 'migrations'. Please " |
| 154 |
"be more specific." |
| 155 |
) |
| 156 |
with self.assertRaisesMessage(CommandError, msg): |
| 157 |
call_command("migrate", app_label="migrations", migration_name="a") |
| 158 |
|
| 159 |
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) |
| 160 |
def test_unknown_prefix(self): |
| 161 |
msg = "Cannot find a migration matching 'nonexistent' from app 'migrations'." |
| 162 |
with self.assertRaisesMessage(CommandError, msg): |
| 163 |
call_command( |
| 164 |
"migrate", app_label="migrations", migration_name="nonexistent" |
| 165 |
) |
| 166 |
|
| 167 |
@override_settings( |
| 168 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_initial_false"} |
| 169 |
) |
| 170 |
def test_migrate_initial_false(self): |
| 171 |
""" |
| 172 |
`Migration.initial = False` skips fake-initial detection. |
| 173 |
""" |
| 174 |
# Make sure no tables are created |
| 175 |
self.assertTableNotExists("migrations_author") |
| 176 |
self.assertTableNotExists("migrations_tribble") |
| 177 |
# Run the migrations to 0001 only |
| 178 |
call_command("migrate", "migrations", "0001", verbosity=0) |
| 179 |
# Fake rollback |
| 180 |
call_command("migrate", "migrations", "zero", fake=True, verbosity=0) |
| 181 |
# Make sure fake-initial detection does not run |
| 182 |
with self.assertRaises(DatabaseError): |
| 183 |
call_command( |
| 184 |
"migrate", "migrations", "0001", fake_initial=True, verbosity=0 |
| 185 |
) |
| 186 |
|
| 187 |
call_command("migrate", "migrations", "0001", fake=True, verbosity=0) |
| 188 |
# Real rollback |
| 189 |
call_command("migrate", "migrations", "zero", verbosity=0) |
| 190 |
# Make sure it's all gone |
| 191 |
self.assertTableNotExists("migrations_author") |
| 192 |
self.assertTableNotExists("migrations_tribble") |
| 193 |
self.assertTableNotExists("migrations_book") |
| 194 |
|
| 195 |
@override_settings( |
| 196 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations"}, |
| 197 |
DATABASE_ROUTERS=["migrations.routers.TestRouter"], |
| 198 |
) |
| 199 |
def test_migrate_fake_initial(self): |
| 200 |
""" |
| 201 |
--fake-initial only works if all tables created in the initial |
| 202 |
migration of an app exists. Database routers must be obeyed when doing |
| 203 |
that check. |
| 204 |
""" |
| 205 |
# Make sure no tables are created |
| 206 |
for db in self.databases: |
| 207 |
self.assertTableNotExists("migrations_author", using=db) |
| 208 |
self.assertTableNotExists("migrations_tribble", using=db) |
| 209 |
|
| 210 |
try: |
| 211 |
# Run the migrations to 0001 only |
| 212 |
call_command("migrate", "migrations", "0001", verbosity=0) |
| 213 |
call_command("migrate", "migrations", "0001", verbosity=0, database="other") |
| 214 |
# Make sure the right tables exist |
| 215 |
self.assertTableExists("migrations_author") |
| 216 |
self.assertTableNotExists("migrations_tribble") |
| 217 |
# Also check the "other" database |
| 218 |
self.assertTableNotExists("migrations_author", using="other") |
| 219 |
self.assertTableExists("migrations_tribble", using="other") |
| 220 |
# Fake a roll-back |
| 221 |
call_command("migrate", "migrations", "zero", fake=True, verbosity=0) |
| 222 |
call_command( |
| 223 |
"migrate", |
| 224 |
"migrations", |
| 225 |
"zero", |
| 226 |
fake=True, |
| 227 |
verbosity=0, |
| 228 |
database="other", |
| 229 |
) |
| 230 |
# Make sure the tables still exist |
| 231 |
self.assertTableExists("migrations_author") |
| 232 |
self.assertTableExists("migrations_tribble", using="other") |
| 233 |
# Try to run initial migration |
| 234 |
with self.assertRaises(DatabaseError): |
| 235 |
call_command("migrate", "migrations", "0001", verbosity=0) |
| 236 |
# Run initial migration with an explicit --fake-initial |
| 237 |
out = io.StringIO() |
| 238 |
with mock.patch( |
| 239 |
"django.core.management.color.supports_color", lambda *args: False |
| 240 |
): |
| 241 |
call_command( |
| 242 |
"migrate", |
| 243 |
"migrations", |
| 244 |
"0001", |
| 245 |
fake_initial=True, |
| 246 |
stdout=out, |
| 247 |
verbosity=1, |
| 248 |
) |
| 249 |
call_command( |
| 250 |
"migrate", |
| 251 |
"migrations", |
| 252 |
"0001", |
| 253 |
fake_initial=True, |
| 254 |
verbosity=0, |
| 255 |
database="other", |
| 256 |
) |
| 257 |
self.assertIn("migrations.0001_initial... faked", out.getvalue().lower()) |
| 258 |
|
| 259 |
# Run migrations all the way. |
| 260 |
call_command("migrate", verbosity=0) |
| 261 |
call_command("migrate", verbosity=0, database="other") |
| 262 |
self.assertTableExists("migrations_author") |
| 263 |
self.assertTableNotExists("migrations_tribble") |
| 264 |
self.assertTableExists("migrations_book") |
| 265 |
self.assertTableNotExists("migrations_author", using="other") |
| 266 |
self.assertTableNotExists("migrations_tribble", using="other") |
| 267 |
self.assertTableNotExists("migrations_book", using="other") |
| 268 |
# Fake a roll-back. |
| 269 |
call_command("migrate", "migrations", "zero", fake=True, verbosity=0) |
| 270 |
call_command( |
| 271 |
"migrate", |
| 272 |
"migrations", |
| 273 |
"zero", |
| 274 |
fake=True, |
| 275 |
verbosity=0, |
| 276 |
database="other", |
| 277 |
) |
| 278 |
self.assertTableExists("migrations_author") |
| 279 |
self.assertTableNotExists("migrations_tribble") |
| 280 |
self.assertTableExists("migrations_book") |
| 281 |
# Run initial migration. |
| 282 |
with self.assertRaises(DatabaseError): |
| 283 |
call_command("migrate", "migrations", verbosity=0) |
| 284 |
# Run initial migration with an explicit --fake-initial. |
| 285 |
with self.assertRaises(DatabaseError): |
| 286 |
# Fails because "migrations_tribble" does not exist but needs |
| 287 |
# to in order to make --fake-initial work. |
| 288 |
call_command("migrate", "migrations", fake_initial=True, verbosity=0) |
| 289 |
# Fake an apply. |
| 290 |
call_command("migrate", "migrations", fake=True, verbosity=0) |
| 291 |
call_command( |
| 292 |
"migrate", "migrations", fake=True, verbosity=0, database="other" |
| 293 |
) |
| 294 |
finally: |
| 295 |
# Unmigrate everything. |
| 296 |
call_command("migrate", "migrations", "zero", verbosity=0) |
| 297 |
call_command("migrate", "migrations", "zero", verbosity=0, database="other") |
| 298 |
# Make sure it's all gone |
| 299 |
for db in self.databases: |
| 300 |
self.assertTableNotExists("migrations_author", using=db) |
| 301 |
self.assertTableNotExists("migrations_tribble", using=db) |
| 302 |
self.assertTableNotExists("migrations_book", using=db) |
| 303 |
|
| 304 |
@skipUnlessDBFeature("ignores_table_name_case") |
| 305 |
def test_migrate_fake_initial_case_insensitive(self): |
| 306 |
with override_settings( |
| 307 |
MIGRATION_MODULES={ |
| 308 |
"migrations": "migrations.test_fake_initial_case_insensitive.initial", |
| 309 |
} |
| 310 |
): |
| 311 |
call_command("migrate", "migrations", "0001", verbosity=0) |
| 312 |
call_command("migrate", "migrations", "zero", fake=True, verbosity=0) |
| 313 |
|
| 314 |
with override_settings( |
| 315 |
MIGRATION_MODULES={ |
| 316 |
"migrations": ( |
| 317 |
"migrations.test_fake_initial_case_insensitive.fake_initial" |
| 318 |
), |
| 319 |
} |
| 320 |
): |
| 321 |
out = io.StringIO() |
| 322 |
call_command( |
| 323 |
"migrate", |
| 324 |
"migrations", |
| 325 |
"0001", |
| 326 |
fake_initial=True, |
| 327 |
stdout=out, |
| 328 |
verbosity=1, |
| 329 |
no_color=True, |
| 330 |
) |
| 331 |
self.assertIn( |
| 332 |
"migrations.0001_initial... faked", |
| 333 |
out.getvalue().lower(), |
| 334 |
) |
| 335 |
|
| 336 |
@override_settings( |
| 337 |
MIGRATION_MODULES={ |
| 338 |
"migrations": "migrations.test_migrations_fake_split_initial" |
| 339 |
} |
| 340 |
) |
| 341 |
def test_migrate_fake_split_initial(self): |
| 342 |
""" |
| 343 |
Split initial migrations can be faked with --fake-initial. |
| 344 |
""" |
| 345 |
try: |
| 346 |
call_command("migrate", "migrations", "0002", verbosity=0) |
| 347 |
call_command("migrate", "migrations", "zero", fake=True, verbosity=0) |
| 348 |
out = io.StringIO() |
| 349 |
with mock.patch( |
| 350 |
"django.core.management.color.supports_color", lambda *args: False |
| 351 |
): |
| 352 |
call_command( |
| 353 |
"migrate", |
| 354 |
"migrations", |
| 355 |
"0002", |
| 356 |
fake_initial=True, |
| 357 |
stdout=out, |
| 358 |
verbosity=1, |
| 359 |
) |
| 360 |
value = out.getvalue().lower() |
| 361 |
self.assertIn("migrations.0001_initial... faked", value) |
| 362 |
self.assertIn("migrations.0002_second... faked", value) |
| 363 |
finally: |
| 364 |
# Fake an apply. |
| 365 |
call_command("migrate", "migrations", fake=True, verbosity=0) |
| 366 |
# Unmigrate everything. |
| 367 |
call_command("migrate", "migrations", "zero", verbosity=0) |
| 368 |
|
| 369 |
@override_settings( |
| 370 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_conflict"} |
| 371 |
) |
| 372 |
def test_migrate_conflict_exit(self): |
| 373 |
""" |
| 374 |
migrate exits if it detects a conflict. |
| 375 |
""" |
| 376 |
msg = ( |
| 377 |
"Conflicting migrations detected; multiple leaf nodes in the " |
| 378 |
"migration graph: (0002_conflicting_second, 0002_second in " |
| 379 |
"migrations).\n" |
| 380 |
"To fix them run 'python manage.py makemigrations --merge'" |
| 381 |
) |
| 382 |
with self.assertRaisesMessage(CommandError, msg): |
| 383 |
call_command("migrate", "migrations") |
| 384 |
|
| 385 |
@override_settings( |
| 386 |
MIGRATION_MODULES={ |
| 387 |
"migrations": "migrations.test_migrations", |
| 388 |
} |
| 389 |
) |
| 390 |
def test_migrate_check(self): |
| 391 |
with self.assertRaises(SystemExit): |
| 392 |
call_command("migrate", "migrations", "0001", check_unapplied=True) |
| 393 |
self.assertTableNotExists("migrations_author") |
| 394 |
self.assertTableNotExists("migrations_tribble") |
| 395 |
self.assertTableNotExists("migrations_book") |
| 396 |
|
| 397 |
@override_settings( |
| 398 |
INSTALLED_APPS=[ |
| 399 |
"migrations.migrations_test_apps.migrated_app", |
| 400 |
] |
| 401 |
) |
| 402 |
def test_migrate_check_migrated_app(self): |
| 403 |
out = io.StringIO() |
| 404 |
try: |
| 405 |
call_command("migrate", "migrated_app", verbosity=0) |
| 406 |
call_command( |
| 407 |
"migrate", |
| 408 |
"migrated_app", |
| 409 |
stdout=out, |
| 410 |
check_unapplied=True, |
| 411 |
) |
| 412 |
self.assertEqual(out.getvalue(), "") |
| 413 |
finally: |
| 414 |
# Unmigrate everything. |
| 415 |
call_command("migrate", "migrated_app", "zero", verbosity=0) |
| 416 |
|
| 417 |
@override_settings( |
| 418 |
MIGRATION_MODULES={ |
| 419 |
"migrations": "migrations.test_migrations_plan", |
| 420 |
} |
| 421 |
) |
| 422 |
def test_migrate_check_plan(self): |
| 423 |
out = io.StringIO() |
| 424 |
with self.assertRaises(SystemExit): |
| 425 |
call_command( |
| 426 |
"migrate", |
| 427 |
"migrations", |
| 428 |
"0001", |
| 429 |
check_unapplied=True, |
| 430 |
plan=True, |
| 431 |
stdout=out, |
| 432 |
no_color=True, |
| 433 |
) |
| 434 |
self.assertEqual( |
| 435 |
"Planned operations:\n" |
| 436 |
"migrations.0001_initial\n" |
| 437 |
" Create model Salamander\n" |
| 438 |
" Raw Python operation -> Grow salamander tail.\n", |
| 439 |
out.getvalue(), |
| 440 |
) |
| 441 |
|
| 442 |
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) |
| 443 |
def test_showmigrations_list(self): |
| 444 |
""" |
| 445 |
showmigrations --list displays migrations and whether or not they're |
| 446 |
applied. |
| 447 |
""" |
| 448 |
out = io.StringIO() |
| 449 |
with mock.patch( |
| 450 |
"django.core.management.color.supports_color", lambda *args: True |
| 451 |
): |
| 452 |
call_command( |
| 453 |
"showmigrations", format="list", stdout=out, verbosity=0, no_color=False |
| 454 |
) |
| 455 |
self.assertEqual( |
| 456 |
"\x1b[1mmigrations\n\x1b[0m [ ] 0001_initial\n [ ] 0002_second\n", |
| 457 |
out.getvalue().lower(), |
| 458 |
) |
| 459 |
|
| 460 |
call_command("migrate", "migrations", "0001", verbosity=0) |
| 461 |
|
| 462 |
out = io.StringIO() |
| 463 |
# Giving the explicit app_label tests for selective `show_list` in the command |
| 464 |
call_command( |
| 465 |
"showmigrations", |
| 466 |
"migrations", |
| 467 |
format="list", |
| 468 |
stdout=out, |
| 469 |
verbosity=0, |
| 470 |
no_color=True, |
| 471 |
) |
| 472 |
self.assertEqual( |
| 473 |
"migrations\n [x] 0001_initial\n [ ] 0002_second\n", out.getvalue().lower() |
| 474 |
) |
| 475 |
out = io.StringIO() |
| 476 |
# Applied datetimes are displayed at verbosity 2+. |
| 477 |
call_command( |
| 478 |
"showmigrations", "migrations", stdout=out, verbosity=2, no_color=True |
| 479 |
) |
| 480 |
migration1 = MigrationRecorder(connection).migration_qs.get( |
| 481 |
app="migrations", name="0001_initial" |
| 482 |
) |
| 483 |
self.assertEqual( |
| 484 |
"migrations\n" |
| 485 |
" [x] 0001_initial (applied at %s)\n" |
| 486 |
" [ ] 0002_second\n" % migration1.applied.strftime("%Y-%m-%d %H:%M:%S"), |
| 487 |
out.getvalue().lower(), |
| 488 |
) |
| 489 |
# Cleanup by unmigrating everything |
| 490 |
call_command("migrate", "migrations", "zero", verbosity=0) |
| 491 |
|
| 492 |
@override_settings( |
| 493 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"} |
| 494 |
) |
| 495 |
def test_showmigrations_list_squashed(self): |
| 496 |
out = io.StringIO() |
| 497 |
call_command( |
| 498 |
"showmigrations", format="list", stdout=out, verbosity=2, no_color=True |
| 499 |
) |
| 500 |
self.assertEqual( |
| 501 |
"migrations\n [ ] 0001_squashed_0002 (2 squashed migrations)\n", |
| 502 |
out.getvalue().lower(), |
| 503 |
) |
| 504 |
out = io.StringIO() |
| 505 |
call_command( |
| 506 |
"migrate", |
| 507 |
"migrations", |
| 508 |
"0001_squashed_0002", |
| 509 |
stdout=out, |
| 510 |
verbosity=2, |
| 511 |
no_color=True, |
| 512 |
) |
| 513 |
try: |
| 514 |
self.assertIn( |
| 515 |
"operations to perform:\n" |
| 516 |
" target specific migration: 0001_squashed_0002, from migrations\n" |
| 517 |
"running pre-migrate handlers for application migrations\n" |
| 518 |
"running migrations:\n" |
| 519 |
" applying migrations.0001_squashed_0002... ok (", |
| 520 |
out.getvalue().lower(), |
| 521 |
) |
| 522 |
out = io.StringIO() |
| 523 |
call_command( |
| 524 |
"showmigrations", format="list", stdout=out, verbosity=2, no_color=True |
| 525 |
) |
| 526 |
self.assertEqual( |
| 527 |
"migrations\n [x] 0001_squashed_0002 (2 squashed migrations)\n", |
| 528 |
out.getvalue().lower(), |
| 529 |
) |
| 530 |
finally: |
| 531 |
# Unmigrate everything. |
| 532 |
call_command("migrate", "migrations", "zero", verbosity=0) |
| 533 |
|
| 534 |
@override_settings( |
| 535 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_run_before"} |
| 536 |
) |
| 537 |
def test_showmigrations_plan(self): |
| 538 |
""" |
| 539 |
Tests --plan output of showmigrations command |
| 540 |
""" |
| 541 |
out = io.StringIO() |
| 542 |
call_command("showmigrations", format="plan", stdout=out) |
| 543 |
self.assertEqual( |
| 544 |
"[ ] migrations.0001_initial\n" |
| 545 |
"[ ] migrations.0003_third\n" |
| 546 |
"[ ] migrations.0002_second\n", |
| 547 |
out.getvalue().lower(), |
| 548 |
) |
| 549 |
|
| 550 |
out = io.StringIO() |
| 551 |
call_command("showmigrations", format="plan", stdout=out, verbosity=2) |
| 552 |
self.assertEqual( |
| 553 |
"[ ] migrations.0001_initial\n" |
| 554 |
"[ ] migrations.0003_third ... (migrations.0001_initial)\n" |
| 555 |
"[ ] migrations.0002_second ... (migrations.0001_initial, " |
| 556 |
"migrations.0003_third)\n", |
| 557 |
out.getvalue().lower(), |
| 558 |
) |
| 559 |
call_command("migrate", "migrations", "0003", verbosity=0) |
| 560 |
|
| 561 |
out = io.StringIO() |
| 562 |
call_command("showmigrations", format="plan", stdout=out) |
| 563 |
self.assertEqual( |
| 564 |
"[x] migrations.0001_initial\n" |
| 565 |
"[x] migrations.0003_third\n" |
| 566 |
"[ ] migrations.0002_second\n", |
| 567 |
out.getvalue().lower(), |
| 568 |
) |
| 569 |
|
| 570 |
out = io.StringIO() |
| 571 |
call_command("showmigrations", format="plan", stdout=out, verbosity=2) |
| 572 |
self.assertEqual( |
| 573 |
"[x] migrations.0001_initial\n" |
| 574 |
"[x] migrations.0003_third ... (migrations.0001_initial)\n" |
| 575 |
"[ ] migrations.0002_second ... (migrations.0001_initial, " |
| 576 |
"migrations.0003_third)\n", |
| 577 |
out.getvalue().lower(), |
| 578 |
) |
| 579 |
|
| 580 |
# Cleanup by unmigrating everything |
| 581 |
call_command("migrate", "migrations", "zero", verbosity=0) |
| 582 |
|
| 583 |
@override_settings( |
| 584 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_plan"} |
| 585 |
) |
| 586 |
def test_migrate_plan(self): |
| 587 |
"""Tests migrate --plan output.""" |
| 588 |
out = io.StringIO() |
| 589 |
# Show the plan up to the third migration. |
| 590 |
call_command( |
| 591 |
"migrate", "migrations", "0003", plan=True, stdout=out, no_color=True |
| 592 |
) |
| 593 |
self.assertEqual( |
| 594 |
"Planned operations:\n" |
| 595 |
"migrations.0001_initial\n" |
| 596 |
" Create model Salamander\n" |
| 597 |
" Raw Python operation -> Grow salamander tail.\n" |
| 598 |
"migrations.0002_second\n" |
| 599 |
" Create model Book\n" |
| 600 |
" Raw SQL operation -> ['SELECT * FROM migrations_book']\n" |
| 601 |
"migrations.0003_third\n" |
| 602 |
" Create model Author\n" |
| 603 |
" Raw SQL operation -> ['SELECT * FROM migrations_author']\n", |
| 604 |
out.getvalue(), |
| 605 |
) |
| 606 |
try: |
| 607 |
# Migrate to the third migration. |
| 608 |
call_command("migrate", "migrations", "0003", verbosity=0) |
| 609 |
out = io.StringIO() |
| 610 |
# Show the plan for when there is nothing to apply. |
| 611 |
call_command( |
| 612 |
"migrate", "migrations", "0003", plan=True, stdout=out, no_color=True |
| 613 |
) |
| 614 |
self.assertEqual( |
| 615 |
"Planned operations:\n No planned migration operations.\n", |
| 616 |
out.getvalue(), |
| 617 |
) |
| 618 |
out = io.StringIO() |
| 619 |
# Show the plan for reverse migration back to 0001. |
| 620 |
call_command( |
| 621 |
"migrate", "migrations", "0001", plan=True, stdout=out, no_color=True |
| 622 |
) |
| 623 |
self.assertEqual( |
| 624 |
"Planned operations:\n" |
| 625 |
"migrations.0003_third\n" |
| 626 |
" Undo Create model Author\n" |
| 627 |
" Raw SQL operation -> ['SELECT * FROM migrations_book']\n" |
| 628 |
"migrations.0002_second\n" |
| 629 |
" Undo Create model Book\n" |
| 630 |
" Raw SQL operation -> ['SELECT * FROM migrations_salamand…\n", |
| 631 |
out.getvalue(), |
| 632 |
) |
| 633 |
out = io.StringIO() |
| 634 |
# Show the migration plan to fourth, with truncated details. |
| 635 |
call_command( |
| 636 |
"migrate", "migrations", "0004", plan=True, stdout=out, no_color=True |
| 637 |
) |
| 638 |
self.assertEqual( |
| 639 |
"Planned operations:\n" |
| 640 |
"migrations.0004_fourth\n" |
| 641 |
" Raw SQL operation -> SELECT * FROM migrations_author WHE…\n", |
| 642 |
out.getvalue(), |
| 643 |
) |
| 644 |
# Show the plan when an operation is irreversible. |
| 645 |
# Migrate to the fourth migration. |
| 646 |
call_command("migrate", "migrations", "0004", verbosity=0) |
| 647 |
out = io.StringIO() |
| 648 |
call_command( |
| 649 |
"migrate", "migrations", "0003", plan=True, stdout=out, no_color=True |
| 650 |
) |
| 651 |
self.assertEqual( |
| 652 |
"Planned operations:\n" |
| 653 |
"migrations.0004_fourth\n" |
| 654 |
" Raw SQL operation -> IRREVERSIBLE\n", |
| 655 |
out.getvalue(), |
| 656 |
) |
| 657 |
out = io.StringIO() |
| 658 |
call_command( |
| 659 |
"migrate", "migrations", "0005", plan=True, stdout=out, no_color=True |
| 660 |
) |
| 661 |
# Operation is marked as irreversible only in the revert plan. |
| 662 |
self.assertEqual( |
| 663 |
"Planned operations:\n" |
| 664 |
"migrations.0005_fifth\n" |
| 665 |
" Raw Python operation\n" |
| 666 |
" Raw Python operation\n" |
| 667 |
" Raw Python operation -> Feed salamander.\n", |
| 668 |
out.getvalue(), |
| 669 |
) |
| 670 |
call_command("migrate", "migrations", "0005", verbosity=0) |
| 671 |
out = io.StringIO() |
| 672 |
call_command( |
| 673 |
"migrate", "migrations", "0004", plan=True, stdout=out, no_color=True |
| 674 |
) |
| 675 |
self.assertEqual( |
| 676 |
"Planned operations:\n" |
| 677 |
"migrations.0005_fifth\n" |
| 678 |
" Raw Python operation -> IRREVERSIBLE\n" |
| 679 |
" Raw Python operation -> IRREVERSIBLE\n" |
| 680 |
" Raw Python operation\n", |
| 681 |
out.getvalue(), |
| 682 |
) |
| 683 |
finally: |
| 684 |
# Cleanup by unmigrating everything: fake the irreversible, then |
| 685 |
# migrate all to zero. |
| 686 |
call_command("migrate", "migrations", "0003", fake=True, verbosity=0) |
| 687 |
call_command("migrate", "migrations", "zero", verbosity=0) |
| 688 |
|
| 689 |
@override_settings( |
| 690 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_empty"} |
| 691 |
) |
| 692 |
def test_showmigrations_no_migrations(self): |
| 693 |
out = io.StringIO() |
| 694 |
call_command("showmigrations", stdout=out, no_color=True) |
| 695 |
self.assertEqual("migrations\n (no migrations)\n", out.getvalue().lower()) |
| 696 |
|
| 697 |
@override_settings( |
| 698 |
INSTALLED_APPS=["migrations.migrations_test_apps.unmigrated_app"] |
| 699 |
) |
| 700 |
def test_showmigrations_unmigrated_app(self): |
| 701 |
out = io.StringIO() |
| 702 |
call_command("showmigrations", "unmigrated_app", stdout=out, no_color=True) |
| 703 |
try: |
| 704 |
self.assertEqual( |
| 705 |
"unmigrated_app\n (no migrations)\n", out.getvalue().lower() |
| 706 |
) |
| 707 |
finally: |
| 708 |
# unmigrated_app.SillyModel has a foreign key to |
| 709 |
# 'migrations.Tribble', but that model is only defined in a |
| 710 |
# migration, so the global app registry never sees it and the |
| 711 |
# reference is left dangling. Remove it to avoid problems in |
| 712 |
# subsequent tests. |
| 713 |
apps._pending_operations.pop(("migrations", "tribble"), None) |
| 714 |
|
| 715 |
@override_settings( |
| 716 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_empty"} |
| 717 |
) |
| 718 |
def test_showmigrations_plan_no_migrations(self): |
| 719 |
""" |
| 720 |
Tests --plan output of showmigrations command without migrations |
| 721 |
""" |
| 722 |
out = io.StringIO() |
| 723 |
call_command("showmigrations", format="plan", stdout=out, no_color=True) |
| 724 |
self.assertEqual("(no migrations)\n", out.getvalue().lower()) |
| 725 |
|
| 726 |
out = io.StringIO() |
| 727 |
call_command( |
| 728 |
"showmigrations", format="plan", stdout=out, verbosity=2, no_color=True |
| 729 |
) |
| 730 |
self.assertEqual("(no migrations)\n", out.getvalue().lower()) |
| 731 |
|
| 732 |
@override_settings( |
| 733 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed_complex"} |
| 734 |
) |
| 735 |
def test_showmigrations_plan_squashed(self): |
| 736 |
""" |
| 737 |
Tests --plan output of showmigrations command with squashed migrations. |
| 738 |
""" |
| 739 |
out = io.StringIO() |
| 740 |
call_command("showmigrations", format="plan", stdout=out) |
| 741 |
self.assertEqual( |
| 742 |
"[ ] migrations.1_auto\n" |
| 743 |
"[ ] migrations.2_auto\n" |
| 744 |
"[ ] migrations.3_squashed_5\n" |
| 745 |
"[ ] migrations.6_auto\n" |
| 746 |
"[ ] migrations.7_auto\n", |
| 747 |
out.getvalue().lower(), |
| 748 |
) |
| 749 |
|
| 750 |
out = io.StringIO() |
| 751 |
call_command("showmigrations", format="plan", stdout=out, verbosity=2) |
| 752 |
self.assertEqual( |
| 753 |
"[ ] migrations.1_auto\n" |
| 754 |
"[ ] migrations.2_auto ... (migrations.1_auto)\n" |
| 755 |
"[ ] migrations.3_squashed_5 ... (migrations.2_auto)\n" |
| 756 |
"[ ] migrations.6_auto ... (migrations.3_squashed_5)\n" |
| 757 |
"[ ] migrations.7_auto ... (migrations.6_auto)\n", |
| 758 |
out.getvalue().lower(), |
| 759 |
) |
| 760 |
|
| 761 |
call_command("migrate", "migrations", "3_squashed_5", verbosity=0) |
| 762 |
|
| 763 |
out = io.StringIO() |
| 764 |
call_command("showmigrations", format="plan", stdout=out) |
| 765 |
self.assertEqual( |
| 766 |
"[x] migrations.1_auto\n" |
| 767 |
"[x] migrations.2_auto\n" |
| 768 |
"[x] migrations.3_squashed_5\n" |
| 769 |
"[ ] migrations.6_auto\n" |
| 770 |
"[ ] migrations.7_auto\n", |
| 771 |
out.getvalue().lower(), |
| 772 |
) |
| 773 |
|
| 774 |
out = io.StringIO() |
| 775 |
call_command("showmigrations", format="plan", stdout=out, verbosity=2) |
| 776 |
self.assertEqual( |
| 777 |
"[x] migrations.1_auto\n" |
| 778 |
"[x] migrations.2_auto ... (migrations.1_auto)\n" |
| 779 |
"[x] migrations.3_squashed_5 ... (migrations.2_auto)\n" |
| 780 |
"[ ] migrations.6_auto ... (migrations.3_squashed_5)\n" |
| 781 |
"[ ] migrations.7_auto ... (migrations.6_auto)\n", |
| 782 |
out.getvalue().lower(), |
| 783 |
) |
| 784 |
|
| 785 |
@override_settings( |
| 786 |
INSTALLED_APPS=[ |
| 787 |
"migrations.migrations_test_apps.mutate_state_b", |
| 788 |
"migrations.migrations_test_apps.alter_fk.author_app", |
| 789 |
"migrations.migrations_test_apps.alter_fk.book_app", |
| 790 |
] |
| 791 |
) |
| 792 |
def test_showmigrations_plan_single_app_label(self): |
| 793 |
""" |
| 794 |
`showmigrations --plan app_label` output with a single app_label. |
| 795 |
""" |
| 796 |
# Single app with no dependencies on other apps. |
| 797 |
out = io.StringIO() |
| 798 |
call_command("showmigrations", "mutate_state_b", format="plan", stdout=out) |
| 799 |
self.assertEqual( |
| 800 |
"[ ] mutate_state_b.0001_initial\n[ ] mutate_state_b.0002_add_field\n", |
| 801 |
out.getvalue(), |
| 802 |
) |
| 803 |
# Single app with dependencies. |
| 804 |
out = io.StringIO() |
| 805 |
call_command("showmigrations", "author_app", format="plan", stdout=out) |
| 806 |
self.assertEqual( |
| 807 |
"[ ] author_app.0001_initial\n" |
| 808 |
"[ ] book_app.0001_initial\n" |
| 809 |
"[ ] author_app.0002_alter_id\n", |
| 810 |
out.getvalue(), |
| 811 |
) |
| 812 |
# Some migrations already applied. |
| 813 |
call_command("migrate", "author_app", "0001", verbosity=0) |
| 814 |
out = io.StringIO() |
| 815 |
call_command("showmigrations", "author_app", format="plan", stdout=out) |
| 816 |
self.assertEqual( |
| 817 |
"[X] author_app.0001_initial\n" |
| 818 |
"[ ] book_app.0001_initial\n" |
| 819 |
"[ ] author_app.0002_alter_id\n", |
| 820 |
out.getvalue(), |
| 821 |
) |
| 822 |
# Cleanup by unmigrating author_app. |
| 823 |
call_command("migrate", "author_app", "zero", verbosity=0) |
| 824 |
|
| 825 |
@override_settings( |
| 826 |
INSTALLED_APPS=[ |
| 827 |
"migrations.migrations_test_apps.mutate_state_b", |
| 828 |
"migrations.migrations_test_apps.alter_fk.author_app", |
| 829 |
"migrations.migrations_test_apps.alter_fk.book_app", |
| 830 |
] |
| 831 |
) |
| 832 |
def test_showmigrations_plan_multiple_app_labels(self): |
| 833 |
""" |
| 834 |
`showmigrations --plan app_label` output with multiple app_labels. |
| 835 |
""" |
| 836 |
# Multiple apps: author_app depends on book_app; mutate_state_b doesn't |
| 837 |
# depend on other apps. |
| 838 |
out = io.StringIO() |
| 839 |
call_command( |
| 840 |
"showmigrations", "mutate_state_b", "author_app", format="plan", stdout=out |
| 841 |
) |
| 842 |
self.assertEqual( |
| 843 |
"[ ] author_app.0001_initial\n" |
| 844 |
"[ ] book_app.0001_initial\n" |
| 845 |
"[ ] author_app.0002_alter_id\n" |
| 846 |
"[ ] mutate_state_b.0001_initial\n" |
| 847 |
"[ ] mutate_state_b.0002_add_field\n", |
| 848 |
out.getvalue(), |
| 849 |
) |
| 850 |
# Multiple apps: args order shouldn't matter (the same result is |
| 851 |
# expected as above). |
| 852 |
out = io.StringIO() |
| 853 |
call_command( |
| 854 |
"showmigrations", "author_app", "mutate_state_b", format="plan", stdout=out |
| 855 |
) |
| 856 |
self.assertEqual( |
| 857 |
"[ ] author_app.0001_initial\n" |
| 858 |
"[ ] book_app.0001_initial\n" |
| 859 |
"[ ] author_app.0002_alter_id\n" |
| 860 |
"[ ] mutate_state_b.0001_initial\n" |
| 861 |
"[ ] mutate_state_b.0002_add_field\n", |
| 862 |
out.getvalue(), |
| 863 |
) |
| 864 |
|
| 865 |
@override_settings( |
| 866 |
INSTALLED_APPS=["migrations.migrations_test_apps.unmigrated_app"] |
| 867 |
) |
| 868 |
def test_showmigrations_plan_app_label_no_migrations(self): |
| 869 |
out = io.StringIO() |
| 870 |
call_command( |
| 871 |
"showmigrations", "unmigrated_app", format="plan", stdout=out, no_color=True |
| 872 |
) |
| 873 |
try: |
| 874 |
self.assertEqual("(no migrations)\n", out.getvalue()) |
| 875 |
finally: |
| 876 |
# unmigrated_app.SillyModel has a foreign key to |
| 877 |
# 'migrations.Tribble', but that model is only defined in a |
| 878 |
# migration, so the global app registry never sees it and the |
| 879 |
# reference is left dangling. Remove it to avoid problems in |
| 880 |
# subsequent tests. |
| 881 |
apps._pending_operations.pop(("migrations", "tribble"), None) |
| 882 |
|
| 883 |
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) |
| 884 |
def test_sqlmigrate_forwards(self): |
| 885 |
""" |
| 886 |
sqlmigrate outputs forward looking SQL. |
| 887 |
""" |
| 888 |
out = io.StringIO() |
| 889 |
call_command("sqlmigrate", "migrations", "0001", stdout=out, no_color=True) |
| 890 |
|
| 891 |
lines = out.getvalue().splitlines() |
| 892 |
|
| 893 |
if connection.features.can_rollback_ddl: |
| 894 |
self.assertEqual(lines[0], connection.ops.start_transaction_sql()) |
| 895 |
self.assertEqual(lines[-1], connection.ops.end_transaction_sql()) |
| 896 |
lines = lines[1:-1] |
| 897 |
|
| 898 |
self.assertEqual( |
| 899 |
lines[:3], |
| 900 |
[ |
| 901 |
"--", |
| 902 |
"-- Create model Author", |
| 903 |
"--", |
| 904 |
], |
| 905 |
) |
| 906 |
self.assertIn( |
| 907 |
"create table %s" % connection.ops.quote_name("migrations_author").lower(), |
| 908 |
lines[3].lower(), |
| 909 |
) |
| 910 |
pos = lines.index("--", 3) |
| 911 |
self.assertEqual( |
| 912 |
lines[pos : pos + 3], |
| 913 |
[ |
| 914 |
"--", |
| 915 |
"-- Create model Tribble", |
| 916 |
"--", |
| 917 |
], |
| 918 |
) |
| 919 |
self.assertIn( |
| 920 |
"create table %s" % connection.ops.quote_name("migrations_tribble").lower(), |
| 921 |
lines[pos + 3].lower(), |
| 922 |
) |
| 923 |
pos = lines.index("--", pos + 3) |
| 924 |
self.assertEqual( |
| 925 |
lines[pos : pos + 3], |
| 926 |
[ |
| 927 |
"--", |
| 928 |
"-- Add field bool to tribble", |
| 929 |
"--", |
| 930 |
], |
| 931 |
) |
| 932 |
pos = lines.index("--", pos + 3) |
| 933 |
self.assertEqual( |
| 934 |
lines[pos : pos + 3], |
| 935 |
[ |
| 936 |
"--", |
| 937 |
"-- Alter unique_together for author (1 constraint(s))", |
| 938 |
"--", |
| 939 |
], |
| 940 |
) |
| 941 |
|
| 942 |
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) |
| 943 |
def test_sqlmigrate_backwards(self): |
| 944 |
""" |
| 945 |
sqlmigrate outputs reverse looking SQL. |
| 946 |
""" |
| 947 |
# Cannot generate the reverse SQL unless we've applied the migration. |
| 948 |
call_command("migrate", "migrations", verbosity=0) |
| 949 |
|
| 950 |
out = io.StringIO() |
| 951 |
call_command( |
| 952 |
"sqlmigrate", |
| 953 |
"migrations", |
| 954 |
"0001", |
| 955 |
stdout=out, |
| 956 |
backwards=True, |
| 957 |
no_color=True, |
| 958 |
) |
| 959 |
|
| 960 |
lines = out.getvalue().splitlines() |
| 961 |
try: |
| 962 |
if connection.features.can_rollback_ddl: |
| 963 |
self.assertEqual(lines[0], connection.ops.start_transaction_sql()) |
| 964 |
self.assertEqual(lines[-1], connection.ops.end_transaction_sql()) |
| 965 |
lines = lines[1:-1] |
| 966 |
|
| 967 |
self.assertEqual( |
| 968 |
lines[:3], |
| 969 |
[ |
| 970 |
"--", |
| 971 |
"-- Alter unique_together for author (1 constraint(s))", |
| 972 |
"--", |
| 973 |
], |
| 974 |
) |
| 975 |
pos = lines.index("--", 3) |
| 976 |
self.assertEqual( |
| 977 |
lines[pos : pos + 3], |
| 978 |
[ |
| 979 |
"--", |
| 980 |
"-- Add field bool to tribble", |
| 981 |
"--", |
| 982 |
], |
| 983 |
) |
| 984 |
pos = lines.index("--", pos + 3) |
| 985 |
self.assertEqual( |
| 986 |
lines[pos : pos + 3], |
| 987 |
[ |
| 988 |
"--", |
| 989 |
"-- Create model Tribble", |
| 990 |
"--", |
| 991 |
], |
| 992 |
) |
| 993 |
next_pos = lines.index("--", pos + 3) |
| 994 |
drop_table_sql = ( |
| 995 |
"drop table %s" |
| 996 |
% connection.ops.quote_name("migrations_tribble").lower() |
| 997 |
) |
| 998 |
for line in lines[pos + 3 : next_pos]: |
| 999 |
if drop_table_sql in line.lower(): |
| 1000 |
break |
| 1001 |
else: |
| 1002 |
self.fail("DROP TABLE (tribble) not found.") |
| 1003 |
pos = next_pos |
| 1004 |
self.assertEqual( |
| 1005 |
lines[pos : pos + 3], |
| 1006 |
[ |
| 1007 |
"--", |
| 1008 |
"-- Create model Author", |
| 1009 |
"--", |
| 1010 |
], |
| 1011 |
) |
| 1012 |
drop_table_sql = ( |
| 1013 |
"drop table %s" % connection.ops.quote_name("migrations_author").lower() |
| 1014 |
) |
| 1015 |
for line in lines[pos + 3 :]: |
| 1016 |
if drop_table_sql in line.lower(): |
| 1017 |
break |
| 1018 |
else: |
| 1019 |
self.fail("DROP TABLE (author) not found.") |
| 1020 |
finally: |
| 1021 |
# Unmigrate everything. |
| 1022 |
call_command("migrate", "migrations", "zero", verbosity=0) |
| 1023 |
|
| 1024 |
@override_settings( |
| 1025 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_non_atomic"} |
| 1026 |
) |
| 1027 |
def test_sqlmigrate_for_non_atomic_migration(self): |
| 1028 |
""" |
| 1029 |
Transaction wrappers aren't shown for non-atomic migrations. |
| 1030 |
""" |
| 1031 |
out = io.StringIO() |
| 1032 |
call_command("sqlmigrate", "migrations", "0001", stdout=out) |
| 1033 |
output = out.getvalue().lower() |
| 1034 |
queries = [q.strip() for q in output.splitlines()] |
| 1035 |
if connection.ops.start_transaction_sql(): |
| 1036 |
self.assertNotIn(connection.ops.start_transaction_sql().lower(), queries) |
| 1037 |
self.assertNotIn(connection.ops.end_transaction_sql().lower(), queries) |
| 1038 |
|
| 1039 |
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) |
| 1040 |
def test_sqlmigrate_for_non_transactional_databases(self): |
| 1041 |
""" |
| 1042 |
Transaction wrappers aren't shown for databases that don't support |
| 1043 |
transactional DDL. |
| 1044 |
""" |
| 1045 |
out = io.StringIO() |
| 1046 |
with mock.patch.object(connection.features, "can_rollback_ddl", False): |
| 1047 |
call_command("sqlmigrate", "migrations", "0001", stdout=out) |
| 1048 |
output = out.getvalue().lower() |
| 1049 |
queries = [q.strip() for q in output.splitlines()] |
| 1050 |
start_transaction_sql = connection.ops.start_transaction_sql() |
| 1051 |
if start_transaction_sql: |
| 1052 |
self.assertNotIn(start_transaction_sql.lower(), queries) |
| 1053 |
self.assertNotIn(connection.ops.end_transaction_sql().lower(), queries) |
| 1054 |
|
| 1055 |
@override_settings( |
| 1056 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"} |
| 1057 |
) |
| 1058 |
def test_sqlmigrate_ambiguous_prefix_squashed_migrations(self): |
| 1059 |
msg = ( |
| 1060 |
"More than one migration matches '0001' in app 'migrations'. " |
| 1061 |
"Please be more specific." |
| 1062 |
) |
| 1063 |
with self.assertRaisesMessage(CommandError, msg): |
| 1064 |
call_command("sqlmigrate", "migrations", "0001") |
| 1065 |
|
| 1066 |
@override_settings( |
| 1067 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"} |
| 1068 |
) |
| 1069 |
def test_sqlmigrate_squashed_migration(self): |
| 1070 |
out = io.StringIO() |
| 1071 |
call_command("sqlmigrate", "migrations", "0001_squashed_0002", stdout=out) |
| 1072 |
output = out.getvalue().lower() |
| 1073 |
self.assertIn("-- create model author", output) |
| 1074 |
self.assertIn("-- create model book", output) |
| 1075 |
self.assertNotIn("-- create model tribble", output) |
| 1076 |
|
| 1077 |
@override_settings( |
| 1078 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"} |
| 1079 |
) |
| 1080 |
def test_sqlmigrate_replaced_migration(self): |
| 1081 |
out = io.StringIO() |
| 1082 |
call_command("sqlmigrate", "migrations", "0001_initial", stdout=out) |
| 1083 |
output = out.getvalue().lower() |
| 1084 |
self.assertIn("-- create model author", output) |
| 1085 |
self.assertIn("-- create model tribble", output) |
| 1086 |
|
| 1087 |
@override_settings( |
| 1088 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_no_operations"} |
| 1089 |
) |
| 1090 |
def test_sqlmigrate_no_operations(self): |
| 1091 |
err = io.StringIO() |
| 1092 |
call_command("sqlmigrate", "migrations", "0001_initial", stderr=err) |
| 1093 |
self.assertEqual(err.getvalue(), "No operations found.\n") |
| 1094 |
|
| 1095 |
@override_settings( |
| 1096 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_noop"} |
| 1097 |
) |
| 1098 |
def test_sqlmigrate_noop(self): |
| 1099 |
out = io.StringIO() |
| 1100 |
call_command("sqlmigrate", "migrations", "0001", stdout=out) |
| 1101 |
lines = out.getvalue().splitlines() |
| 1102 |
|
| 1103 |
if connection.features.can_rollback_ddl: |
| 1104 |
lines = lines[1:-1] |
| 1105 |
self.assertEqual( |
| 1106 |
lines, |
| 1107 |
[ |
| 1108 |
"--", |
| 1109 |
"-- Raw SQL operation", |
| 1110 |
"--", |
| 1111 |
"-- (no-op)", |
| 1112 |
], |
| 1113 |
) |
| 1114 |
|
| 1115 |
@override_settings( |
| 1116 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_manual_porting"} |
| 1117 |
) |
| 1118 |
def test_sqlmigrate_unrepresentable(self): |
| 1119 |
out = io.StringIO() |
| 1120 |
call_command("sqlmigrate", "migrations", "0002", stdout=out) |
| 1121 |
lines = out.getvalue().splitlines() |
| 1122 |
|
| 1123 |
if connection.features.can_rollback_ddl: |
| 1124 |
lines = lines[1:-1] |
| 1125 |
self.assertEqual( |
| 1126 |
lines, |
| 1127 |
[ |
| 1128 |
"--", |
| 1129 |
"-- Raw Python operation", |
| 1130 |
"--", |
| 1131 |
"-- THIS OPERATION CANNOT BE WRITTEN AS SQL", |
| 1132 |
], |
| 1133 |
) |
| 1134 |
|
| 1135 |
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) |
| 1136 |
def test_sqlmigrate_transaction_keywords_not_colorized(self): |
| 1137 |
out = io.StringIO() |
| 1138 |
with mock.patch( |
| 1139 |
"django.core.management.color.supports_color", lambda *args: True |
| 1140 |
): |
| 1141 |
call_command("sqlmigrate", "migrations", "0001", stdout=out, no_color=False) |
| 1142 |
self.assertNotIn("\x1b", out.getvalue()) |
| 1143 |
|
| 1144 |
@override_settings( |
| 1145 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_no_operations"}, |
| 1146 |
INSTALLED_APPS=["django.contrib.auth"], |
| 1147 |
) |
| 1148 |
def test_sqlmigrate_system_checks_colorized(self): |
| 1149 |
with ( |
| 1150 |
mock.patch( |
| 1151 |
"django.core.management.color.supports_color", lambda *args: True |
| 1152 |
), |
| 1153 |
self.assertRaisesMessage(SystemCheckError, "\x1b"), |
| 1154 |
): |
| 1155 |
call_command( |
| 1156 |
"sqlmigrate", "migrations", "0001", skip_checks=False, no_color=False |
| 1157 |
) |
| 1158 |
|
| 1159 |
@override_settings( |
| 1160 |
INSTALLED_APPS=[ |
| 1161 |
"migrations.migrations_test_apps.migrated_app", |
| 1162 |
"migrations.migrations_test_apps.migrated_unapplied_app", |
| 1163 |
"migrations.migrations_test_apps.unmigrated_app", |
| 1164 |
], |
| 1165 |
) |
| 1166 |
def test_regression_22823_unmigrated_fk_to_migrated_model(self): |
| 1167 |
""" |
| 1168 |
Assuming you have 3 apps, `A`, `B`, and `C`, such that: |
| 1169 |
|
| 1170 |
* `A` has migrations |
| 1171 |
* `B` has a migration we want to apply |
| 1172 |
* `C` has no migrations, but has an FK to `A` |
| 1173 |
|
| 1174 |
When we try to migrate "B", an exception occurs because the |
| 1175 |
"B" was not included in the ProjectState that is used to detect |
| 1176 |
soft-applied migrations (#22823). |
| 1177 |
""" |
| 1178 |
call_command("migrate", "migrated_unapplied_app", verbosity=0) |
| 1179 |
|
| 1180 |
# unmigrated_app.SillyModel has a foreign key to 'migrations.Tribble', |
| 1181 |
# but that model is only defined in a migration, so the global app |
| 1182 |
# registry never sees it and the reference is left dangling. Remove it |
| 1183 |
# to avoid problems in subsequent tests. |
| 1184 |
apps._pending_operations.pop(("migrations", "tribble"), None) |
| 1185 |
|
| 1186 |
@override_settings( |
| 1187 |
INSTALLED_APPS=["migrations.migrations_test_apps.unmigrated_app_syncdb"] |
| 1188 |
) |
| 1189 |
def test_migrate_syncdb_deferred_sql_executed_with_schemaeditor(self): |
| 1190 |
""" |
| 1191 |
For an app without migrations, editor.execute() is used for executing |
| 1192 |
the syncdb deferred SQL. |
| 1193 |
""" |
| 1194 |
stdout = io.StringIO() |
| 1195 |
with mock.patch.object(BaseDatabaseSchemaEditor, "execute") as execute: |
| 1196 |
call_command( |
| 1197 |
"migrate", run_syncdb=True, verbosity=1, stdout=stdout, no_color=True |
| 1198 |
) |
| 1199 |
create_table_count = len( |
| 1200 |
[call for call in execute.mock_calls if "CREATE TABLE" in str(call)] |
| 1201 |
) |
| 1202 |
self.assertEqual(create_table_count, 2) |
| 1203 |
# There's at least one deferred SQL for creating the foreign key |
| 1204 |
# index. |
| 1205 |
self.assertGreater(len(execute.mock_calls), 2) |
| 1206 |
stdout = stdout.getvalue() |
| 1207 |
self.assertIn("Synchronize unmigrated apps: unmigrated_app_syncdb", stdout) |
| 1208 |
self.assertIn("Creating tables...", stdout) |
| 1209 |
table_name = truncate_name( |
| 1210 |
"unmigrated_app_syncdb_classroom", connection.ops.max_name_length() |
| 1211 |
) |
| 1212 |
self.assertIn("Creating table %s" % table_name, stdout) |
| 1213 |
|
| 1214 |
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) |
| 1215 |
def test_migrate_syncdb_app_with_migrations(self): |
| 1216 |
msg = "Can't use run_syncdb with app 'migrations' as it has migrations." |
| 1217 |
with self.assertRaisesMessage(CommandError, msg): |
| 1218 |
call_command("migrate", "migrations", run_syncdb=True, verbosity=0) |
| 1219 |
|
| 1220 |
@override_settings( |
| 1221 |
INSTALLED_APPS=[ |
| 1222 |
"migrations.migrations_test_apps.unmigrated_app_syncdb", |
| 1223 |
"migrations.migrations_test_apps.unmigrated_app_simple", |
| 1224 |
] |
| 1225 |
) |
| 1226 |
def test_migrate_syncdb_app_label(self): |
| 1227 |
""" |
| 1228 |
Running migrate --run-syncdb with an app_label only creates tables for |
| 1229 |
the specified app. |
| 1230 |
""" |
| 1231 |
stdout = io.StringIO() |
| 1232 |
with mock.patch.object(BaseDatabaseSchemaEditor, "execute") as execute: |
| 1233 |
call_command( |
| 1234 |
"migrate", "unmigrated_app_syncdb", run_syncdb=True, stdout=stdout |
| 1235 |
) |
| 1236 |
create_table_count = len( |
| 1237 |
[call for call in execute.mock_calls if "CREATE TABLE" in str(call)] |
| 1238 |
) |
| 1239 |
self.assertEqual(create_table_count, 2) |
| 1240 |
self.assertGreater(len(execute.mock_calls), 2) |
| 1241 |
self.assertIn( |
| 1242 |
"Synchronize unmigrated app: unmigrated_app_syncdb", stdout.getvalue() |
| 1243 |
) |
| 1244 |
|
| 1245 |
@override_settings( |
| 1246 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"} |
| 1247 |
) |
| 1248 |
def test_migrate_record_replaced(self): |
| 1249 |
""" |
| 1250 |
Running a single squashed migration should record all of the original |
| 1251 |
replaced migrations as run. |
| 1252 |
""" |
| 1253 |
recorder = MigrationRecorder(connection) |
| 1254 |
out = io.StringIO() |
| 1255 |
call_command("migrate", "migrations", verbosity=0) |
| 1256 |
call_command("showmigrations", "migrations", stdout=out, no_color=True) |
| 1257 |
self.assertEqual( |
| 1258 |
"migrations\n [x] 0001_squashed_0002 (2 squashed migrations)\n", |
| 1259 |
out.getvalue().lower(), |
| 1260 |
) |
| 1261 |
applied_migrations = recorder.applied_migrations() |
| 1262 |
self.assertIn(("migrations", "0001_initial"), applied_migrations) |
| 1263 |
self.assertIn(("migrations", "0002_second"), applied_migrations) |
| 1264 |
self.assertIn(("migrations", "0001_squashed_0002"), applied_migrations) |
| 1265 |
# Rollback changes |
| 1266 |
call_command("migrate", "migrations", "zero", verbosity=0) |
| 1267 |
|
| 1268 |
@override_settings( |
| 1269 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"} |
| 1270 |
) |
| 1271 |
def test_migrate_record_squashed(self): |
| 1272 |
""" |
| 1273 |
Running migrate for a squashed migration should record as run |
| 1274 |
if all of the replaced migrations have been run (#25231). |
| 1275 |
""" |
| 1276 |
recorder = MigrationRecorder(connection) |
| 1277 |
recorder.record_applied("migrations", "0001_initial") |
| 1278 |
recorder.record_applied("migrations", "0002_second") |
| 1279 |
out = io.StringIO() |
| 1280 |
call_command("showmigrations", "migrations", stdout=out, no_color=True) |
| 1281 |
self.assertEqual( |
| 1282 |
"migrations\n" |
| 1283 |
" [-] 0001_squashed_0002 (2 squashed migrations) " |
| 1284 |
"run 'manage.py migrate' to finish recording.\n", |
| 1285 |
out.getvalue().lower(), |
| 1286 |
) |
| 1287 |
|
| 1288 |
out = io.StringIO() |
| 1289 |
call_command("migrate", "migrations", verbosity=0) |
| 1290 |
call_command("showmigrations", "migrations", stdout=out, no_color=True) |
| 1291 |
self.assertEqual( |
| 1292 |
"migrations\n [x] 0001_squashed_0002 (2 squashed migrations)\n", |
| 1293 |
out.getvalue().lower(), |
| 1294 |
) |
| 1295 |
self.assertIn( |
| 1296 |
("migrations", "0001_squashed_0002"), recorder.applied_migrations() |
| 1297 |
) |
| 1298 |
# No changes were actually applied so there is nothing to rollback |
| 1299 |
|
| 1300 |
def test_migrate_partially_applied_squashed_migration(self): |
| 1301 |
""" |
| 1302 |
Migrating to a squashed migration specified by name should succeed |
| 1303 |
even if it is partially applied. |
| 1304 |
""" |
| 1305 |
with self.temporary_migration_module(module="migrations.test_migrations"): |
| 1306 |
recorder = MigrationRecorder(connection) |
| 1307 |
try: |
| 1308 |
call_command("migrate", "migrations", "0001_initial", verbosity=0) |
| 1309 |
call_command( |
| 1310 |
"squashmigrations", |
| 1311 |
"migrations", |
| 1312 |
"0002", |
| 1313 |
interactive=False, |
| 1314 |
verbosity=0, |
| 1315 |
) |
| 1316 |
call_command( |
| 1317 |
"migrate", |
| 1318 |
"migrations", |
| 1319 |
"0001_squashed_0002_second", |
| 1320 |
verbosity=0, |
| 1321 |
) |
| 1322 |
applied_migrations = recorder.applied_migrations() |
| 1323 |
self.assertIn(("migrations", "0002_second"), applied_migrations) |
| 1324 |
finally: |
| 1325 |
# Unmigrate everything. |
| 1326 |
call_command("migrate", "migrations", "zero", verbosity=0) |
| 1327 |
|
| 1328 |
@override_settings( |
| 1329 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"} |
| 1330 |
) |
| 1331 |
def test_migrate_backward_to_squashed_migration(self): |
| 1332 |
try: |
| 1333 |
call_command("migrate", "migrations", "0001_squashed_0002", verbosity=0) |
| 1334 |
self.assertTableExists("migrations_author") |
| 1335 |
self.assertTableExists("migrations_book") |
| 1336 |
call_command("migrate", "migrations", "0001_initial", verbosity=0) |
| 1337 |
self.assertTableExists("migrations_author") |
| 1338 |
self.assertTableNotExists("migrations_book") |
| 1339 |
finally: |
| 1340 |
# Unmigrate everything. |
| 1341 |
call_command("migrate", "migrations", "zero", verbosity=0) |
| 1342 |
|
| 1343 |
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) |
| 1344 |
def test_migrate_inconsistent_history(self): |
| 1345 |
""" |
| 1346 |
Running migrate with some migrations applied before their dependencies |
| 1347 |
should not be allowed. |
| 1348 |
""" |
| 1349 |
recorder = MigrationRecorder(connection) |
| 1350 |
recorder.record_applied("migrations", "0002_second") |
| 1351 |
msg = ( |
| 1352 |
"Migration migrations.0002_second is applied before its dependency " |
| 1353 |
"migrations.0001_initial" |
| 1354 |
) |
| 1355 |
with self.assertRaisesMessage(InconsistentMigrationHistory, msg): |
| 1356 |
call_command("migrate") |
| 1357 |
applied_migrations = recorder.applied_migrations() |
| 1358 |
self.assertNotIn(("migrations", "0001_initial"), applied_migrations) |
| 1359 |
|
| 1360 |
@override_settings( |
| 1361 |
INSTALLED_APPS=[ |
| 1362 |
"migrations.migrations_test_apps.migrated_unapplied_app", |
| 1363 |
"migrations.migrations_test_apps.migrated_app", |
| 1364 |
] |
| 1365 |
) |
| 1366 |
def test_migrate_not_reflected_changes(self): |
| 1367 |
class NewModel1(models.Model): |
| 1368 |
class Meta: |
| 1369 |
app_label = "migrated_app" |
| 1370 |
|
| 1371 |
class NewModel2(models.Model): |
| 1372 |
class Meta: |
| 1373 |
app_label = "migrated_unapplied_app" |
| 1374 |
|
| 1375 |
out = io.StringIO() |
| 1376 |
try: |
| 1377 |
call_command("migrate", verbosity=0) |
| 1378 |
call_command("migrate", stdout=out, no_color=True) |
| 1379 |
self.assertEqual( |
| 1380 |
"operations to perform:\n" |
| 1381 |
" apply all migrations: migrated_app, migrated_unapplied_app\n" |
| 1382 |
"running migrations:\n" |
| 1383 |
" no migrations to apply.\n" |
| 1384 |
" your models in app(s): 'migrated_app', " |
| 1385 |
"'migrated_unapplied_app' have changes that are not yet " |
| 1386 |
"reflected in a migration, and so won't be applied.\n" |
| 1387 |
" run 'manage.py makemigrations' to make new migrations, and " |
| 1388 |
"then re-run 'manage.py migrate' to apply them.\n", |
| 1389 |
out.getvalue().lower(), |
| 1390 |
) |
| 1391 |
finally: |
| 1392 |
# Unmigrate everything. |
| 1393 |
call_command("migrate", "migrated_app", "zero", verbosity=0) |
| 1394 |
call_command("migrate", "migrated_unapplied_app", "zero", verbosity=0) |
| 1395 |
|
| 1396 |
@override_settings( |
| 1397 |
MIGRATION_MODULES={ |
| 1398 |
"migrations": "migrations.test_migrations_squashed_no_replaces", |
| 1399 |
} |
| 1400 |
) |
| 1401 |
def test_migrate_prune(self): |
| 1402 |
""" |
| 1403 |
With prune=True, references to migration files deleted from the |
| 1404 |
migrations module (such as after being squashed) are removed from the |
| 1405 |
django_migrations table. |
| 1406 |
""" |
| 1407 |
recorder = MigrationRecorder(connection) |
| 1408 |
recorder.record_applied("migrations", "0001_initial") |
| 1409 |
recorder.record_applied("migrations", "0002_second") |
| 1410 |
recorder.record_applied("migrations", "0001_squashed_0002") |
| 1411 |
out = io.StringIO() |
| 1412 |
try: |
| 1413 |
call_command("migrate", "migrations", prune=True, stdout=out, no_color=True) |
| 1414 |
self.assertEqual( |
| 1415 |
out.getvalue(), |
| 1416 |
"Pruning migrations:\n" |
| 1417 |
" Pruning migrations.0001_initial OK\n" |
| 1418 |
" Pruning migrations.0002_second OK\n", |
| 1419 |
) |
| 1420 |
applied_migrations = [ |
| 1421 |
migration |
| 1422 |
for migration in recorder.applied_migrations() |
| 1423 |
if migration[0] == "migrations" |
| 1424 |
] |
| 1425 |
self.assertEqual(applied_migrations, [("migrations", "0001_squashed_0002")]) |
| 1426 |
finally: |
| 1427 |
recorder.record_unapplied("migrations", "0001_initial") |
| 1428 |
recorder.record_unapplied("migrations", "0001_second") |
| 1429 |
recorder.record_unapplied("migrations", "0001_squashed_0002") |
| 1430 |
|
| 1431 |
@override_settings( |
| 1432 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"} |
| 1433 |
) |
| 1434 |
def test_prune_deleted_squashed_migrations_in_replaces(self): |
| 1435 |
out = io.StringIO() |
| 1436 |
with self.temporary_migration_module( |
| 1437 |
module="migrations.test_migrations_squashed" |
| 1438 |
) as migration_dir: |
| 1439 |
try: |
| 1440 |
call_command("migrate", "migrations", verbosity=0) |
| 1441 |
# Delete the replaced migrations. |
| 1442 |
os.remove(os.path.join(migration_dir, "0001_initial.py")) |
| 1443 |
os.remove(os.path.join(migration_dir, "0002_second.py")) |
| 1444 |
# --prune cannot be used before removing the "replaces" |
| 1445 |
# attribute. |
| 1446 |
call_command( |
| 1447 |
"migrate", |
| 1448 |
"migrations", |
| 1449 |
prune=True, |
| 1450 |
stdout=out, |
| 1451 |
no_color=True, |
| 1452 |
) |
| 1453 |
self.assertEqual( |
| 1454 |
out.getvalue(), |
| 1455 |
"Pruning migrations:\n" |
| 1456 |
" Cannot use --prune because the following squashed " |
| 1457 |
"migrations have their 'replaces' attributes and may not " |
| 1458 |
"be recorded as applied:\n" |
| 1459 |
" migrations.0001_squashed_0002\n" |
| 1460 |
" Re-run 'manage.py migrate' if they are not marked as " |
| 1461 |
"applied, and remove 'replaces' attributes in their " |
| 1462 |
"Migration classes.\n", |
| 1463 |
) |
| 1464 |
finally: |
| 1465 |
# Unmigrate everything. |
| 1466 |
call_command("migrate", "migrations", "zero", verbosity=0) |
| 1467 |
|
| 1468 |
@override_settings( |
| 1469 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"} |
| 1470 |
) |
| 1471 |
def test_prune_no_migrations_to_prune(self): |
| 1472 |
out = io.StringIO() |
| 1473 |
call_command("migrate", "migrations", prune=True, stdout=out, no_color=True) |
| 1474 |
self.assertEqual( |
| 1475 |
out.getvalue(), |
| 1476 |
"Pruning migrations:\n No migrations to prune.\n", |
| 1477 |
) |
| 1478 |
out = io.StringIO() |
| 1479 |
call_command( |
| 1480 |
"migrate", |
| 1481 |
"migrations", |
| 1482 |
prune=True, |
| 1483 |
stdout=out, |
| 1484 |
no_color=True, |
| 1485 |
verbosity=0, |
| 1486 |
) |
| 1487 |
self.assertEqual(out.getvalue(), "") |
| 1488 |
|
| 1489 |
def test_prune_no_app_label(self): |
| 1490 |
msg = "Migrations can be pruned only when an app is specified." |
| 1491 |
with self.assertRaisesMessage(CommandError, msg): |
| 1492 |
call_command("migrate", prune=True) |
| 1493 |
|
| 1494 |
@override_settings( |
| 1495 |
MIGRATION_MODULES={ |
| 1496 |
"migrations": "migrations.test_migrations_squashed_no_replaces", |
| 1497 |
"migrations2": "migrations2.test_migrations_2_squashed_with_replaces", |
| 1498 |
}, |
| 1499 |
INSTALLED_APPS=["migrations", "migrations2"], |
| 1500 |
) |
| 1501 |
def test_prune_respect_app_label(self): |
| 1502 |
recorder = MigrationRecorder(connection) |
| 1503 |
recorder.record_applied("migrations", "0001_initial") |
| 1504 |
recorder.record_applied("migrations", "0002_second") |
| 1505 |
recorder.record_applied("migrations", "0001_squashed_0002") |
| 1506 |
# Second app has squashed migrations with replaces. |
| 1507 |
recorder.record_applied("migrations2", "0001_initial") |
| 1508 |
recorder.record_applied("migrations2", "0002_second") |
| 1509 |
recorder.record_applied("migrations2", "0001_squashed_0002") |
| 1510 |
out = io.StringIO() |
| 1511 |
try: |
| 1512 |
call_command("migrate", "migrations", prune=True, stdout=out, no_color=True) |
| 1513 |
self.assertEqual( |
| 1514 |
out.getvalue(), |
| 1515 |
"Pruning migrations:\n" |
| 1516 |
" Pruning migrations.0001_initial OK\n" |
| 1517 |
" Pruning migrations.0002_second OK\n", |
| 1518 |
) |
| 1519 |
applied_migrations = [ |
| 1520 |
migration |
| 1521 |
for migration in recorder.applied_migrations() |
| 1522 |
if migration[0] in ["migrations", "migrations2"] |
| 1523 |
] |
| 1524 |
self.assertEqual( |
| 1525 |
applied_migrations, |
| 1526 |
[ |
| 1527 |
("migrations", "0001_squashed_0002"), |
| 1528 |
("migrations2", "0001_initial"), |
| 1529 |
("migrations2", "0002_second"), |
| 1530 |
("migrations2", "0001_squashed_0002"), |
| 1531 |
], |
| 1532 |
) |
| 1533 |
finally: |
| 1534 |
recorder.record_unapplied("migrations", "0001_initial") |
| 1535 |
recorder.record_unapplied("migrations", "0001_second") |
| 1536 |
recorder.record_unapplied("migrations", "0001_squashed_0002") |
| 1537 |
recorder.record_unapplied("migrations2", "0001_initial") |
| 1538 |
recorder.record_unapplied("migrations2", "0002_second") |
| 1539 |
recorder.record_unapplied("migrations2", "0001_squashed_0002") |
| 1540 |
|
| 1541 |
|
| 1542 |
class MakeMigrationsTests(MigrationTestBase): |
| 1543 |
""" |
| 1544 |
Tests running the makemigrations command. |
| 1545 |
""" |
| 1546 |
|
| 1547 |
def setUp(self): |
| 1548 |
super().setUp() |
| 1549 |
self._old_models = apps.app_configs["migrations"].models.copy() |
| 1550 |
|
| 1551 |
def tearDown(self): |
| 1552 |
apps.app_configs["migrations"].models = self._old_models |
| 1553 |
apps.all_models["migrations"] = self._old_models |
| 1554 |
apps.clear_cache() |
| 1555 |
super().tearDown() |
| 1556 |
|
| 1557 |
def test_files_content(self): |
| 1558 |
self.assertTableNotExists("migrations_unicodemodel") |
| 1559 |
apps.register_model("migrations", UnicodeModel) |
| 1560 |
with self.temporary_migration_module() as migration_dir: |
| 1561 |
call_command("makemigrations", "migrations", verbosity=0) |
| 1562 |
|
| 1563 |
# Check for empty __init__.py file in migrations folder |
| 1564 |
init_file = os.path.join(migration_dir, "__init__.py") |
| 1565 |
self.assertTrue(os.path.exists(init_file)) |
| 1566 |
|
| 1567 |
with open(init_file) as fp: |
| 1568 |
content = fp.read() |
| 1569 |
self.assertEqual(content, "") |
| 1570 |
|
| 1571 |
# Check for existing 0001_initial.py file in migration folder |
| 1572 |
initial_file = os.path.join(migration_dir, "0001_initial.py") |
| 1573 |
self.assertTrue(os.path.exists(initial_file)) |
| 1574 |
|
| 1575 |
with open(initial_file, encoding="utf-8") as fp: |
| 1576 |
content = fp.read() |
| 1577 |
self.assertIn("migrations.CreateModel", content) |
| 1578 |
self.assertIn("initial = True", content) |
| 1579 |
|
| 1580 |
self.assertIn("úñí©óðé µóðéø", content) # Meta.verbose_name |
| 1581 |
self.assertIn("úñí©óðé µóðéøß", content) # Meta.verbose_name_plural |
| 1582 |
self.assertIn("ÚÑÍ¢ÓÐÉ", content) # title.verbose_name |
| 1583 |
self.assertIn("“Ðjáñgó”", content) # title.default |
| 1584 |
|
| 1585 |
def test_makemigrations_order(self): |
| 1586 |
""" |
| 1587 |
makemigrations should recognize number-only migrations (0001.py). |
| 1588 |
""" |
| 1589 |
module = "migrations.test_migrations_order" |
| 1590 |
with self.temporary_migration_module(module=module) as migration_dir: |
| 1591 |
if hasattr(importlib, "invalidate_caches"): |
| 1592 |
# importlib caches os.listdir() on some platforms like macOS |
| 1593 |
# (#23850). |
| 1594 |
importlib.invalidate_caches() |
| 1595 |
call_command( |
| 1596 |
"makemigrations", "migrations", "--empty", "-n", "a", "-v", "0" |
| 1597 |
) |
| 1598 |
self.assertTrue(os.path.exists(os.path.join(migration_dir, "0002_a.py"))) |
| 1599 |
|
| 1600 |
def test_makemigrations_empty_connections(self): |
| 1601 |
empty_connections = ConnectionHandler({"default": {}}) |
| 1602 |
with mock.patch( |
| 1603 |
"django.core.management.commands.makemigrations.connections", |
| 1604 |
new=empty_connections, |
| 1605 |
): |
| 1606 |
# with no apps |
| 1607 |
out = io.StringIO() |
| 1608 |
call_command("makemigrations", stdout=out) |
| 1609 |
self.assertIn("No changes detected", out.getvalue()) |
| 1610 |
# with an app |
| 1611 |
with self.temporary_migration_module() as migration_dir: |
| 1612 |
call_command("makemigrations", "migrations", verbosity=0) |
| 1613 |
init_file = os.path.join(migration_dir, "__init__.py") |
| 1614 |
self.assertTrue(os.path.exists(init_file)) |
| 1615 |
|
| 1616 |
@override_settings(INSTALLED_APPS=["migrations", "migrations2"]) |
| 1617 |
def test_makemigrations_consistency_checks_respect_routers(self): |
| 1618 |
""" |
| 1619 |
The history consistency checks in makemigrations respect |
| 1620 |
settings.DATABASE_ROUTERS. |
| 1621 |
""" |
| 1622 |
|
| 1623 |
def patched_has_table(migration_recorder): |
| 1624 |
if migration_recorder.connection is connections["other"]: |
| 1625 |
raise Exception("Other connection") |
| 1626 |
else: |
| 1627 |
return mock.DEFAULT |
| 1628 |
|
| 1629 |
self.assertTableNotExists("migrations_unicodemodel") |
| 1630 |
apps.register_model("migrations", UnicodeModel) |
| 1631 |
with mock.patch.object( |
| 1632 |
MigrationRecorder, "has_table", autospec=True, side_effect=patched_has_table |
| 1633 |
) as has_table: |
| 1634 |
with self.temporary_migration_module() as migration_dir: |
| 1635 |
call_command("makemigrations", "migrations", verbosity=0) |
| 1636 |
initial_file = os.path.join(migration_dir, "0001_initial.py") |
| 1637 |
self.assertTrue(os.path.exists(initial_file)) |
| 1638 |
self.assertEqual(has_table.call_count, 1) # 'default' is checked |
| 1639 |
|
| 1640 |
# Router says not to migrate 'other' so consistency shouldn't |
| 1641 |
# be checked. |
| 1642 |
with self.settings(DATABASE_ROUTERS=["migrations.routers.TestRouter"]): |
| 1643 |
call_command("makemigrations", "migrations", verbosity=0) |
| 1644 |
self.assertEqual(has_table.call_count, 2) # 'default' again |
| 1645 |
|
| 1646 |
# With a router that doesn't prohibit migrating 'other', |
| 1647 |
# consistency is checked. |
| 1648 |
with self.settings( |
| 1649 |
DATABASE_ROUTERS=["migrations.routers.DefaultOtherRouter"] |
| 1650 |
): |
| 1651 |
with self.assertRaisesMessage(Exception, "Other connection"): |
| 1652 |
call_command("makemigrations", "migrations", verbosity=0) |
| 1653 |
self.assertEqual(has_table.call_count, 4) # 'default' and 'other' |
| 1654 |
|
| 1655 |
# With a router that doesn't allow migrating on any database, |
| 1656 |
# no consistency checks are made. |
| 1657 |
with self.settings(DATABASE_ROUTERS=["migrations.routers.TestRouter"]): |
| 1658 |
with mock.patch.object( |
| 1659 |
TestRouter, "allow_migrate", return_value=False |
| 1660 |
) as allow_migrate: |
| 1661 |
call_command("makemigrations", "migrations", verbosity=0) |
| 1662 |
allow_migrate.assert_any_call( |
| 1663 |
"other", "migrations", model_name="UnicodeModel" |
| 1664 |
) |
| 1665 |
# allow_migrate() is called with the correct arguments. |
| 1666 |
self.assertGreater(len(allow_migrate.mock_calls), 0) |
| 1667 |
called_aliases = set() |
| 1668 |
for mock_call in allow_migrate.mock_calls: |
| 1669 |
_, call_args, call_kwargs = mock_call |
| 1670 |
connection_alias, app_name = call_args |
| 1671 |
called_aliases.add(connection_alias) |
| 1672 |
# Raises an error if invalid app_name/model_name occurs. |
| 1673 |
apps.get_app_config(app_name).get_model(call_kwargs["model_name"]) |
| 1674 |
self.assertEqual(called_aliases, set(connections)) |
| 1675 |
self.assertEqual(has_table.call_count, 4) |
| 1676 |
|
| 1677 |
def test_failing_migration(self): |
| 1678 |
# If a migration fails to serialize, it shouldn't generate an empty file. #21280 |
| 1679 |
apps.register_model("migrations", UnserializableModel) |
| 1680 |
|
| 1681 |
with self.temporary_migration_module() as migration_dir: |
| 1682 |
with self.assertRaisesMessage(ValueError, "Cannot serialize"): |
| 1683 |
call_command("makemigrations", "migrations", verbosity=0) |
| 1684 |
|
| 1685 |
initial_file = os.path.join(migration_dir, "0001_initial.py") |
| 1686 |
self.assertFalse(os.path.exists(initial_file)) |
| 1687 |
|
| 1688 |
def test_makemigrations_conflict_exit(self): |
| 1689 |
""" |
| 1690 |
makemigrations exits if it detects a conflict. |
| 1691 |
""" |
| 1692 |
with self.temporary_migration_module( |
| 1693 |
module="migrations.test_migrations_conflict" |
| 1694 |
): |
| 1695 |
with self.assertRaises(CommandError) as context: |
| 1696 |
call_command("makemigrations") |
| 1697 |
self.assertEqual( |
| 1698 |
str(context.exception), |
| 1699 |
"Conflicting migrations detected; multiple leaf nodes in the " |
| 1700 |
"migration graph: (0002_conflicting_second, 0002_second in " |
| 1701 |
"migrations).\n" |
| 1702 |
"To fix them run 'python manage.py makemigrations --merge'", |
| 1703 |
) |
| 1704 |
|
| 1705 |
def test_makemigrations_merge_no_conflict(self): |
| 1706 |
""" |
| 1707 |
makemigrations exits if in merge mode with no conflicts. |
| 1708 |
""" |
| 1709 |
out = io.StringIO() |
| 1710 |
with self.temporary_migration_module(module="migrations.test_migrations"): |
| 1711 |
call_command("makemigrations", merge=True, stdout=out) |
| 1712 |
self.assertIn("No conflicts detected to merge.", out.getvalue()) |
| 1713 |
|
| 1714 |
def test_makemigrations_empty_no_app_specified(self): |
| 1715 |
""" |
| 1716 |
makemigrations exits if no app is specified with 'empty' mode. |
| 1717 |
""" |
| 1718 |
msg = "You must supply at least one app label when using --empty." |
| 1719 |
with self.assertRaisesMessage(CommandError, msg): |
| 1720 |
call_command("makemigrations", empty=True) |
| 1721 |
|
| 1722 |
def test_makemigrations_empty_migration(self): |
| 1723 |
""" |
| 1724 |
makemigrations properly constructs an empty migration. |
| 1725 |
""" |
| 1726 |
with self.temporary_migration_module() as migration_dir: |
| 1727 |
call_command("makemigrations", "migrations", empty=True, verbosity=0) |
| 1728 |
|
| 1729 |
# Check for existing 0001_initial.py file in migration folder |
| 1730 |
initial_file = os.path.join(migration_dir, "0001_initial.py") |
| 1731 |
self.assertTrue(os.path.exists(initial_file)) |
| 1732 |
|
| 1733 |
with open(initial_file, encoding="utf-8") as fp: |
| 1734 |
content = fp.read() |
| 1735 |
|
| 1736 |
# Remove all whitespace to check for empty dependencies and operations |
| 1737 |
content = content.replace(" ", "") |
| 1738 |
self.assertIn( |
| 1739 |
"dependencies=[]" if HAS_BLACK else "dependencies=[\n]", content |
| 1740 |
) |
| 1741 |
self.assertIn( |
| 1742 |
"operations=[]" if HAS_BLACK else "operations=[\n]", content |
| 1743 |
) |
| 1744 |
|
| 1745 |
@override_settings(MIGRATION_MODULES={"migrations": None}) |
| 1746 |
def test_makemigrations_disabled_migrations_for_app(self): |
| 1747 |
""" |
| 1748 |
makemigrations raises a nice error when migrations are disabled for an |
| 1749 |
app. |
| 1750 |
""" |
| 1751 |
msg = ( |
| 1752 |
"Django can't create migrations for app 'migrations' because migrations " |
| 1753 |
"have been disabled via the MIGRATION_MODULES setting." |
| 1754 |
) |
| 1755 |
with self.assertRaisesMessage(ValueError, msg): |
| 1756 |
call_command("makemigrations", "migrations", empty=True, verbosity=0) |
| 1757 |
|
| 1758 |
def test_makemigrations_no_changes_no_apps(self): |
| 1759 |
""" |
| 1760 |
makemigrations exits when there are no changes and no apps are specified. |
| 1761 |
""" |
| 1762 |
out = io.StringIO() |
| 1763 |
call_command("makemigrations", stdout=out) |
| 1764 |
self.assertIn("No changes detected", out.getvalue()) |
| 1765 |
|
| 1766 |
def test_makemigrations_no_changes(self): |
| 1767 |
""" |
| 1768 |
makemigrations exits when there are no changes to an app. |
| 1769 |
""" |
| 1770 |
out = io.StringIO() |
| 1771 |
with self.temporary_migration_module( |
| 1772 |
module="migrations.test_migrations_no_changes" |
| 1773 |
): |
| 1774 |
call_command("makemigrations", "migrations", stdout=out) |
| 1775 |
self.assertIn("No changes detected in app 'migrations'", out.getvalue()) |
| 1776 |
|
| 1777 |
def test_makemigrations_no_apps_initial(self): |
| 1778 |
""" |
| 1779 |
makemigrations should detect initial is needed on empty migration |
| 1780 |
modules if no app provided. |
| 1781 |
""" |
| 1782 |
out = io.StringIO() |
| 1783 |
with self.temporary_migration_module(module="migrations.test_migrations_empty"): |
| 1784 |
call_command("makemigrations", stdout=out) |
| 1785 |
self.assertIn("0001_initial.py", out.getvalue()) |
| 1786 |
|
| 1787 |
def test_makemigrations_no_init(self): |
| 1788 |
"""Migration directories without an __init__.py file are allowed.""" |
| 1789 |
out = io.StringIO() |
| 1790 |
with self.temporary_migration_module( |
| 1791 |
module="migrations.test_migrations_no_init" |
| 1792 |
): |
| 1793 |
call_command("makemigrations", stdout=out) |
| 1794 |
self.assertIn("0001_initial.py", out.getvalue()) |
| 1795 |
|
| 1796 |
def test_makemigrations_no_init_ambiguous(self): |
| 1797 |
""" |
| 1798 |
Migration directories without an __init__.py file are not allowed if |
| 1799 |
there are multiple namespace search paths that resolve to them. |
| 1800 |
""" |
| 1801 |
out = io.StringIO() |
| 1802 |
with self.temporary_migration_module( |
| 1803 |
module="migrations.test_migrations_no_init" |
| 1804 |
) as migration_dir: |
| 1805 |
# Copy the project directory into another place under sys.path. |
| 1806 |
app_dir = Path(migration_dir).parent |
| 1807 |
os.remove(app_dir / "__init__.py") |
| 1808 |
project_dir = app_dir.parent |
| 1809 |
dest = project_dir.parent / "other_dir_in_path" |
| 1810 |
shutil.copytree(project_dir, dest) |
| 1811 |
with extend_sys_path(str(dest)): |
| 1812 |
call_command("makemigrations", stdout=out) |
| 1813 |
self.assertEqual("No changes detected\n", out.getvalue()) |
| 1814 |
|
| 1815 |
def test_makemigrations_migrations_announce(self): |
| 1816 |
""" |
| 1817 |
makemigrations announces the migration at the default verbosity level. |
| 1818 |
""" |
| 1819 |
out = io.StringIO() |
| 1820 |
with self.temporary_migration_module(): |
| 1821 |
call_command("makemigrations", "migrations", stdout=out) |
| 1822 |
self.assertIn("Migrations for 'migrations'", out.getvalue()) |
| 1823 |
|
| 1824 |
def test_makemigrations_no_common_ancestor(self): |
| 1825 |
""" |
| 1826 |
makemigrations fails to merge migrations with no common ancestor. |
| 1827 |
""" |
| 1828 |
with self.assertRaises(ValueError) as context: |
| 1829 |
with self.temporary_migration_module( |
| 1830 |
module="migrations.test_migrations_no_ancestor" |
| 1831 |
): |
| 1832 |
call_command("makemigrations", "migrations", merge=True) |
| 1833 |
exception_message = str(context.exception) |
| 1834 |
self.assertIn("Could not find common ancestor of", exception_message) |
| 1835 |
self.assertIn("0002_second", exception_message) |
| 1836 |
self.assertIn("0002_conflicting_second", exception_message) |
| 1837 |
|
| 1838 |
def test_makemigrations_interactive_reject(self): |
| 1839 |
""" |
| 1840 |
makemigrations enters and exits interactive mode properly. |
| 1841 |
""" |
| 1842 |
# Monkeypatch interactive questioner to auto reject |
| 1843 |
with mock.patch("builtins.input", mock.Mock(return_value="N")): |
| 1844 |
with self.temporary_migration_module( |
| 1845 |
module="migrations.test_migrations_conflict" |
| 1846 |
) as migration_dir: |
| 1847 |
with captured_stdout(): |
| 1848 |
call_command( |
| 1849 |
"makemigrations", |
| 1850 |
"migrations", |
| 1851 |
name="merge", |
| 1852 |
merge=True, |
| 1853 |
interactive=True, |
| 1854 |
verbosity=0, |
| 1855 |
) |
| 1856 |
merge_file = os.path.join(migration_dir, "0003_merge.py") |
| 1857 |
self.assertFalse(os.path.exists(merge_file)) |
| 1858 |
|
| 1859 |
def test_makemigrations_interactive_accept(self): |
| 1860 |
""" |
| 1861 |
makemigrations enters interactive mode and merges properly. |
| 1862 |
""" |
| 1863 |
# Monkeypatch interactive questioner to auto accept |
| 1864 |
with mock.patch("builtins.input", mock.Mock(return_value="y")): |
| 1865 |
out = io.StringIO() |
| 1866 |
with self.temporary_migration_module( |
| 1867 |
module="migrations.test_migrations_conflict" |
| 1868 |
) as migration_dir: |
| 1869 |
call_command( |
| 1870 |
"makemigrations", |
| 1871 |
"migrations", |
| 1872 |
name="merge", |
| 1873 |
merge=True, |
| 1874 |
interactive=True, |
| 1875 |
stdout=out, |
| 1876 |
) |
| 1877 |
merge_file = os.path.join(migration_dir, "0003_merge.py") |
| 1878 |
self.assertTrue(os.path.exists(merge_file)) |
| 1879 |
self.assertIn("Created new merge migration", out.getvalue()) |
| 1880 |
|
| 1881 |
def test_makemigrations_default_merge_name(self): |
| 1882 |
out = io.StringIO() |
| 1883 |
with self.temporary_migration_module( |
| 1884 |
module="migrations.test_migrations_conflict" |
| 1885 |
) as migration_dir: |
| 1886 |
call_command( |
| 1887 |
"makemigrations", |
| 1888 |
"migrations", |
| 1889 |
merge=True, |
| 1890 |
interactive=False, |
| 1891 |
stdout=out, |
| 1892 |
) |
| 1893 |
merge_file = os.path.join( |
| 1894 |
migration_dir, |
| 1895 |
"0003_merge_0002_conflicting_second_0002_second.py", |
| 1896 |
) |
| 1897 |
self.assertIs(os.path.exists(merge_file), True) |
| 1898 |
with open(merge_file, encoding="utf-8") as fp: |
| 1899 |
content = fp.read() |
| 1900 |
if HAS_BLACK: |
| 1901 |
target_str = '("migrations", "0002_conflicting_second")' |
| 1902 |
else: |
| 1903 |
target_str = "('migrations', '0002_conflicting_second')" |
| 1904 |
self.assertIn(target_str, content) |
| 1905 |
self.assertIn("Created new merge migration %s" % merge_file, out.getvalue()) |
| 1906 |
|
| 1907 |
@mock.patch("django.db.migrations.utils.datetime") |
| 1908 |
def test_makemigrations_auto_merge_name(self, mock_datetime): |
| 1909 |
mock_datetime.datetime.now.return_value = datetime.datetime(2016, 1, 2, 3, 4) |
| 1910 |
with mock.patch("builtins.input", mock.Mock(return_value="y")): |
| 1911 |
out = io.StringIO() |
| 1912 |
with self.temporary_migration_module( |
| 1913 |
module="migrations.test_migrations_conflict_long_name" |
| 1914 |
) as migration_dir: |
| 1915 |
call_command( |
| 1916 |
"makemigrations", |
| 1917 |
"migrations", |
| 1918 |
merge=True, |
| 1919 |
interactive=True, |
| 1920 |
stdout=out, |
| 1921 |
) |
| 1922 |
merge_file = os.path.join(migration_dir, "0003_merge_20160102_0304.py") |
| 1923 |
self.assertTrue(os.path.exists(merge_file)) |
| 1924 |
self.assertIn("Created new merge migration", out.getvalue()) |
| 1925 |
|
| 1926 |
def test_makemigrations_non_interactive_not_null_addition(self): |
| 1927 |
""" |
| 1928 |
Non-interactive makemigrations fails when a default is missing on a |
| 1929 |
new not-null field. |
| 1930 |
""" |
| 1931 |
|
| 1932 |
class SillyModel(models.Model): |
| 1933 |
silly_field = models.BooleanField(default=False) |
| 1934 |
silly_int = models.IntegerField() |
| 1935 |
|
| 1936 |
class Meta: |
| 1937 |
app_label = "migrations" |
| 1938 |
|
| 1939 |
with self.assertRaises(SystemExit): |
| 1940 |
with self.temporary_migration_module( |
| 1941 |
module="migrations.test_migrations_no_default" |
| 1942 |
): |
| 1943 |
with captured_stdout() as out: |
| 1944 |
call_command("makemigrations", "migrations", interactive=False) |
| 1945 |
self.assertIn( |
| 1946 |
"Field 'silly_int' on model 'sillymodel' not migrated: it is " |
| 1947 |
"impossible to add a non-nullable field without specifying a " |
| 1948 |
"default.", |
| 1949 |
out.getvalue(), |
| 1950 |
) |
| 1951 |
|
| 1952 |
def test_makemigrations_interactive_not_null_addition(self): |
| 1953 |
""" |
| 1954 |
makemigrations messages when adding a NOT NULL field in interactive |
| 1955 |
mode. |
| 1956 |
""" |
| 1957 |
|
| 1958 |
class Author(models.Model): |
| 1959 |
silly_field = models.BooleanField(null=False) |
| 1960 |
|
| 1961 |
class Meta: |
| 1962 |
app_label = "migrations" |
| 1963 |
|
| 1964 |
input_msg = ( |
| 1965 |
"It is impossible to add a non-nullable field 'silly_field' to " |
| 1966 |
"author without specifying a default. This is because the " |
| 1967 |
"database needs something to populate existing rows.\n" |
| 1968 |
"Please select a fix:\n" |
| 1969 |
" 1) Provide a one-off default now (will be set on all existing " |
| 1970 |
"rows with a null value for this column)\n" |
| 1971 |
" 2) Quit and manually define a default value in models.py." |
| 1972 |
) |
| 1973 |
with self.temporary_migration_module(module="migrations.test_migrations"): |
| 1974 |
# 2 - quit. |
| 1975 |
with mock.patch("builtins.input", return_value="2"): |
| 1976 |
with captured_stdout() as out, self.assertRaises(SystemExit): |
| 1977 |
call_command("makemigrations", "migrations", interactive=True) |
| 1978 |
self.assertIn(input_msg, out.getvalue()) |
| 1979 |
# 1 - provide a default. |
| 1980 |
with mock.patch("builtins.input", return_value="1"): |
| 1981 |
with captured_stdout() as out: |
| 1982 |
call_command("makemigrations", "migrations", interactive=True) |
| 1983 |
output = out.getvalue() |
| 1984 |
self.assertIn(input_msg, output) |
| 1985 |
self.assertIn("Please enter the default value as valid Python.", output) |
| 1986 |
self.assertIn( |
| 1987 |
"The datetime and django.utils.timezone modules are " |
| 1988 |
"available, so it is possible to provide e.g. timezone.now as " |
| 1989 |
"a value", |
| 1990 |
output, |
| 1991 |
) |
| 1992 |
self.assertIn("Type 'exit' to exit this prompt", output) |
| 1993 |
|
| 1994 |
def test_makemigrations_non_interactive_not_null_alteration(self): |
| 1995 |
""" |
| 1996 |
Non-interactive makemigrations fails when a default is missing on a |
| 1997 |
field changed to not-null. |
| 1998 |
""" |
| 1999 |
|
| 2000 |
class Author(models.Model): |
| 2001 |
name = models.CharField(max_length=255) |
| 2002 |
slug = models.SlugField() |
| 2003 |
age = models.IntegerField(default=0) |
| 2004 |
|
| 2005 |
class Meta: |
| 2006 |
app_label = "migrations" |
| 2007 |
|
| 2008 |
with self.temporary_migration_module(module="migrations.test_migrations"): |
| 2009 |
with captured_stdout() as out: |
| 2010 |
call_command("makemigrations", "migrations", interactive=False) |
| 2011 |
self.assertIn("Alter field slug on author", out.getvalue()) |
| 2012 |
self.assertIn( |
| 2013 |
"Field 'slug' on model 'author' given a default of NOT PROVIDED " |
| 2014 |
"and must be corrected.", |
| 2015 |
out.getvalue(), |
| 2016 |
) |
| 2017 |
|
| 2018 |
def test_makemigrations_interactive_not_null_alteration(self): |
| 2019 |
""" |
| 2020 |
makemigrations messages when changing a NULL field to NOT NULL in |
| 2021 |
interactive mode. |
| 2022 |
""" |
| 2023 |
|
| 2024 |
class Author(models.Model): |
| 2025 |
slug = models.SlugField(null=False) |
| 2026 |
|
| 2027 |
class Meta: |
| 2028 |
app_label = "migrations" |
| 2029 |
|
| 2030 |
input_msg = ( |
| 2031 |
"It is impossible to change a nullable field 'slug' on author to " |
| 2032 |
"non-nullable without providing a default. This is because the " |
| 2033 |
"database needs something to populate existing rows.\n" |
| 2034 |
"Please select a fix:\n" |
| 2035 |
" 1) Provide a one-off default now (will be set on all existing " |
| 2036 |
"rows with a null value for this column)\n" |
| 2037 |
" 2) Ignore for now. Existing rows that contain NULL values will " |
| 2038 |
"have to be handled manually, for example with a RunPython or " |
| 2039 |
"RunSQL operation.\n" |
| 2040 |
" 3) Quit and manually define a default value in models.py." |
| 2041 |
) |
| 2042 |
with self.temporary_migration_module(module="migrations.test_migrations"): |
| 2043 |
# No message appears if --dry-run. |
| 2044 |
with captured_stdout() as out: |
| 2045 |
call_command( |
| 2046 |
"makemigrations", |
| 2047 |
"migrations", |
| 2048 |
interactive=True, |
| 2049 |
dry_run=True, |
| 2050 |
) |
| 2051 |
self.assertNotIn(input_msg, out.getvalue()) |
| 2052 |
# 3 - quit. |
| 2053 |
with mock.patch("builtins.input", return_value="3"): |
| 2054 |
with captured_stdout() as out, self.assertRaises(SystemExit): |
| 2055 |
call_command("makemigrations", "migrations", interactive=True) |
| 2056 |
self.assertIn(input_msg, out.getvalue()) |
| 2057 |
# 1 - provide a default. |
| 2058 |
with mock.patch("builtins.input", return_value="1"): |
| 2059 |
with captured_stdout() as out: |
| 2060 |
call_command("makemigrations", "migrations", interactive=True) |
| 2061 |
output = out.getvalue() |
| 2062 |
self.assertIn(input_msg, output) |
| 2063 |
self.assertIn("Please enter the default value as valid Python.", output) |
| 2064 |
self.assertIn( |
| 2065 |
"The datetime and django.utils.timezone modules are " |
| 2066 |
"available, so it is possible to provide e.g. timezone.now as " |
| 2067 |
"a value", |
| 2068 |
output, |
| 2069 |
) |
| 2070 |
self.assertIn("Type 'exit' to exit this prompt", output) |
| 2071 |
|
| 2072 |
def test_makemigrations_non_interactive_no_model_rename(self): |
| 2073 |
""" |
| 2074 |
makemigrations adds and removes a possible model rename in |
| 2075 |
non-interactive mode. |
| 2076 |
""" |
| 2077 |
|
| 2078 |
class RenamedModel(models.Model): |
| 2079 |
silly_field = models.BooleanField(default=False) |
| 2080 |
|
| 2081 |
class Meta: |
| 2082 |
app_label = "migrations" |
| 2083 |
|
| 2084 |
out = io.StringIO() |
| 2085 |
with self.temporary_migration_module( |
| 2086 |
module="migrations.test_migrations_no_default" |
| 2087 |
): |
| 2088 |
call_command("makemigrations", "migrations", interactive=False, stdout=out) |
| 2089 |
self.assertIn("Delete model SillyModel", out.getvalue()) |
| 2090 |
self.assertIn("Create model RenamedModel", out.getvalue()) |
| 2091 |
|
| 2092 |
def test_makemigrations_non_interactive_no_field_rename(self): |
| 2093 |
""" |
| 2094 |
makemigrations adds and removes a possible field rename in |
| 2095 |
non-interactive mode. |
| 2096 |
""" |
| 2097 |
|
| 2098 |
class SillyModel(models.Model): |
| 2099 |
silly_rename = models.BooleanField(default=False) |
| 2100 |
|
| 2101 |
class Meta: |
| 2102 |
app_label = "migrations" |
| 2103 |
|
| 2104 |
out = io.StringIO() |
| 2105 |
with self.temporary_migration_module( |
| 2106 |
module="migrations.test_migrations_no_default" |
| 2107 |
): |
| 2108 |
call_command("makemigrations", "migrations", interactive=False, stdout=out) |
| 2109 |
self.assertIn("Remove field silly_field from sillymodel", out.getvalue()) |
| 2110 |
self.assertIn("Add field silly_rename to sillymodel", out.getvalue()) |
| 2111 |
|
| 2112 |
@mock.patch("builtins.input", return_value="Y") |
| 2113 |
def test_makemigrations_model_rename_interactive(self, mock_input): |
| 2114 |
class RenamedModel(models.Model): |
| 2115 |
silly_field = models.BooleanField(default=False) |
| 2116 |
|
| 2117 |
class Meta: |
| 2118 |
app_label = "migrations" |
| 2119 |
|
| 2120 |
with self.temporary_migration_module( |
| 2121 |
module="migrations.test_migrations_no_default", |
| 2122 |
): |
| 2123 |
with captured_stdout() as out: |
| 2124 |
call_command("makemigrations", "migrations", interactive=True) |
| 2125 |
self.assertIn("Rename model SillyModel to RenamedModel", out.getvalue()) |
| 2126 |
|
| 2127 |
@mock.patch("builtins.input", return_value="Y") |
| 2128 |
def test_makemigrations_field_rename_interactive(self, mock_input): |
| 2129 |
class SillyModel(models.Model): |
| 2130 |
silly_rename = models.BooleanField(default=False) |
| 2131 |
|
| 2132 |
class Meta: |
| 2133 |
app_label = "migrations" |
| 2134 |
|
| 2135 |
with self.temporary_migration_module( |
| 2136 |
module="migrations.test_migrations_no_default", |
| 2137 |
): |
| 2138 |
with captured_stdout() as out: |
| 2139 |
call_command("makemigrations", "migrations", interactive=True) |
| 2140 |
self.assertIn( |
| 2141 |
"Rename field silly_field on sillymodel to silly_rename", |
| 2142 |
out.getvalue(), |
| 2143 |
) |
| 2144 |
|
| 2145 |
def test_makemigrations_handle_merge(self): |
| 2146 |
""" |
| 2147 |
makemigrations properly merges the conflicting migrations with --noinput. |
| 2148 |
""" |
| 2149 |
out = io.StringIO() |
| 2150 |
with self.temporary_migration_module( |
| 2151 |
module="migrations.test_migrations_conflict" |
| 2152 |
) as migration_dir: |
| 2153 |
call_command( |
| 2154 |
"makemigrations", |
| 2155 |
"migrations", |
| 2156 |
name="merge", |
| 2157 |
merge=True, |
| 2158 |
interactive=False, |
| 2159 |
stdout=out, |
| 2160 |
) |
| 2161 |
merge_file = os.path.join(migration_dir, "0003_merge.py") |
| 2162 |
self.assertTrue(os.path.exists(merge_file)) |
| 2163 |
output = out.getvalue() |
| 2164 |
self.assertIn("Merging migrations", output) |
| 2165 |
self.assertIn("Branch 0002_second", output) |
| 2166 |
self.assertIn("Branch 0002_conflicting_second", output) |
| 2167 |
self.assertIn("Created new merge migration", output) |
| 2168 |
|
| 2169 |
def test_makemigration_merge_dry_run(self): |
| 2170 |
""" |
| 2171 |
makemigrations respects --dry-run option when fixing migration |
| 2172 |
conflicts (#24427). |
| 2173 |
""" |
| 2174 |
out = io.StringIO() |
| 2175 |
with self.temporary_migration_module( |
| 2176 |
module="migrations.test_migrations_conflict" |
| 2177 |
) as migration_dir: |
| 2178 |
call_command( |
| 2179 |
"makemigrations", |
| 2180 |
"migrations", |
| 2181 |
name="merge", |
| 2182 |
dry_run=True, |
| 2183 |
merge=True, |
| 2184 |
interactive=False, |
| 2185 |
stdout=out, |
| 2186 |
) |
| 2187 |
merge_file = os.path.join(migration_dir, "0003_merge.py") |
| 2188 |
self.assertFalse(os.path.exists(merge_file)) |
| 2189 |
output = out.getvalue() |
| 2190 |
self.assertIn("Merging migrations", output) |
| 2191 |
self.assertIn("Branch 0002_second", output) |
| 2192 |
self.assertIn("Branch 0002_conflicting_second", output) |
| 2193 |
self.assertNotIn("Created new merge migration", output) |
| 2194 |
|
| 2195 |
def test_makemigration_merge_dry_run_verbosity_3(self): |
| 2196 |
""" |
| 2197 |
`makemigrations --merge --dry-run` writes the merge migration file to |
| 2198 |
stdout with `verbosity == 3` (#24427). |
| 2199 |
""" |
| 2200 |
out = io.StringIO() |
| 2201 |
with self.temporary_migration_module( |
| 2202 |
module="migrations.test_migrations_conflict" |
| 2203 |
) as migration_dir: |
| 2204 |
call_command( |
| 2205 |
"makemigrations", |
| 2206 |
"migrations", |
| 2207 |
name="merge", |
| 2208 |
dry_run=True, |
| 2209 |
merge=True, |
| 2210 |
interactive=False, |
| 2211 |
stdout=out, |
| 2212 |
verbosity=3, |
| 2213 |
) |
| 2214 |
merge_file = os.path.join(migration_dir, "0003_merge.py") |
| 2215 |
self.assertFalse(os.path.exists(merge_file)) |
| 2216 |
output = out.getvalue() |
| 2217 |
self.assertIn("Merging migrations", output) |
| 2218 |
self.assertIn("Branch 0002_second", output) |
| 2219 |
self.assertIn("Branch 0002_conflicting_second", output) |
| 2220 |
self.assertNotIn("Created new merge migration", output) |
| 2221 |
|
| 2222 |
# Additional output caused by verbosity 3 |
| 2223 |
# The complete merge migration file that would be written |
| 2224 |
self.assertIn("class Migration(migrations.Migration):", output) |
| 2225 |
self.assertIn("dependencies = [", output) |
| 2226 |
self.assertIn("('migrations', '0002_second')", output) |
| 2227 |
self.assertIn("('migrations', '0002_conflicting_second')", output) |
| 2228 |
self.assertIn("operations = [", output) |
| 2229 |
self.assertIn("]", output) |
| 2230 |
|
| 2231 |
def test_makemigrations_dry_run(self): |
| 2232 |
""" |
| 2233 |
`makemigrations --dry-run` should not ask for defaults. |
| 2234 |
""" |
| 2235 |
|
| 2236 |
class SillyModel(models.Model): |
| 2237 |
silly_field = models.BooleanField(default=False) |
| 2238 |
silly_date = models.DateField() # Added field without a default |
| 2239 |
silly_auto_now = models.DateTimeField(auto_now_add=True) |
| 2240 |
|
| 2241 |
class Meta: |
| 2242 |
app_label = "migrations" |
| 2243 |
|
| 2244 |
out = io.StringIO() |
| 2245 |
with self.temporary_migration_module( |
| 2246 |
module="migrations.test_migrations_no_default" |
| 2247 |
): |
| 2248 |
call_command("makemigrations", "migrations", dry_run=True, stdout=out) |
| 2249 |
# Output the expected changes directly, without asking for defaults |
| 2250 |
self.assertIn("Add field silly_date to sillymodel", out.getvalue()) |
| 2251 |
|
| 2252 |
def test_makemigrations_dry_run_verbosity_3(self): |
| 2253 |
""" |
| 2254 |
Allow `makemigrations --dry-run` to output the migrations file to |
| 2255 |
stdout (with verbosity == 3). |
| 2256 |
""" |
| 2257 |
|
| 2258 |
class SillyModel(models.Model): |
| 2259 |
silly_field = models.BooleanField(default=False) |
| 2260 |
silly_char = models.CharField(default="") |
| 2261 |
|
| 2262 |
class Meta: |
| 2263 |
app_label = "migrations" |
| 2264 |
|
| 2265 |
out = io.StringIO() |
| 2266 |
with self.temporary_migration_module( |
| 2267 |
module="migrations.test_migrations_no_default" |
| 2268 |
): |
| 2269 |
call_command( |
| 2270 |
"makemigrations", "migrations", dry_run=True, stdout=out, verbosity=3 |
| 2271 |
) |
| 2272 |
|
| 2273 |
# Normal --dry-run output |
| 2274 |
self.assertIn("+ Add field silly_char to sillymodel", out.getvalue()) |
| 2275 |
|
| 2276 |
# Additional output caused by verbosity 3 |
| 2277 |
# The complete migrations file that would be written |
| 2278 |
self.assertIn("class Migration(migrations.Migration):", out.getvalue()) |
| 2279 |
self.assertIn("dependencies = [", out.getvalue()) |
| 2280 |
self.assertIn("('migrations', '0001_initial'),", out.getvalue()) |
| 2281 |
self.assertIn("migrations.AddField(", out.getvalue()) |
| 2282 |
self.assertIn("model_name='sillymodel',", out.getvalue()) |
| 2283 |
self.assertIn("name='silly_char',", out.getvalue()) |
| 2284 |
|
| 2285 |
def test_makemigrations_scriptable(self): |
| 2286 |
""" |
| 2287 |
With scriptable=True, log output is diverted to stderr, and only the |
| 2288 |
paths of generated migration files are written to stdout. |
| 2289 |
""" |
| 2290 |
out = io.StringIO() |
| 2291 |
err = io.StringIO() |
| 2292 |
with self.temporary_migration_module( |
| 2293 |
module="migrations.migrations.test_migrations", |
| 2294 |
) as migration_dir: |
| 2295 |
call_command( |
| 2296 |
"makemigrations", |
| 2297 |
"migrations", |
| 2298 |
scriptable=True, |
| 2299 |
stdout=out, |
| 2300 |
stderr=err, |
| 2301 |
) |
| 2302 |
initial_file = os.path.join(migration_dir, "0001_initial.py") |
| 2303 |
self.assertEqual(out.getvalue(), f"{initial_file}\n") |
| 2304 |
self.assertIn(" + Create model ModelWithCustomBase\n", err.getvalue()) |
| 2305 |
|
| 2306 |
@mock.patch("builtins.input", return_value="Y") |
| 2307 |
def test_makemigrations_scriptable_merge(self, mock_input): |
| 2308 |
out = io.StringIO() |
| 2309 |
err = io.StringIO() |
| 2310 |
with self.temporary_migration_module( |
| 2311 |
module="migrations.test_migrations_conflict", |
| 2312 |
) as migration_dir: |
| 2313 |
call_command( |
| 2314 |
"makemigrations", |
| 2315 |
"migrations", |
| 2316 |
merge=True, |
| 2317 |
name="merge", |
| 2318 |
scriptable=True, |
| 2319 |
stdout=out, |
| 2320 |
stderr=err, |
| 2321 |
) |
| 2322 |
merge_file = os.path.join(migration_dir, "0003_merge.py") |
| 2323 |
self.assertEqual(out.getvalue(), f"{merge_file}\n") |
| 2324 |
self.assertIn(f"Created new merge migration {merge_file}", err.getvalue()) |
| 2325 |
|
| 2326 |
def test_makemigrations_failure_to_format_code(self): |
| 2327 |
self.assertFormatterFailureCaught("makemigrations", "migrations") |
| 2328 |
|
| 2329 |
def test_merge_makemigrations_failure_to_format_code(self): |
| 2330 |
self.assertFormatterFailureCaught("makemigrations", "migrations", empty=True) |
| 2331 |
self.assertFormatterFailureCaught( |
| 2332 |
"makemigrations", |
| 2333 |
"migrations", |
| 2334 |
merge=True, |
| 2335 |
interactive=False, |
| 2336 |
module="migrations.test_migrations_conflict", |
| 2337 |
) |
| 2338 |
|
| 2339 |
def test_makemigrations_migrations_modules_path_not_exist(self): |
| 2340 |
""" |
| 2341 |
makemigrations creates migrations when specifying a custom location |
| 2342 |
for migration files using MIGRATION_MODULES if the custom path |
| 2343 |
doesn't already exist. |
| 2344 |
""" |
| 2345 |
|
| 2346 |
class SillyModel(models.Model): |
| 2347 |
silly_field = models.BooleanField(default=False) |
| 2348 |
|
| 2349 |
class Meta: |
| 2350 |
app_label = "migrations" |
| 2351 |
|
| 2352 |
out = io.StringIO() |
| 2353 |
migration_module = "migrations.test_migrations_path_doesnt_exist.foo.bar" |
| 2354 |
with self.temporary_migration_module(module=migration_module) as migration_dir: |
| 2355 |
call_command("makemigrations", "migrations", stdout=out) |
| 2356 |
|
| 2357 |
# Migrations file is actually created in the expected path. |
| 2358 |
initial_file = os.path.join(migration_dir, "0001_initial.py") |
| 2359 |
self.assertTrue(os.path.exists(initial_file)) |
| 2360 |
|
| 2361 |
# Command output indicates the migration is created. |
| 2362 |
self.assertIn(" + Create model SillyModel", out.getvalue()) |
| 2363 |
|
| 2364 |
@override_settings(MIGRATION_MODULES={"migrations": "some.nonexistent.path"}) |
| 2365 |
def test_makemigrations_migrations_modules_nonexistent_toplevel_package(self): |
| 2366 |
msg = ( |
| 2367 |
"Could not locate an appropriate location to create migrations " |
| 2368 |
"package some.nonexistent.path. Make sure the toplevel package " |
| 2369 |
"exists and can be imported." |
| 2370 |
) |
| 2371 |
with self.assertRaisesMessage(ValueError, msg): |
| 2372 |
call_command("makemigrations", "migrations", empty=True, verbosity=0) |
| 2373 |
|
| 2374 |
def test_makemigrations_interactive_by_default(self): |
| 2375 |
""" |
| 2376 |
The user is prompted to merge by default if there are conflicts and |
| 2377 |
merge is True. Answer negative to differentiate it from behavior when |
| 2378 |
--noinput is specified. |
| 2379 |
""" |
| 2380 |
# Monkeypatch interactive questioner to auto reject |
| 2381 |
out = io.StringIO() |
| 2382 |
with mock.patch("builtins.input", mock.Mock(return_value="N")): |
| 2383 |
with self.temporary_migration_module( |
| 2384 |
module="migrations.test_migrations_conflict" |
| 2385 |
) as migration_dir: |
| 2386 |
call_command( |
| 2387 |
"makemigrations", "migrations", name="merge", merge=True, stdout=out |
| 2388 |
) |
| 2389 |
merge_file = os.path.join(migration_dir, "0003_merge.py") |
| 2390 |
# This will fail if interactive is False by default |
| 2391 |
self.assertFalse(os.path.exists(merge_file)) |
| 2392 |
self.assertNotIn("Created new merge migration", out.getvalue()) |
| 2393 |
|
| 2394 |
@override_settings( |
| 2395 |
INSTALLED_APPS=[ |
| 2396 |
"migrations", |
| 2397 |
"migrations.migrations_test_apps.unspecified_app_with_conflict", |
| 2398 |
] |
| 2399 |
) |
| 2400 |
def test_makemigrations_unspecified_app_with_conflict_no_merge(self): |
| 2401 |
""" |
| 2402 |
makemigrations does not raise a CommandError when an unspecified app |
| 2403 |
has conflicting migrations. |
| 2404 |
""" |
| 2405 |
with self.temporary_migration_module( |
| 2406 |
module="migrations.test_migrations_no_changes" |
| 2407 |
): |
| 2408 |
call_command("makemigrations", "migrations", merge=False, verbosity=0) |
| 2409 |
|
| 2410 |
@override_settings( |
| 2411 |
INSTALLED_APPS=[ |
| 2412 |
"migrations.migrations_test_apps.migrated_app", |
| 2413 |
"migrations.migrations_test_apps.unspecified_app_with_conflict", |
| 2414 |
] |
| 2415 |
) |
| 2416 |
def test_makemigrations_unspecified_app_with_conflict_merge(self): |
| 2417 |
""" |
| 2418 |
makemigrations does not create a merge for an unspecified app even if |
| 2419 |
it has conflicting migrations. |
| 2420 |
""" |
| 2421 |
# Monkeypatch interactive questioner to auto accept |
| 2422 |
with mock.patch("builtins.input", mock.Mock(return_value="y")): |
| 2423 |
out = io.StringIO() |
| 2424 |
with self.temporary_migration_module( |
| 2425 |
app_label="migrated_app" |
| 2426 |
) as migration_dir: |
| 2427 |
call_command( |
| 2428 |
"makemigrations", |
| 2429 |
"migrated_app", |
| 2430 |
name="merge", |
| 2431 |
merge=True, |
| 2432 |
interactive=True, |
| 2433 |
stdout=out, |
| 2434 |
) |
| 2435 |
merge_file = os.path.join(migration_dir, "0003_merge.py") |
| 2436 |
self.assertFalse(os.path.exists(merge_file)) |
| 2437 |
self.assertIn("No conflicts detected to merge.", out.getvalue()) |
| 2438 |
|
| 2439 |
@override_settings( |
| 2440 |
INSTALLED_APPS=[ |
| 2441 |
"migrations.migrations_test_apps.migrated_app", |
| 2442 |
"migrations.migrations_test_apps.conflicting_app_with_dependencies", |
| 2443 |
] |
| 2444 |
) |
| 2445 |
def test_makemigrations_merge_dont_output_dependency_operations(self): |
| 2446 |
""" |
| 2447 |
makemigrations --merge does not output any operations from apps that |
| 2448 |
don't belong to a given app. |
| 2449 |
""" |
| 2450 |
# Monkeypatch interactive questioner to auto accept |
| 2451 |
with mock.patch("builtins.input", mock.Mock(return_value="N")): |
| 2452 |
out = io.StringIO() |
| 2453 |
with mock.patch( |
| 2454 |
"django.core.management.color.supports_color", lambda *args: False |
| 2455 |
): |
| 2456 |
call_command( |
| 2457 |
"makemigrations", |
| 2458 |
"conflicting_app_with_dependencies", |
| 2459 |
merge=True, |
| 2460 |
interactive=True, |
| 2461 |
stdout=out, |
| 2462 |
) |
| 2463 |
self.assertEqual( |
| 2464 |
out.getvalue().lower(), |
| 2465 |
"merging conflicting_app_with_dependencies\n" |
| 2466 |
" branch 0002_conflicting_second\n" |
| 2467 |
" + create model something\n" |
| 2468 |
" branch 0002_second\n" |
| 2469 |
" - delete model tribble\n" |
| 2470 |
" - remove field silly_field from author\n" |
| 2471 |
" + add field rating to author\n" |
| 2472 |
" + create model book\n" |
| 2473 |
"\n" |
| 2474 |
"merging will only work if the operations printed above do not " |
| 2475 |
"conflict\n" |
| 2476 |
"with each other (working on different fields or models)\n" |
| 2477 |
"should these migration branches be merged? [y/n] ", |
| 2478 |
) |
| 2479 |
|
| 2480 |
def test_makemigrations_with_custom_name(self): |
| 2481 |
""" |
| 2482 |
makemigrations --name generate a custom migration name. |
| 2483 |
""" |
| 2484 |
with self.temporary_migration_module() as migration_dir: |
| 2485 |
|
| 2486 |
def cmd(migration_count, migration_name, *args): |
| 2487 |
call_command( |
| 2488 |
"makemigrations", |
| 2489 |
"migrations", |
| 2490 |
"--verbosity", |
| 2491 |
"0", |
| 2492 |
"--name", |
| 2493 |
migration_name, |
| 2494 |
*args, |
| 2495 |
) |
| 2496 |
migration_file = os.path.join( |
| 2497 |
migration_dir, "%s_%s.py" % (migration_count, migration_name) |
| 2498 |
) |
| 2499 |
# Check for existing migration file in migration folder |
| 2500 |
self.assertTrue(os.path.exists(migration_file)) |
| 2501 |
with open(migration_file, encoding="utf-8") as fp: |
| 2502 |
content = fp.read() |
| 2503 |
content = content.replace(" ", "") |
| 2504 |
return content |
| 2505 |
|
| 2506 |
# generate an initial migration |
| 2507 |
migration_name_0001 = "my_initial_migration" |
| 2508 |
content = cmd("0001", migration_name_0001) |
| 2509 |
self.assertIn( |
| 2510 |
"dependencies=[]" if HAS_BLACK else "dependencies=[\n]", content |
| 2511 |
) |
| 2512 |
|
| 2513 |
# importlib caches os.listdir() on some platforms like macOS |
| 2514 |
# (#23850). |
| 2515 |
if hasattr(importlib, "invalidate_caches"): |
| 2516 |
importlib.invalidate_caches() |
| 2517 |
|
| 2518 |
# generate an empty migration |
| 2519 |
migration_name_0002 = "my_custom_migration" |
| 2520 |
content = cmd("0002", migration_name_0002, "--empty") |
| 2521 |
if HAS_BLACK: |
| 2522 |
template_str = 'dependencies=[\n("migrations","0001_%s"),\n]' |
| 2523 |
else: |
| 2524 |
template_str = "dependencies=[\n('migrations','0001_%s'),\n]" |
| 2525 |
self.assertIn( |
| 2526 |
template_str % migration_name_0001, |
| 2527 |
content, |
| 2528 |
) |
| 2529 |
self.assertIn("operations=[]" if HAS_BLACK else "operations=[\n]", content) |
| 2530 |
|
| 2531 |
def test_makemigrations_with_invalid_custom_name(self): |
| 2532 |
msg = "The migration name must be a valid Python identifier." |
| 2533 |
with self.assertRaisesMessage(CommandError, msg): |
| 2534 |
call_command( |
| 2535 |
"makemigrations", "migrations", "--name", "invalid name", "--empty" |
| 2536 |
) |
| 2537 |
|
| 2538 |
def test_makemigrations_check_with_changes(self): |
| 2539 |
""" |
| 2540 |
makemigrations --check should exit with a non-zero status when |
| 2541 |
there are changes to an app requiring migrations. |
| 2542 |
""" |
| 2543 |
out = io.StringIO() |
| 2544 |
with self.temporary_migration_module() as tmpdir: |
| 2545 |
with self.assertRaises(SystemExit) as cm: |
| 2546 |
call_command( |
| 2547 |
"makemigrations", |
| 2548 |
"--check", |
| 2549 |
"migrations", |
| 2550 |
stdout=out, |
| 2551 |
) |
| 2552 |
self.assertEqual(os.listdir(tmpdir), ["__init__.py"]) |
| 2553 |
self.assertEqual(cm.exception.code, 1) |
| 2554 |
self.assertIn("Migrations for 'migrations':", out.getvalue()) |
| 2555 |
|
| 2556 |
def test_makemigrations_check_no_changes(self): |
| 2557 |
""" |
| 2558 |
makemigrations --check should exit with a zero status when there are no |
| 2559 |
changes. |
| 2560 |
""" |
| 2561 |
out = io.StringIO() |
| 2562 |
with self.temporary_migration_module( |
| 2563 |
module="migrations.test_migrations_no_changes" |
| 2564 |
): |
| 2565 |
call_command("makemigrations", "--check", "migrations", stdout=out) |
| 2566 |
self.assertEqual("No changes detected in app 'migrations'\n", out.getvalue()) |
| 2567 |
|
| 2568 |
def test_makemigrations_migration_path_output(self): |
| 2569 |
""" |
| 2570 |
makemigrations should print the relative paths to the migrations unless |
| 2571 |
they are outside of the current tree, in which case the absolute path |
| 2572 |
should be shown. |
| 2573 |
""" |
| 2574 |
out = io.StringIO() |
| 2575 |
apps.register_model("migrations", UnicodeModel) |
| 2576 |
with self.temporary_migration_module() as migration_dir: |
| 2577 |
call_command("makemigrations", "migrations", stdout=out) |
| 2578 |
self.assertIn( |
| 2579 |
os.path.join(migration_dir, "0001_initial.py"), out.getvalue() |
| 2580 |
) |
| 2581 |
|
| 2582 |
def test_makemigrations_migration_path_output_valueerror(self): |
| 2583 |
""" |
| 2584 |
makemigrations prints the absolute path if os.path.relpath() raises a |
| 2585 |
ValueError when it's impossible to obtain a relative path, e.g. on |
| 2586 |
Windows if Django is installed on a different drive than where the |
| 2587 |
migration files are created. |
| 2588 |
""" |
| 2589 |
out = io.StringIO() |
| 2590 |
with self.temporary_migration_module() as migration_dir: |
| 2591 |
with mock.patch("os.path.relpath", side_effect=ValueError): |
| 2592 |
call_command("makemigrations", "migrations", stdout=out) |
| 2593 |
self.assertIn(os.path.join(migration_dir, "0001_initial.py"), out.getvalue()) |
| 2594 |
|
| 2595 |
def test_makemigrations_inconsistent_history(self): |
| 2596 |
""" |
| 2597 |
makemigrations should raise InconsistentMigrationHistory exception if |
| 2598 |
there are some migrations applied before their dependencies. |
| 2599 |
""" |
| 2600 |
recorder = MigrationRecorder(connection) |
| 2601 |
recorder.record_applied("migrations", "0002_second") |
| 2602 |
msg = ( |
| 2603 |
"Migration migrations.0002_second is applied before its dependency " |
| 2604 |
"migrations.0001_initial" |
| 2605 |
) |
| 2606 |
with self.temporary_migration_module(module="migrations.test_migrations"): |
| 2607 |
with self.assertRaisesMessage(InconsistentMigrationHistory, msg): |
| 2608 |
call_command("makemigrations") |
| 2609 |
|
| 2610 |
def test_makemigrations_inconsistent_history_db_failure(self): |
| 2611 |
msg = ( |
| 2612 |
"Got an error checking a consistent migration history performed " |
| 2613 |
"for database connection 'default': could not connect to server" |
| 2614 |
) |
| 2615 |
with mock.patch( |
| 2616 |
"django.db.migrations.loader.MigrationLoader.check_consistent_history", |
| 2617 |
side_effect=OperationalError("could not connect to server"), |
| 2618 |
): |
| 2619 |
with self.temporary_migration_module(): |
| 2620 |
with self.assertWarns(RuntimeWarning) as cm: |
| 2621 |
call_command("makemigrations", verbosity=0) |
| 2622 |
self.assertEqual(str(cm.warning), msg) |
| 2623 |
|
| 2624 |
@mock.patch("builtins.input", return_value="1") |
| 2625 |
@mock.patch( |
| 2626 |
"django.db.migrations.questioner.sys.stdin", |
| 2627 |
mock.MagicMock(encoding=sys.getdefaultencoding()), |
| 2628 |
) |
| 2629 |
def test_makemigrations_auto_now_add_interactive(self, *args): |
| 2630 |
""" |
| 2631 |
makemigrations prompts the user when adding auto_now_add to an existing |
| 2632 |
model. |
| 2633 |
""" |
| 2634 |
|
| 2635 |
class Entry(models.Model): |
| 2636 |
title = models.CharField(max_length=255) |
| 2637 |
creation_date = models.DateTimeField(auto_now_add=True) |
| 2638 |
|
| 2639 |
class Meta: |
| 2640 |
app_label = "migrations" |
| 2641 |
|
| 2642 |
input_msg = ( |
| 2643 |
"It is impossible to add the field 'creation_date' with " |
| 2644 |
"'auto_now_add=True' to entry without providing a default. This " |
| 2645 |
"is because the database needs something to populate existing " |
| 2646 |
"rows.\n" |
| 2647 |
" 1) Provide a one-off default now which will be set on all " |
| 2648 |
"existing rows\n" |
| 2649 |
" 2) Quit and manually define a default value in models.py." |
| 2650 |
) |
| 2651 |
# Monkeypatch interactive questioner to auto accept |
| 2652 |
prompt_stdout = io.StringIO() |
| 2653 |
with self.temporary_migration_module(module="migrations.test_auto_now_add"): |
| 2654 |
call_command( |
| 2655 |
"makemigrations", "migrations", interactive=True, stdout=prompt_stdout |
| 2656 |
) |
| 2657 |
prompt_output = prompt_stdout.getvalue() |
| 2658 |
self.assertIn(input_msg, prompt_output) |
| 2659 |
self.assertIn("Please enter the default value as valid Python.", prompt_output) |
| 2660 |
self.assertIn( |
| 2661 |
"Accept the default 'timezone.now' by pressing 'Enter' or provide " |
| 2662 |
"another value.", |
| 2663 |
prompt_output, |
| 2664 |
) |
| 2665 |
self.assertIn("Type 'exit' to exit this prompt", prompt_output) |
| 2666 |
self.assertIn("Add field creation_date to entry", prompt_output) |
| 2667 |
|
| 2668 |
@mock.patch("builtins.input", return_value="2") |
| 2669 |
def test_makemigrations_auto_now_add_interactive_quit(self, mock_input): |
| 2670 |
class Author(models.Model): |
| 2671 |
publishing_date = models.DateField(auto_now_add=True) |
| 2672 |
|
| 2673 |
class Meta: |
| 2674 |
app_label = "migrations" |
| 2675 |
|
| 2676 |
with self.temporary_migration_module(module="migrations.test_migrations"): |
| 2677 |
with captured_stdout(): |
| 2678 |
with self.assertRaises(SystemExit): |
| 2679 |
call_command("makemigrations", "migrations", interactive=True) |
| 2680 |
|
| 2681 |
def test_makemigrations_non_interactive_auto_now_add_addition(self): |
| 2682 |
""" |
| 2683 |
Non-interactive makemigrations fails when a default is missing on a |
| 2684 |
new field when auto_now_add=True. |
| 2685 |
""" |
| 2686 |
|
| 2687 |
class Entry(models.Model): |
| 2688 |
creation_date = models.DateTimeField(auto_now_add=True) |
| 2689 |
|
| 2690 |
class Meta: |
| 2691 |
app_label = "migrations" |
| 2692 |
|
| 2693 |
with self.temporary_migration_module(module="migrations.test_auto_now_add"): |
| 2694 |
with self.assertRaises(SystemExit), captured_stdout() as out: |
| 2695 |
call_command("makemigrations", "migrations", interactive=False) |
| 2696 |
self.assertIn( |
| 2697 |
"Field 'creation_date' on model 'entry' not migrated: it is " |
| 2698 |
"impossible to add a field with 'auto_now_add=True' without " |
| 2699 |
"specifying a default.", |
| 2700 |
out.getvalue(), |
| 2701 |
) |
| 2702 |
|
| 2703 |
def test_makemigrations_interactive_unique_callable_default_addition(self): |
| 2704 |
""" |
| 2705 |
makemigrations prompts the user when adding a unique field with |
| 2706 |
a callable default. |
| 2707 |
""" |
| 2708 |
|
| 2709 |
class Book(models.Model): |
| 2710 |
created = models.DateTimeField(unique=True, default=timezone.now) |
| 2711 |
|
| 2712 |
class Meta: |
| 2713 |
app_label = "migrations" |
| 2714 |
|
| 2715 |
version = get_docs_version() |
| 2716 |
input_msg = ( |
| 2717 |
f"Callable default on unique field book.created will not generate " |
| 2718 |
f"unique values upon migrating.\n" |
| 2719 |
f"Please choose how to proceed:\n" |
| 2720 |
f" 1) Continue making this migration as the first step in writing " |
| 2721 |
f"a manual migration to generate unique values described here: " |
| 2722 |
f"https://docs.djangoproject.com/en/{version}/howto/" |
| 2723 |
f"writing-migrations/#migrations-that-add-unique-fields.\n" |
| 2724 |
f" 2) Quit and edit field options in models.py.\n" |
| 2725 |
) |
| 2726 |
with self.temporary_migration_module(module="migrations.test_migrations"): |
| 2727 |
# 2 - quit. |
| 2728 |
with mock.patch("builtins.input", return_value="2"): |
| 2729 |
with captured_stdout() as out, self.assertRaises(SystemExit): |
| 2730 |
call_command("makemigrations", "migrations", interactive=True) |
| 2731 |
out_value = out.getvalue() |
| 2732 |
self.assertIn(input_msg, out_value) |
| 2733 |
self.assertNotIn("Add field created to book", out_value) |
| 2734 |
# 1 - continue. |
| 2735 |
with mock.patch("builtins.input", return_value="1"): |
| 2736 |
with captured_stdout() as out: |
| 2737 |
call_command("makemigrations", "migrations", interactive=True) |
| 2738 |
out_value = out.getvalue() |
| 2739 |
self.assertIn(input_msg, out_value) |
| 2740 |
self.assertIn("Add field created to book", out_value) |
| 2741 |
|
| 2742 |
def test_makemigrations_non_interactive_unique_callable_default_addition(self): |
| 2743 |
class Book(models.Model): |
| 2744 |
created = models.DateTimeField(unique=True, default=timezone.now) |
| 2745 |
|
| 2746 |
class Meta: |
| 2747 |
app_label = "migrations" |
| 2748 |
|
| 2749 |
with self.temporary_migration_module(module="migrations.test_migrations"): |
| 2750 |
with captured_stdout() as out: |
| 2751 |
call_command("makemigrations", "migrations", interactive=False) |
| 2752 |
out_value = out.getvalue() |
| 2753 |
self.assertIn("Add field created to book", out_value) |
| 2754 |
|
| 2755 |
@override_settings( |
| 2756 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed"}, |
| 2757 |
) |
| 2758 |
def test_makemigrations_continues_number_sequence_after_squash(self): |
| 2759 |
with self.temporary_migration_module( |
| 2760 |
module="migrations.test_migrations_squashed" |
| 2761 |
): |
| 2762 |
with captured_stdout() as out: |
| 2763 |
call_command( |
| 2764 |
"makemigrations", |
| 2765 |
"migrations", |
| 2766 |
interactive=False, |
| 2767 |
empty=True, |
| 2768 |
) |
| 2769 |
out_value = out.getvalue() |
| 2770 |
self.assertIn("0003_auto", out_value) |
| 2771 |
|
| 2772 |
def test_makemigrations_update(self): |
| 2773 |
with self.temporary_migration_module( |
| 2774 |
module="migrations.test_migrations" |
| 2775 |
) as migration_dir: |
| 2776 |
migration_file = os.path.join(migration_dir, "0002_second.py") |
| 2777 |
with open(migration_file) as fp: |
| 2778 |
initial_content = fp.read() |
| 2779 |
|
| 2780 |
with captured_stdout() as out: |
| 2781 |
call_command("makemigrations", "migrations", update=True) |
| 2782 |
self.assertFalse( |
| 2783 |
any( |
| 2784 |
filename.startswith("0003") |
| 2785 |
for filename in os.listdir(migration_dir) |
| 2786 |
) |
| 2787 |
) |
| 2788 |
self.assertIs(os.path.exists(migration_file), False) |
| 2789 |
new_migration_file = os.path.join( |
| 2790 |
migration_dir, |
| 2791 |
"0002_delete_tribble_author_rating_modelwithcustombase_and_more.py", |
| 2792 |
) |
| 2793 |
with open(new_migration_file) as fp: |
| 2794 |
self.assertNotEqual(initial_content, fp.read()) |
| 2795 |
self.assertIn(f"Deleted {migration_file}", out.getvalue()) |
| 2796 |
|
| 2797 |
def test_makemigrations_update_existing_name(self): |
| 2798 |
with self.temporary_migration_module( |
| 2799 |
module="migrations.test_auto_now_add" |
| 2800 |
) as migration_dir: |
| 2801 |
migration_file = os.path.join(migration_dir, "0001_initial.py") |
| 2802 |
with open(migration_file) as fp: |
| 2803 |
initial_content = fp.read() |
| 2804 |
|
| 2805 |
with captured_stdout() as out: |
| 2806 |
call_command("makemigrations", "migrations", update=True) |
| 2807 |
self.assertIs(os.path.exists(migration_file), False) |
| 2808 |
new_migration_file = os.path.join( |
| 2809 |
migration_dir, |
| 2810 |
"0001_initial_updated.py", |
| 2811 |
) |
| 2812 |
with open(new_migration_file) as fp: |
| 2813 |
self.assertNotEqual(initial_content, fp.read()) |
| 2814 |
self.assertIn(f"Deleted {migration_file}", out.getvalue()) |
| 2815 |
|
| 2816 |
def test_makemigrations_update_custom_name(self): |
| 2817 |
custom_name = "delete_something" |
| 2818 |
with self.temporary_migration_module( |
| 2819 |
module="migrations.test_migrations" |
| 2820 |
) as migration_dir: |
| 2821 |
old_migration_file = os.path.join(migration_dir, "0002_second.py") |
| 2822 |
with open(old_migration_file) as fp: |
| 2823 |
initial_content = fp.read() |
| 2824 |
|
| 2825 |
with captured_stdout() as out: |
| 2826 |
call_command( |
| 2827 |
"makemigrations", "migrations", update=True, name=custom_name |
| 2828 |
) |
| 2829 |
self.assertFalse( |
| 2830 |
any( |
| 2831 |
filename.startswith("0003") |
| 2832 |
for filename in os.listdir(migration_dir) |
| 2833 |
) |
| 2834 |
) |
| 2835 |
self.assertIs(os.path.exists(old_migration_file), False) |
| 2836 |
new_migration_file = os.path.join(migration_dir, f"0002_{custom_name}.py") |
| 2837 |
self.assertIs(os.path.exists(new_migration_file), True) |
| 2838 |
with open(new_migration_file) as fp: |
| 2839 |
self.assertNotEqual(initial_content, fp.read()) |
| 2840 |
self.assertIn(f"Deleted {old_migration_file}", out.getvalue()) |
| 2841 |
|
| 2842 |
def test_makemigrations_update_applied_migration(self): |
| 2843 |
recorder = MigrationRecorder(connection) |
| 2844 |
recorder.record_applied("migrations", "0001_initial") |
| 2845 |
recorder.record_applied("migrations", "0002_second") |
| 2846 |
with self.temporary_migration_module(module="migrations.test_migrations"): |
| 2847 |
msg = "Cannot update applied migration 'migrations.0002_second'." |
| 2848 |
with self.assertRaisesMessage(CommandError, msg): |
| 2849 |
call_command("makemigrations", "migrations", update=True) |
| 2850 |
|
| 2851 |
def test_makemigrations_update_no_migration(self): |
| 2852 |
with self.temporary_migration_module(module="migrations.test_migrations_empty"): |
| 2853 |
msg = "App migrations has no migration, cannot update last migration." |
| 2854 |
with self.assertRaisesMessage(CommandError, msg): |
| 2855 |
call_command("makemigrations", "migrations", update=True) |
| 2856 |
|
| 2857 |
def test_makemigrations_update_squash_migration(self): |
| 2858 |
with self.temporary_migration_module( |
| 2859 |
module="migrations.test_migrations_squashed" |
| 2860 |
): |
| 2861 |
msg = "Cannot update squash migration 'migrations.0001_squashed_0002'." |
| 2862 |
with self.assertRaisesMessage(CommandError, msg): |
| 2863 |
call_command("makemigrations", "migrations", update=True) |
| 2864 |
|
| 2865 |
def test_makemigrations_update_manual_porting(self): |
| 2866 |
with self.temporary_migration_module( |
| 2867 |
module="migrations.test_migrations_plan" |
| 2868 |
) as migration_dir: |
| 2869 |
with captured_stdout() as out: |
| 2870 |
call_command("makemigrations", "migrations", update=True) |
| 2871 |
# Previous migration exists. |
| 2872 |
previous_migration_file = os.path.join(migration_dir, "0005_fifth.py") |
| 2873 |
self.assertIs(os.path.exists(previous_migration_file), True) |
| 2874 |
# New updated migration exists. |
| 2875 |
files = [f for f in os.listdir(migration_dir) if f.startswith("0005_auto")] |
| 2876 |
updated_migration_file = os.path.join(migration_dir, files[0]) |
| 2877 |
self.assertIs(os.path.exists(updated_migration_file), True) |
| 2878 |
self.assertIn( |
| 2879 |
f"Updated migration {updated_migration_file} requires manual porting.\n" |
| 2880 |
f"Previous migration {previous_migration_file} was kept and must be " |
| 2881 |
f"deleted after porting functions manually.", |
| 2882 |
out.getvalue(), |
| 2883 |
) |
| 2884 |
|
| 2885 |
@override_settings( |
| 2886 |
INSTALLED_APPS=[ |
| 2887 |
"migrations.migrations_test_apps.alter_fk.author_app", |
| 2888 |
"migrations.migrations_test_apps.alter_fk.book_app", |
| 2889 |
] |
| 2890 |
) |
| 2891 |
def test_makemigrations_update_dependency_migration(self): |
| 2892 |
with self.temporary_migration_module(app_label="book_app"): |
| 2893 |
msg = ( |
| 2894 |
"Cannot update migration 'book_app.0001_initial' that migrations " |
| 2895 |
"'author_app.0002_alter_id' depend on." |
| 2896 |
) |
| 2897 |
with self.assertRaisesMessage(CommandError, msg): |
| 2898 |
call_command("makemigrations", "book_app", update=True) |
| 2899 |
|
| 2900 |
|
| 2901 |
class SquashMigrationsTests(MigrationTestBase): |
| 2902 |
""" |
| 2903 |
Tests running the squashmigrations command. |
| 2904 |
""" |
| 2905 |
|
| 2906 |
def test_squashmigrations_squashes(self): |
| 2907 |
""" |
| 2908 |
squashmigrations squashes migrations. |
| 2909 |
""" |
| 2910 |
out = io.StringIO() |
| 2911 |
with self.temporary_migration_module( |
| 2912 |
module="migrations.test_migrations" |
| 2913 |
) as migration_dir: |
| 2914 |
call_command( |
| 2915 |
"squashmigrations", |
| 2916 |
"migrations", |
| 2917 |
"0002", |
| 2918 |
interactive=False, |
| 2919 |
stdout=out, |
| 2920 |
no_color=True, |
| 2921 |
) |
| 2922 |
|
| 2923 |
squashed_migration_file = os.path.join( |
| 2924 |
migration_dir, "0001_squashed_0002_second.py" |
| 2925 |
) |
| 2926 |
self.assertTrue(os.path.exists(squashed_migration_file)) |
| 2927 |
self.assertEqual( |
| 2928 |
out.getvalue(), |
| 2929 |
"Will squash the following migrations:\n" |
| 2930 |
" - 0001_initial\n" |
| 2931 |
" - 0002_second\n" |
| 2932 |
"Optimizing...\n" |
| 2933 |
" Optimized from 8 operations to 2 operations.\n" |
| 2934 |
"Created new squashed migration %s\n" |
| 2935 |
" You should commit this migration but leave the old ones in place;\n" |
| 2936 |
" the new migration will be used for new installs. Once you are sure\n" |
| 2937 |
" all instances of the codebase have applied the migrations you " |
| 2938 |
"squashed,\n" |
| 2939 |
" you can delete them.\n" % squashed_migration_file, |
| 2940 |
) |
| 2941 |
|
| 2942 |
def test_squashmigrations_initial_attribute(self): |
| 2943 |
with self.temporary_migration_module( |
| 2944 |
module="migrations.test_migrations" |
| 2945 |
) as migration_dir: |
| 2946 |
call_command( |
| 2947 |
"squashmigrations", "migrations", "0002", interactive=False, verbosity=0 |
| 2948 |
) |
| 2949 |
|
| 2950 |
squashed_migration_file = os.path.join( |
| 2951 |
migration_dir, "0001_squashed_0002_second.py" |
| 2952 |
) |
| 2953 |
with open(squashed_migration_file, encoding="utf-8") as fp: |
| 2954 |
content = fp.read() |
| 2955 |
self.assertIn("initial = True", content) |
| 2956 |
|
| 2957 |
def test_squashmigrations_optimizes(self): |
| 2958 |
""" |
| 2959 |
squashmigrations optimizes operations. |
| 2960 |
""" |
| 2961 |
out = io.StringIO() |
| 2962 |
with self.temporary_migration_module(module="migrations.test_migrations"): |
| 2963 |
call_command( |
| 2964 |
"squashmigrations", |
| 2965 |
"migrations", |
| 2966 |
"0002", |
| 2967 |
interactive=False, |
| 2968 |
verbosity=1, |
| 2969 |
stdout=out, |
| 2970 |
) |
| 2971 |
self.assertIn("Optimized from 8 operations to 2 operations.", out.getvalue()) |
| 2972 |
|
| 2973 |
def test_ticket_23799_squashmigrations_no_optimize(self): |
| 2974 |
""" |
| 2975 |
squashmigrations --no-optimize doesn't optimize operations. |
| 2976 |
""" |
| 2977 |
out = io.StringIO() |
| 2978 |
with self.temporary_migration_module(module="migrations.test_migrations"): |
| 2979 |
call_command( |
| 2980 |
"squashmigrations", |
| 2981 |
"migrations", |
| 2982 |
"0002", |
| 2983 |
interactive=False, |
| 2984 |
verbosity=1, |
| 2985 |
no_optimize=True, |
| 2986 |
stdout=out, |
| 2987 |
) |
| 2988 |
self.assertIn("Skipping optimization", out.getvalue()) |
| 2989 |
|
| 2990 |
def test_squashmigrations_valid_start(self): |
| 2991 |
""" |
| 2992 |
squashmigrations accepts a starting migration. |
| 2993 |
""" |
| 2994 |
out = io.StringIO() |
| 2995 |
with self.temporary_migration_module( |
| 2996 |
module="migrations.test_migrations_no_changes" |
| 2997 |
) as migration_dir: |
| 2998 |
call_command( |
| 2999 |
"squashmigrations", |
| 3000 |
"migrations", |
| 3001 |
"0002", |
| 3002 |
"0003", |
| 3003 |
interactive=False, |
| 3004 |
verbosity=1, |
| 3005 |
stdout=out, |
| 3006 |
) |
| 3007 |
|
| 3008 |
squashed_migration_file = os.path.join( |
| 3009 |
migration_dir, "0002_second_squashed_0003_third.py" |
| 3010 |
) |
| 3011 |
with open(squashed_migration_file, encoding="utf-8") as fp: |
| 3012 |
content = fp.read() |
| 3013 |
if HAS_BLACK: |
| 3014 |
test_str = ' ("migrations", "0001_initial")' |
| 3015 |
else: |
| 3016 |
test_str = " ('migrations', '0001_initial')" |
| 3017 |
self.assertIn(test_str, content) |
| 3018 |
self.assertNotIn("initial = True", content) |
| 3019 |
out = out.getvalue() |
| 3020 |
self.assertNotIn(" - 0001_initial", out) |
| 3021 |
self.assertIn(" - 0002_second", out) |
| 3022 |
self.assertIn(" - 0003_third", out) |
| 3023 |
|
| 3024 |
def test_squashmigrations_invalid_start(self): |
| 3025 |
""" |
| 3026 |
squashmigrations doesn't accept a starting migration after the ending migration. |
| 3027 |
""" |
| 3028 |
with self.temporary_migration_module( |
| 3029 |
module="migrations.test_migrations_no_changes" |
| 3030 |
): |
| 3031 |
msg = ( |
| 3032 |
"The migration 'migrations.0003_third' cannot be found. Maybe " |
| 3033 |
"it comes after the migration 'migrations.0002_second'" |
| 3034 |
) |
| 3035 |
with self.assertRaisesMessage(CommandError, msg): |
| 3036 |
call_command( |
| 3037 |
"squashmigrations", |
| 3038 |
"migrations", |
| 3039 |
"0003", |
| 3040 |
"0002", |
| 3041 |
interactive=False, |
| 3042 |
verbosity=0, |
| 3043 |
) |
| 3044 |
|
| 3045 |
def test_squashed_name_with_start_migration_name(self): |
| 3046 |
"""--squashed-name specifies the new migration's name.""" |
| 3047 |
squashed_name = "squashed_name" |
| 3048 |
with self.temporary_migration_module( |
| 3049 |
module="migrations.test_migrations" |
| 3050 |
) as migration_dir: |
| 3051 |
call_command( |
| 3052 |
"squashmigrations", |
| 3053 |
"migrations", |
| 3054 |
"0001", |
| 3055 |
"0002", |
| 3056 |
squashed_name=squashed_name, |
| 3057 |
interactive=False, |
| 3058 |
verbosity=0, |
| 3059 |
) |
| 3060 |
squashed_migration_file = os.path.join( |
| 3061 |
migration_dir, "0001_%s.py" % squashed_name |
| 3062 |
) |
| 3063 |
self.assertTrue(os.path.exists(squashed_migration_file)) |
| 3064 |
|
| 3065 |
def test_squashed_name_without_start_migration_name(self): |
| 3066 |
"""--squashed-name also works if a start migration is omitted.""" |
| 3067 |
squashed_name = "squashed_name" |
| 3068 |
with self.temporary_migration_module( |
| 3069 |
module="migrations.test_migrations" |
| 3070 |
) as migration_dir: |
| 3071 |
call_command( |
| 3072 |
"squashmigrations", |
| 3073 |
"migrations", |
| 3074 |
"0001", |
| 3075 |
squashed_name=squashed_name, |
| 3076 |
interactive=False, |
| 3077 |
verbosity=0, |
| 3078 |
) |
| 3079 |
squashed_migration_file = os.path.join( |
| 3080 |
migration_dir, "0001_%s.py" % squashed_name |
| 3081 |
) |
| 3082 |
self.assertTrue(os.path.exists(squashed_migration_file)) |
| 3083 |
|
| 3084 |
def test_squashed_name_exists(self): |
| 3085 |
msg = "Migration 0001_initial already exists. Use a different name." |
| 3086 |
with self.temporary_migration_module(module="migrations.test_migrations"): |
| 3087 |
with self.assertRaisesMessage(CommandError, msg): |
| 3088 |
call_command( |
| 3089 |
"squashmigrations", |
| 3090 |
"migrations", |
| 3091 |
"0001", |
| 3092 |
"0002", |
| 3093 |
squashed_name="initial", |
| 3094 |
interactive=False, |
| 3095 |
verbosity=0, |
| 3096 |
) |
| 3097 |
|
| 3098 |
def test_squashmigrations_manual_porting(self): |
| 3099 |
out = io.StringIO() |
| 3100 |
with self.temporary_migration_module( |
| 3101 |
module="migrations.test_migrations_manual_porting", |
| 3102 |
) as migration_dir: |
| 3103 |
call_command( |
| 3104 |
"squashmigrations", |
| 3105 |
"migrations", |
| 3106 |
"0002", |
| 3107 |
interactive=False, |
| 3108 |
stdout=out, |
| 3109 |
no_color=True, |
| 3110 |
) |
| 3111 |
squashed_migration_file = os.path.join( |
| 3112 |
migration_dir, |
| 3113 |
"0001_squashed_0002_second.py", |
| 3114 |
) |
| 3115 |
self.assertTrue(os.path.exists(squashed_migration_file)) |
| 3116 |
black_warning = "" |
| 3117 |
if HAS_BLACK: |
| 3118 |
black_warning = ( |
| 3119 |
"Squashed migration couldn't be formatted using the " |
| 3120 |
'"black" command. You can call it manually.\n' |
| 3121 |
) |
| 3122 |
self.assertEqual( |
| 3123 |
out.getvalue(), |
| 3124 |
f"Will squash the following migrations:\n" |
| 3125 |
f" - 0001_initial\n" |
| 3126 |
f" - 0002_second\n" |
| 3127 |
f"Optimizing...\n" |
| 3128 |
f" No optimizations possible.\n" |
| 3129 |
f"Created new squashed migration {squashed_migration_file}\n" |
| 3130 |
f" You should commit this migration but leave the old ones in place;\n" |
| 3131 |
f" the new migration will be used for new installs. Once you are sure\n" |
| 3132 |
f" all instances of the codebase have applied the migrations you " |
| 3133 |
f"squashed,\n" |
| 3134 |
f" you can delete them.\n" |
| 3135 |
f"Manual porting required\n" |
| 3136 |
f" Your migrations contained functions that must be manually copied " |
| 3137 |
f"over,\n" |
| 3138 |
f" as we could not safely copy their implementation.\n" |
| 3139 |
f" See the comment at the top of the squashed migration for details.\n" |
| 3140 |
+ black_warning, |
| 3141 |
) |
| 3142 |
|
| 3143 |
def test_failure_to_format_code(self): |
| 3144 |
self.assertFormatterFailureCaught( |
| 3145 |
"squashmigrations", "migrations", "0002", interactive=False |
| 3146 |
) |
| 3147 |
|
| 3148 |
|
| 3149 |
class AppLabelErrorTests(TestCase): |
| 3150 |
""" |
| 3151 |
This class inherits TestCase because MigrationTestBase uses |
| 3152 |
`available_apps = ['migrations']` which means that it's the only installed |
| 3153 |
app. 'django.contrib.auth' must be in INSTALLED_APPS for some of these |
| 3154 |
tests. |
| 3155 |
""" |
| 3156 |
|
| 3157 |
nonexistent_app_error = "No installed app with label 'nonexistent_app'." |
| 3158 |
did_you_mean_auth_error = ( |
| 3159 |
"No installed app with label 'django.contrib.auth'. Did you mean 'auth'?" |
| 3160 |
) |
| 3161 |
|
| 3162 |
def test_makemigrations_nonexistent_app_label(self): |
| 3163 |
err = io.StringIO() |
| 3164 |
with self.assertRaises(SystemExit): |
| 3165 |
call_command("makemigrations", "nonexistent_app", stderr=err) |
| 3166 |
self.assertIn(self.nonexistent_app_error, err.getvalue()) |
| 3167 |
|
| 3168 |
def test_makemigrations_app_name_specified_as_label(self): |
| 3169 |
err = io.StringIO() |
| 3170 |
with self.assertRaises(SystemExit): |
| 3171 |
call_command("makemigrations", "django.contrib.auth", stderr=err) |
| 3172 |
self.assertIn(self.did_you_mean_auth_error, err.getvalue()) |
| 3173 |
|
| 3174 |
def test_migrate_nonexistent_app_label(self): |
| 3175 |
with self.assertRaisesMessage(CommandError, self.nonexistent_app_error): |
| 3176 |
call_command("migrate", "nonexistent_app") |
| 3177 |
|
| 3178 |
def test_migrate_app_name_specified_as_label(self): |
| 3179 |
with self.assertRaisesMessage(CommandError, self.did_you_mean_auth_error): |
| 3180 |
call_command("migrate", "django.contrib.auth") |
| 3181 |
|
| 3182 |
def test_showmigrations_nonexistent_app_label(self): |
| 3183 |
err = io.StringIO() |
| 3184 |
with self.assertRaises(SystemExit): |
| 3185 |
call_command("showmigrations", "nonexistent_app", stderr=err) |
| 3186 |
self.assertIn(self.nonexistent_app_error, err.getvalue()) |
| 3187 |
|
| 3188 |
def test_showmigrations_app_name_specified_as_label(self): |
| 3189 |
err = io.StringIO() |
| 3190 |
with self.assertRaises(SystemExit): |
| 3191 |
call_command("showmigrations", "django.contrib.auth", stderr=err) |
| 3192 |
self.assertIn(self.did_you_mean_auth_error, err.getvalue()) |
| 3193 |
|
| 3194 |
def test_sqlmigrate_nonexistent_app_label(self): |
| 3195 |
with self.assertRaisesMessage(CommandError, self.nonexistent_app_error): |
| 3196 |
call_command("sqlmigrate", "nonexistent_app", "0002") |
| 3197 |
|
| 3198 |
def test_sqlmigrate_app_name_specified_as_label(self): |
| 3199 |
with self.assertRaisesMessage(CommandError, self.did_you_mean_auth_error): |
| 3200 |
call_command("sqlmigrate", "django.contrib.auth", "0002") |
| 3201 |
|
| 3202 |
def test_squashmigrations_nonexistent_app_label(self): |
| 3203 |
with self.assertRaisesMessage(CommandError, self.nonexistent_app_error): |
| 3204 |
call_command("squashmigrations", "nonexistent_app", "0002") |
| 3205 |
|
| 3206 |
def test_squashmigrations_app_name_specified_as_label(self): |
| 3207 |
with self.assertRaisesMessage(CommandError, self.did_you_mean_auth_error): |
| 3208 |
call_command("squashmigrations", "django.contrib.auth", "0002") |
| 3209 |
|
| 3210 |
def test_optimizemigration_nonexistent_app_label(self): |
| 3211 |
with self.assertRaisesMessage(CommandError, self.nonexistent_app_error): |
| 3212 |
call_command("optimizemigration", "nonexistent_app", "0002") |
| 3213 |
|
| 3214 |
def test_optimizemigration_app_name_specified_as_label(self): |
| 3215 |
with self.assertRaisesMessage(CommandError, self.did_you_mean_auth_error): |
| 3216 |
call_command("optimizemigration", "django.contrib.auth", "0002") |
| 3217 |
|
| 3218 |
|
| 3219 |
class OptimizeMigrationTests(MigrationTestBase): |
| 3220 |
def test_no_optimization_possible(self): |
| 3221 |
out = io.StringIO() |
| 3222 |
with self.temporary_migration_module( |
| 3223 |
module="migrations.test_migrations" |
| 3224 |
) as migration_dir: |
| 3225 |
call_command( |
| 3226 |
"optimizemigration", "migrations", "0002", stdout=out, no_color=True |
| 3227 |
) |
| 3228 |
migration_file = os.path.join(migration_dir, "0002_second.py") |
| 3229 |
self.assertTrue(os.path.exists(migration_file)) |
| 3230 |
call_command( |
| 3231 |
"optimizemigration", |
| 3232 |
"migrations", |
| 3233 |
"0002", |
| 3234 |
stdout=out, |
| 3235 |
no_color=True, |
| 3236 |
verbosity=0, |
| 3237 |
) |
| 3238 |
self.assertEqual(out.getvalue(), "No optimizations possible.\n") |
| 3239 |
|
| 3240 |
def test_optimization(self): |
| 3241 |
out = io.StringIO() |
| 3242 |
with self.temporary_migration_module( |
| 3243 |
module="migrations.test_migrations" |
| 3244 |
) as migration_dir: |
| 3245 |
call_command( |
| 3246 |
"optimizemigration", "migrations", "0001", stdout=out, no_color=True |
| 3247 |
) |
| 3248 |
initial_migration_file = os.path.join(migration_dir, "0001_initial.py") |
| 3249 |
self.assertTrue(os.path.exists(initial_migration_file)) |
| 3250 |
with open(initial_migration_file) as fp: |
| 3251 |
content = fp.read() |
| 3252 |
self.assertIn( |
| 3253 |
( |
| 3254 |
'("bool", models.BooleanField' |
| 3255 |
if HAS_BLACK |
| 3256 |
else "('bool', models.BooleanField" |
| 3257 |
), |
| 3258 |
content, |
| 3259 |
) |
| 3260 |
self.assertEqual( |
| 3261 |
out.getvalue(), |
| 3262 |
f"Optimizing from 4 operations to 2 operations.\n" |
| 3263 |
f"Optimized migration {initial_migration_file}\n", |
| 3264 |
) |
| 3265 |
|
| 3266 |
def test_optimization_no_verbosity(self): |
| 3267 |
out = io.StringIO() |
| 3268 |
with self.temporary_migration_module( |
| 3269 |
module="migrations.test_migrations" |
| 3270 |
) as migration_dir: |
| 3271 |
call_command( |
| 3272 |
"optimizemigration", |
| 3273 |
"migrations", |
| 3274 |
"0001", |
| 3275 |
stdout=out, |
| 3276 |
no_color=True, |
| 3277 |
verbosity=0, |
| 3278 |
) |
| 3279 |
initial_migration_file = os.path.join(migration_dir, "0001_initial.py") |
| 3280 |
self.assertTrue(os.path.exists(initial_migration_file)) |
| 3281 |
with open(initial_migration_file) as fp: |
| 3282 |
content = fp.read() |
| 3283 |
self.assertIn( |
| 3284 |
( |
| 3285 |
'("bool", models.BooleanField' |
| 3286 |
if HAS_BLACK |
| 3287 |
else "('bool', models.BooleanField" |
| 3288 |
), |
| 3289 |
content, |
| 3290 |
) |
| 3291 |
self.assertEqual(out.getvalue(), "") |
| 3292 |
|
| 3293 |
def test_creates_replace_migration_manual_porting(self): |
| 3294 |
out = io.StringIO() |
| 3295 |
with self.temporary_migration_module( |
| 3296 |
module="migrations.test_migrations_manual_porting" |
| 3297 |
) as migration_dir: |
| 3298 |
call_command( |
| 3299 |
"optimizemigration", "migrations", "0003", stdout=out, no_color=True |
| 3300 |
) |
| 3301 |
optimized_migration_file = os.path.join( |
| 3302 |
migration_dir, "0003_third_optimized.py" |
| 3303 |
) |
| 3304 |
self.assertTrue(os.path.exists(optimized_migration_file)) |
| 3305 |
with open(optimized_migration_file) as fp: |
| 3306 |
content = fp.read() |
| 3307 |
self.assertIn("replaces = [", content) |
| 3308 |
black_warning = "" |
| 3309 |
if HAS_BLACK: |
| 3310 |
black_warning = ( |
| 3311 |
"Optimized migration couldn't be formatted using the " |
| 3312 |
'"black" command. You can call it manually.\n' |
| 3313 |
) |
| 3314 |
self.assertEqual( |
| 3315 |
out.getvalue(), |
| 3316 |
"Optimizing from 3 operations to 2 operations.\n" |
| 3317 |
"Manual porting required\n" |
| 3318 |
" Your migrations contained functions that must be manually copied over,\n" |
| 3319 |
" as we could not safely copy their implementation.\n" |
| 3320 |
" See the comment at the top of the optimized migration for details.\n" |
| 3321 |
+ black_warning |
| 3322 |
+ f"Optimized migration {optimized_migration_file}\n", |
| 3323 |
) |
| 3324 |
|
| 3325 |
def test_fails_squash_migration_manual_porting(self): |
| 3326 |
out = io.StringIO() |
| 3327 |
with self.temporary_migration_module( |
| 3328 |
module="migrations.test_migrations_manual_porting" |
| 3329 |
) as migration_dir: |
| 3330 |
version = get_docs_version() |
| 3331 |
msg = ( |
| 3332 |
f"Migration will require manual porting but is already a squashed " |
| 3333 |
f"migration.\nTransition to a normal migration first: " |
| 3334 |
f"https://docs.djangoproject.com/en/{version}/topics/migrations/" |
| 3335 |
f"#squashing-migrations" |
| 3336 |
) |
| 3337 |
with self.assertRaisesMessage(CommandError, msg): |
| 3338 |
call_command("optimizemigration", "migrations", "0004", stdout=out) |
| 3339 |
optimized_migration_file = os.path.join( |
| 3340 |
migration_dir, "0004_fourth_optimized.py" |
| 3341 |
) |
| 3342 |
self.assertFalse(os.path.exists(optimized_migration_file)) |
| 3343 |
self.assertEqual( |
| 3344 |
out.getvalue(), "Optimizing from 3 operations to 2 operations.\n" |
| 3345 |
) |
| 3346 |
|
| 3347 |
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) |
| 3348 |
def test_optimizemigration_check(self): |
| 3349 |
with self.assertRaises(SystemExit): |
| 3350 |
call_command( |
| 3351 |
"optimizemigration", "--check", "migrations", "0001", verbosity=0 |
| 3352 |
) |
| 3353 |
|
| 3354 |
call_command("optimizemigration", "--check", "migrations", "0002", verbosity=0) |
| 3355 |
|
| 3356 |
@override_settings( |
| 3357 |
INSTALLED_APPS=["migrations.migrations_test_apps.unmigrated_app_simple"], |
| 3358 |
) |
| 3359 |
def test_app_without_migrations(self): |
| 3360 |
msg = "App 'unmigrated_app_simple' does not have migrations." |
| 3361 |
with self.assertRaisesMessage(CommandError, msg): |
| 3362 |
call_command("optimizemigration", "unmigrated_app_simple", "0001") |
| 3363 |
|
| 3364 |
@override_settings( |
| 3365 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations_clashing_prefix"}, |
| 3366 |
) |
| 3367 |
def test_ambigious_prefix(self): |
| 3368 |
msg = ( |
| 3369 |
"More than one migration matches 'a' in app 'migrations'. Please " |
| 3370 |
"be more specific." |
| 3371 |
) |
| 3372 |
with self.assertRaisesMessage(CommandError, msg): |
| 3373 |
call_command("optimizemigration", "migrations", "a") |
| 3374 |
|
| 3375 |
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"}) |
| 3376 |
def test_unknown_prefix(self): |
| 3377 |
msg = "Cannot find a migration matching 'nonexistent' from app 'migrations'." |
| 3378 |
with self.assertRaisesMessage(CommandError, msg): |
| 3379 |
call_command("optimizemigration", "migrations", "nonexistent") |
| 3380 |
|
| 3381 |
def test_failure_to_format_code(self): |
| 3382 |
self.assertFormatterFailureCaught("optimizemigration", "migrations", "0001") |
| 3383 |
|
| 3384 |
|
| 3385 |
class CustomMigrationCommandTests(MigrationTestBase): |
| 3386 |
@override_settings( |
| 3387 |
MIGRATION_MODULES={"migrations": "migrations.test_migrations"}, |
| 3388 |
INSTALLED_APPS=["migrations.migrations_test_apps.migrated_app"], |
| 3389 |
) |
| 3390 |
@isolate_apps("migrations.migrations_test_apps.migrated_app") |
| 3391 |
def test_makemigrations_custom_autodetector(self): |
| 3392 |
class CustomAutodetector(MigrationAutodetector): |
| 3393 |
def changes(self, *args, **kwargs): |
| 3394 |
return [] |
| 3395 |
|
| 3396 |
class CustomMakeMigrationsCommand(MakeMigrationsCommand): |
| 3397 |
autodetector = CustomAutodetector |
| 3398 |
|
| 3399 |
class NewModel(models.Model): |
| 3400 |
class Meta: |
| 3401 |
app_label = "migrated_app" |
| 3402 |
|
| 3403 |
out = io.StringIO() |
| 3404 |
command = CustomMakeMigrationsCommand(stdout=out) |
| 3405 |
call_command(command, "migrated_app", stdout=out) |
| 3406 |
self.assertIn("No changes detected", out.getvalue()) |
| 3407 |
|
| 3408 |
@override_settings(INSTALLED_APPS=["migrations.migrations_test_apps.migrated_app"]) |
| 3409 |
@isolate_apps("migrations.migrations_test_apps.migrated_app") |
| 3410 |
def test_migrate_custom_autodetector(self): |
| 3411 |
class CustomAutodetector(MigrationAutodetector): |
| 3412 |
def changes(self, *args, **kwargs): |
| 3413 |
return [] |
| 3414 |
|
| 3415 |
class CustomMigrateCommand(MigrateCommand): |
| 3416 |
autodetector = CustomAutodetector |
| 3417 |
|
| 3418 |
class NewModel(models.Model): |
| 3419 |
class Meta: |
| 3420 |
app_label = "migrated_app" |
| 3421 |
|
| 3422 |
out = io.StringIO() |
| 3423 |
command = CustomMigrateCommand(stdout=out) |
| 3424 |
|
| 3425 |
out = io.StringIO() |
| 3426 |
try: |
| 3427 |
call_command(command, verbosity=0) |
| 3428 |
call_command(command, stdout=out, no_color=True) |
| 3429 |
command_stdout = out.getvalue().lower() |
| 3430 |
self.assertEqual( |
| 3431 |
"operations to perform:\n" |
| 3432 |
" apply all migrations: migrated_app\n" |
| 3433 |
"running migrations:\n" |
| 3434 |
" no migrations to apply.\n", |
| 3435 |
command_stdout, |
| 3436 |
) |
| 3437 |
finally: |
| 3438 |
call_command(command, "migrated_app", "zero", verbosity=0) |