Skip to content

Commit

Permalink
🗃️ [#4920] Empty obsoleted data migrations
Browse files Browse the repository at this point in the history
Replaced some RunPython operations that are guaranteed to have been
run on Open Forms 2.8.x with empty operations so that we can
properly optimize migrations during squashing.
  • Loading branch information
sergei-maertens committed Dec 27, 2024
1 parent 693d3aa commit 7439c21
Show file tree
Hide file tree
Showing 4 changed files with 7 additions and 151 deletions.
53 changes: 2 additions & 51 deletions src/openforms/config/migrations/0054_v250_to_v270.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,59 +5,12 @@
from django.db import migrations, models

import tinymce.models
from flags.conditions import boolean_condition

import openforms.config.models.config
import openforms.emails.validators
import openforms.payments.validators
import openforms.template.validators

FIELDS = (
"enable_demo_plugins",
"display_sdk_information",
)


def move_from_config_to_flagstate(apps, _):
GlobalConfiguration = apps.get_model("config", "GlobalConfiguration")
FlagState = apps.get_model("flags", "FlagState")

# ensure we have an instance, for a fresh install, this will set up the
# defaults explicitly.
config = GlobalConfiguration.objects.first() or GlobalConfiguration()
for field in FIELDS:
current_value = getattr(config, field)
FlagState.objects.get_or_create(
name=field.upper(),
defaults={
"condition": "boolean",
"value": str(current_value),
"required": False,
},
)


def move_from_flagstate_to_config(apps, _):
GlobalConfiguration = apps.get_model("config", "GlobalConfiguration")
FlagState = apps.get_model("flags", "FlagState")

# if there's no config, there's nothing to do
config = GlobalConfiguration.objects.first()
if config is None:
return

for field in FIELDS:
flag_state = FlagState.objects.filter(
name=field.upper(), condition="boolean"
).first()
if flag_state is None:
continue

value = boolean_condition(flag_state.value)
setattr(config, field, value)

config.save()


class Migration(migrations.Migration):

Expand Down Expand Up @@ -162,10 +115,8 @@ class Migration(migrations.Migration):
model_name="globalconfiguration",
name="show_form_link_in_cosign_email",
),
migrations.RunPython(
code=move_from_config_to_flagstate,
reverse_code=move_from_flagstate_to_config,
),
# RunPython operation removed as part of 3.0 release cycle - these migrations are
# guaranteed to have been executed on Open Forms 2.8.x for existing instances.
migrations.RemoveField(
model_name="globalconfiguration",
name="display_sdk_information",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,17 +3,6 @@
from django.db import migrations


def disable_objects_api_prefill_plugin(apps, _):
GlobalConfiguration = apps.get_model("config", "GlobalConfiguration")
config, _ = GlobalConfiguration.objects.get_or_create(
pk=1
) # must match GlobalConfiguration.singleton_instance_id

config.plugin_configuration.setdefault("prefill", {})
config.plugin_configuration["prefill"].setdefault("objects_api", {"enabled": False})
config.save()


class Migration(migrations.Migration):

dependencies = [
Expand All @@ -23,8 +12,6 @@ class Migration(migrations.Migration):
),
]

operations = [
migrations.RunPython(
disable_objects_api_prefill_plugin, migrations.RunPython.noop
),
]
# RunPython operation removed as part of 3.0 release cycle - these migrations are
# guaranteed to have been executed on Open Forms 2.8.x for existing instances.
operations = []
Original file line number Diff line number Diff line change
Expand Up @@ -4,27 +4,6 @@

import django_jsonform.models.fields

from openforms.config.constants import UploadFileType


def add_extra_zip_mimetypes(apps, _):
"""
Set up the correct zip mimetypes.
This ensures all the allowed mimetypes concerning zip files are included.
"""
GlobalConfiguration = apps.get_model("config", "GlobalConfiguration")
if not GlobalConfiguration.objects.exists():
return

config = GlobalConfiguration.objects.get()
if "application/zip" not in config.form_upload_default_file_types:
return

config.form_upload_default_file_types.remove("application/zip")
config.form_upload_default_file_types.append(UploadFileType.zip)
config.save(update_fields=("form_upload_default_file_types",))


class Migration(migrations.Migration):

Expand Down Expand Up @@ -81,8 +60,6 @@ class Migration(migrations.Migration):
verbose_name="Default allowed file upload types",
),
),
migrations.RunPython(
add_extra_zip_mimetypes,
migrations.RunPython.noop,
),
# RunPython operation removed as part of 3.0 release cycle - these migrations are
# guaranteed to have been executed on Open Forms 2.8.x for existing instances.
]
59 changes: 0 additions & 59 deletions src/openforms/config/tests/test_migrations.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,65 +3,6 @@
from openforms.utils.tests.test_migrations import TestMigrations


class MigrateFeatureFlagsTests(TestMigrations):
app = "config"
migrate_from = "0001_initial_to_v250"
migrate_to = "0054_v250_to_v270"

def setUpBeforeMigration(self, apps: StateApps):
GlobalConfiguration = apps.get_model("config", "GlobalConfiguration")
GlobalConfiguration.objects.update_or_create(
pk=1,
defaults={
"enable_demo_plugins": True,
"display_sdk_information": False,
},
)
FlagState = apps.get_model("flags", "FlagState")
FlagState.objects.all().delete()

def test_feature_flags_created(self):
FlagState = self.apps.get_model("flags", "FlagState")
flags = FlagState.objects.all()

self.assertEqual(len(flags), 2)
by_name = {flag.name: flag for flag in flags}

flag1 = by_name["ENABLE_DEMO_PLUGINS"]
self.assertEqual(flag1.condition, "boolean")
self.assertEqual(flag1.value, "True")

flag2 = by_name["DISPLAY_SDK_INFORMATION"]
self.assertEqual(flag2.condition, "boolean")
self.assertEqual(flag2.value, "False")


class ReverseMigrateFeatureFlagsTests(TestMigrations):
app = "config"
migrate_from = "0054_v250_to_v270"
migrate_to = "0001_initial_to_v250"

def setUpBeforeMigration(self, apps: StateApps):
FlagState = apps.get_model("flags", "FlagState")
FlagState.objects.all().delete()
FlagState.objects.create(
name="ENABLE_DEMO_PLUGINS", condition="boolean", value="yes"
)
FlagState.objects.create(
name="UNRELATED", condition="invalid", value="irrelevant"
)

GlobalConfiguration = apps.get_model("config", "GlobalConfiguration")
GlobalConfiguration.objects.get_or_create(pk=1)

def test_feature_flags_created(self):
GlobalConfiguration = self.apps.get_model("config", "GlobalConfiguration")
config = GlobalConfiguration.objects.get()

self.assertTrue(config.enable_demo_plugins)
self.assertFalse(config.display_sdk_information)


class MigrateSummaryTag(TestMigrations):
app = "config"
migrate_from = "0068_alter_globalconfiguration_cosign_request_template_and_more"
Expand Down

0 comments on commit 7439c21

Please sign in to comment.