From da60a1adb17397979b52db4624c5561850aa03ee Mon Sep 17 00:00:00 2001 From: Jason Loux Date: Tue, 21 Jul 2026 17:46:34 -0400 Subject: [PATCH 1/2] Migrate parent observation ids on ou_level to brapi dbIds --- .../V006_001__migrate_parent_ou_ids.sql | 27 +++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/main/resources/db/migration/V006_001__migrate_parent_ou_ids.sql diff --git a/src/main/resources/db/migration/V006_001__migrate_parent_ou_ids.sql b/src/main/resources/db/migration/V006_001__migrate_parent_ou_ids.sql new file mode 100644 index 00000000..09533d4f --- /dev/null +++ b/src/main/resources/db/migration/V006_001__migrate_parent_ou_ids.sql @@ -0,0 +1,27 @@ +WITH + levels_and_parent_ou_bi_id AS ( +-- Find observation_unit_level codes that contain a key by checking for a space in the level code. +-- Only other level codes that can exist are block/rep + SELECT + id AS level_id, + substring(level_code FROM '^([^ ]+)') AS parent_ou_id + FROM observation_unit_level + WHERE level_code LIKE '% %' + ), + ou_ids_matched_on_levels AS ( + SELECT + ou.id AS ou_id, + levels_and_parent_ou_bi_id.level_id + FROM observation_unit ou + JOIN observation_unit_external_references ouex ON ou.id = ouex.observation_unit_entity_id + JOIN external_reference ex ON ouex.external_references_id = ex.id + JOIN levels_and_parent_ou_bi_id ON ex.external_reference_id = levels_and_parent_ou_bi_id.parent_ou_id + ) +UPDATE observation_unit_level +SET level_code = regexp_replace( + observation_unit_level.level_code, + '^[^ ]+', + mol.ou_id::text +) +FROM ou_ids_matched_on_levels mol +WHERE observation_unit_level.id = mol.level_id \ No newline at end of file From c2afc76cafd3acf1f3cc20514e2883e13c7966d1 Mon Sep 17 00:00:00 2001 From: Jason Loux Date: Tue, 21 Jul 2026 17:59:40 -0400 Subject: [PATCH 2/2] Add some clarifying comments --- .../db/migration/V006_001__migrate_parent_ou_ids.sql | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/main/resources/db/migration/V006_001__migrate_parent_ou_ids.sql b/src/main/resources/db/migration/V006_001__migrate_parent_ou_ids.sql index 09533d4f..3dd227b5 100644 --- a/src/main/resources/db/migration/V006_001__migrate_parent_ou_ids.sql +++ b/src/main/resources/db/migration/V006_001__migrate_parent_ou_ids.sql @@ -1,21 +1,23 @@ WITH levels_and_parent_ou_bi_id AS ( -- Find observation_unit_level codes that contain a key by checking for a space in the level code. --- Only other level codes that can exist are block/rep +-- Only other level codes that can exist are block/rep. +-- Then extract the uuid from the level code, since program key is also contained there. SELECT id AS level_id, - substring(level_code FROM '^([^ ]+)') AS parent_ou_id + substring(level_code FROM '^([^ ]+)') AS parent_bi_ou_id FROM observation_unit_level WHERE level_code LIKE '% %' ), ou_ids_matched_on_levels AS ( +-- Now match the bi-generated exref ou ids to ex refs ids, and keep observation_unit_level ids for matching in next part SELECT ou.id AS ou_id, levels_and_parent_ou_bi_id.level_id FROM observation_unit ou JOIN observation_unit_external_references ouex ON ou.id = ouex.observation_unit_entity_id JOIN external_reference ex ON ouex.external_references_id = ex.id - JOIN levels_and_parent_ou_bi_id ON ex.external_reference_id = levels_and_parent_ou_bi_id.parent_ou_id + JOIN levels_and_parent_ou_bi_id ON ex.external_reference_id = levels_and_parent_ou_bi_id.parent_bi_ou_id ) UPDATE observation_unit_level SET level_code = regexp_replace(