From 06bf06a911695c5c84f746742f764c040e237ece Mon Sep 17 00:00:00 2001 From: leondaz Date: Mon, 9 Sep 2024 19:15:40 +0300 Subject: Fixed #35656 -- Added an autodetector attribute to the makemigrations and migrate commands. --- .../management/commands/makemigrations.py | 7 +++ tests/check_framework/test_commands.py | 25 +++++++++ tests/migrations/test_commands.py | 63 +++++++++++++++++++++- 3 files changed, 94 insertions(+), 1 deletion(-) create mode 100644 tests/check_framework/custom_commands_app/management/commands/makemigrations.py create mode 100644 tests/check_framework/test_commands.py (limited to 'tests') diff --git a/tests/check_framework/custom_commands_app/management/commands/makemigrations.py b/tests/check_framework/custom_commands_app/management/commands/makemigrations.py new file mode 100644 index 0000000000..a6494cba4c --- /dev/null +++ b/tests/check_framework/custom_commands_app/management/commands/makemigrations.py @@ -0,0 +1,7 @@ +from django.core.management.commands.makemigrations import ( + Command as MakeMigrationsCommand, +) + + +class Command(MakeMigrationsCommand): + autodetector = int diff --git a/tests/check_framework/test_commands.py b/tests/check_framework/test_commands.py new file mode 100644 index 0000000000..a51db77402 --- /dev/null +++ b/tests/check_framework/test_commands.py @@ -0,0 +1,25 @@ +from django.core import checks +from django.core.checks import Error +from django.test import SimpleTestCase +from django.test.utils import isolate_apps, override_settings, override_system_checks + + +@isolate_apps("check_framework.custom_commands_app", attr_name="apps") +@override_settings(INSTALLED_APPS=["check_framework.custom_commands_app"]) +@override_system_checks([checks.commands.migrate_and_makemigrations_autodetector]) +class CommandCheckTests(SimpleTestCase): + def test_migrate_and_makemigrations_autodetector_different(self): + expected_error = Error( + "The migrate and makemigrations commands must have the same " + "autodetector.", + hint=( + "makemigrations.Command.autodetector is int, but " + "migrate.Command.autodetector is MigrationAutodetector." + ), + id="commands.E001", + ) + + self.assertEqual( + checks.run_checks(app_configs=self.apps.get_app_configs()), + [expected_error], + ) diff --git a/tests/migrations/test_commands.py b/tests/migrations/test_commands.py index cab2906ed1..724c88a28f 100644 --- a/tests/migrations/test_commands.py +++ b/tests/migrations/test_commands.py @@ -9,6 +9,10 @@ from unittest import mock from django.apps import apps from django.core.management import CommandError, call_command +from django.core.management.commands.makemigrations import ( + Command as MakeMigrationsCommand, +) +from django.core.management.commands.migrate import Command as MigrateCommand from django.db import ( ConnectionHandler, DatabaseError, @@ -19,10 +23,11 @@ from django.db import ( ) from django.db.backends.base.schema import BaseDatabaseSchemaEditor from django.db.backends.utils import truncate_name +from django.db.migrations.autodetector import MigrationAutodetector from django.db.migrations.exceptions import InconsistentMigrationHistory from django.db.migrations.recorder import MigrationRecorder from django.test import TestCase, override_settings, skipUnlessDBFeature -from django.test.utils import captured_stdout, extend_sys_path +from django.test.utils import captured_stdout, extend_sys_path, isolate_apps from django.utils import timezone from django.utils.version import get_docs_version @@ -3296,3 +3301,59 @@ class OptimizeMigrationTests(MigrationTestBase): msg = "Cannot find a migration matching 'nonexistent' from app 'migrations'." with self.assertRaisesMessage(CommandError, msg): call_command("optimizemigration", "migrations", "nonexistent") + + +class CustomMigrationCommandTests(MigrationTestBase): + @override_settings( + MIGRATION_MODULES={"migrations": "migrations.test_migrations"}, + INSTALLED_APPS=["migrations.migrations_test_apps.migrated_app"], + ) + @isolate_apps("migrations.migrations_test_apps.migrated_app") + def test_makemigrations_custom_autodetector(self): + class CustomAutodetector(MigrationAutodetector): + def changes(self, *args, **kwargs): + return [] + + class CustomMakeMigrationsCommand(MakeMigrationsCommand): + autodetector = CustomAutodetector + + class NewModel(models.Model): + class Meta: + app_label = "migrated_app" + + out = io.StringIO() + command = CustomMakeMigrationsCommand(stdout=out) + call_command(command, "migrated_app", stdout=out) + self.assertIn("No changes detected", out.getvalue()) + + @override_settings(INSTALLED_APPS=["migrations.migrations_test_apps.migrated_app"]) + @isolate_apps("migrations.migrations_test_apps.migrated_app") + def test_migrate_custom_autodetector(self): + class CustomAutodetector(MigrationAutodetector): + def changes(self, *args, **kwargs): + return [] + + class CustomMigrateCommand(MigrateCommand): + autodetector = CustomAutodetector + + class NewModel(models.Model): + class Meta: + app_label = "migrated_app" + + out = io.StringIO() + command = CustomMigrateCommand(stdout=out) + + out = io.StringIO() + try: + call_command(command, verbosity=0) + call_command(command, stdout=out, no_color=True) + command_stdout = out.getvalue().lower() + self.assertEqual( + "operations to perform:\n" + " apply all migrations: migrated_app\n" + "running migrations:\n" + " no migrations to apply.\n", + command_stdout, + ) + finally: + call_command(command, "migrated_app", "zero", verbosity=0) -- cgit v1.3