From 67d020d322b5f8dc7238e96dcda75c9f5a81e5a1 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Fri, 10 Mar 2023 17:51:58 +0100 Subject: [PATCH] 50-51. Insert in accompanyingPeriods table --- sql/import.sql | 20 ++++++++++++++++++++ 1 file changed, 20 insertions(+) diff --git a/sql/import.sql b/sql/import.sql index 75b35d0..e275030 100644 --- a/sql/import.sql +++ b/sql/import.sql @@ -313,6 +313,19 @@ INSERT INTO chill_person_household_composition (id, household_id, startdate, hou FROM import.personnes WHERE household_composition_type1 IS NOT NULL ; +-- 50. Prepare id mapping before insertion +ALTER TABLE import.periodes ADD column period_id BIGINT; +UPDATE import.periodes SET period_id = periodid + FROM (SELECT id, nextval('chill_person_accompanying_period_id_seq') AS periodid FROM import.periodes ORDER BY id) AS t + WHERE import.periodes.id = t.id; +--SELECT ipe.id, ipe.fullname, ipe.period_id, ip.id, ip.person_id FROM import.periodes ipe JOIN import.personnes ip ON ipe.id = ip.id ORDER BY ipe.id; + +-- 51. Insert in chill_person_accompanying_period +INSERT INTO chill_person_accompanying_period (id, openingdate, step, remark, intensity, createdby_id, createdat, updatedby_id, updatedat) SELECT + period_id, date(date_trunc('year', CURRENT_DATE)), 'CONFIRMED', COALESCE(TRIM(remark), ''), intensity1, + (SELECT distinct(first_value(id) OVER(ORDER BY id)) FROM users), CURRENT_DATE, + (SELECT distinct(first_value(id) OVER(ORDER BY id)) FROM users), CURRENT_DATE +FROM import.periodes; -- ========================================================================================= -- @@ -320,6 +333,13 @@ WHERE household_composition_type1 IS NOT NULL ; -- DOWN -- +-- Undo 51. +DELETE FROM chill_person_accompanying_period acp USING import.periodes ip WHERE acp.id = ip.period_id; +SELECT setval('chill_person_accompanying_period_id_seq', (SELECT COALESCE(max(id), 1) FROM chill_person_accompanying_period)); + +-- Undo 50. +ALTER TABLE import.periodes DROP column period_id; + -- Undo 46. DELETE FROM chill_person_household_composition c USING import.personnes ip WHERE c.household_id = ip.person_id; SELECT setval('chill_person_household_composition_id_seq', (SELECT COALESCE(max(id),1) FROM chill_person_household_composition));