From 9648c8ba42eb5833aa165e3d0ed5ad4baf2f7c26 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Mon, 26 Feb 2024 20:23:34 +0100 Subject: [PATCH] chore: migrate existing local remoteURL datalayers --- umap/migrations/0018_datalayer_uuid.py | 10 +++- ...0019_migrate_internal_remote_datalayers.py | 52 +++++++++++++++++++ 2 files changed, 60 insertions(+), 2 deletions(-) create mode 100644 umap/migrations/0019_migrate_internal_remote_datalayers.py diff --git a/umap/migrations/0018_datalayer_uuid.py b/umap/migrations/0018_datalayer_uuid.py index 7e9a1ee8..02468444 100644 --- a/umap/migrations/0018_datalayer_uuid.py +++ b/umap/migrations/0018_datalayer_uuid.py @@ -15,7 +15,9 @@ class Migration(migrations.Migration): migrations.AddField( model_name="datalayer", name="uuid", - field=models.UUIDField(default=uuid.uuid4, editable=False, null=True), + field=models.UUIDField( + default=uuid.uuid4, editable=False, null=True, serialize=False + ), ), # Generate UUIDs for existing records migrations.RunSQL("UPDATE umap_datalayer SET uuid = gen_random_uuid()"), @@ -41,7 +43,11 @@ class Migration(migrations.Migration): model_name="datalayer", name="uuid", field=models.UUIDField( - default=uuid.uuid4, editable=False, unique=True, primary_key=True + default=uuid.uuid4, + editable=False, + unique=True, + primary_key=True, + serialize=False, ), ), ] diff --git a/umap/migrations/0019_migrate_internal_remote_datalayers.py b/umap/migrations/0019_migrate_internal_remote_datalayers.py new file mode 100644 index 00000000..59f91cd8 --- /dev/null +++ b/umap/migrations/0019_migrate_internal_remote_datalayers.py @@ -0,0 +1,52 @@ +# Generated by Django 4.2 on 2024-02-26 14:09 + +import re + +from django.conf import settings +from django.db import migrations +from django.urls import NoReverseMatch, reverse + +# Some users hacked uMap to use another map datalayer as a remote data source. +# This script gently handles the migration for them. + + +def migrate_datalayers(apps, schema_editor): + DataLayer = apps.get_model("umap", "DataLayer") + + datalayers = DataLayer.objects.filter( + settings__remoteData__url__icontains="datalayer" + ) + + for item in datalayers: + old_url = item.settings["remoteData"]["url"] + match = re.search( + rf"{settings.SITE_URL}/datalayer/(?P\d+)/(?P\d+)", + old_url, + ) + if match: + remote_id = match.group("datalayer_id") + map_id = match.group("map_id") + try: + remote_uuid = DataLayer.objects.get(id=remote_id).uuid + except DataLayer.DoesNotExist: + pass + else: + try: + new_url = settings.SITE_URL + reverse( + "datalayer_view", args=[map_id, remote_uuid] + ) + except NoReverseMatch: + pass + else: + item.settings["remoteData"]["url"] = new_url + item.save() + + +class Migration(migrations.Migration): + dependencies = [ + ("umap", "0018_datalayer_uuid"), + ] + + operations = [ + migrations.RunPython(migrate_datalayers, reverse_code=migrations.RunPython.noop) + ]