8. complete chill_person_accompanying_period_closingmotive table

This commit is contained in:
Mathieu Jaumotte 2023-02-16 18:28:36 +01:00
parent 32da5540fe
commit 44d24155a1
1 changed files with 19 additions and 0 deletions

View File

@ -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));