Compare commits

..

5 Commits

View File

@ -315,6 +315,7 @@ 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
@ -324,7 +325,8 @@ UPDATE import.periodes SET period_id = periodid
-- 51. Insert in chill_person_accompanying_period
INSERT INTO chill_person_accompanying_period (id, openingdate, closingdate, step, remark, intensity, createdby_id, createdat, updatedby_id, updatedat) SELECT
period_id, COALESCE(openingdate1, date(date_trunc('year', CURRENT_DATE))), closingdate1,
period_id,
COALESCE(openingdate1, date(date_trunc('year', CURRENT_DATE))), closingdate1,
'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
@ -345,7 +347,7 @@ INSERT INTO chill_main_address (id, postcode_id, street, streetnumber, validFrom
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
-- 54. Link person or temporary address location to periods
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
@ -363,6 +365,45 @@ INSERT INTO chill_person_accompanying_period_location_history (id, period_id, st
FROM chill_person_accompanying_period acp
WHERE id NOT IN (SELECT period_id FROM chill_person_accompanying_period_location_history) AND step LIKE 'CONFIRMED' ORDER BY id;
-- 56. Link socialIssues to periods
INSERT INTO chill_person_accompanying_period_social_issues (accompanyingperiod_id, socialissue_id)
SELECT
DISTINCT ON (t.period_id) t.period_id,
COALESCE(t.enfant_id, t.parent_id, 1) AS socialissue_id -- quid si pas de socialissue ?
FROM (
SELECT p.period_id,
(SELECT id FROM chill_person_social_issue WHERE title::jsonb->>'fr' = icp.parent1::jsonb->>'fr' AND parent_id IS NULL) AS parent_id, icp.parent1,
(SELECT id FROM chill_person_social_issue WHERE title::jsonb->>'fr' = icp.enfant1::jsonb->>'fr' AND parent_id =
(SELECT id FROM chill_person_social_issue WHERE title::jsonb->>'fr' = icp.parent1::jsonb->>'fr' AND parent_id IS NULL)) AS enfant_id, icp.enfant1
FROM import.periodes p
JOIN import.choix_periodes icp ON p.acp_socialissues = icp.acp_socialissues
ORDER BY id) AS t;
-- 57. Link referrer to periods
UPDATE chill_person_accompanying_period acp
SET user_id = (SELECT id FROM users WHERE users.username = ip.referrer)
FROM import.periodes ip WHERE acp.id = ip.period_id;
--SELECT ip.id, (SELECT id FROM users WHERE users.username = ip.referrer) AS referrer_id, ip.referrer, acp.id as period_id, acp.user_id FROM chill_person_accompanying_period acp JOIN import.periodes ip ON ip.period_id = acp.id ORDER BY ip.id;
-- 58. Link scopes to periods
INSERT INTO accompanying_periods_scopes (accompanying_period_id, scope_id)
SELECT ip.period_id, COALESCE(
(SELECT id FROM scopes s WHERE ip.acp_scopes1::jsonb->>'fr' = s.name::jsonb->>'fr'),
(SELECT id from scopes s WHERE s.name::jsonb->>'fr' = 'tous') -- default 'tous'
)
FROM import.periodes ip;
-- 59. Link origin to periods
UPDATE chill_person_accompanying_period acp SET origin_id =
(SELECT id FROM chill_person_accompanying_period_origin o WHERE o.label::jsonb->>'fr' = ip.origin1::jsonb->>'fr')
FROM import.periodes ip WHERE acp.id = ip.period_id;
--SELECT ip.id, ip.origin1, acp.id as period_id, acp.origin_id FROM chill_person_accompanying_period acp JOIN import.periodes ip ON ip.period_id = acp.id ORDER BY ip.id;
-- 60. Link jobs to periods
UPDATE chill_person_accompanying_period acp SET job_id =
(SELECT id FROM chill_main_user_job j WHERE j.label::jsonb->>'fr' = ip.job1::jsonb->>'fr')
FROM import.periodes ip WHERE acp.id = ip.period_id;
-- ========================================================================================= --
@ -370,6 +411,21 @@ INSERT INTO chill_person_accompanying_period_location_history (id, period_id, st
-- DOWN
--
-- Undo 60.
UPDATE chill_person_accompanying_period acp SET job_id = null FROM import.periodes ip WHERE acp.id = ip.period_id;
-- Undo 59.
UPDATE chill_person_accompanying_period acp SET origin_id = null FROM import.periodes ip WHERE ip.period_id = acp.id;
-- Undo 58.
DELETE FROM accompanying_periods_scopes acs USING import.periodes ip WHERE acs.accompanying_period_id = ip.period_id;
-- Undo 57.
UPDATE chill_person_accompanying_period acp SET user_id = null FROM import.periodes ip WHERE ip.period_id = acp.id;
-- Undo 56.
DELETE FROM chill_person_accompanying_period_social_issues asi USING import.periodes ip WHERE asi.accompanyingperiod_id = ip.period_id;
-- Undo 55.
DELETE FROM chill_person_accompanying_period_location_history history USING import.periodes ip WHERE history.period_id = ip.period_id;
SELECT setval('chill_person_accompanying_period_location_history_id_seq', (SELECT COALESCE(max(id), 1) FROM chill_person_accompanying_period_location_history));