Skip to content

Commit

Permalink
fix sqlmigrate with idempotent mode
Browse files Browse the repository at this point in the history
  • Loading branch information
tbicr committed Jun 3, 2024
1 parent 8645ff2 commit bf789cc
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 1 deletion.
1 change: 1 addition & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 5 additions & 1 deletion django_zero_downtime_migrations/backends/postgres/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -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(
Expand Down
14 changes: 14 additions & 0 deletions tests/integration/test_migrations.py
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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)
Expand Down

0 comments on commit bf789cc

Please sign in to comment.