chore: Rename datalayer id to old_id

This commit is contained in:
Alexis Métaireau 2024-02-29 11:01:02 +01:00
parent f82897f202
commit 1b41ff0ddc
4 changed files with 13 additions and 7 deletions

View file

@ -9,7 +9,7 @@ BEGIN
EXECUTE 'ALTER TABLE umap_datalayer DROP CONSTRAINT ' || (
SELECT indexname
FROM pg_indexes
WHERE tablename = 'umap_datalayer' AND indexname LIKE '%pk'
WHERE tablename = 'umap_datalayer' AND indexname LIKE '%pk%'
);
END $$;
"""
@ -40,6 +40,10 @@ class Migration(migrations.Migration):
migrations.AlterField(
"datalayer", name="id", field=models.IntegerField(null=True, blank=True)
),
# Rename "id" to "old id"
migrations.RenameField(
model_name="datalayer", old_name="id", new_name="old_id"
),
# … to put it back on the "uuid"
migrations.AlterField(
model_name="datalayer",
@ -52,5 +56,7 @@ class Migration(migrations.Migration):
serialize=False,
),
),
# When applying the migration backwards, we need to drop the pk index
# Before addding a new one.
migrations.RunSQL(migrations.RunSQL.noop, reverse_sql=drop_index),
]

View file

@ -27,7 +27,7 @@ def migrate_datalayers(apps, schema_editor):
remote_id = match.group("datalayer_id")
map_id = match.group("map_id")
try:
remote_uuid = DataLayer.objects.get(id=remote_id).uuid
remote_uuid = DataLayer.objects.get(old_id=remote_id).uuid
except DataLayer.DoesNotExist:
pass
else:

View file

@ -377,7 +377,7 @@ class DataLayer(NamedModel):
uuid = models.UUIDField(
unique=True, primary_key=True, default=uuid.uuid4, editable=False
)
id = models.IntegerField(null=True, blank=True)
old_id = models.IntegerField(null=True, blank=True)
map = models.ForeignKey(Map, on_delete=models.CASCADE)
description = models.TextField(blank=True, null=True, verbose_name=_("description"))
geojson = models.FileField(upload_to=upload_to, blank=True, null=True)
@ -450,8 +450,8 @@ class DataLayer(NamedModel):
def is_valid_version(self, name):
valid_prefixes = [name.startswith("%s_" % self.pk)]
if self.id:
valid_prefixes.append(name.startswith("%s_" % self.id))
if self.old_id:
valid_prefixes.append(name.startswith("%s_" % self.old_id))
return any(valid_prefixes) and name.endswith(".geojson")
def version_metadata(self, name):

View file

@ -229,7 +229,7 @@ def test_versions_can_return_old_format(client, datalayer, map, settings):
map.share_status = Map.PUBLIC
map.save()
root = datalayer.storage_root()
datalayer.id = 123 # old datalayer id (now replaced by uuid)
datalayer.old_id = 123 # old datalayer id (now replaced by uuid)
datalayer.save()
datalayer.geojson.storage.save(
@ -240,7 +240,7 @@ 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
old_format_version = "%s_1440918637.geojson" % datalayer.old_id
datalayer.geojson.storage.save(
("%s/" % root) + old_format_version, ContentFile("{}")
)