From e67dc4e4e75c98d5dcea9addad92a12051944897 Mon Sep 17 00:00:00 2001 From: Theresa Kamerman Date: Fri, 1 May 2026 10:25:57 -0400 Subject: [PATCH] Make parcel names unique - also goes through and adds (1), (2), ...etc, to parcels with duplicate names --- .../Aerie/34_unique_parcel_names/down.sql | 2 ++ .../Aerie/34_unique_parcel_names/up.sql | 31 +++++++++++++++++++ .../sql/applied_migrations.sql | 2 ++ .../sql/tables/sequencing/parcel.sql | 2 +- 4 files changed, 36 insertions(+), 1 deletion(-) create mode 100644 deployment/hasura/migrations/Aerie/34_unique_parcel_names/down.sql create mode 100644 deployment/hasura/migrations/Aerie/34_unique_parcel_names/up.sql diff --git a/deployment/hasura/migrations/Aerie/34_unique_parcel_names/down.sql b/deployment/hasura/migrations/Aerie/34_unique_parcel_names/down.sql new file mode 100644 index 0000000000..c00d8c2e4e --- /dev/null +++ b/deployment/hasura/migrations/Aerie/34_unique_parcel_names/down.sql @@ -0,0 +1,2 @@ +alter table sequencing.parcel drop constraint parcel_name_key; +call migrations.mark_migration_rolled_back(34); diff --git a/deployment/hasura/migrations/Aerie/34_unique_parcel_names/up.sql b/deployment/hasura/migrations/Aerie/34_unique_parcel_names/up.sql new file mode 100644 index 0000000000..0e7e02d2d4 --- /dev/null +++ b/deployment/hasura/migrations/Aerie/34_unique_parcel_names/up.sql @@ -0,0 +1,31 @@ +-- Data migration: deduplicate parcel names: +do $$ +begin + -- While there are duplicate names in the parcel table... + while exists( + select from sequencing.parcel + group by name + having count(name) > 1 + ) loop + -- ...deduplicate them + update sequencing.parcel + set name = name || '(' || ir.row || ')' + from ( + select id, row_number() over (partition by name) - 1 as row + from sequencing.parcel + where name in ( + select p.name + from sequencing.parcel p + group by p.name + having count(1) > 1) + ) as ir + where ir.id = parcel.id + and row > 0; +end loop; +end $$; + +-- Add new uniqueness constraint to the parcel name +alter table sequencing.parcel +add unique(name); + +call migrations.mark_migration_applied(34); diff --git a/deployment/postgres-init-db/sql/applied_migrations.sql b/deployment/postgres-init-db/sql/applied_migrations.sql index 69f6176042..1b9150b1ab 100644 --- a/deployment/postgres-init-db/sql/applied_migrations.sql +++ b/deployment/postgres-init-db/sql/applied_migrations.sql @@ -34,3 +34,5 @@ call migrations.mark_migration_applied(29); call migrations.mark_migration_applied(30); call migrations.mark_migration_applied(31); call migrations.mark_migration_applied(32); + +call migrations.mark_migration_applied(34); diff --git a/deployment/postgres-init-db/sql/tables/sequencing/parcel.sql b/deployment/postgres-init-db/sql/tables/sequencing/parcel.sql index bb09b987ca..5c3719e52a 100644 --- a/deployment/postgres-init-db/sql/tables/sequencing/parcel.sql +++ b/deployment/postgres-init-db/sql/tables/sequencing/parcel.sql @@ -1,7 +1,7 @@ create table sequencing.parcel ( id integer generated always as identity, - name text not null, + name text not null unique, command_dictionary_id integer not null, channel_dictionary_id integer default null,