-
Notifications
You must be signed in to change notification settings - Fork 0
[BI-2995] Migrate parent ou ids in ou_level table to brapi ids #19
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: feature/BI-2861
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,29 @@ | ||
| 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. | ||
| -- 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_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_bi_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 | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Could we add a migration assertion that fails when an expected parent relationship cannot be mapped, or when legacy parent IDs remain after this update? Rows missing a matching external reference are currently skipped silently, so Flyway could mark the migration successful while some records still contain the old BI-generated IDs. |
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Just a thought, could we restrict this match to the BI observation-unit
external_reference_sourceand verify that eachlevel_idmaps to exactly oneou.id?external_reference_idis not unique in the schema, so matching on it alone could produce multiple parent OUs for the same level. In that case, PostgreSQLUPDATE ... FROMmay select an unintended parent ID.