Skip to content

Commit

Permalink
chore: make the datalayer uuid migration reversible
Browse files Browse the repository at this point in the history
  • Loading branch information
almet committed Feb 26, 2024
1 parent 9648c8b commit f82897f
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions umap/migrations/0018_datalayer_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,16 @@

from django.db import migrations, models

drop_index = """DO $$
BEGIN
EXECUTE 'ALTER TABLE umap_datalayer DROP CONSTRAINT ' || (
SELECT indexname
FROM pg_indexes
WHERE tablename = 'umap_datalayer' AND indexname LIKE '%pk'
);
END $$;
"""


class Migration(migrations.Migration):
dependencies = [
Expand All @@ -20,20 +30,12 @@ class Migration(migrations.Migration):
),
),
# Generate UUIDs for existing records
migrations.RunSQL("UPDATE umap_datalayer SET uuid = gen_random_uuid()"),
# Remove the primary key constraint
migrations.RunSQL(
"""
DO $$
BEGIN
EXECUTE 'ALTER TABLE umap_datalayer DROP CONSTRAINT ' || (
SELECT indexname
FROM pg_indexes
WHERE tablename = 'umap_datalayer' AND indexname LIKE '%pkey'
);
END $$;
"""
"UPDATE umap_datalayer SET uuid = gen_random_uuid()",
reverse_sql=migrations.RunSQL.noop,
),
# Remove the primary key constraint
migrations.RunSQL(drop_index, reverse_sql=migrations.RunSQL.noop),
# Drop the "id" primary key…
migrations.AlterField(
"datalayer", name="id", field=models.IntegerField(null=True, blank=True)
Expand All @@ -50,4 +52,5 @@ class Migration(migrations.Migration):
serialize=False,
),
),
migrations.RunSQL(migrations.RunSQL.noop, reverse_sql=drop_index),
]

0 comments on commit f82897f

Please sign in to comment.