Skip to content

Commit

Permalink
🗃️ [#4267] Make migration more robust
Browse files Browse the repository at this point in the history
  • Loading branch information
sergei-maertens committed Jul 2, 2024
1 parent 3e39192 commit a7c4e62
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
# Generated by Django 4.2.11 on 2024-07-02 15:18

from django.db import migrations

from django.db.migrations.state import StateApps
from django.db.backends.base.schema import BaseDatabaseSchemaEditor
from django.db.migrations.state import StateApps


def add_default_objects_api_group(
Expand All @@ -13,17 +12,33 @@ def add_default_objects_api_group(
ObjectsAPIGroupConfig = apps.get_model(
"registrations_objects_api", "ObjectsAPIGroupConfig"
)
backends_qs = FormRegistrationBackend.objects.filter(backend="objects_api")
# nothing to do if there are no relevant backends
if not backends_qs.exists():
return

objects_api_group_config = ObjectsAPIGroupConfig.objects.order_by("pk").first()
if objects_api_group_config is None:
# Because this migration runs after registrations_objects_api/0017_move_singleton_data,
# Having no Objects API Group means we had no solo config in the first place, thus
# it is safe to assume no Objects API registration backend was set up.
return
# This shouldn't happen because of:
# * upgrade checks
# * operations in registrations_objects_api/0017_move_singleton_data
#
# BUT that doesn't mean it's impossible, like on continuously deployed
# environments... For those cases, we generate a (knowingly broken)
# configuration so that migrations don't crash and options have at least the
# right shape so that we can trust the type annotations.
objects_api_group_config = ObjectsAPIGroupConfig.objects.create(
name="AUTO_GENERATED - FIXME",
# DeprecationWarning
# Open Forms 3.0 will drop the nullable fields, so this data migration needs
# to be gone by then.
objects_service=None,
objecttypes_service=None,
drc_service=None,
catalogi_service=None,
)

for registration_backend in FormRegistrationBackend.objects.filter(
backend="objects_api"
):
for registration_backend in backends_qs:
registration_backend.options.setdefault(
"objects_api_group", objects_api_group_config.pk
)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -313,6 +313,40 @@ def test_sets_default_objects_api_group(self) -> None:
)


class AddDefaultObjectsAPIGroupWithBrokenStateMigrationTests(TestMigrations):
app = "registrations_objects_api"
migrate_from = "0018_remove_objectsapiconfig_catalogi_service_and_more"
migrate_to = "0019_add_default_objects_api_group"

def setUpBeforeMigration(self, apps: StateApps) -> None:
Form = apps.get_model("forms", "Form")
FormRegistrationBackend = apps.get_model("forms", "FormRegistrationBackend")
ObjectsAPIGroupConfig = apps.get_model(
"registrations_objects_api", "ObjectsAPIGroupConfig"
)

form = Form.objects.create(name="test form")
FormRegistrationBackend.objects.create(
form=form,
name="Objects API backend",
key="backend",
backend="objects_api",
)
assert not ObjectsAPIGroupConfig.objects.exists()

def test_sets_default_objects_api_group(self) -> None:
FormRegistrationBackend = self.apps.get_model(
"forms", "FormRegistrationBackend"
)
ObjectsAPIGroupConfig = self.apps.get_model(
"registrations_objects_api", "ObjectsAPIGroupConfig"
)
auto_created_config = ObjectsAPIGroupConfig.objects.get()

backend = FormRegistrationBackend.objects.get()
self.assertEqual(backend.options["objects_api_group"], auto_created_config.pk)


class ObjecttypeUrltoUuidMigrationTests(TestMigrations):
app = "registrations_objects_api"
migrate_from = "0019_add_default_objects_api_group"
Expand Down

0 comments on commit a7c4e62

Please sign in to comment.