Compare commits

...

6 Commits

View File

@ -3,7 +3,7 @@
-- version v0.6 (== version canevas)
--
-- /!\ IMPORTANT
-- * Avant de migrer (UP), il faut d'abord importer les codes postaux !!!
-- Avant de migrer (UP), il faut d'abord avoir importé les codes postaux !!!
--
@ -33,7 +33,7 @@ INSERT INTO chill_main_civility (id, name, abbreviation, active, ordering)
max_ordering.max_ordering + row_number() OVER () as ordering
FROM ( SELECT DISTINCT ON (civility) civility1 FROM import.choix_personnes WHERE civility1 IS NOT NULL ) t
CROSS JOIN max_ordering
WHERE NOT EXISTS ( SELECT 1 FROM chill_main_civility WHERE name::jsonb = t.civility1 );
WHERE NOT EXISTS ( SELECT 1 FROM chill_main_civility WHERE name::jsonb->>'fr' = t.civility1::jsonb->>'fr' );
-- 4. Complete table Country
ALTER TABLE import.choix_personnes ADD COLUMN country1 JSONB;
@ -41,7 +41,7 @@ UPDATE import.choix_personnes SET country1=json_build_object('fr', trim(country)
INSERT INTO country (id, name, countrycode)
SELECT nextval('country_id_seq'), t.country1, 'ZZ'
FROM ( SELECT DISTINCT ON (country) country1 FROM import.choix_personnes WHERE country1 IS NOT NULL ) t
WHERE NOT EXISTS ( SELECT 1 FROM country WHERE name::jsonb = t.country1 );
WHERE NOT EXISTS ( SELECT 1 FROM country WHERE name::jsonb->>'fr' = t.country1::jsonb->>'fr' );
-- 5. Complete table marital_status
ALTER TABLE import.choix_personnes ADD COLUMN maritalstatus1 JSONB;
@ -49,7 +49,7 @@ UPDATE import.choix_personnes SET maritalstatus1=json_build_object('fr', trim(ma
INSERT INTO chill_person_marital_status (id, name)
SELECT substr(md5(random()::text), 1, 7), t.maritalstatus1
FROM ( SELECT DISTINCT ON (maritalstatus) maritalstatus1 FROM import.choix_personnes WHERE maritalstatus1 IS NOT NULL ) t
WHERE NOT EXISTS ( SELECT 1 FROM chill_person_marital_status WHERE name::jsonb = t.maritalstatus1 );
WHERE NOT EXISTS ( SELECT 1 FROM chill_person_marital_status WHERE name::jsonb->>'fr' = t.maritalstatus1::jsonb->>'fr' );
-- 6. Complete table household_composition_type
ALTER TABLE import.choix_personnes ADD COLUMN household_composition_type1 JSONB;
@ -57,7 +57,7 @@ UPDATE import.choix_personnes SET household_composition_type1=json_build_object(
INSERT INTO chill_person_household_composition_type (id, active, label)
SELECT nextval('chill_person_household_composition_type_id_seq'), true, t.household_composition_type1
FROM ( SELECT DISTINCT ON (household_composition_type) household_composition_type1 FROM import.choix_personnes WHERE household_composition_type1 IS NOT NULL ) t
WHERE NOT EXISTS ( SELECT 1 FROM chill_person_household_composition_type WHERE label::jsonb = t.household_composition_type1 );
WHERE NOT EXISTS ( SELECT 1 FROM chill_person_household_composition_type WHERE label::jsonb->>'fr' = t.household_composition_type1::jsonb->>'fr' );
-- 7. Complete table household_position
ALTER TABLE import.choix_personnes ADD COLUMN household_position1 JSONB;
@ -69,7 +69,7 @@ INSERT INTO chill_person_household_position (id, label, sharehousehold, allowhol
max_ordering.max_ordering + row_number() OVER () as ordering
FROM ( SELECT DISTINCT ON (household_position) household_position1 FROM import.choix_personnes WHERE household_position1 IS NOT NULL ) t
CROSS JOIN max_ordering
WHERE NOT EXISTS ( SELECT 1 FROM chill_person_household_position WHERE label::jsonb = t.household_position1 );
WHERE NOT EXISTS ( SELECT 1 FROM chill_person_household_position WHERE label::jsonb->>'fr' = t.household_position1::jsonb->>'fr' );
-- 8. Complete table accompanying_period_closingmotive
ALTER TABLE import.choix_periodes ADD COLUMN closingmotive1 JSONB;
@ -81,7 +81,7 @@ INSERT INTO chill_person_accompanying_period_closingmotive (id, name, active, pa
max_ordering.max_ordering + row_number() OVER () as ordering
FROM ( SELECT DISTINCT ON (closingmotive) closingmotive1 FROM import.choix_periodes WHERE closingmotive1 IS NOT NULL ) t
CROSS JOIN max_ordering
WHERE NOT EXISTS ( SELECT 1 FROM chill_person_accompanying_period_closingmotive WHERE name::jsonb = t.closingmotive1 );
WHERE NOT EXISTS ( SELECT 1 FROM chill_person_accompanying_period_closingmotive WHERE name::jsonb->>'fr' = t.closingmotive1::jsonb->>'fr' );
-- 9. Complete table accompanying_period_origin
ALTER TABLE import.choix_periodes ADD COLUMN origin1 JSONB;
@ -89,7 +89,7 @@ UPDATE import.choix_periodes SET origin1=json_build_object('fr', trim(origin)) W
INSERT INTO chill_person_accompanying_period_origin (id, label, noactiveafter)
SELECT nextval('chill_person_accompanying_period_origin_id_seq'), t.origin1, null
FROM ( SELECT DISTINCT ON (origin) origin1 FROM import.choix_periodes WHERE origin1 IS NOT NULL ) t
WHERE NOT EXISTS ( SELECT 1 FROM chill_person_accompanying_period_origin WHERE label::jsonb = t.origin1 );
WHERE NOT EXISTS ( SELECT 1 FROM chill_person_accompanying_period_origin WHERE label::jsonb->>'fr' = t.origin1::jsonb->>'fr' );
-- 10. Complete table user_job
ALTER TABLE import.choix_periodes ADD COLUMN job1 JSONB;
@ -97,7 +97,7 @@ UPDATE import.choix_periodes SET job1=json_build_object('fr', trim(job)) WHERE j
INSERT INTO chill_main_user_job (id, label, active)
SELECT nextval('chill_main_user_job_id_seq'), t.job1, true
FROM ( SELECT DISTINCT ON (job) job1 FROM import.choix_periodes WHERE job1 IS NOT NULL ) t
WHERE NOT EXISTS( SELECT 1 FROM chill_main_user_job WHERE label::jsonb = t.job1 );
WHERE NOT EXISTS( SELECT 1 FROM chill_main_user_job WHERE label::jsonb->>'fr' = t.job1::jsonb->>'fr' );
-- 11. Complete table Scopes
ALTER TABLE import.choix_periodes ADD COLUMN acp_scopes1 JSONB;
@ -105,13 +105,15 @@ UPDATE import.choix_periodes SET acp_scopes1=json_build_object('fr', trim(acp_sc
INSERT INTO scopes (id, name, active)
SELECT nextval('scopes_id_seq'), t.acp_scopes1, true
FROM ( SELECT DISTINCT ON (acp_scopes) acp_scopes1 FROM import.choix_periodes WHERE acp_scopes1 IS NOT NULL ) t
WHERE NOT EXISTS ( SELECT 1 FROM scopes WHERE name::jsonb = t.acp_scopes1 );
WHERE NOT EXISTS ( SELECT 1 FROM scopes WHERE name::jsonb->>'fr' = t.acp_scopes1::jsonb->>'fr' );
-- SELECT i.acp_scopes1::jsonb, s.name FROM scopes s LEFT JOIN import.choix_periodes i ON s.name::jsonb = i.acp_scopes1::jsonb WHERE s.name IS NOT NULL or i.acp_scopes1 IS NOT NULL ;
-- 12. Complete table Users (acp referrer)
INSERT INTO users (id, username, password, enabled, locked, attributes, label)
SELECT nextval('users_id_seq'), t.referrer, '', false, false, '[]', t.referrer
FROM ( SELECT DISTINCT ON (referrer) referrer FROM import.choix_periodes WHERE referrer <> '') t
WHERE NOT EXISTS ( SELECT 1 FROM users WHERE username::text = t.referrer );
-- SELECT * FROM users;
-- 13. Complete table SocialIssues
ALTER TABLE import.choix_periodes ADD COLUMN parent1 jsonb;
@ -125,20 +127,20 @@ INSERT INTO chill_person_social_issue (id, parent_id, title, ordering)
max_ordering.max_ordering + row_number() OVER () as ordering
FROM ( SELECT DISTINCT ON (parent) parent1 FROM import.choix_periodes WHERE parent1 IS NOT NULL ) t
CROSS JOIN max_ordering
WHERE NOT EXISTS ( SELECT 1 FROM chill_person_social_issue WHERE title::jsonb = t.parent1 );
WHERE NOT EXISTS ( SELECT 1 FROM chill_person_social_issue WHERE title::jsonb->>'fr' = t.parent1::jsonb->>'fr' );
WITH max_ordering AS ( SELECT MAX(ordering) as max_ordering FROM chill_person_social_issue )
INSERT INTO chill_person_social_issue (id, parent_id, title, ordering)
SELECT nextval('chill_person_social_issue_id_seq'),
( SELECT id FROM chill_person_social_issue WHERE parent_id IS NULL AND title = t.parent1 ), t.enfant1,
( SELECT id FROM chill_person_social_issue WHERE parent_id IS NULL AND title::jsonb->>'fr' = t.parent1::jsonb->>'fr' ), t.enfant1,
max_ordering.max_ordering + row_number() OVER () as ordering
FROM ( SELECT DISTINCT ON (acp_socialissues) parent1, enfant1 FROM import.choix_periodes WHERE enfant1 IS NOT NULL ) t
CROSS JOIN max_ordering
WHERE NOT EXISTS ( SELECT 1 FROM chill_person_social_issue
WHERE title::jsonb = t.enfant1
AND parent_id = (SELECT id FROM chill_person_social_issue WHERE title::jsonb = t.parent1));
WHERE title::jsonb->>'fr' = t.enfant1::jsonb->>'fr'
AND parent_id = (SELECT id FROM chill_person_social_issue WHERE title::jsonb->>'fr' = t.parent1::jsonb->>'fr'));
-- 14. Complete table WorkSocialActions
-- (not yet implemented in canvas)
-- 20. Prepare personnes civility
ALTER TABLE import.personnes ADD COLUMN civility1 jsonb;
@ -211,13 +213,13 @@ UPDATE import.periodes SET intensity1='occasional'; UPDATE import.periodes SET i
ALTER TABLE import.personnes ADD column mobilenumber1 text;
UPDATE import.personnes SET mobilenumber1=NULLIF(regexp_replace(mobilenumber, '[^0-9]', '', 'g'), ''); -- remove all NaN chars
UPDATE import.personnes SET mobilenumber1=regexp_replace(mobilenumber1, '^0', '') WHERE mobilenumber1 like '0%'; -- remove first 0 prefix
-- UPDATE import.personnes SET mobilenumber1=regexp_replace(mobilenumber1, '(.*)', '+32\1'); -- add belgium intl prefix
UPDATE import.personnes SET mobilenumber1=regexp_replace(mobilenumber1, '(.*)', '+32\1'); -- add belgium intl prefix
-- 34. Format phone numbers
ALTER TABLE import.personnes ADD column phonenumber1 text;
UPDATE import.personnes SET phonenumber1=NULLIF(regexp_replace(phonenumber, '[^0-9]', '', 'g'), ''); -- remove all NaN chars
UPDATE import.personnes SET phonenumber1=regexp_replace(phonenumber1, '^0', '') WHERE phonenumber1 like '0%'; -- remove first 0 prefix
-- UPDATE import.personnes SET phonenumber1=regexp_replace(phonenumber1, '(.*)', '+32\1'); -- add belgium intl prefix
UPDATE import.personnes SET phonenumber1=regexp_replace(phonenumber1, '(.*)', '+32\1'); -- add belgium intl prefix
-- SELECT mobilenumber, mobilenumber1, phonenumber, phonenumber1 FROM import.personnes;
@ -247,10 +249,10 @@ INSERT INTO chill_person_person (
*/
) SELECT
person_id -- id
, (SELECT c.id FROM country c WHERE c.name::jsonb = ip.nationality1::jsonb AND ip.nationality1 IS NOT NULL) -- nationality_id,
, (SELECT c.id FROM country c WHERE c.name::jsonb = ip.countryofbirth1::jsonb AND ip.countryofbirth1 IS NOT NULL ) -- countryofbirth_id,
, (SELECT c.id FROM chill_main_civility c WHERE c.name::jsonb = ip.civility1::jsonb AND ip.civility1 IS NOT NULL ) -- civility_id
, (SELECT ms.id FROM chill_person_marital_status ms WHERE ms.name::jsonb = ip.maritalstatus1::jsonb AND ip.maritalstatus1 IS NOT NULL ) -- maritalstatus_id,
, (SELECT c.id FROM country c WHERE c.name::jsonb->>'fr' = ip.nationality1::jsonb->>'fr' AND ip.nationality1 IS NOT NULL) -- nationality_id,
, (SELECT c.id FROM country c WHERE c.name::jsonb->>'fr' = ip.countryofbirth1::jsonb->>'fr' AND ip.countryofbirth1 IS NOT NULL ) -- countryofbirth_id,
, (SELECT c.id FROM chill_main_civility c WHERE c.name::jsonb->>'fr' = ip.civility1::jsonb->>'fr' AND ip.civility1 IS NOT NULL ) -- civility_id
, (SELECT ms.id FROM chill_person_marital_status ms WHERE ms.name::jsonb->>'fr' = ip.maritalstatus1::jsonb->>'fr' AND ip.maritalstatus1 IS NOT NULL ) -- maritalstatus_id,
, TRIM(firstname), UPPER(TRIM(lastname)), birthdate1, TRIM(place_of_birth), TRIM(memo), TRIM(email), TRIM(contactinfo), phonenumber1, mobilenumber1, numberofchildren1, gender1, deathdate1, false
, CURRENT_DATE, CURRENT_DATE -- createdat, updatedat
, (SELECT distinct(first_value(id) OVER(ORDER BY id)) FROM users), (SELECT distinct(first_value(id) OVER(ORDER BY id)) FROM users) -- createdby_id, updatedby_id
@ -263,34 +265,70 @@ INSERT INTO chill_person_person (
*/
FROM import.personnes ip;
-- 42. Add address, and link it to person
-- 42. Fill Person center history
INSERT INTO chill_person_person_center_history (id, person_id, center_id, startdate) SELECT
nextval('chill_person_person_center_history_id_seq'), person_id,
(SELECT distinct(first_value(id) OVER(ORDER BY id)) FROM centers) , -- center_id
CURRENT_DATE
FROM import.personnes ip;
-- SELECT ip.id, ip.lastname, ip.firstname, ip.person_id, hist.id, hist.person_id, hist.center_id, hist.startdate, p.id, p.fullnamecanonical, p.email FROM chill_person_person p RIGHT JOIN import.personnes ip ON ip.person_id = p.id FULL JOIN chill_person_person_center_history hist on p.id = hist.person_id ORDER BY ip.person_id;
-- 43. Add address, and link it to person
ALTER TABLE import.personnes ADD column address_id BIGINT;
UPDATE import.personnes SET address_id = nextval('chill_main_address_id_seq');
ALTER TABLE import.personnes ADD column postcode_id BIGINT;
UPDATE import.personnes SET postcode_id = cmpc.id FROM chill_main_postal_code cmpc WHERE cmpc.code = postcode AND cmpc.origin = 0;
INSERT INTO chill_main_address (id, postcode_id, street, streetnumber, extra, validFrom) SELECT
address_id, postcode_id, street, streetnumber, extra, CURRENT_DATE
FROM import.personnes WHERE postcode_id IS NOT NULL;
-- ajouter dans chill_person_accompanying_course
FROM import.personnes WHERE postcode_id IS NOT NULL;
SELECT ip.person_id, ip.address_id FROM import.personnes ip ;
-- SELECT ip.id, ip.person_id, ip.firstname, ip.lastname, addr.* FROM chill_main_address addr RIGHT JOIN import.personnes ip ON addr.id = ip.address_id ORDER BY ip.id;
-- 44. Add houshold and household member with position
INSERT INTO chill_person_household (id) SELECT person_id from import.personnes;
SELECT setval('chill_person_household_id_seq', (SELECT max(id) FROM chill_person_household));
INSERT INTO chill_person_household_members (id, person_id, household_id, startdate, sharedhousehold, position_id, holder) SELECT
nextval('chill_person_household_members_id_seq'), person_id, person_id, COALESCE(household_startdate1, CURRENT_DATE),
true, (SELECT id FROM chill_person_household_position pos WHERE household_position1::jsonb->>'fr' = pos.label::jsonb->>'fr'), false
FROM import.personnes;
-- SELECT ip.id, ip.person_id, ip.firstname, ip.lastname, hh.* FROM chill_person_household hh FULL JOIN import.personnes ip ON hh.id = ip.person_id ORDER BY hh.id;
-- SELECT ip.id, ip.person_id, ip.firstname, ip.lastname, memb.* FROM chill_person_household_members memb JOIN import.personnes ip ON memb.household_id = ip.person_id ORDER BY memb.household_id;
-- 45. Add address to household
INSERT INTO chill_person_household_to_addresses (household_id, address_id) SELECT person_id, address_id
FROM import.personnes WHERE postcode_id IS NOT NULL; -- cfr (*) adresse insérées seulement si postcode
-- 46. Add household composition
-- (not yet implemented in canvas)
-- ========================================================================================= --
--
-- DOWN
--
-- Undo 42.
DELETE FROM chill_main_address cma USING import.personnes ip WHERE cma.id = ip.address_id;
-- Undo 45.
DELETE FROM chill_person_household_to_addresses hhaddr USING import.personnes ip WHERE hhaddr.household_id = ip.person_id ;
-- Undo 44.
DELETE FROM chill_person_household_members memb USING import.personnes ip WHERE memb.person_id = ip.person_id;
SELECT setval('chill_person_household_members_id_seq', (SELECT max(id) FROM chill_person_household_members));
DELETE FROM chill_person_household hh USING import.personnes ip WHERE hh.id = ip.person_id;
SELECT setval('chill_person_household_id_seq', (SELECT max(id) FROM chill_person_household));
-- Undo 43.
DELETE FROM chill_main_address addr USING import.personnes ip WHERE addr.id = ip.address_id;
SELECT setval('chill_main_address_id_seq', (SELECT max(id) FROM chill_main_address));
ALTER TABLE import.personnes DROP column postcode_id;
ALTER TABLE import.personnes DROP column address_id;
-- Undo 42.
DELETE FROM chill_person_person_center_history hist USING import.personnes ip WHERE hist.person_id = ip.person_id;
SELECT setval('chill_person_person_center_history_id_seq', (SELECT max(id) FROM chill_person_person_center_history));
-- Undo 41.
DELETE FROM chill_person_person cpp USING import.personnes ip WHERE cpp.id = ip.person_id;
DELETE FROM chill_person_person p USING import.personnes ip WHERE p.id = ip.person_id;
SELECT setval('chill_person_person_id_seq', (SELECT max(id) FROM chill_person_person));
-- Undo 40.
@ -411,4 +449,8 @@ ALTER TABLE import.periodes DROP COLUMN closingdate1;
-- -------------
-- tiers choices_list: civility kind profession category
-- =============
-- QUESTIONS
--
-- définir par défaut: quel user, quel centre ?
--