From 3978ee8b1a19a9272b8379e3adf5447b905e2280 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Wed, 8 Mar 2023 21:05:06 +0100 Subject: [PATCH] Fix WHERE jsonb comparison --- sql/import.sql | 36 +++++++++++++++++++----------------- 1 file changed, 19 insertions(+), 17 deletions(-) diff --git a/sql/import.sql b/sql/import.sql index 0b51b74..4a68df9 100644 --- a/sql/import.sql +++ b/sql/import.sql @@ -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,17 +127,17 @@ 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) @@ -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