mirror of
https://github.com/django/django.git
synced 2025-08-03 02:23:12 +00:00
Fixed #23359 -- Added showmigrations command to list migrations and plan.
Thanks to Collin Anderson, Tim Graham, Gabe Jackson, and Marc Tamlyn for their input, ideas, and review.
This commit is contained in:
parent
8952757698
commit
a1487deebf
7 changed files with 314 additions and 57 deletions
|
@ -9,8 +9,9 @@ from django.apps import apps
|
|||
from django.db import connection, models
|
||||
from django.core.management import call_command, CommandError
|
||||
from django.db.migrations import questioner
|
||||
from django.test import override_settings
|
||||
from django.test import ignore_warnings, override_settings
|
||||
from django.utils import six
|
||||
from django.utils.deprecation import RemovedInDjango20Warning
|
||||
from django.utils.encoding import force_text
|
||||
|
||||
from .models import UnicodeModel, UnserializableModel
|
||||
|
@ -50,6 +51,15 @@ class MigrateTests(MigrationTestBase):
|
|||
self.assertTableNotExists("migrations_tribble")
|
||||
self.assertTableNotExists("migrations_book")
|
||||
|
||||
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_conflict"})
|
||||
def test_migrate_conflict_exit(self):
|
||||
"""
|
||||
Makes sure that migrate exits if it detects a conflict.
|
||||
"""
|
||||
with self.assertRaisesMessage(CommandError, "Conflicting migrations detected"):
|
||||
call_command("migrate", "migrations")
|
||||
|
||||
@ignore_warnings(category=RemovedInDjango20Warning)
|
||||
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
|
||||
def test_migrate_list(self):
|
||||
"""
|
||||
|
@ -72,13 +82,137 @@ class MigrateTests(MigrationTestBase):
|
|||
# Cleanup by unmigrating everything
|
||||
call_command("migrate", "migrations", "zero", verbosity=0)
|
||||
|
||||
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_conflict"})
|
||||
def test_migrate_conflict_exit(self):
|
||||
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
|
||||
def test_showmigrations_list(self):
|
||||
"""
|
||||
Makes sure that migrate exits if it detects a conflict.
|
||||
Tests --list output of showmigrations command
|
||||
"""
|
||||
with self.assertRaises(CommandError):
|
||||
call_command("migrate", "migrations")
|
||||
out = six.StringIO()
|
||||
call_command("showmigrations", format='list', stdout=out, verbosity=0)
|
||||
self.assertIn("migrations", out.getvalue().lower())
|
||||
self.assertIn("[ ] 0001_initial", out.getvalue().lower())
|
||||
self.assertIn("[ ] 0002_second", out.getvalue().lower())
|
||||
|
||||
call_command("migrate", "migrations", "0001", verbosity=0)
|
||||
|
||||
out = six.StringIO()
|
||||
# Giving the explicit app_label tests for selective `show_list` in the command
|
||||
call_command("showmigrations", "migrations", format='list', stdout=out, verbosity=0)
|
||||
self.assertIn("migrations", out.getvalue().lower())
|
||||
self.assertIn("[x] 0001_initial", out.getvalue().lower())
|
||||
self.assertIn("[ ] 0002_second", out.getvalue().lower())
|
||||
# Cleanup by unmigrating everything
|
||||
call_command("migrate", "migrations", "zero", verbosity=0)
|
||||
|
||||
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_run_before"})
|
||||
def test_showmigrations_plan(self):
|
||||
"""
|
||||
Tests --plan output of showmigrations command
|
||||
"""
|
||||
out = six.StringIO()
|
||||
call_command("showmigrations", format='plan', stdout=out)
|
||||
self.assertIn(
|
||||
"[ ] migrations.0001_initial\n"
|
||||
"[ ] migrations.0003_third\n"
|
||||
"[ ] migrations.0002_second",
|
||||
out.getvalue().lower()
|
||||
)
|
||||
|
||||
out = six.StringIO()
|
||||
call_command("showmigrations", format='plan', stdout=out, verbosity=2)
|
||||
self.assertIn(
|
||||
"[ ] migrations.0001_initial\n"
|
||||
"[ ] migrations.0003_third ... (migrations.0001_initial)\n"
|
||||
"[ ] migrations.0002_second ... (migrations.0001_initial)",
|
||||
out.getvalue().lower()
|
||||
)
|
||||
|
||||
call_command("migrate", "migrations", "0003", verbosity=0)
|
||||
|
||||
out = six.StringIO()
|
||||
call_command("showmigrations", format='plan', stdout=out)
|
||||
self.assertIn(
|
||||
"[x] migrations.0001_initial\n"
|
||||
"[x] migrations.0003_third\n"
|
||||
"[ ] migrations.0002_second",
|
||||
out.getvalue().lower()
|
||||
)
|
||||
|
||||
out = six.StringIO()
|
||||
call_command("showmigrations", format='plan', stdout=out, verbosity=2)
|
||||
self.assertIn(
|
||||
"[x] migrations.0001_initial\n"
|
||||
"[x] migrations.0003_third ... (migrations.0001_initial)\n"
|
||||
"[ ] migrations.0002_second ... (migrations.0001_initial)",
|
||||
out.getvalue().lower()
|
||||
)
|
||||
|
||||
# Cleanup by unmigrating everything
|
||||
call_command("migrate", "migrations", "zero", verbosity=0)
|
||||
|
||||
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_empty"})
|
||||
def test_showmigrations_plan_no_migrations(self):
|
||||
"""
|
||||
Tests --plan output of showmigrations command without migrations
|
||||
"""
|
||||
out = six.StringIO()
|
||||
call_command("showmigrations", format='plan', stdout=out)
|
||||
self.assertEqual("", out.getvalue().lower())
|
||||
|
||||
out = six.StringIO()
|
||||
call_command("showmigrations", format='plan', stdout=out, verbosity=2)
|
||||
self.assertEqual("", out.getvalue().lower())
|
||||
|
||||
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations_squashed_complex"})
|
||||
def test_showmigrations_plan_squashed(self):
|
||||
"""
|
||||
Tests --plan output of showmigrations command with squashed migrations.
|
||||
"""
|
||||
out = six.StringIO()
|
||||
call_command("showmigrations", format='plan', stdout=out)
|
||||
self.assertEqual(
|
||||
"[ ] migrations.1_auto\n"
|
||||
"[ ] migrations.2_auto\n"
|
||||
"[ ] migrations.3_squashed_5\n"
|
||||
"[ ] migrations.6_auto\n"
|
||||
"[ ] migrations.7_auto\n",
|
||||
out.getvalue().lower()
|
||||
)
|
||||
|
||||
out = six.StringIO()
|
||||
call_command("showmigrations", format='plan', stdout=out, verbosity=2)
|
||||
self.assertEqual(
|
||||
"[ ] migrations.1_auto\n"
|
||||
"[ ] migrations.2_auto ... (migrations.1_auto)\n"
|
||||
"[ ] migrations.3_squashed_5 ... (migrations.2_auto)\n"
|
||||
"[ ] migrations.6_auto ... (migrations.3_squashed_5)\n"
|
||||
"[ ] migrations.7_auto ... (migrations.6_auto)\n",
|
||||
out.getvalue().lower()
|
||||
)
|
||||
|
||||
call_command("migrate", "migrations", "3_squashed_5", verbosity=0)
|
||||
|
||||
out = six.StringIO()
|
||||
call_command("showmigrations", format='plan', stdout=out)
|
||||
self.assertEqual(
|
||||
"[x] migrations.1_auto\n"
|
||||
"[x] migrations.2_auto\n"
|
||||
"[x] migrations.3_squashed_5\n"
|
||||
"[ ] migrations.6_auto\n"
|
||||
"[ ] migrations.7_auto\n",
|
||||
out.getvalue().lower()
|
||||
)
|
||||
|
||||
out = six.StringIO()
|
||||
call_command("showmigrations", format='plan', stdout=out, verbosity=2)
|
||||
self.assertEqual(
|
||||
"[x] migrations.1_auto\n"
|
||||
"[x] migrations.2_auto ... (migrations.1_auto)\n"
|
||||
"[x] migrations.3_squashed_5 ... (migrations.2_auto)\n"
|
||||
"[ ] migrations.6_auto ... (migrations.3_squashed_5)\n"
|
||||
"[ ] migrations.7_auto ... (migrations.6_auto)\n",
|
||||
out.getvalue().lower()
|
||||
)
|
||||
|
||||
@override_settings(MIGRATION_MODULES={"migrations": "migrations.test_migrations"})
|
||||
def test_sqlmigrate(self):
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue