github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/server/migration/organismes_dons.sql (about) 1 BEGIN; 2 CREATE TABLE organismes ( 3 id serial PRIMARY KEY, 4 nom varchar NOT NULL, 5 contact_propre boolean NOT NULL, 6 contact jsonb NOT NULL CONSTRAINT contact_structgen_validate_json_struct_4151713969 CHECK (structgen_validate_json_struct_4151713969 (contact)), 7 id_contact integer, 8 id_contact_don integer, 9 exemplaires jsonb NOT NULL CONSTRAINT exemplaires_structgen_validate_json_struct_2953253573 CHECK (structgen_validate_json_struct_2953253573 (exemplaires)) 10 ); 11 CREATE TABLE don_donateurs ( 12 id_don integer NOT NULL, 13 id_personne integer, 14 id_organisme integer 15 ); 16 ALTER TABLE don_donateurs 17 ADD FOREIGN KEY (id_don) REFERENCES dons ON DELETE CASCADE; 18 ALTER TABLE don_donateurs 19 ADD FOREIGN KEY (id_personne) REFERENCES personnes; 20 ALTER TABLE don_donateurs 21 ADD FOREIGN KEY (id_organisme) REFERENCES organismes; 22 ALTER TABLE don_donateurs 23 ADD CHECK (id_personne IS NOT NULL 24 OR id_organisme IS NOT NULL); 25 ALTER TABLE don_donateurs 26 ADD CHECK (id_personne IS NULL 27 OR id_organisme IS NULL); 28 ALTER TABLE organismes 29 ADD FOREIGN KEY (id_contact) REFERENCES personnes; 30 ALTER TABLE organismes 31 ADD FOREIGN KEY (id_contact_don) REFERENCES personnes; 32 ALTER TABLE organismes 33 ADD CHECK (contact_propre <> (id_contact IS NOT NULL)); 34 -- import des don_donateurs existants 35 INSERT INTO don_donateurs (id_don, id_personne) 36 SELECT 37 id, 38 id_personne 39 FROM 40 dons 41 WHERE 42 id_personne IS NOT NULL; 43 -- transfert des organismes actuellement sous forme de personnes 44 -- attention aux cas particuliers 45 46 WITH deleted AS ( 47 DELETE FROM personnes 48 WHERE (trim(prenom) = '' 49 AND contact_groupe -> 'pub_ete' -> 'nb_exemplaires' IS NOT NULL) 50 RETURNING 51 *) 52 INSERT INTO organismes (nom, exemplaires, contact_propre, contact) 53 SELECT 54 regexp_replace(nom, '\s+$', ''), 55 jsonb_build_object('pub_ete', coalesce((contact_groupe -> 'pub_ete' -> 'nb_exemplaires')::int, 0), 'pub_hiver', coalesce((contact_groupe -> 'pub_hiver' -> 'nb_exemplaires')::int, 0), 'echo_rocher', coalesce((contact_groupe -> 'echo_rocher' -> 'nb_exemplaires')::int, 0)), 56 TRUE, 57 jsonb_build_object('tels', tels, 'mail', mail, 'adresse', adresse, 'code_postal', code_postal, 'ville', ville, 'pays', pays) 58 FROM 59 deleted; 60 -- transfert des groupe de contacts 61 CREATE OR REPLACE FUNCTION convert_contact_name (nom varchar) 62 RETURNS varchar 63 AS $f$ 64 DECLARE 65 trimmed varchar; 66 BEGIN 67 trimmed = trim(regexp_replace(nom, '^Contact ', '')); 68 RETURN upper(substring(trimmed FROM 1 FOR 1)) || substring(trimmed FROM 2 FOR length(trimmed)); 69 END; 70 $f$ 71 LANGUAGE 'plpgsql' 72 IMMUTABLE; 73 INSERT INTO organismes (nom, exemplaires, contact_propre, id_contact, contact) 74 SELECT 75 CASE WHEN convert_contact_name (contact_groupe -> 'pub_ete' ->> 'label') IS NOT NULL THEN 76 convert_contact_name (contact_groupe -> 'pub_ete' ->> 'label') 77 ELSE 78 'Contact ' || nom || ' ' || prenom 79 END, 80 jsonb_build_object('pub_ete', coalesce((contact_groupe -> 'pub_ete' -> 'nb_exemplaires')::int, 0), 'pub_hiver', coalesce((contact_groupe -> 'pub_hiver' -> 'nb_exemplaires')::int, 0), 'echo_rocher', coalesce((contact_groupe -> 'echo_rocher' -> 'nb_exemplaires')::int, 0)), 81 FALSE, 82 id, 83 '{}' 84 FROM 85 personnes 86 WHERE 87 trim(prenom) <> '' 88 AND ((contact_groupe -> 'pub_ete' -> 'nb_exemplaires')::int > 0 89 OR (contact_groupe -> 'pub_hiver' -> 'nb_exemplaires')::int > 0 90 OR (contact_groupe -> 'echo_rocher' -> 'nb_exemplaires')::int > 0); 91 -- suppression des champs non utilisés 92 ALTER TABLE dons 93 DROP COLUMN id_personne; 94 ALTER TABLE personnes 95 DROP COLUMN contact_groupe; 96 -- ajout des nouveaux champs 97 ALTER TABLE dons 98 ADD COLUMN numero varchar; 99 UPDATE 100 dons 101 SET 102 numero = ''; 103 ALTER TABLE dons 104 ALTER COLUMN numero SET NOT NULL; 105 ALTER TABLE dons 106 ADD COLUMN affectation varchar; 107 UPDATE 108 dons 109 SET 110 affectation = ''; 111 ALTER TABLE dons 112 ALTER COLUMN affectation SET NOT NULL; 113 -- cleanup 114 DROP FUNCTION convert_contact_name; 115 COMMIT; 116 117 -- réduction des infos hello asso 118 UPDATE 119 dons 120 SET 121 infos = jsonb_build_object('id_paiement_hello_asso', infos -> 'HelloAsso' -> 'id'); 122