From 51889f3238109ec3f9967c8c299ab773f07e88ed Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Mon, 26 Feb 2024 14:43:47 +0100 Subject: [PATCH] chore: fetch datalayer index name before dropping it. This can be helpful in situations where the name of the index is not known, as it can be with pre 1.0 deployed instance. This commit also generates the UUIDs directly using an SQL statement. --- umap/migrations/0018_datalayer_uuid.py | 27 +++++++++++++++----------- umap/tests/test_datalayer_views.py | 9 +++------ 2 files changed, 19 insertions(+), 17 deletions(-) diff --git a/umap/migrations/0018_datalayer_uuid.py b/umap/migrations/0018_datalayer_uuid.py index e0a655c9..7e9a1ee8 100644 --- a/umap/migrations/0018_datalayer_uuid.py +++ b/umap/migrations/0018_datalayer_uuid.py @@ -5,32 +5,38 @@ import uuid from django.db import migrations, models -def gen_uuid(apps, schema_editor): - DataLayer = apps.get_model("umap", "DataLayer") - for row in DataLayer.objects.all(): - row.uuid = uuid.uuid4() - row.save(update_fields=["uuid"]) - - class Migration(migrations.Migration): dependencies = [ ("umap", "0017_migrate_to_openstreetmap_oauth2"), ] operations = [ + # Add the new uuid field migrations.AddField( model_name="datalayer", name="uuid", field=models.UUIDField(default=uuid.uuid4, editable=False, null=True), ), - migrations.RunPython(gen_uuid, reverse_code=migrations.RunPython.noop), + # Generate UUIDs for existing records + migrations.RunSQL("UPDATE umap_datalayer SET uuid = gen_random_uuid()"), + # Remove the primary key constraint migrations.RunSQL( - "ALTER TABLE umap_datalayer DROP CONSTRAINT umap_datalayer_pkey" + """ + DO $$ + BEGIN + EXECUTE 'ALTER TABLE umap_datalayer DROP CONSTRAINT ' || ( + SELECT indexname + FROM pg_indexes + WHERE tablename = 'umap_datalayer' AND indexname LIKE '%pkey' + ); + END $$; + """ ), - # migrations.RemoveConstraint("datalayer", "id"), + # Drop the "id" primary key… migrations.AlterField( "datalayer", name="id", field=models.IntegerField(null=True, blank=True) ), + # … to put it back on the "uuid" migrations.AlterField( model_name="datalayer", name="uuid", @@ -38,5 +44,4 @@ class Migration(migrations.Migration): default=uuid.uuid4, editable=False, unique=True, primary_key=True ), ), - # migrations.RemoveConstraint("datalayer", "") ] diff --git a/umap/tests/test_datalayer_views.py b/umap/tests/test_datalayer_views.py index 5cab92aa..d3a1125d 100644 --- a/umap/tests/test_datalayer_views.py +++ b/umap/tests/test_datalayer_views.py @@ -240,22 +240,19 @@ def test_versions_can_return_old_format(client, datalayer, map, settings): ) # store with the id prefix (rather than the uuid) - old_format_version = "%s_1440918637.geojson" % (datalayer.id) - datalayer.geojson.storage.save( - ("%s/" % root) + old_format_version, ContentFile("{}") - ) + old_format_version = "%s/%s_1440918637.geojson" % (root, datalayer.id) + datalayer.geojson.storage.save(old_format_version, ContentFile("{}")) url = reverse("datalayer_versions", args=(map.pk, datalayer.pk)) versions = json.loads(client.get(url).content.decode()) assert len(versions["versions"]) == 4 version = { - "name": old_format_version, + "name": "%s_1440918637.geojson" % datalayer.id, "size": 2, "at": "1440918637", } assert version in versions["versions"] - # Check we can access the old format client.get( reverse("datalayer_version", args=(map.pk, datalayer.pk, old_format_version)) )