53-54. Link period to person or temporary address location

This commit is contained in:
Mathieu Jaumotte 2023-03-10 19:27:31 +01:00
parent 6900d66206
commit 59daee4790

View File

@ -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));