From 1b41ff0ddc7e97a592fabaa5da7580bde9f960c9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexis=20M=C3=A9taireau?= Date: Thu, 29 Feb 2024 11:01:02 +0100 Subject: [PATCH] chore: Rename datalayer id to old_id --- umap/migrations/0018_datalayer_uuid.py | 8 +++++++- .../migrations/0019_migrate_internal_remote_datalayers.py | 2 +- umap/models.py | 6 +++--- umap/tests/test_datalayer_views.py | 4 ++-- 4 files changed, 13 insertions(+), 7 deletions(-) diff --git a/umap/migrations/0018_datalayer_uuid.py b/umap/migrations/0018_datalayer_uuid.py index 08b84fe5..86c52ec0 100644 --- a/umap/migrations/0018_datalayer_uuid.py +++ b/umap/migrations/0018_datalayer_uuid.py @@ -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), ] diff --git a/umap/migrations/0019_migrate_internal_remote_datalayers.py b/umap/migrations/0019_migrate_internal_remote_datalayers.py index 59f91cd8..1a0bc77e 100644 --- a/umap/migrations/0019_migrate_internal_remote_datalayers.py +++ b/umap/migrations/0019_migrate_internal_remote_datalayers.py @@ -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: diff --git a/umap/models.py b/umap/models.py index f10fc876..aa60b334 100644 --- a/umap/models.py +++ b/umap/models.py @@ -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): diff --git a/umap/tests/test_datalayer_views.py b/umap/tests/test_datalayer_views.py index 1ae87b8f..22ca9b95 100644 --- a/umap/tests/test_datalayer_views.py +++ b/umap/tests/test_datalayer_views.py @@ -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("{}") )