20-32. Prepare import personnes and periodes tables

This commit is contained in:
Mathieu Jaumotte 2023-02-19 14:58:11 +01:00
parent dd89aa6ff1
commit b6f26900a7

View File

@ -139,10 +139,189 @@ INSERT INTO chill_person_social_issue (id, parent_id, title, ordering)
-- 14. Complete table WorkSocialActions
-- 20. Prepare personnes civility
ALTER TABLE import.personnes ADD COLUMN civility1 jsonb;
UPDATE import.personnes SET civility1=json_build_object('fr', trim(civility)) WHERE civility!='';
-- SELECT i.civility, c.name as civility_name, c.id as civility_id FROM import.personnes i JOIN chill_main_civility c ON i.civility1::jsonb = c.name::jsonb;
-- 21. Prepare personnes gender
ALTER TABLE import.personnes ADD COLUMN gender1 VARCHAR;
UPDATE import.personnes SET gender1 = CASE
WHEN trim(gender) IN ('Femme', 'femme', 'Woman', 'woman', 'Female', 'female') THEN 'woman'
WHEN trim(gender) IN ('Homme', 'homme', 'Man', 'man', 'Male', 'male') THEN 'man'
WHEN trim(gender) IN ('', 'Inconnu', 'inconnu', 'Unknown', 'unknown') THEN 'unknown'
ELSE 'both' END;
-- 22. Prepare personnes nationality
ALTER TABLE import.personnes ADD COLUMN nationality1 jsonb;
UPDATE import.personnes SET nationality1=json_build_object('fr', trim(nationality)) WHERE nationality!='';
-- SELECT i.nationality, c.name as country_name, c.id as country_id FROM import.personnes i JOIN country c ON i.nationality1::jsonb = c.name::jsonb;
-- 23. Prepare personnes countryofbirth
ALTER TABLE import.personnes ADD COLUMN countryofbirth1 jsonb;
UPDATE import.personnes SET countryofbirth1=json_build_object('fr', trim(countryofbirth)) WHERE countryofbirth!='';
-- SELECT i.countryofbirth, c.name as country_name, c.id as country_id FROM import.personnes i JOIN country c ON i.countryofbirth1::jsonb = c.name::jsonb;
-- 24. Prepare personnes maritalstatus
ALTER TABLE import.personnes ADD COLUMN maritalstatus1 jsonb;
UPDATE import.personnes SET maritalstatus1=json_build_object('fr', trim(maritalstatus)) WHERE maritalstatus!='';
-- SELECT i.maritalstatus, ms.name as maritalstatus_name, ms.id as maritalstatus_id FROM import.personnes i JOIN chill_person_marital_status ms ON i.maritalstatus1::jsonb = ms.name::jsonb;
-- 25. Prepare personnes numberofchildren
ALTER TABLE import.personnes ADD COLUMN numberofchildren1 integer;
UPDATE import.personnes SET numberofchildren1=to_number(trim(numberofchildren::text) ) WHERE numberofchildren!=''; -- <== case BOUM
-- 26. Prepare personnes household_composition_type
ALTER TABLE import.personnes ADD COLUMN household_composition_type1 JSONB;
UPDATE import.personnes SET household_composition_type1=json_build_object('fr', trim(household_composition_type)) WHERE household_composition_type!='';
-- SELECT i.household_composition_type, cphct.id as household_compo_type_id, cphct.label as household_compo_type_label FROM import.personnes i JOIN chill_person_household_composition_type cphct ON i.household_composition_type1::jsonb = cphct.label::jsonb;
-- 27. Prepare personnes household_position
ALTER TABLE import.personnes ADD COLUMN household_position1 jsonb;
UPDATE import.personnes SET household_position1=json_build_object('fr', trim(household_position)) WHERE household_position!='';
-- SELECT i.household_position, hp.label as household_position_label, hp.id as household_position_id FROM import.personnes i JOIN chill_person_household_position hp ON i.household_position1::jsonb = hp.label::jsonb;
-- 28. Prepare periodes closingmotive
ALTER TABLE import.periodes ADD COLUMN closingmotive1 JSONB;
UPDATE import.periodes SET closingmotive1=json_build_object('fr', trim(closingmotive)) WHERE closingmotive!='';
-- SELECT i.closingmotive1, cm.id as closingmotive_id, cm.name as closingmotive_name FROM import.periodes i JOIN chill_person_accompanying_period_closingmotive cm ON i.closingmotive1::jsonb = cm.name::jsonb;
-- 29. Prepare periodes origin
ALTER TABLE import.periodes ADD COLUMN origin1 jsonb;
UPDATE import.periodes SET origin1=json_build_object('fr', trim(origin)) WHERE origin!='';
-- SELECT i.origin1, o.id as origin_id, o.label as origin_label FROM import.periodes i JOIN chill_person_accompanying_period_origin o ON i.origin1::jsonb = o.label::jsonb;
-- 30. Prepare periodes job
ALTER TABLE import.periodes ADD COLUMN job1 jsonb;
UPDATE import.periodes SET job1=json_build_object('fr', trim(job)) WHERE job!='';
-- SELECT i.job1, o.id as job_id, o.label as job_label FROM import.periodes i JOIN chill_main_user_job o ON i.job1::jsonb = o.label::jsonb;
-- 31. Prepare periodes acp_scopes
ALTER TABLE import.periodes ADD COLUMN acp_scopes1 jsonb;
UPDATE import.periodes SET acp_scopes1=json_build_object('fr', trim(acp_scopes)) WHERE acp_scopes!='';
-- SELECT i.acp_scopes1, s.id as scopes_id, s.name as scopes_name FROM import.periodes i JOIN scopes s ON i.acp_scopes1::jsonb = s.name::jsonb;
-- 32. Prepare periodes intensity
ALTER TABLE import.periodes ADD COLUMN intensity1 text;
UPDATE import.periodes SET intensity1='occasional'; UPDATE import.periodes SET intensity1='regular' WHERE intensity='regular';
-- 33. Prepare required default dates
-- address_validfrom1 | household_startdate1
-- acp_openingdate1
-- 34. Format phone numbers
-- phonenumber | mobilenumber
-- 40. insert in chill_person_person
INSERT INTO chill_person_person (
id
, nationality_id, countryofbirth_id, civility_id, maritalstatus_id
, firstname, lastname
, birthdate, place_of_birth
, memo, email, contactinfo
, phonenumber, mobilenumber
, numberofchildren
, gender
, deathdate
/*
maritalstatusdate
acceptsms
acceptemail
gendercomment_comment
gendercomment_userid
gendercomment_date
maritalstatuscomment_comment
maritalstatuscomment_userid
maritalstatuscomment_date
createdat
updatedat
createdby_id
updatedby_id
center_id
*/
) SELECT
id -- id
, (SELECT c.id as country_id FROM import.personnes i JOIN country c ON i.nationality1::jsonb = c.name::jsonb) -- nationality_id,
, (SELECT c.id as country_id FROM import.personnes i JOIN country c ON i.countryofbirth1::jsonb = c.name::jsonb) -- countryofbirth_id,
, (SELECT c.id as civility_id FROM import.personnes i JOIN chill_main_civility c ON i.civility1::jsonb = c.name::jsonb) -- civility_id
, (SELECT ms.id as maritalstatus_id FROM import.personnes i JOIN chill_person_marital_status ms ON i.maritalstatus1::jsonb = ms.name::jsonb) -- maritalstatus_id,
, firstname, lastname -- firstname, -- lastname,
, birthdate1, place_of_birth -- birthdate, -- place_of_birth,
, memo, email, contactinfo -- memo, -- email, -- contactinfo,
, phonenumber , mobilenumber -- phonenumber, -- mobilenumber,
, numberofchildren1 -- numberofchildren,
, gender1 -- gender,
, deathdate1 -- deathdate,
FROM import.personnes;
/*
-- maritalstatusdate,
-- acceptsms,
-- acceptemail,
-- gendercomment_comment,
-- gendercomment_userid,
-- gendercomment_date,
-- maritalstatuscomment_comment,
-- maritalstatuscomment_userid,
-- maritalstatuscomment_date,
-- createdat,
-- updatedat,
-- createdby_id,
-- updatedby_id,
-- center_id,
*/
-- lier civility,
-- country, adresse,
-- ajouter dans chill_person_accompanying_course
--
-- DOWN
--
-- Undo 32.
ALTER TABLE import.periodes DROP COLUMN intensity1;
-- Undo 31.
ALTER TABLE import.periodes DROP COLUMN acp_scopes1;
-- Undo 30.
ALTER TABLE import.periodes DROP COLUMN job1;
-- Undo 29.
ALTER TABLE import.periodes DROP COLUMN origin1;
-- Undo 28.
ALTER TABLE import.periodes DROP COLUMN closingmotive1;
-- Undo 27.
ALTER TABLE import.personnes DROP COLUMN household_position1;
-- Undo 26.
ALTER TABLE import.personnes DROP COLUMN household_composition_type1;
-- Undo 25.
ALTER TABLE import.personnes DROP COLUMN numberofchildren1;
-- Undo 24.
ALTER TABLE import.personnes DROP COLUMN maritalstatus1;
-- Undo 23.
ALTER TABLE import.personnes DROP COLUMN countryofbirth1;
-- Undo 22.
ALTER TABLE import.personnes DROP COLUMN nationality1;
-- Undo 21.
ALTER TABLE import.personnes DROP COLUMN gender1;
-- Undo 20.
ALTER TABLE import.personnes DROP COLUMN civility1;
-- Undo 14.
-- Undo 13.
@ -211,13 +390,6 @@ ALTER TABLE import.periodes DROP COLUMN closingdate1;
-- -------------
-- personnes choices_list: gender
-- periodes choices_list: intensity
-- tiers choices_list: civility kind profession category
--
-- SELECT
--
SELECT DISTINCT civility FROM import.personnes;
SELECT * FROM import.periodes;