From 152d218ec4e1ff2e30f39d94e10aad2e10c4dc5d Mon Sep 17 00:00:00 2001 From: Jacob Nesbitt Date: Wed, 4 Oct 2023 13:30:33 -0400 Subject: [PATCH 1/2] Align Postgres tables/indexes/constraints with Django --- ..._zarr_rename_tables_indexes_constraints.py | 79 +++++++++++++++++++ dandiapi/zarr/models.py | 6 -- 2 files changed, 79 insertions(+), 6 deletions(-) create mode 100644 dandiapi/zarr/migrations/0003_zarr_rename_tables_indexes_constraints.py diff --git a/dandiapi/zarr/migrations/0003_zarr_rename_tables_indexes_constraints.py b/dandiapi/zarr/migrations/0003_zarr_rename_tables_indexes_constraints.py new file mode 100644 index 000000000..d661f99a7 --- /dev/null +++ b/dandiapi/zarr/migrations/0003_zarr_rename_tables_indexes_constraints.py @@ -0,0 +1,79 @@ +# Generated by Django 4.1.11 on 2023-10-04 15:34 + +from django.db import migrations + + +class Migration(migrations.Migration): + dependencies = [ + ('zarr', '0002_remove_zarruploadfile_zarr_archive_and_more'), + ] + + # This renaming is necessary as there is a mismatch between what Django thinks the name of + # database indexes/constraints are, and what they actually are. + operations = [ + # Tables + migrations.AlterModelTable(name='zarrarchive', table=None), + migrations.AlterModelTable(name='embargoedzarrarchive', table=None), + # Sequences + migrations.RunSQL( + 'ALTER SEQUENCE api_zarrarchive_id_seq RENAME to zarr_zarrarchive_id_seq;' + ), + migrations.RunSQL( + 'ALTER SEQUENCE api_embargoedzarrarchive_id_seq RENAME to ' + 'zarr_embargoedzarrarchive_id_seq;' + ), + # zarr_id unique constraint + migrations.RunSQL( + 'ALTER TABLE zarr_zarrarchive RENAME CONSTRAINT "api_zarrarchive_zarr_id_key" to ' + '"zarr_zarrarchive_zarr_id_key";' + ), + migrations.RunSQL( + 'ALTER TABLE zarr_embargoedzarrarchive RENAME CONSTRAINT ' + '"api_embargoedzarrarchive_zarr_id_key" to "zarr_embargoedzarrarchive_zarr_id_key";' + ), + # primary key index + migrations.RunSQL('ALTER INDEX "api_zarrarchive_pkey" RENAME TO "zarr_zarrarchive_pkey";'), + migrations.RunSQL( + 'ALTER INDEX "api_embargoedzarrarchive_pkey" RENAME TO ' + '"zarr_embargoedzarrarchive_pkey";' + ), + # dandiset_id index + migrations.RunSQL( + 'ALTER INDEX "api_zarrarchive_dandiset_id_68510762" RENAME TO ' + '"zarr_zarrarchive_dandiset_id_68510762";' + ), + migrations.RunSQL( + 'ALTER INDEX "api_embargoedzarrarchive_dandiset_id_b61f0a08" RENAME TO ' + '"zarr_embargoedzarrarchive_dandiset_id_b61f0a08";' + ), + # dandiset_id foreign key constraint + migrations.RunSQL( + 'ALTER TABLE zarr_zarrarchive RENAME CONSTRAINT ' + '"api_zarrarchive_dandiset_id_68510762_fk" TO ' + '"zarr_zarrarchive_dandiset_id_68510762_fk";' + ), + migrations.RunSQL( + 'ALTER TABLE zarr_embargoedzarrarchive RENAME CONSTRAINT ' + '"api_embargoedzarrarchive_dandiset_id_b61f0a08_fk" TO ' + '"zarr_embargoedzarrarchive_dandiset_id_b61f0a08_fk";' + ), + # Constraints defined in Django + migrations.RunSQL( + 'ALTER TABLE zarr_zarrarchive RENAME CONSTRAINT ' + '"api-zarrarchive-consistent-checksum-status" TO ' + '"zarr-zarrarchive-consistent-checksum-status";' + ), + migrations.RunSQL( + 'ALTER TABLE zarr_zarrarchive RENAME CONSTRAINT "api-zarrarchive-unique-name" TO ' + '"zarr-zarrarchive-unique-name";' + ), + migrations.RunSQL( + 'ALTER TABLE zarr_embargoedzarrarchive RENAME CONSTRAINT ' + '"api-embargoedzarrarchive-consistent-checksum-status" TO ' + '"zarr-embargoedzarrarchive-consistent-checksum-status";' + ), + migrations.RunSQL( + 'ALTER TABLE zarr_embargoedzarrarchive RENAME CONSTRAINT ' + '"api-embargoedzarrarchive-unique-name" TO "zarr-embargoedzarrarchive-unique-name";' + ), + ] diff --git a/dandiapi/zarr/models.py b/dandiapi/zarr/models.py index cda42066c..ef48edd8d 100644 --- a/dandiapi/zarr/models.py +++ b/dandiapi/zarr/models.py @@ -93,9 +93,6 @@ def delete_files(self, paths: list[str]): class ZarrArchive(BaseZarrArchive): - class Meta(BaseZarrArchive.Meta): - db_table = 'api_zarrarchive' - storage = get_storage() dandiset = models.ForeignKey(Dandiset, related_name='zarr_archives', on_delete=models.CASCADE) @@ -105,9 +102,6 @@ def s3_path(self, zarr_path: str | Path): class EmbargoedZarrArchive(BaseZarrArchive): - class Meta(BaseZarrArchive.Meta): - db_table = 'api_embargoedzarrarchive' - storage = get_embargo_storage() dandiset = models.ForeignKey( Dandiset, related_name='embargoed_zarr_archives', on_delete=models.CASCADE From 3d52b4b2630b5a73f7a27d6704a089049a11f95e Mon Sep 17 00:00:00 2001 From: Jacob Nesbitt Date: Fri, 6 Oct 2023 13:05:00 -0400 Subject: [PATCH 2/2] Use correct hash for zarr index/constraint names --- .../0003_zarr_rename_tables_indexes_constraints.py | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/dandiapi/zarr/migrations/0003_zarr_rename_tables_indexes_constraints.py b/dandiapi/zarr/migrations/0003_zarr_rename_tables_indexes_constraints.py index d661f99a7..a312e6ee3 100644 --- a/dandiapi/zarr/migrations/0003_zarr_rename_tables_indexes_constraints.py +++ b/dandiapi/zarr/migrations/0003_zarr_rename_tables_indexes_constraints.py @@ -40,22 +40,22 @@ class Migration(migrations.Migration): # dandiset_id index migrations.RunSQL( 'ALTER INDEX "api_zarrarchive_dandiset_id_68510762" RENAME TO ' - '"zarr_zarrarchive_dandiset_id_68510762";' + '"zarr_zarrarchive_dandiset_id_2fdb93be";' ), migrations.RunSQL( 'ALTER INDEX "api_embargoedzarrarchive_dandiset_id_b61f0a08" RENAME TO ' - '"zarr_embargoedzarrarchive_dandiset_id_b61f0a08";' + '"zarr_embargoedzarrarchive_dandiset_id_9a6f3365";' ), # dandiset_id foreign key constraint migrations.RunSQL( 'ALTER TABLE zarr_zarrarchive RENAME CONSTRAINT ' '"api_zarrarchive_dandiset_id_68510762_fk" TO ' - '"zarr_zarrarchive_dandiset_id_68510762_fk";' + '"zarr_zarrarchive_dandiset_id_2fdb93be_fk";' ), migrations.RunSQL( 'ALTER TABLE zarr_embargoedzarrarchive RENAME CONSTRAINT ' '"api_embargoedzarrarchive_dandiset_id_b61f0a08_fk" TO ' - '"zarr_embargoedzarrarchive_dandiset_id_b61f0a08_fk";' + '"zarr_embargoedzarrarchive_dandiset_id_9a6f3365_fk";' ), # Constraints defined in Django migrations.RunSQL(