13. Complete chill_person_social_issue table

This commit is contained in:
Mathieu Jaumotte 2023-02-17 19:15:55 +01:00
parent e14f6896e3
commit cb12f3000b

View File

@ -112,11 +112,43 @@ INSERT INTO users (id, username, password, enabled, locked, attributes, label)
FROM ( SELECT DISTINCT ON (referrer) referrer FROM import.choix_periodes WHERE referrer <> '') t 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 ); WHERE NOT EXISTS ( SELECT 1 FROM users WHERE username::text = t.referrer );
-- 13. Complete Social Issues
ALTER TABLE import.choix_periodes ADD COLUMN parent1 jsonb;
UPDATE import.choix_periodes SET parent1=json_build_object('fr', upper(trim(parent))) WHERE parent !='';
ALTER TABLE import.choix_periodes ADD COLUMN enfant1 jsonb;
UPDATE import.choix_periodes SET enfant1=json_build_object('fr', upper(trim(enfant))) WHERE enfant !='';
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'),
null, t.parent1,
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 );
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,
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));
-- --
-- DOWN -- DOWN
-- --
-- Undo 13.
DELETE FROM chill_person_social_issue USING import.choix_periodes i
WHERE parent_id IN ( SELECT id FROM chill_person_social_issue cpsi WHERE cpsi.title::jsonb = i.parent1::jsonb AND cpsi.parent_id IS NULL );
DELETE FROM chill_person_social_issue cpsi USING import.choix_periodes icp WHERE cpsi.title::jsonb = icp.parent1::jsonb AND cpsi.parent_id IS NULL ;
SELECT setval('chill_person_social_issue_id_seq', (SELECT COALESCE(max(id),1) FROM chill_person_social_issue));
ALTER TABLE import.choix_periodes DROP COLUMN enfant1;
ALTER TABLE import.choix_periodes DROP COLUMN parent1;
-- Undo 12. -- Undo 12.
DELETE FROM users USING import.choix_periodes icp WHERE users.username::text = icp.referrer::text AND users.password = '' AND users.enabled = false; DELETE FROM users USING import.choix_periodes icp WHERE users.username::text = icp.referrer::text AND users.password = '' AND users.enabled = false;
SELECT setval('users_id_seq', (SELECT COALESCE(max(id),1) FROM users)); SELECT setval('users_id_seq', (SELECT COALESCE(max(id),1) FROM users));