From 59daee47905c6f1d429d2c8bd1d483cc2f07c466 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Fri, 10 Mar 2023 19:27:31 +0100 Subject: [PATCH] 53-54. Link period to person or temporary address location --- sql/import.sql | 28 ++++++++++++++++++++++++++++ 1 file changed, 28 insertions(+) diff --git a/sql/import.sql b/sql/import.sql index 22ba61d..ef0a2df 100644 --- a/sql/import.sql +++ b/sql/import.sql @@ -335,12 +335,40 @@ INSERT INTO chill_person_accompanying_period_participation (id, person_id, accom nextval('chill_person_accompanying_period_participation_id_seq'), t.person_id, t.period_id, date(date_trunc('year', CURRENT_DATE)) FROM (SELECT person_id, period_id FROM import.periodes ip JOIN import.personnes p ON ip.id = p.id ORDER BY person_id) AS t; +-- 53. Prepare temporary address location +INSERT INTO chill_main_address (id, postcode_id, street, streetnumber, validFrom) SELECT + nextval('chill_main_address_id_seq'), + (SELECT pc.id FROM chill_main_postal_code pc WHERE pc.code = postcode), + street, streetnumber, date(date_trunc('year', CURRENT_DATE)) + FROM import.choix_periodes WHERE street != ''; +ALTER TABLE import.choix_periodes ADD COLUMN address_location_id BIGINT; +UPDATE import.choix_periodes SET address_location_id = (SELECT max(id) FROM chill_main_address) WHERE street != ''; + +-- 54. Link period to person or temporary address location +UPDATE chill_person_accompanying_period acp + SET addresslocation_id = (SELECT address_location_id FROM import.choix_periodes WHERE address_location_id IS NOT NULL LIMIT 1) + FROM import.personnes pson JOIN import.periodes piod ON pson.id = piod.id + WHERE piod.period_id = acp.id AND piod.addresslocation = 'oui'; +UPDATE chill_person_accompanying_period acp + SET personlocation_id = pson.person_id + FROM import.personnes pson JOIN import.periodes piod ON pson.id = piod.id + WHERE piod.period_id = acp.id AND piod.personlocation = 'oui'; + + -- ========================================================================================= -- -- -- DOWN -- +-- Undo 54. +UPDATE chill_person_accompanying_period acp SET addresslocation_id = NULL, personlocation_id = NULL FROM import.periodes piod WHERE piod.period_id = acp.id; + +-- Undo 53. +DELETE FROM chill_main_address addr USING import.choix_periodes ic WHERE addr.id = ic.address_location_id; +SELECT setval('chill_main_address_id_seq', (SELECT COALESCE(max(id), 1) FROM chill_main_address)); +ALTER TABLE import.choix_periodes DROP COLUMN address_location_id; + -- Undo 52. DELETE FROM chill_person_accompanying_period_participation part USING import.periodes ip WHERE part.accompanyingperiod_id = ip.period_id; SELECT setval('chill_person_accompanying_period_participation_id_seq', (SELECT COALESCE(max(id), 1) FROM chill_person_accompanying_period_participation));