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.
This commit is contained in:
Alexis Métaireau 2024-02-26 14:43:47 +01:00
parent ff4870730a
commit 51889f3238
2 changed files with 19 additions and 17 deletions

View file

@ -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", "")
]

View file

@ -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))
)