Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix sqlmigrate with idempotent mode #64

Merged
merged 1 commit into from
Jun 3, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading