Skip to content

Commit

Permalink
Merge pull request #3927 from open-formulieren/chore/squash-migrations
Browse files Browse the repository at this point in the history
Squashing migrations
  • Loading branch information
sergei-maertens authored Feb 26, 2024
2 parents 1673fbc + d7e4e23 commit 3830bd4
Show file tree
Hide file tree
Showing 48 changed files with 2,890 additions and 3,151 deletions.
Original file line number Diff line number Diff line change
@@ -1,78 +1,16 @@
# Generated by Django 3.2.21 on 2023-11-22 17:27

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

from openforms.analytics_tools.constants import AnalyticsTools

SITEIMPROVE_VALUES = [
"https://siteimproveanalytics.com",
"https://siteimproveanalytics.com",
"https://*.siteimproveanalytics.io",
]
GA_VALUES = ["https://www.googleanalytics.com", "https://www.googletagmanager.com"]

FIELD_TO_IDENTIFIER = {
"matomo_url": AnalyticsTools.matomo,
"piwik_pro_url": AnalyticsTools.piwik_pro,
"piwik_url": AnalyticsTools.piwik,
}


def set_identifier(apps: StateApps, schema_editor: BaseDatabaseSchemaEditor) -> None:
"""Set the corresponding analytic tool as the ``CSPSetting.identifier`` field.
Depending on the analytic tool used, the ``CSPSetting.value`` field can be fixed
(e.g. with GA or Siteimprove) or configured by the user. The latter requires more work,
as we need to iterate over the ``AnalyticsToolsConfiguration`` fields to find the right match.
"""

AnalyticsToolsConfiguration = apps.get_model(
"analytics_tools", "AnalyticsToolsConfiguration"
)
try:
analytics_conf = AnalyticsToolsConfiguration.objects.get()
except AnalyticsToolsConfiguration.DoesNotExist:
return

ContentType = apps.get_model("contenttypes", "ContentType")
analytics_content_type = ContentType.objects.get_for_model(
AnalyticsToolsConfiguration
)

CSPSetting = apps.get_model("config", "CSPSetting")

set_content_type = False
for csp_setting in CSPSetting.objects.filter(identifier="").iterator():
if csp_setting.value in SITEIMPROVE_VALUES:
csp_setting.identifier = AnalyticsTools.siteimprove
set_content_type = True
elif csp_setting.value in GA_VALUES:
csp_setting.identifier = AnalyticsTools.google_analytics
set_content_type = True
else:
for field, identifier in FIELD_TO_IDENTIFIER.items():
if getattr(analytics_conf, field, None) == csp_setting.value:
csp_setting.identifier = identifier
set_content_type = True

if set_content_type:
# `content_object` is not available in migrations,
# so we set `content_type` and `object_id` instead:
csp_setting.content_type = analytics_content_type
csp_setting.object_id = analytics_conf.id

csp_setting.save()
from django.db.migrations.state import StateApps


class Migration(migrations.Migration):

dependencies = [
("analytics_tools", "0002_auto_20230119_1500"),
("config", "0063_auto_20231122_1816"),
("config", "0053_v230_to_v250"),
]

operations = [
migrations.RunPython(set_identifier, migrations.RunPython.noop),
]
# RunPython operation is removed, it was executed as part of the 2.5.0 upgrade.
operations = []
75 changes: 0 additions & 75 deletions src/openforms/analytics_tools/tests/test_migrations.py

This file was deleted.

Empty file.
Empty file.

This file was deleted.

Loading

0 comments on commit 3830bd4

Please sign in to comment.