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:
parent
ff4870730a
commit
51889f3238
2 changed files with 19 additions and 17 deletions
|
@ -5,32 +5,38 @@ import uuid
|
||||||
from django.db import migrations, models
|
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):
|
class Migration(migrations.Migration):
|
||||||
dependencies = [
|
dependencies = [
|
||||||
("umap", "0017_migrate_to_openstreetmap_oauth2"),
|
("umap", "0017_migrate_to_openstreetmap_oauth2"),
|
||||||
]
|
]
|
||||||
|
|
||||||
operations = [
|
operations = [
|
||||||
|
# Add the new uuid field
|
||||||
migrations.AddField(
|
migrations.AddField(
|
||||||
model_name="datalayer",
|
model_name="datalayer",
|
||||||
name="uuid",
|
name="uuid",
|
||||||
field=models.UUIDField(default=uuid.uuid4, editable=False, null=True),
|
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(
|
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(
|
migrations.AlterField(
|
||||||
"datalayer", name="id", field=models.IntegerField(null=True, blank=True)
|
"datalayer", name="id", field=models.IntegerField(null=True, blank=True)
|
||||||
),
|
),
|
||||||
|
# … to put it back on the "uuid"
|
||||||
migrations.AlterField(
|
migrations.AlterField(
|
||||||
model_name="datalayer",
|
model_name="datalayer",
|
||||||
name="uuid",
|
name="uuid",
|
||||||
|
@ -38,5 +44,4 @@ class Migration(migrations.Migration):
|
||||||
default=uuid.uuid4, editable=False, unique=True, primary_key=True
|
default=uuid.uuid4, editable=False, unique=True, primary_key=True
|
||||||
),
|
),
|
||||||
),
|
),
|
||||||
# migrations.RemoveConstraint("datalayer", "")
|
|
||||||
]
|
]
|
||||||
|
|
|
@ -240,22 +240,19 @@ def test_versions_can_return_old_format(client, datalayer, map, settings):
|
||||||
)
|
)
|
||||||
|
|
||||||
# store with the id prefix (rather than the uuid)
|
# store with the id prefix (rather than the uuid)
|
||||||
old_format_version = "%s_1440918637.geojson" % (datalayer.id)
|
old_format_version = "%s/%s_1440918637.geojson" % (root, datalayer.id)
|
||||||
datalayer.geojson.storage.save(
|
datalayer.geojson.storage.save(old_format_version, ContentFile("{}"))
|
||||||
("%s/" % root) + old_format_version, ContentFile("{}")
|
|
||||||
)
|
|
||||||
|
|
||||||
url = reverse("datalayer_versions", args=(map.pk, datalayer.pk))
|
url = reverse("datalayer_versions", args=(map.pk, datalayer.pk))
|
||||||
versions = json.loads(client.get(url).content.decode())
|
versions = json.loads(client.get(url).content.decode())
|
||||||
assert len(versions["versions"]) == 4
|
assert len(versions["versions"]) == 4
|
||||||
version = {
|
version = {
|
||||||
"name": old_format_version,
|
"name": "%s_1440918637.geojson" % datalayer.id,
|
||||||
"size": 2,
|
"size": 2,
|
||||||
"at": "1440918637",
|
"at": "1440918637",
|
||||||
}
|
}
|
||||||
assert version in versions["versions"]
|
assert version in versions["versions"]
|
||||||
|
|
||||||
# Check we can access the old format
|
|
||||||
client.get(
|
client.get(
|
||||||
reverse("datalayer_version", args=(map.pk, datalayer.pk, old_format_version))
|
reverse("datalayer_version", args=(map.pk, datalayer.pk, old_format_version))
|
||||||
)
|
)
|
||||||
|
|
Loading…
Reference in a new issue