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 7682d68
Showing 1 changed file with 12 additions and 13 deletions.
25 changes: 12 additions & 13 deletions umap/migrations/0018_datalayer_uuid.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@

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 +29,9 @@ class Migration(migrations.Migration):
),
),
# Generate UUIDs for existing records
migrations.RunSQL("UPDATE umap_datalayer SET uuid = gen_random_uuid()"),
migrations.RunSQL("UPDATE umap_datalayer SET uuid = gen_random_uuid()", reverse_sql=migrations.RunSQL.noop),
# 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 $$;
"""
),
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 +48,5 @@ class Migration(migrations.Migration):
serialize=False,
),
),
migrations.RunSQL(migrations.RunSQL.noop, reverse_sql=drop_index)
]

0 comments on commit 7682d68

Please sign in to comment.