From 44795aa8e1e187f9f5f5e2cb9ebb67fe3344e926 Mon Sep 17 00:00:00 2001 From: Tristan Slater <1631008+trslater@users.noreply.github.com> Date: Fri, 20 Dec 2024 09:22:03 -0800 Subject: [PATCH] Check for OATS schema before running migration --- ...49-create_bond_conditions_where_missing.ts | 122 ++++++++++-------- 1 file changed, 66 insertions(+), 56 deletions(-) diff --git a/services/apps/alcs/src/providers/typeorm/migrations/1734644882049-create_bond_conditions_where_missing.ts b/services/apps/alcs/src/providers/typeorm/migrations/1734644882049-create_bond_conditions_where_missing.ts index 63a5c3d8e..8abb283b7 100644 --- a/services/apps/alcs/src/providers/typeorm/migrations/1734644882049-create_bond_conditions_where_missing.ts +++ b/services/apps/alcs/src/providers/typeorm/migrations/1734644882049-create_bond_conditions_where_missing.ts @@ -4,72 +4,82 @@ export class CreateBondConditionsWhereMissing1734644882049 implements MigrationI public async up(queryRunner: QueryRunner): Promise { // Applications queryRunner.query(` - insert into - alcs.application_decision_condition ( - audit_created_by, - decision_uuid, - security_amount, - type_code - ) - select - distinct 'oats_etl' as audit_created_by, - ad."uuid" as decision_uuid, - oaad.security_amt as security_amount, - 'BOND' as type_code - from - alcs.application_decision ad - join alcs.application_decision_condition adc on ad.uuid = adc.decision_uuid - right join oats.oats_alr_appl_decisions oaad on oaad.alr_appl_decision_id = ad.oats_alr_appl_decision_id - where - ad."uuid" not in ( + do $$ + begin + if exists (select schema_name from information_schema.schemata where schema_name = 'oats') then + insert into + alcs.application_decision_condition ( + audit_created_by, + decision_uuid, + security_amount, + type_code + ) select - adc.decision_uuid + distinct 'oats_etl' as audit_created_by, + ad."uuid" as decision_uuid, + oaad.security_amt as security_amount, + 'BOND' as type_code from - alcs.application_decision_condition adc + alcs.application_decision ad + join alcs.application_decision_condition adc on ad.uuid = adc.decision_uuid + right join oats.oats_alr_appl_decisions oaad on oaad.alr_appl_decision_id = ad.oats_alr_appl_decision_id where - adc.type_code = 'BOND' - ) - and adc.security_amount > 0 - and ad.audit_created_by = 'oats_etl' - and ( - oaad.security_amt = adc.security_amount - or adc.security_amount is null - ) on conflict ("uuid") do nothing; + ad."uuid" not in ( + select + adc.decision_uuid + from + alcs.application_decision_condition adc + where + adc.type_code = 'BOND' + ) + and adc.security_amount > 0 + and ad.audit_created_by = 'oats_etl' + and ( + oaad.security_amt = adc.security_amount + or adc.security_amount is null + ) on conflict ("uuid") do nothing; + end if; + end $$; `); // NOI's queryRunner.query(` - insert into - alcs.notice_of_intent_decision_condition ( - audit_created_by, - decision_uuid, - security_amount, - type_code - ) - select - distinct 'oats_etl' as audit_created_by, - noid."uuid" as decision_uuid, - oaad.security_amt as security_amount, - 'BOND' as type_code - from - alcs.notice_of_intent_decision noid - join alcs.notice_of_intent_decision_condition noidc on noid.uuid = noidc.decision_uuid - right join oats.oats_alr_appl_decisions oaad on oaad.alr_appl_decision_id = noid.oats_alr_appl_decision_id - where - noid."uuid" not in ( + do $$ + begin + if exists (select schema_name from information_schema.schemata where schema_name = 'oats') then + insert into + alcs.notice_of_intent_decision_condition ( + audit_created_by, + decision_uuid, + security_amount, + type_code + ) select - noidc.decision_uuid + distinct 'oats_etl' as audit_created_by, + noid."uuid" as decision_uuid, + oaad.security_amt as security_amount, + 'BOND' as type_code from - alcs.notice_of_intent_decision_condition noidc + alcs.notice_of_intent_decision noid + join alcs.notice_of_intent_decision_condition noidc on noid.uuid = noidc.decision_uuid + right join oats.oats_alr_appl_decisions oaad on oaad.alr_appl_decision_id = noid.oats_alr_appl_decision_id where - noidc.type_code = 'BOND' - ) - and noidc.security_amount > 0 - and noid.audit_created_by = 'oats_etl' - and ( - oaad.security_amt = noidc.security_amount - or noidc.security_amount is null - ) on conflict ("uuid") do nothing; + noid."uuid" not in ( + select + noidc.decision_uuid + from + alcs.notice_of_intent_decision_condition noidc + where + noidc.type_code = 'BOND' + ) + and noidc.security_amount > 0 + and noid.audit_created_by = 'oats_etl' + and ( + oaad.security_amt = noidc.security_amount + or noidc.security_amount is null + ) on conflict ("uuid") do nothing; + end if; + end $$; `); }