From 44d24155a1a8841bcdc7e73e0e223107a80ecc70 Mon Sep 17 00:00:00 2001 From: Mathieu Jaumotte Date: Thu, 16 Feb 2023 18:28:36 +0100 Subject: [PATCH] 8. complete chill_person_accompanying_period_closingmotive table --- sql/import.sql | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/sql/import.sql b/sql/import.sql index bb34096..f7285bc 100644 --- a/sql/import.sql +++ b/sql/import.sql @@ -70,10 +70,29 @@ INSERT INTO chill_person_household_position (id, label, sharehousehold, allowhol CROSS JOIN max_ordering WHERE NOT EXISTS ( SELECT 1 FROM chill_person_household_position WHERE label::jsonb = t.household_position1 ); +-- 8. Complete closingmotive +ALTER TABLE import.choix_periodes ADD COLUMN closingmotive1 JSONB; +UPDATE import.choix_periodes SET closingmotive1=json_build_object('fr', trim(closingmotive)) WHERE closingmotive!=''; +WITH max_ordering AS ( SELECT MAX(ordering) as max_ordering FROM chill_person_accompanying_period_closingmotive ) +INSERT INTO chill_person_accompanying_period_closingmotive (id, name, active, parent_id, ordering) + SELECT nextval('chill_person_accompanying_period_closingmotive_id_seq'), + t.closingmotive1, true, null, + 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 ); + + -- -- DOWN -- + +-- Undo 8. +DELETE FROM chill_person_accompanying_period_closingmotive cpapcm USING import.choix_periodes icp WHERE cpapcm.name::jsonb = icp.closingmotive1::jsonb; +SELECT setval('chill_person_accompanying_period_closingmotive_id_seq', (SELECT max(id) FROM chill_person_accompanying_period_closingmotive)); +ALTER TABLE import.choix_periodes DROP COLUMN closingmotive1; + -- Undo 7. DELETE FROM chill_person_household_position cphp USING import.choix_personnes icp WHERE cphp.label::jsonb = icp.household_position1::jsonb; SELECT setval('chill_person_household_position_id_seq', (SELECT max(id) FROM chill_person_household_position));