Skip to content

Commit

Permalink
Merge pull request #3554 from open-formulieren/chore/squash-variables…
Browse files Browse the repository at this point in the history
…-migrations

Squash variables migrations
  • Loading branch information
sergei-maertens authored Oct 27, 2023
2 parents 66d9bee + 703852b commit 7ebc30c
Show file tree
Hide file tree
Showing 5 changed files with 141 additions and 196 deletions.
135 changes: 135 additions & 0 deletions src/openforms/variables/migrations/0001_initial_to_openforms_v230.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
# Generated by Django 3.2.21 on 2023-10-24 14:35

import django.db.models.deletion
from django.db import migrations, models

import openforms.variables.validators


class Migration(migrations.Migration):

replaces = [
("variables", "0001_add_service_fetch_configuration"),
("variables", "0002_servicefetchconfiguration_name"),
("variables", "0003_servicefetchconfiguration__query_params"),
("variables", "0004_migrate_query_params"),
("variables", "0005_remove_servicefetchconfiguration_query_params"),
(
"variables",
"0006_rename__query_params_servicefetchconfiguration_query_params",
),
("variables", "0007_alter_servicefetchconfiguration_headers"),
("variables", "0008_alter_servicefetchconfiguration_query_params"),
("variables", "0009_servicefetchconfiguration_name"),
("variables", "0010_alter_servicefetchconfiguration_name"),
("variables", "0011_migrate_interpolation_format"),
]

dependencies = [
("zgw_consumers", "0016_auto_20220818_1412"),
]

operations = [
migrations.CreateModel(
name="ServiceFetchConfiguration",
fields=[
(
"id",
models.BigAutoField(
auto_created=True,
primary_key=True,
serialize=False,
verbose_name="ID",
),
),
(
"path",
models.CharField(
blank=True,
default="",
help_text="path relative to the Service API root",
max_length=250,
verbose_name="path",
),
),
(
"method",
models.CharField(
choices=[("GET", "GET"), ("POST", "POST")],
default="GET",
help_text="POST is allowed, but should not be used to mutate data",
max_length=4,
verbose_name="HTTP method",
),
),
(
"headers",
models.JSONField(
blank=True,
default=dict,
help_text="Additions and overrides for the HTTP request headers as defined in the Service.",
validators=[openforms.variables.validators.HeaderValidator()],
verbose_name="HTTP request headers",
),
),
(
"body",
models.JSONField(
blank=True,
help_text='Request body for POST requests (only "application/json" is supported)',
null=True,
verbose_name="HTTP request body",
),
),
(
"data_mapping_type",
models.CharField(
blank=True,
choices=[("JsonLogic", "JsonLogic"), ("jq", "jq")],
default="",
max_length=10,
verbose_name="mapping expression language",
),
),
(
"mapping_expression",
models.JSONField(
blank=True,
help_text="For jq, pass a string containing the filter expression",
null=True,
verbose_name="mapping expression",
),
),
(
"service",
models.ForeignKey(
on_delete=django.db.models.deletion.PROTECT,
to="zgw_consumers.service",
verbose_name="service",
),
),
(
"name",
models.CharField(
help_text="human readable name for the configuration",
max_length=250,
),
),
(
"query_params",
models.JSONField(
blank=True,
default=dict,
validators=[
openforms.variables.validators.QueryParameterValidator()
],
verbose_name="HTTP query string",
),
),
],
options={
"verbose_name": "service fetch configuration",
"verbose_name_plural": "service fetch configurations",
},
),
]
33 changes: 2 additions & 31 deletions src/openforms/variables/migrations/0004_migrate_query_params.py
Original file line number Diff line number Diff line change
@@ -1,42 +1,13 @@
# Generated by Django 3.2.18 on 2023-03-13 15:38

from urllib.parse import parse_qs, urlencode

from django.db import migrations


def migrate_query_params_to_jsonfield(apps, _):
ServiceFetchConfiguration = apps.get_model("variables", "ServiceFetchConfiguration")

for config in ServiceFetchConfiguration.objects.all():
if config.query_params:
config._query_params = parse_qs(
config.query_params[1:]
if config.query_params.startswith("?")
else config.query_params
)
else:
config._query_params = {}
config.save()


def migrate_query_params_to_textfield(apps, _):
ServiceFetchConfiguration = apps.get_model("variables", "ServiceFetchConfiguration")

for config in ServiceFetchConfiguration.objects.all():
if config._query_params:
config.query_params = f"?{urlencode(config._query_params, doseq=True)}"
config.save()


class Migration(migrations.Migration):

dependencies = [
("variables", "0003_servicefetchconfiguration__query_params"),
]

operations = [
migrations.RunPython(
migrate_query_params_to_jsonfield, migrate_query_params_to_textfield
)
]
# data migration removed, as these have been executed on Open Forms 2.3.0+
operations = []
Original file line number Diff line number Diff line change
Expand Up @@ -3,19 +3,11 @@
from django.db import migrations


def populate_names(apps, _):
ServiceFetchConfiguration = apps.get_model("variables", "ServiceFetchConfiguration")

for config in ServiceFetchConfiguration.objects.all():
if not config.name:
config.name = f"{config.method} - {config.service.api_root}"
config.save()


class Migration(migrations.Migration):

dependencies = [
("variables", "0008_alter_servicefetchconfiguration_query_params"),
]

operations = [migrations.RunPython(populate_names, migrations.RunPython.noop)]
# data migration removed, as these have been executed on Open Forms 2.3.0+
operations = []
Original file line number Diff line number Diff line change
@@ -1,48 +1,13 @@
# Generated by Django 3.2.18 on 2023-03-28 12:35

import re

from django.db import migrations

MATCH_SINGLE_BRACKETS = r"{([A-z0-9_\.]*)}"
REPL_SINGLE_BRACKETS = r"{{\1}}"


def apply_regex_nested(data, pattern, repl):
if isinstance(data, dict):
for key, value in data.items():
data[key] = apply_regex_nested(value, pattern, repl)
elif isinstance(data, list):
data = [apply_regex_nested(value, pattern, repl) for value in data]
elif isinstance(data, str):
return re.sub(pattern, repl, data)
return data


def forwards(apps, _):
ServiceFetchConfiguration = apps.get_model("variables", "ServiceFetchConfiguration")

for config in ServiceFetchConfiguration.objects.all():
if config.body:
config.body = apply_regex_nested(
config.body, MATCH_SINGLE_BRACKETS, REPL_SINGLE_BRACKETS
)
if config.headers:
config.headers = apply_regex_nested(
config.headers, MATCH_SINGLE_BRACKETS, REPL_SINGLE_BRACKETS
)
if config.query_params:
config.query_params = apply_regex_nested(
config.query_params, MATCH_SINGLE_BRACKETS, REPL_SINGLE_BRACKETS
)

config.save()


class Migration(migrations.Migration):

dependencies = [
("variables", "0010_alter_servicefetchconfiguration_name"),
]

operations = [migrations.RunPython(forwards, migrations.RunPython.noop)]
# data migration removed, as these have been executed on Open Forms 2.3.0+
operations = []
118 changes: 0 additions & 118 deletions src/openforms/variables/tests/test_migrations.py

This file was deleted.

0 comments on commit 7ebc30c

Please sign in to comment.