From bf789cc41cbbc8051d9155d74f03837087f1a502 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Pave=C5=82=20Ty=C5=9Blacki?= Date: Mon, 3 Jun 2024 12:42:43 +0100 Subject: [PATCH] fix sqlmigrate with idempotent mode --- CHANGES.md | 1 + .../backends/postgres/schema.py | 6 +++++- tests/integration/test_migrations.py | 14 ++++++++++++++ 3 files changed, 20 insertions(+), 1 deletion(-) diff --git a/CHANGES.md b/CHANGES.md index ad3c561..23285b3 100644 --- a/CHANGES.md +++ b/CHANGES.md @@ -4,6 +4,7 @@ - changed `ADD COLUMN DEFAULT NULL` to safe operation for code default - changed `ADD COLUMN DEFAULT NOT NULL` to safe operation for `db_default` in django 5.0+ - added `ZERO_DOWNTIME_MIGRATIONS_KEEP_DEFAULT` settings and changed `ADD COLUMN DEFAULT NOT NULL` with this settings to safe operation for django<5.0 + - fixed sqlmigrate in idempotent mode - updated unsafe migrations links to documentation - updated patched code to latest django version - improved README diff --git a/django_zero_downtime_migrations/backends/postgres/schema.py b/django_zero_downtime_migrations/backends/postgres/schema.py index c4a408f..71ff5e7 100644 --- a/django_zero_downtime_migrations/backends/postgres/schema.py +++ b/django_zero_downtime_migrations/backends/postgres/schema.py @@ -495,7 +495,11 @@ def __init__(self, connection, collect_sql=False, atomic=True): settings, "ZERO_DOWNTIME_MIGRATIONS_FLEXIBLE_STATEMENT_TIMEOUT", False) self.RAISE_FOR_UNSAFE = getattr(settings, "ZERO_DOWNTIME_MIGRATIONS_RAISE_FOR_UNSAFE", False) self.DEFERRED_SQL = getattr(settings, "ZERO_DOWNTIME_MIGRATIONS_DEFERRED_SQL", True) - self.IDEMPOTENT_SQL = getattr(settings, "ZERO_DOWNTIME_MIGRATIONS_IDEMPOTENT_SQL", False) + self.IDEMPOTENT_SQL = ( + getattr(settings, "ZERO_DOWNTIME_MIGRATIONS_IDEMPOTENT_SQL", False) + if not collect_sql else + False # disable idempotent mode for sqlmigrate + ) self.KEEP_DEFAULT = getattr(settings, "ZERO_DOWNTIME_MIGRATIONS_KEEP_DEFAULT", False) if django.VERSION[:2] >= (5, 0) and hasattr(settings, 'ZERO_DOWNTIME_MIGRATIONS_KEEP_DEFAULT'): warnings.warn( diff --git a/tests/integration/test_migrations.py b/tests/integration/test_migrations.py index b294d44..9650232 100644 --- a/tests/integration/test_migrations.py +++ b/tests/integration/test_migrations.py @@ -1,4 +1,7 @@ +import os + import django +from django.apps import apps from django.conf import settings from django.core.management import call_command from django.db import connection @@ -16,6 +19,17 @@ ) +@pytest.mark.django_db(transaction=True) +@modify_settings(INSTALLED_APPS={"append": "tests.apps.good_flow_app"}) +@override_settings(ZERO_DOWNTIME_MIGRATIONS_RAISE_FOR_UNSAFE=True, + ZERO_DOWNTIME_MIGRATIONS_IDEMPOTENT_SQL=True) +def test_sqlmigrate_with_idempotent_mode(): + app_config = apps.get_app_config("good_flow_app") + for migration in os.listdir(os.path.join(app_config.path, "migrations")): + if not migration.startswith("_") and migration.endswith(".py"): + call_command("sqlmigrate", "good_flow_app", migration[:4]) + + @pytest.mark.django_db(transaction=True) @modify_settings(INSTALLED_APPS={"append": "tests.apps.good_flow_alter_table_with_same_db_table"}) @override_settings(ZERO_DOWNTIME_MIGRATIONS_RAISE_FOR_UNSAFE=True)