From 7c5143848f3a7709193918fda3dae1344a4af9df Mon Sep 17 00:00:00 2001 From: Manuel Kaufmann Date: Tue, 5 Nov 2024 19:28:18 +0100 Subject: [PATCH] Addons: split model and data migrations (#11744) --- .../migrations/0128_addons_notifications.py | 25 -------------- ...0129_addons_notification_data_migration.py | 33 +++++++++++++++++++ ...ds.py => 0130_addons_remove_old_fields.py} | 2 +- 3 files changed, 34 insertions(+), 26 deletions(-) create mode 100644 readthedocs/projects/migrations/0129_addons_notification_data_migration.py rename readthedocs/projects/migrations/{0129_addons_remove_old_fields.py => 0130_addons_remove_old_fields.py} (92%) diff --git a/readthedocs/projects/migrations/0128_addons_notifications.py b/readthedocs/projects/migrations/0128_addons_notifications.py index e8f1a1ee88c..274ebe28fc8 100644 --- a/readthedocs/projects/migrations/0128_addons_notifications.py +++ b/readthedocs/projects/migrations/0128_addons_notifications.py @@ -4,30 +4,6 @@ from django_safemigrate import Safe -def forward_add_fields(apps, schema_editor): - AddonsConfig = apps.get_model("projects", "AddonsConfig") - for addons in AddonsConfig.objects.filter(project__isnull=False): - addons.notifications_show_on_latest = ( - addons.stable_latest_version_warning_enabled - ) - addons.notifications_show_on_non_stable = ( - addons.stable_latest_version_warning_enabled - ) - addons.notifications_show_on_external = addons.external_version_warning_enabled - addons.save() - - -def reverse_remove_fields(apps, schema_editor): - AddonsConfig = apps.get_model("projects", "AddonsConfig") - for addons in AddonsConfig.objects.filter(project__isnull=False): - addons.stable_latest_version_warning_enabled = ( - addons.notifications_show_on_latest - or addons.notifications_show_on_non_stable - ) - addons.external_version_warning_enabled = addons.notifications_show_on_external - addons.save() - - class Migration(migrations.Migration): safe = Safe.before_deploy @@ -76,5 +52,4 @@ class Migration(migrations.Migration): name="notifications_show_on_non_stable", field=models.BooleanField(default=True), ), - migrations.RunPython(forward_add_fields, reverse_remove_fields), ] diff --git a/readthedocs/projects/migrations/0129_addons_notification_data_migration.py b/readthedocs/projects/migrations/0129_addons_notification_data_migration.py new file mode 100644 index 00000000000..520616b1a23 --- /dev/null +++ b/readthedocs/projects/migrations/0129_addons_notification_data_migration.py @@ -0,0 +1,33 @@ +# Generated by Django 4.2.16 on 2024-11-05 17:33 + +from django.db import migrations +from django.db.models import F +from django_safemigrate import Safe + + +def forward_add_fields(apps, schema_editor): + AddonsConfig = apps.get_model("projects", "AddonsConfig") + AddonsConfig.objects.filter(project__isnull=False).update( + notifications_show_on_latest=F("stable_latest_version_warning_enabled"), + notifications_show_on_non_stable=F("stable_latest_version_warning_enabled"), + notifications_show_on_external=F("external_version_warning_enabled"), + ) + + +def reverse_remove_fields(apps, schema_editor): + AddonsConfig = apps.get_model("projects", "AddonsConfig") + AddonsConfig.objects.filter(project__isnull=False).update( + stable_latest_version_warning_enabled=F("notifications_show_on_latest"), + external_version_warning_enabled=F("notifications_show_on_external"), + ) + +class Migration(migrations.Migration): + safe = Safe.before_deploy + + dependencies = [ + ("projects", "0128_addons_notifications"), + ] + + operations = [ + migrations.RunPython(forward_add_fields, reverse_remove_fields), + ] diff --git a/readthedocs/projects/migrations/0129_addons_remove_old_fields.py b/readthedocs/projects/migrations/0130_addons_remove_old_fields.py similarity index 92% rename from readthedocs/projects/migrations/0129_addons_remove_old_fields.py rename to readthedocs/projects/migrations/0130_addons_remove_old_fields.py index 502b53fa0b0..80c16bd5494 100644 --- a/readthedocs/projects/migrations/0129_addons_remove_old_fields.py +++ b/readthedocs/projects/migrations/0130_addons_remove_old_fields.py @@ -8,7 +8,7 @@ class Migration(migrations.Migration): safe = Safe.after_deploy dependencies = [ - ("projects", "0128_addons_notifications"), + ("projects", "0129_addons_notification_data_migration"), ] operations = [