github.com/benoitkugler/goacve@v0.0.0-20201217100549-151ce6e55dc8/server/migration/v2_to_v3/main.sql (about) 1 -- on commence par déplacer les tables existantes 2 ALTER TABLE 3 personnes RENAME TO old_personnes; 4 5 ALTER TABLE 6 aides RENAME TO old_aides; 7 8 ALTER TABLE 9 camps RENAME TO old_camps; 10 11 ALTER TABLE 12 documents RENAME TO old_documents; 13 14 ALTER TABLE 15 dons RENAME TO old_dons; 16 17 ALTER TABLE 18 factures RENAME TO old_factures; 19 20 ALTER TABLE 21 inscriptions RENAME TO old_inscriptions; 22 23 ALTER TABLE 24 paiements RENAME TO old_paiements; 25 26 ALTER TABLE 27 participants RENAME TO old_participants; 28 29 ALTER TABLE 30 structures_aides RENAME TO old_structures_aides; 31 32 ALTER TABLE 33 users RENAME TO old_users; 34 -- DO NOT EDIT - autogenerated by structgen 35 36 37 CREATE TABLE aides ( 38 id serial PRIMARY KEY, 39 id_structureaide integer NOT NULL, 40 id_participant integer NOT NULL, 41 valeur real NOT NULL, 42 valide boolean NOT NULL, 43 par_jour boolean NOT NULL, 44 nb_jours_max integer NOT NULL 45 ); 46 47 48 CREATE TABLE camps ( 49 id serial PRIMARY KEY, 50 lieu varchar NOT NULL, 51 nom varchar NOT NULL, 52 prix real NOT NULL, 53 nb_places integer NOT NULL, 54 password varchar NOT NULL, 55 ouvert boolean NOT NULL, 56 nb_places_reservees integer NOT NULL, 57 numero_js varchar NOT NULL, 58 need_equilibre_gf boolean NOT NULL, 59 age_min integer NOT NULL, 60 age_max integer NOT NULL, 61 options jsonb NOT NULL, 62 date_debut date NOT NULL, 63 date_fin date NOT NULL, 64 liste_vetements jsonb NOT NULL, 65 schema_paiement varchar NOT NULL CHECK (schema_paiement IN ('acompte', 'total')), 66 joomeo_album_id varchar NOT NULL, 67 envois jsonb NOT NULL, 68 lien_compta varchar NOT NULL, 69 option_prix jsonb NOT NULL, 70 inscription_simple boolean NOT NULL 71 ); 72 73 74 CREATE TABLE camp_contraintes ( 75 id_camp integer NOT NULL, 76 id_contrainte integer NOT NULL 77 ); 78 79 80 CREATE TABLE contenu_documents ( 81 id_document integer NOT NULL, 82 contenu bytea, 83 miniature bytea 84 ); 85 86 87 CREATE TABLE contraintes ( 88 id serial PRIMARY KEY, 89 id_personne integer, 90 id_document integer, 91 builtin varchar NOT NULL CHECK (builtin IN ('autre', 'bafa', 'bafa_equiv', 'bafd', 'bafd_equiv', 'carte_id', 'carte_vitale', 'cert_med_cuisine', 'haccp', '', 'permis', 'sb', 'scolarite', 'secour', 'test_nautique', 'vaccin')), 92 nom varchar NOT NULL, 93 description varchar NOT NULL, 94 max_docs integer NOT NULL, 95 jours_valide integer NOT NULL 96 ); 97 98 99 CREATE TABLE documents ( 100 id serial PRIMARY KEY, 101 taille integer NOT NULL, 102 nom_client varchar NOT NULL, 103 description varchar NOT NULL, 104 date_heure_modif timestamp (0) with time zone 105 ); 106 107 108 CREATE TABLE document_aides ( 109 id_document integer NOT NULL, 110 id_aide integer NOT NULL 111 ); 112 113 114 CREATE TABLE document_camps ( 115 id_document integer NOT NULL, 116 id_camp integer NOT NULL, 117 is_lettre boolean NOT NULL 118 ); 119 120 121 CREATE TABLE document_personnes ( 122 id_document integer NOT NULL, 123 id_personne integer NOT NULL, 124 id_contrainte integer NOT NULL 125 ); 126 127 128 CREATE TABLE dons ( 129 id serial PRIMARY KEY, 130 id_personne integer, 131 valeur real NOT NULL, 132 mode_paiement varchar NOT NULL CHECK (mode_paiement IN ('ancv', '', 'cb', 'cheque', 'esp', 'helloasso', 'vir')), 133 date_reception date NOT NULL, 134 date_merci date NOT NULL, 135 personne_merci varchar NOT NULL, 136 recu_emis date NOT NULL, 137 infos jsonb NOT NULL 138 ); 139 140 141 CREATE TABLE equipiers ( 142 id serial PRIMARY KEY, 143 id_camp integer NOT NULL, 144 id_personne integer NOT NULL, 145 roles varchar[], 146 diplome varchar NOT NULL CHECK (diplome IN ('agreg', 'ass_sociale', '', 'bafa', 'bafa_stag', 'bafd', 'bafd_stag', 'bapaat', 'beatep', 'bjeps', 'cap', 'deug', 'dut', 'educ_spe', 'eje', 'instit', 'mon_educ', 'prof', 'staps', 'zzautre')), 147 appro varchar NOT NULL CHECK (appro IN ('', 'autre', 'canoe', 'moto', 'sb', 'voile')), 148 presence jsonb NOT NULL, 149 invitation_equipier integer NOT NULL, 150 charte integer NOT NULL CHECK (charte IN (-1, 1, 0)) 151 ); 152 153 154 CREATE TABLE equipier_contraintes ( 155 id_equipier integer NOT NULL, 156 id_contrainte integer NOT NULL, 157 optionnel boolean NOT NULL 158 ); 159 160 161 CREATE TABLE factures ( 162 id serial PRIMARY KEY, 163 id_personne integer NOT NULL, 164 destinataires_optionnels jsonb, 165 key varchar NOT NULL, 166 copies_mails varchar[], 167 last_connection timestamp (0) with time zone, 168 is_confirmed boolean NOT NULL, 169 is_validated boolean NOT NULL 170 ); 171 172 173 CREATE TABLE groupes ( 174 id serial PRIMARY KEY, 175 id_camp integer NOT NULL, 176 nom varchar NOT NULL, 177 plage jsonb NOT NULL, 178 couleur varchar NOT NULL, 179 isSimple boolean NOT NULL 180 ); 181 182 183 CREATE TABLE groupe_contraintes ( 184 id_groupe integer NOT NULL, 185 id_contrainte integer NOT NULL 186 ); 187 188 189 CREATE TABLE groupe_equipiers ( 190 id_groupe integer NOT NULL, 191 id_equipier integer NOT NULL, 192 id_camp integer NOT NULL 193 ); 194 195 196 CREATE TABLE groupe_participants ( 197 id_participant integer NOT NULL, 198 id_groupe integer NOT NULL, 199 id_camp integer NOT NULL, 200 manuel boolean NOT NULL 201 ); 202 203 204 CREATE TABLE imageuploadeds ( 205 id_camp integer NOT NULL, 206 filename varchar NOT NULL, 207 lien varchar NOT NULL, 208 content bytea 209 ); 210 211 212 CREATE TABLE inscriptions ( 213 id serial PRIMARY KEY, 214 info varchar NOT NULL, 215 date_heure timestamp (0) with time zone, 216 copies_mails varchar[], 217 responsable jsonb NOT NULL, 218 participants jsonb 219 ); 220 221 222 CREATE TABLE lettredirecteurs ( 223 id_camp integer NOT NULL, 224 html varchar NOT NULL, 225 use_coord_centre boolean NOT NULL, 226 show_adresse_postale boolean NOT NULL, 227 color_coord varchar NOT NULL 228 ); 229 230 231 CREATE TABLE messages ( 232 id serial PRIMARY KEY, 233 id_facture integer NOT NULL, 234 kind integer NOT NULL CHECK (kind IN (3, 7, 2, 5, 4, 6, 9, 11, 10, 1, 8, 0)), 235 created timestamp (0) with time zone, 236 modified timestamp (0) with time zone, 237 vu boolean NOT NULL 238 ); 239 240 241 CREATE TABLE message_attestations ( 242 id_message integer NOT NULL, 243 distribution integer NOT NULL CHECK (distribution IN (0, 1, 2)), 244 guard_kind integer NOT NULL CHECK (guard_kind IN (3, 7, 2, 5, 4, 6, 9, 11, 10, 1, 8, 0)) 245 ); 246 247 248 CREATE TABLE message_documents ( 249 id_message integer NOT NULL, 250 id_camp integer NOT NULL, 251 guardKind integer NOT NULL CHECK (guardKind IN (3, 7, 2, 5, 4, 6, 9, 11, 10, 1, 8, 0)) 252 ); 253 254 255 CREATE TABLE message_messages ( 256 id_message integer NOT NULL, 257 contenu varchar NOT NULL, 258 guard_kind integer NOT NULL CHECK (guard_kind IN (3, 7, 2, 5, 4, 6, 9, 11, 10, 1, 8, 0)) 259 ); 260 261 262 CREATE TABLE message_placeliberes ( 263 id_message integer NOT NULL, 264 id_participant integer NOT NULL, 265 guardKind integer NOT NULL CHECK (guardKind IN (3, 7, 2, 5, 4, 6, 9, 11, 10, 1, 8, 0)) 266 ); 267 268 269 CREATE TABLE message_sondages ( 270 id_message integer NOT NULL, 271 id_camp integer NOT NULL, 272 guardKind integer NOT NULL CHECK (guardKind IN (3, 7, 2, 5, 4, 6, 9, 11, 10, 1, 8, 0)), 273 isSimple boolean NOT NULL 274 ); 275 276 277 CREATE TABLE paiements ( 278 id serial PRIMARY KEY, 279 id_facture integer NOT NULL, 280 is_acompte boolean NOT NULL, 281 is_remboursement boolean NOT NULL, 282 in_bordereau timestamp (0) with time zone, 283 label_payeur varchar NOT NULL, 284 nom_banque varchar NOT NULL, 285 mode_paiement varchar NOT NULL CHECK (mode_paiement IN ('ancv', '', 'cb', 'cheque', 'esp', 'helloasso', 'vir')), 286 numero varchar NOT NULL, 287 valeur real NOT NULL, 288 is_invalide boolean NOT NULL, 289 date_reglement timestamp (0) with time zone, 290 details varchar NOT NULL 291 ); 292 293 294 CREATE TABLE participants ( 295 id serial PRIMARY KEY, 296 id_camp integer NOT NULL, 297 id_personne integer NOT NULL, 298 id_facture integer, 299 liste_attente jsonb NOT NULL, 300 remises jsonb NOT NULL, 301 option_prix jsonb NOT NULL, 302 options jsonb NOT NULL, 303 date_heure timestamp (0) with time zone, 304 isSimple boolean NOT NULL 305 ); 306 307 308 CREATE TABLE participant_equipiers ( 309 id_participant integer NOT NULL, 310 id_equipier integer NOT NULL, 311 id_groupe integer NOT NULL 312 ); 313 314 315 CREATE TABLE participantsimples ( 316 id serial PRIMARY KEY, 317 id_personne integer NOT NULL, 318 id_camp integer NOT NULL, 319 date_heure timestamp (0) with time zone, 320 info varchar NOT NULL, 321 isSimple boolean NOT NULL 322 ); 323 324 325 CREATE TABLE personnes ( 326 id serial PRIMARY KEY, 327 nom varchar NOT NULL, 328 nom_jeune_fille varchar NOT NULL, 329 prenom varchar NOT NULL, 330 date_naissance date NOT NULL, 331 ville_naissance varchar NOT NULL, 332 departement_naissance varchar NOT NULL, 333 sexe varchar NOT NULL CHECK (sexe IN ('', 'F', 'M')), 334 tels varchar[], 335 mail varchar NOT NULL, 336 adresse varchar NOT NULL, 337 code_postal varchar NOT NULL, 338 ville varchar NOT NULL, 339 pays varchar NOT NULL, 340 securite_sociale varchar NOT NULL, 341 profession varchar NOT NULL, 342 etudiant boolean NOT NULL, 343 fonctionnaire boolean NOT NULL, 344 version_papier boolean NOT NULL, 345 pub_hiver boolean NOT NULL, 346 pub_ete boolean NOT NULL, 347 echo_rocher boolean NOT NULL, 348 rang_membre_asso varchar NOT NULL CHECK (rang_membre_asso IN ('3', '2', '1', '')), 349 quotient_familial integer NOT NULL, 350 cotisation integer[], 351 eonews boolean NOT NULL, 352 contact_groupe jsonb NOT NULL, 353 fiche_sanitaire jsonb NOT NULL, 354 is_temporaire boolean NOT NULL 355 ); 356 357 358 CREATE TABLE sondages ( 359 id serial PRIMARY KEY, 360 id_camp integer NOT NULL, 361 id_facture integer NOT NULL, 362 modified timestamp (0) with time zone, 363 infos_avant_sejour integer NOT NULL CHECK (infos_avant_sejour IN (1, 2, 3, 4, 0)), 364 infos_pendant_sejour integer NOT NULL CHECK (infos_pendant_sejour IN (1, 2, 3, 4, 0)), 365 hebergement integer NOT NULL CHECK (hebergement IN (1, 2, 3, 4, 0)), 366 activites integer NOT NULL CHECK (activites IN (1, 2, 3, 4, 0)), 367 theme integer NOT NULL CHECK (theme IN (1, 2, 3, 4, 0)), 368 nourriture integer NOT NULL CHECK (nourriture IN (1, 2, 3, 4, 0)), 369 hygiene integer NOT NULL CHECK (hygiene IN (1, 2, 3, 4, 0)), 370 ambiance integer NOT NULL CHECK (ambiance IN (1, 2, 3, 4, 0)), 371 ressenti integer NOT NULL CHECK (ressenti IN (1, 2, 3, 4, 0)), 372 message_enfant varchar NOT NULL, 373 message_responsable varchar NOT NULL 374 ); 375 376 377 CREATE TABLE structureaides ( 378 id serial PRIMARY KEY, 379 nom varchar NOT NULL, 380 immatriculation varchar NOT NULL, 381 adresse varchar NOT NULL, 382 code_postal varchar NOT NULL, 383 ville varchar NOT NULL, 384 telephone varchar NOT NULL, 385 info varchar NOT NULL 386 ); 387 388 389 CREATE TABLE users ( 390 id serial PRIMARY KEY, 391 label varchar NOT NULL, 392 mdp varchar NOT NULL, 393 is_admin boolean NOT NULL, 394 modules jsonb NOT NULL 395 ); 396 397 ALTER TABLE aides ADD FOREIGN KEY(id_structureaide) REFERENCES structureaides ; 398 ALTER TABLE aides ADD FOREIGN KEY(id_participant) REFERENCES participants ON DELETE CASCADE; 399 ALTER TABLE camp_contraintes ADD FOREIGN KEY(id_camp) REFERENCES camps ON DELETE CASCADE; 400 ALTER TABLE camp_contraintes ADD FOREIGN KEY(id_contrainte) REFERENCES contraintes ; 401 ALTER TABLE contenu_documents ADD FOREIGN KEY(id_document) REFERENCES documents ON DELETE CASCADE; 402 ALTER TABLE contraintes ADD FOREIGN KEY(id_personne) REFERENCES personnes ON DELETE CASCADE; 403 ALTER TABLE contraintes ADD FOREIGN KEY(id_document) REFERENCES documents ; 404 ALTER TABLE document_aides ADD FOREIGN KEY(id_document) REFERENCES documents ON DELETE CASCADE; 405 ALTER TABLE document_aides ADD FOREIGN KEY(id_aide) REFERENCES aides ; 406 ALTER TABLE document_camps ADD FOREIGN KEY(id_document) REFERENCES documents ON DELETE CASCADE; 407 ALTER TABLE document_camps ADD FOREIGN KEY(id_camp) REFERENCES camps ; 408 ALTER TABLE document_personnes ADD FOREIGN KEY(id_document) REFERENCES documents ON DELETE CASCADE; 409 ALTER TABLE document_personnes ADD FOREIGN KEY(id_personne) REFERENCES personnes ; 410 ALTER TABLE document_personnes ADD FOREIGN KEY(id_contrainte) REFERENCES contraintes ; 411 ALTER TABLE dons ADD FOREIGN KEY(id_personne) REFERENCES personnes ON DELETE CASCADE; 412 ALTER TABLE equipiers ADD FOREIGN KEY(id_camp) REFERENCES camps ; 413 ALTER TABLE equipiers ADD FOREIGN KEY(id_personne) REFERENCES personnes ON DELETE CASCADE; 414 ALTER TABLE equipier_contraintes ADD FOREIGN KEY(id_equipier) REFERENCES equipiers ON DELETE CASCADE; 415 ALTER TABLE equipier_contraintes ADD FOREIGN KEY(id_contrainte) REFERENCES contraintes ; 416 ALTER TABLE factures ADD FOREIGN KEY(id_personne) REFERENCES personnes ; 417 ALTER TABLE groupes ADD FOREIGN KEY(id_camp) REFERENCES camps ON DELETE CASCADE; 418 ALTER TABLE groupe_contraintes ADD FOREIGN KEY(id_groupe) REFERENCES groupes ON DELETE CASCADE; 419 ALTER TABLE groupe_contraintes ADD FOREIGN KEY(id_contrainte) REFERENCES contraintes ; 420 ALTER TABLE groupe_equipiers ADD FOREIGN KEY(id_groupe) REFERENCES groupes ON DELETE CASCADE; 421 ALTER TABLE groupe_equipiers ADD FOREIGN KEY(id_equipier) REFERENCES equipiers ON DELETE CASCADE; 422 ALTER TABLE groupe_equipiers ADD FOREIGN KEY(id_camp) REFERENCES camps ON DELETE CASCADE; 423 ALTER TABLE groupe_participants ADD FOREIGN KEY(id_participant) REFERENCES participants ON DELETE CASCADE; 424 ALTER TABLE groupe_participants ADD FOREIGN KEY(id_groupe) REFERENCES groupes ON DELETE CASCADE; 425 ALTER TABLE groupe_participants ADD FOREIGN KEY(id_camp) REFERENCES camps ; 426 ALTER TABLE imageuploadeds ADD FOREIGN KEY(id_camp) REFERENCES camps ON DELETE CASCADE; 427 ALTER TABLE lettredirecteurs ADD FOREIGN KEY(id_camp) REFERENCES camps ON DELETE CASCADE; 428 ALTER TABLE messages ADD FOREIGN KEY(id_facture) REFERENCES factures ON DELETE CASCADE; 429 ALTER TABLE message_attestations ADD FOREIGN KEY(id_message) REFERENCES messages ON DELETE CASCADE; 430 ALTER TABLE message_documents ADD FOREIGN KEY(id_message) REFERENCES messages ON DELETE CASCADE; 431 ALTER TABLE message_documents ADD FOREIGN KEY(id_camp) REFERENCES camps ; 432 ALTER TABLE message_messages ADD FOREIGN KEY(id_message) REFERENCES messages ON DELETE CASCADE; 433 ALTER TABLE message_placeliberes ADD FOREIGN KEY(id_message) REFERENCES messages ON DELETE CASCADE; 434 ALTER TABLE message_placeliberes ADD FOREIGN KEY(id_participant) REFERENCES participants ; 435 ALTER TABLE message_sondages ADD FOREIGN KEY(id_message) REFERENCES messages ON DELETE CASCADE; 436 ALTER TABLE message_sondages ADD FOREIGN KEY(id_camp) REFERENCES camps ; 437 ALTER TABLE paiements ADD FOREIGN KEY(id_facture) REFERENCES factures ; 438 ALTER TABLE participants ADD FOREIGN KEY(id_camp) REFERENCES camps ; 439 ALTER TABLE participants ADD FOREIGN KEY(id_personne) REFERENCES personnes ON DELETE CASCADE; 440 ALTER TABLE participants ADD FOREIGN KEY(id_facture) REFERENCES factures ON DELETE SET NULL; 441 ALTER TABLE participant_equipiers ADD FOREIGN KEY(id_participant) REFERENCES participants ON DELETE CASCADE; 442 ALTER TABLE participant_equipiers ADD FOREIGN KEY(id_equipier) REFERENCES equipiers ON DELETE CASCADE; 443 ALTER TABLE participant_equipiers ADD FOREIGN KEY(id_groupe) REFERENCES groupes ON DELETE CASCADE; 444 ALTER TABLE participantsimples ADD FOREIGN KEY(id_personne) REFERENCES personnes ON DELETE CASCADE; 445 ALTER TABLE participantsimples ADD FOREIGN KEY(id_camp) REFERENCES camps ; 446 ALTER TABLE sondages ADD FOREIGN KEY(id_camp) REFERENCES camps ON DELETE CASCADE; 447 ALTER TABLE sondages ADD FOREIGN KEY(id_facture) REFERENCES factures ON DELETE CASCADE; 448 ALTER TABLE camps ADD UNIQUE(id,inscription_simple); 449 ALTER TABLE lettredirecteurs ADD UNIQUE(id_camp); 450 ALTER TABLE imageuploadeds ADD UNIQUE(id_camp, lien); 451 ALTER TABLE groupes ADD UNIQUE(id_camp, nom); 452 ALTER TABLE groupes ADD UNIQUE(id, id_camp); 453 ALTER TABLE groupes ALTER COLUMN isSimple SET DEFAULT false; 454 ALTER TABLE groupes ADD CHECK(isSimple = false); 455 ALTER TABLE groupes ADD FOREIGN KEY (id_camp, isSimple) REFERENCES camps(id,inscription_simple); 456 ALTER TABLE sondages ADD UNIQUE(id_camp, id_facture); 457 ALTER TABLE participants ADD UNIQUE(id_personne, id_camp); 458 ALTER TABLE participants ADD UNIQUE(id, id_camp); 459 ALTER TABLE participants ALTER COLUMN isSimple SET DEFAULT false; 460 ALTER TABLE participants ADD CHECK(isSimple = false); 461 ALTER TABLE participants ADD FOREIGN KEY (id_camp, isSimple) REFERENCES camps(id,inscription_simple); 462 ALTER TABLE groupe_participants ADD UNIQUE (id_participant); 463 ALTER TABLE groupe_participants ADD UNIQUE (id_participant, id_groupe); 464 ALTER TABLE groupe_participants ADD FOREIGN KEY (id_participant, id_camp) REFERENCES participants(id,id_camp); 465 ALTER TABLE groupe_participants ADD FOREIGN KEY (id_groupe, id_camp) REFERENCES groupes(id,id_camp); 466 ALTER TABLE equipiers ADD UNIQUE(id_personne, id_camp); 467 ALTER TABLE equipiers ADD UNIQUE(id, id_camp); 468 ALTER TABLE groupe_equipiers ADD UNIQUE(id_groupe, id_equipier); 469 ALTER TABLE groupe_equipiers ADD FOREIGN KEY (id_equipier, id_camp) REFERENCES equipiers(id,id_camp); 470 ALTER TABLE groupe_equipiers ADD FOREIGN KEY (id_groupe, id_camp) REFERENCES groupes(id,id_camp); 471 ALTER TABLE participant_equipiers ADD UNIQUE (id_participant); 472 ALTER TABLE participant_equipiers ADD FOREIGN KEY (id_participant, id_groupe) REFERENCES groupe_participants(id_participant, id_groupe); 473 ALTER TABLE participant_equipiers ADD FOREIGN KEY (id_equipier, id_groupe) REFERENCES groupe_equipiers(id_equipier, id_groupe) ON DELETE CASCADE; 474 ALTER TABLE participantsimples ADD UNIQUE(id_personne, id_camp); 475 ALTER TABLE participantsimples ALTER COLUMN isSimple SET DEFAULT TRUE; 476 ALTER TABLE participantsimples ADD CHECK(isSimple = TRUE); 477 ALTER TABLE participantsimples ADD FOREIGN KEY (id_camp, isSimple) REFERENCES camps(id,inscription_simple); 478 ALTER TABLE contraintes ADD UNIQUE(nom, builtin, id_personne); 479 ALTER TABLE contraintes ADD CHECK(builtin <> '' OR id_personne IS NOT NULL); 480 ALTER TABLE equipier_contraintes ADD UNIQUE(id_equipier, id_contrainte); 481 ALTER TABLE camp_contraintes ADD UNIQUE(id_camp, id_contrainte); 482 ALTER TABLE groupe_contraintes ADD UNIQUE(id_groupe, id_contrainte); 483 ALTER TABLE contenu_documents ADD UNIQUE(id_document); 484 ALTER TABLE document_camps ADD UNIQUE(id_document); 485 ALTER TABLE document_personnes ADD UNIQUE(id_document); 486 ALTER TABLE document_aides ADD UNIQUE(id_document); 487 ALTER TABLE messages ADD UNIQUE(id, kind); 488 CREATE FUNCTION m_responsable() RETURNS int LANGUAGE sql IMMUTABLE PARALLEL SAFE AS 'SELECT 1'; 489 CREATE FUNCTION m_centre() RETURNS int LANGUAGE sql IMMUTABLE PARALLEL SAFE AS 'SELECT 2'; 490 CREATE FUNCTION m_accuse_reception() RETURNS int LANGUAGE sql IMMUTABLE PARALLEL SAFE AS 'SELECT 3'; 491 CREATE FUNCTION m_facture() RETURNS int LANGUAGE sql IMMUTABLE PARALLEL SAFE AS 'SELECT 4'; 492 CREATE FUNCTION m_documents() RETURNS int LANGUAGE sql IMMUTABLE PARALLEL SAFE AS 'SELECT 5'; 493 CREATE FUNCTION m_facture_acquittee() RETURNS int LANGUAGE sql IMMUTABLE PARALLEL SAFE AS 'SELECT 6'; 494 CREATE FUNCTION m_attestation_presence() RETURNS int LANGUAGE sql IMMUTABLE PARALLEL SAFE AS 'SELECT 7'; 495 CREATE FUNCTION m_sondage() RETURNS int LANGUAGE sql IMMUTABLE PARALLEL SAFE AS 'SELECT 8'; 496 CREATE FUNCTION m_inscription() RETURNS int LANGUAGE sql IMMUTABLE PARALLEL SAFE AS 'SELECT 9'; 497 CREATE FUNCTION m_place_liberee() RETURNS int LANGUAGE sql IMMUTABLE PARALLEL SAFE AS 'SELECT 10'; 498 ALTER TABLE message_documents ADD UNIQUE(id_message); 499 ALTER TABLE message_documents ADD CHECK(guardKind = 5); 500 ALTER TABLE message_documents ALTER COLUMN guardKind SET DEFAULT 5; 501 ALTER TABLE message_documents ADD FOREIGN KEY (id_message, guardKind) REFERENCES messages(id,kind); 502 ALTER TABLE message_sondages ADD UNIQUE(id_message); 503 ALTER TABLE message_sondages ADD CHECK(guardKind = 8); 504 ALTER TABLE message_sondages ALTER COLUMN guardKind SET DEFAULT 8; 505 ALTER TABLE message_sondages ADD FOREIGN KEY (id_message, guardKind) REFERENCES messages(id,kind); 506 ALTER TABLE message_sondages ALTER COLUMN isSimple SET DEFAULT FALSE; 507 ALTER TABLE message_sondages ADD CHECK(isSimple = FALSE); 508 ALTER TABLE message_sondages ADD FOREIGN KEY (id_camp, isSimple) REFERENCES camps(id,inscription_simple); 509 ALTER TABLE message_placeliberes ADD UNIQUE(id_message); 510 ALTER TABLE message_placeliberes ADD CHECK(guardKind = 10); 511 ALTER TABLE message_placeliberes ALTER COLUMN guardKind SET DEFAULT 10; 512 ALTER TABLE message_placeliberes ADD FOREIGN KEY (id_message, guardKind) REFERENCES messages(id,kind); 513 ALTER TABLE message_attestations ADD UNIQUE(id_message); 514 ALTER TABLE message_attestations ADD CHECK(guard_kind = 6 OR guard_kind = 7); 515 ALTER TABLE message_attestations ADD FOREIGN KEY (id_message, guard_kind) REFERENCES messages(id,kind); 516 CREATE FUNCTION d_mail() RETURNS int LANGUAGE sql IMMUTABLE PARALLEL SAFE AS 'SELECT 1'; 517 ALTER TABLE message_messages ADD UNIQUE(id_message); 518 ALTER TABLE message_messages ADD CHECK(guard_kind = 1 OR guard_kind = 2); 519 ALTER TABLE message_messages ADD FOREIGN KEY (id_message, guard_kind) REFERENCES messages(id,kind); 520 -- Commandes à lancer pour setup la base de données 521 -- 522 -- Contraintes de document 'built-in' 523 INSERT INTO 524 contraintes ( 525 builtin, 526 nom, 527 max_docs, 528 description, 529 jours_valide 530 ) 531 VALUES 532 ('bafa', 'BAFA', 1, '', 0), 533 ('bafd', 'BAFD', 1, '', 0), 534 ( 535 'carte_id', 536 'Carte d''identité/Passeport', 537 2, 538 '', 539 0 540 ), 541 ('permis', 'Permis de conduire', 2, '', 0), 542 ('sb', 'Surveillant de baignade', 1, '', 0), 543 ('secour', 'Secourisme (PSC1 - AFPS)', 1, '', 0), 544 ('bafd_equiv', 'Equivalent BAFD', 1, '', 0), 545 ('bafa_equiv', 'Equivalent BAFA', 1, '', 0), 546 ('carte_vitale', 'Carte Vitale', 2, '', 0), 547 ( 548 'haccp', 549 'Cuisine (HACCP)', 550 1, 551 'Certificat concernant les normes HACCP, délivré sur place après une formation.', 552 0 553 ), 554 ('scolarite', 'Certificat de scolarité', 1, '', 0), 555 ('autre', 'Autre', 1, '', 0); 556 557 INSERT INTO 558 contraintes ( 559 builtin, 560 nom, 561 max_docs, 562 description, 563 jours_valide 564 ) 565 VALUES 566 ('vaccin', 'Vaccin', 5, '', 0); 567 568 -- le certificat de non contre indication est valable 3 mois 569 INSERT INTO 570 contraintes ( 571 builtin, 572 nom, 573 max_docs, 574 description, 575 jours_valide 576 ) 577 VALUES 578 ( 579 'cert_med_cuisine', 580 'Certificat médical Cuisine', 581 1, 582 'Certificat de non contre-indication à la cuisine de collectivité, à demander à son médecin traitant.', 583 90 584 ); 585 586 -- un administrateur doit uploader le test vierge 587 INSERT INTO 588 contraintes ( 589 builtin, 590 nom, 591 max_docs, 592 description, 593 jours_valide 594 ) 595 VALUES 596 ('test_nautique', 'Test nautique', 1, '', 0);-- script de transition depuis une base de données v2 vers une base de données v3 597 -- seul changement : on abandonne la liste des enfants 598 599 UPDATE 600 old_personnes 601 SET 602 rang_membre_asso = '' 603 WHERE 604 rang_membre_asso = '0'; 605 606 INSERT INTO personnes (id, nom, prenom, sexe, mail, adresse, code_postal, ville, tels, version_papier, pub_hiver, pub_ete, echo_rocher, rang_membre_asso, quotient_familial, pays, ville_naissance, departement_naissance, nom_jeune_fille, date_naissance, profession, etudiant, fonctionnaire, securite_sociale, cotisation, eonews, contact_groupe, fiche_sanitaire, is_temporaire) 607 SELECT 608 id, 609 coalesce(nom, ''), 610 coalesce(prenom, ''), 611 coalesce(sexe, ''), 612 coalesce(mail, ''), 613 coalesce(adresse, ''), 614 coalesce(code_postal, ''), 615 coalesce(ville, ''), 616 coalesce(tels, '{}'), 617 coalesce(version_papier, FALSE), 618 coalesce(pub_hiver, FALSE), 619 coalesce(pub_ete, FALSE), 620 coalesce(echo_rocher, FALSE), 621 coalesce(rang_membre_asso, ''), 622 coalesce(quotient_familial, 0), 623 coalesce(pays, ''), 624 coalesce(ville_naissance, ''), 625 coalesce(departement_naissance, ''), 626 coalesce(nom_jeune_fille, ''), 627 coalesce(date_naissance, '0001-01-01'), 628 coalesce(profession, ''), 629 coalesce(etudiant, FALSE), 630 coalesce(fonctionnaire, FALSE), 631 coalesce(securite_sociale, ''), 632 coalesce(cotisation, '{}'), 633 coalesce(eonews, FALSE), 634 coalesce(contact_groupe, '{}'), 635 coalesce(fiche_sanitaire, '{}'), 636 coalesce(is_temporaire, FALSE) 637 FROM 638 old_personnes; 639 640 -- on supprime les factures sans participants 641 DELETE FROM old_paiements 642 WHERE id_facture IN ( 643 SELECT 644 old_factures.id 645 FROM 646 old_factures 647 WHERE ( 648 SELECT 649 count(*) 650 FROM 651 old_participants 652 WHERE 653 old_factures.id = old_participants.id_facture) = 0); 654 655 DELETE FROM old_factures 656 WHERE ( 657 SELECT 658 count(*) 659 FROM 660 old_participants 661 WHERE 662 old_factures.id = old_participants.id_facture) = 0; 663 664 -- on enregistre une connection fictive 665 -- pour "marquer" comme vu 666 667 INSERT INTO factures (id, id_personne, destinataires_optionnels, KEY, copies_mails, last_connection, is_confirmed, is_validated) 668 SELECT 669 id, 670 id_responsable, 671 CASE WHEN alt_dest ->> 'nom_prenom' <> '' THEN 672 jsonb_build_array(alt_dest - '__actif__' - 'mail') 673 ELSE 674 '[]' 675 END, 676 KEY, 677 coalesce(copies_mails, '{}'), 678 now(), 679 TRUE, 680 TRUE 681 FROM 682 old_factures; 683 684 ALTER TABLE old_camps RENAME COLUMN id_js TO numero_js; 685 686 -- cleanup des groupes liés aux séjours simples 687 UPDATE 688 old_camps 689 SET 690 groupes = NULL 691 WHERE 692 old_camps.inscription_simple = TRUE; 693 694 UPDATE 695 old_camps 696 SET 697 schema_paiement = 'total' 698 WHERE 699 schema_paiement = '' 700 OR schema_paiement IS NULL; 701 702 INSERT INTO camps (id, lieu, nom, prix, nb_places, PASSWORD, ouvert, nb_places_reservees, numero_js, need_equilibre_gf, age_min, age_max, options, date_debut, date_fin, liste_vetements, schema_paiement, joomeo_album_id, envois, lien_compta, option_prix, inscription_simple) 703 SELECT 704 id, 705 lieu, 706 nom, 707 prix, 708 nb_places, 709 PASSWORD, 710 ouvert, 711 nb_places_reservees, 712 coalesce(numero_js, ''), 713 need_equilibre_gf, 714 coalesce(age_min, 0), 715 coalesce(age_max, 0), 716 options, 717 date_debut, 718 date_fin, 719 coalesce(liste_vetements, '{}'), 720 coalesce(schema_paiement, ''), 721 coalesce(joomeo_album_id, ''), 722 envois, 723 coalesce(lien_compta, ''), 724 option_prix, 725 coalesce(inscription_simple, FALSE) 726 FROM 727 old_camps; 728 729 -- on sépare le contenu html de la lettre directeur 730 -- et on utilise toujours les coordonnées du directeur 731 -- pour les camps importés 732 733 INSERT INTO lettredirecteurs (id_camp, html, use_coord_centre, show_adresse_postale, color_coord) 734 SELECT 735 id, 736 coalesce(lettre_directeur ->> 'html', ''), 737 FALSE, 738 FALSE, 739 '' 740 FROM 741 old_camps 742 WHERE 743 coalesce(lettre_directeur ->> 'html', '') <> ''; 744 745 -- on a besoin de lier les anciens aux ids des nouveaux 746 CREATE TABLE tmp_groupes ( 747 id serial, 748 id_camp integer, 749 old_id varchar, 750 nom varchar NOT NULL, 751 plage jsonb NOT NULL, 752 need_aisance_aquatique boolean NOT NULL 753 ); 754 755 -- on transforme les groupes en gardant la correspondances des ids 756 INSERT INTO tmp_groupes (id_camp, old_id, nom, plage, need_aisance_aquatique) 757 SELECT 758 old_camps.id, 759 grs.key, 760 grs.value ->> 'nom', 761 jsonb_build_object('from', grs.value -> 'date_debut', 'to', grs.value -> 'date_fin'), 762 coalesce(grs.value ->> 'need_aisance_aquatique', 'false') = 'true' 763 FROM 764 old_camps 765 JOIN LATERAL jsonb_each(old_camps.groupes) AS grs ON TRUE 766 WHERE ((coalesce(old_camps.inscription_simple, FALSE) = FALSE) 767 AND (old_camps.groupes IS NOT NULL) 768 AND (jsonb_typeof(old_camps.groupes) <> 'null') 769 AND (grs.value ->> 'nom' <> 'null')); 770 771 -- On peut maintenant transférer les groupes vers la table définitive ... 772 INSERT INTO groupes (id, id_camp, nom, plage, couleur) 773 SELECT 774 id, 775 id_camp, 776 nom, 777 plage, 778 '' 779 FROM 780 tmp_groupes; 781 782 -- ... et ajouter les contraintes 'aisance_aquatique' 783 INSERT INTO groupe_contraintes (id_groupe, id_contrainte) 784 SELECT 785 id, 786 ( 787 SELECT 788 id 789 FROM 790 contraintes 791 WHERE 792 builtin = 'test_nautique') 793 FROM 794 tmp_groupes 795 WHERE 796 need_aisance_aquatique; 797 798 -- On copie les demandes de test nautique des séjours 799 INSERT INTO camp_contraintes (id_camp, id_contrainte) 800 SELECT 801 id, 802 ( 803 SELECT 804 id 805 FROM 806 contraintes 807 WHERE 808 builtin = 'test_nautique') 809 FROM 810 old_camps 811 WHERE 812 need_aisance_aquatique; 813 814 -- copie des inscriptions (sans la case autorisation_photos) 815 INSERT INTO inscriptions (info, date_heure, copies_mails, responsable, participants) 816 SELECT 817 info, 818 date_heure, 819 copies_mails, 820 -- on agrège les champs du responsable légal 821 jsonb_build_object('lienid', lienid, 'nom', nom, 'prenom', prenom, 'sexe', sexe, 'mail', mail, 'adresse', adresse, 'code_postal', code_postal, 'ville', ville, 'tels', tels, 'date_naissance', to_char(date_naissance::date, 'YYYY-MM-DD'), 'pays', pays), 822 -- on nettoie les vieux champs 823 ( 824 SELECT 825 jsonb_agg(value - 'date_heure' - 'info' - 'autorisation_photos') 826 FROM jsonb_array_elements(participants)) 827 FROM 828 old_inscriptions 829 WHERE 830 date_heure::date > '0001-01-01'; 831 832 -- copie des inscrits et attente 833 INSERT INTO participants (id, id_camp, id_personne, id_facture, liste_attente, remises, option_prix, options, date_heure) 834 SELECT 835 id, 836 id_camp, 837 id_personne, 838 id_facture, 839 jsonb_build_object('statut', CASE WHEN ROLE = '_attente' THEN 840 1 841 WHEN ROLE = '_attente_reponse' THEN 842 2 843 ELSE 844 0 845 END, 'raison', raison_attente), 846 coalesce(remises, '{}'), 847 coalesce(option_prix, '{}'), 848 coalesce(options, '{}'), 849 date_heure 850 FROM 851 old_participants 852 WHERE (old_participants.role IN ('_campeur', '_attente', '_attente_reponse')) 853 AND ( 854 SELECT 855 coalesce(inscription_simple, FALSE) 856 FROM 857 old_camps 858 WHERE 859 old_camps.id = old_participants.id_camp) = FALSE; 860 861 -- modification de l'option JOUR 862 CREATE OR REPLACE FUNCTION compute_jours (jours jsonb, date_debut timestamp) 863 RETURNS integer[] 864 AS $$ 865 DECLARE 866 plage_from timestamp; 867 plage_to timestamp; 868 index_from int; 869 index_to int; 870 BEGIN 871 IF jours IS NULL THEN 872 RETURN '{}'; 873 END IF; 874 IF coalesce(jours ->> 'from', '') = '' OR coalesce(jours ->> 'to', '') = '' THEN 875 RETURN '{}'; 876 END IF; 877 plage_from := to_date(jours ->> 'from', 'YYYY-MM-DD"T"HH24:MI:SS"Z"'); 878 plage_to := to_date(jours ->> 'to', 'YYYY-MM-DD"T"HH24:MI:SS"Z"'); 879 IF coalesce(plage_from, '0001-01-01T00:00:00Z') = '0001-01-01T00:00:00Z' OR coalesce(plage_to, '0001-01-01T00:00:00Z') = '0001-01-01T00:00:00Z' THEN 880 RETURN '{}'; 881 END IF; 882 index_from := extract(DAYS FROM plage_from - date_debut); 883 index_to := extract(DAYS FROM plage_to - date_debut); 884 RETURN ARRAY ( 885 SELECT 886 a.n 887 FROM 888 generate_series(index_from, index_to) AS a (n)); 889 END; 890 $$ 891 LANGUAGE plpgsql; 892 893 UPDATE 894 participants 895 SET 896 option_prix = jsonb_set(participants.option_prix, '{jour}', to_jsonb (compute_jours (participants.option_prix -> 'jour', camps.date_debut))) 897 FROM 898 camps 899 WHERE 900 participants.id_camp = camps.id; 901 902 DROP FUNCTION compute_jours (jours jsonb, date_debut timestamp); 903 904 -- copie des groupes 905 INSERT INTO groupe_participants (id_participant, id_groupe, id_camp, manuel) 906 SELECT 907 id, 908 -- on trouve le groupe, possiblement null 909 ( 910 SELECT 911 id FROM tmp_groupes 912 WHERE 913 tmp_groupes.id_camp = old_participants.id_camp 914 AND tmp_groupes.old_id = old_participants.id_groupe) AS idG, 915 id_camp, 916 FALSE -- automatique par défaut 917 FROM 918 old_participants 919 WHERE 920 old_participants.id = ANY ( 921 SELECT 922 id 923 FROM 924 participants) 925 AND ( 926 SELECT 927 id 928 FROM 929 tmp_groupes 930 WHERE 931 tmp_groupes.id_camp = old_participants.id_camp 932 AND tmp_groupes.old_id = old_participants.id_groupe) IS NOT NULL; 933 934 -- copie des participants simples : 935 INSERT INTO participantsimples (id_personne, id_camp, date_heure, info) 936 SELECT 937 id_personne, 938 id_camp, 939 date_heure, 940 coalesce(info, '') 941 FROM 942 old_participants 943 WHERE ( 944 SELECT 945 inscription_simple 946 FROM 947 camps 948 WHERE 949 camps.id = old_participants.id_camp) = TRUE 950 AND old_participants.role IN ('_campeur', '_attente', '_attente_reponse'); 951 952 -- copie des équipiers 953 UPDATE 954 old_participants 955 SET 956 appro = '' 957 WHERE 958 appro = 'aucun' 959 OR appro = 'null'; 960 961 UPDATE 962 old_participants 963 SET 964 diplome = '' 965 WHERE 966 diplome = 'null'; 967 968 INSERT INTO equipiers (id, id_camp, id_personne, roles, diplome, appro, presence, invitation_equipier, charte) 969 SELECT 970 id, 971 id_camp, 972 id_personne, 973 ARRAY[ROLE], 974 coalesce(diplome, ''), 975 coalesce(appro, ''), 976 coalesce(presence, '{}'), 977 coalesce(invitation_equipier, 0), 978 0 979 FROM 980 old_participants 981 WHERE 982 NOT ROLE IN ('_campeur', '_attente', '_attente_reponse'); 983 984 INSERT INTO structureaides (id, nom, immatriculation, adresse, code_postal, ville, telephone, info) 985 SELECT 986 id, 987 coalesce(nom, ''), 988 coalesce(immatriculation, ''), 989 coalesce(adresse, ''), 990 coalesce(code_postal, ''), 991 coalesce(ville, ''), 992 coalesce(telephone, ''), 993 coalesce(info, '') 994 FROM 995 old_structures_aides; 996 997 -- on ignore les aides sur les camps simples et les équipiers 998 INSERT INTO aides (id, id_structureaide, id_participant, valeur, valide, par_jour, nb_jours_max) 999 SELECT 1000 old_aides.id, 1001 old_aides.id_structure, 1002 old_aides.id_participant, 1003 old_aides.valeur, 1004 coalesce(old_aides.valide, TRUE), 1005 coalesce(old_aides.par_jour, FALSE), 1006 coalesce(old_aides.nb_jours_max, 0) 1007 FROM 1008 old_aides 1009 JOIN old_participants ON old_aides.id_participant = old_participants.id 1010 JOIN old_camps ON old_participants.id_camp = old_camps.id 1011 WHERE (coalesce(old_camps.inscription_simple, FALSE) = FALSE) 1012 AND old_participants.role IN ('_campeur', '_attente', '_attente_reponse'); 1013 1014 -- on nettoie les documents sans contenu 1015 DELETE FROM old_documents 1016 WHERE content IS NULL; 1017 1018 -- on copie les meta données ... 1019 INSERT INTO documents (id, taille, nom_client, description, date_heure_modif) 1020 SELECT 1021 id, 1022 taille, 1023 nom_client, 1024 description, 1025 date_heure_modif 1026 FROM 1027 old_documents; 1028 1029 -- ... puis le contenu 1030 INSERT INTO contenu_documents (id_document, contenu, miniature) 1031 SELECT 1032 id, 1033 content, 1034 miniature 1035 FROM 1036 old_documents; 1037 1038 -- on sépare suivant les targets 1039 INSERT INTO document_aides (id_document, id_aide) 1040 SELECT 1041 id, 1042 id_target 1043 FROM 1044 old_documents 1045 WHERE 1046 target = 'Ai'; 1047 1048 INSERT INTO document_camps (id_document, id_camp, is_lettre) 1049 SELECT 1050 id, 1051 id_target, 1052 target = 'Le' 1053 FROM 1054 old_documents 1055 WHERE 1056 target = 'Le' 1057 OR target = 'Pj'; 1058 1059 INSERT INTO document_personnes (id_document, id_personne, id_contrainte) 1060 SELECT 1061 id, 1062 id_target, 1063 ( 1064 SELECT 1065 id 1066 FROM 1067 contraintes 1068 WHERE 1069 builtin = categorie) 1070 FROM 1071 old_documents 1072 WHERE 1073 target = 'Pe'; 1074 1075 -- on nettoie les description "undefined" qui viennent du frontend 1076 UPDATE 1077 documents 1078 SET 1079 description = '' 1080 WHERE 1081 description = 'undefined'; 1082 1083 INSERT INTO dons (id, id_personne, valeur, mode_paiement, date_reception, date_merci, personne_merci, recu_emis, infos) 1084 SELECT 1085 id, 1086 id_donateur, 1087 valeur, 1088 coalesce(mode_paiement, ''), 1089 coalesce(date_reception, '0001-01-01'), 1090 coalesce(date_merci, '0001-01-01'), 1091 coalesce(personne_merci, ''), 1092 coalesce(recu_emis, '0001-01-01'), 1093 coalesce(infos, '{}') 1094 FROM 1095 old_dons; 1096 1097 INSERT INTO paiements (id, id_facture, is_acompte, is_remboursement, in_bordereau, label_payeur, nom_banque, mode_paiement, numero, valeur, is_invalide, date_reglement, details) 1098 SELECT 1099 id, 1100 id_facture, 1101 coalesce(is_acompte, FALSE), 1102 coalesce(is_remboursement, FALSE), 1103 coalesce(in_bordereau, '0001-01-01'), 1104 coalesce(label_payeur, ''), 1105 coalesce(nom_banque, ''), 1106 coalesce(mode_paiement, ''), 1107 coalesce(numero, ''), 1108 coalesce(valeur, 0), 1109 coalesce(is_invalide, FALSE), 1110 coalesce(date_reglement, '0001-01-01'), 1111 CASE WHEN date_encaissement > '0001-01-01' THEN 1112 'encaissement: ' || to_char(date_encaissement, 'DD/MM/YYYY') 1113 ELSE 1114 '' 1115 END 1116 FROM 1117 old_paiements; 1118 1119 -- transformation du champ info 1120 -- (1) MResponsable 1121 -- (2) MCentre 1122 -- (3) MAccuseReception 1123 -- (4) MFacture 1124 -- (5) MDocuments 1125 -- (6) MFactureAquittee 1126 -- (7) MAttestationPresence 1127 -- (8) MSondage 1128 -- (9) MInscription 1129 -- (10) MPlaceLiberee 1130 1131 INSERT INTO messages (id_facture, kind, created, modified, vu) 1132 SELECT 1133 id, 1134 m_responsable (), 1135 CASE WHEN ( SELECT DISTINCT 1136 (date_heure)::date 1137 FROM 1138 old_participants 1139 WHERE 1140 id_facture = old_factures.id 1141 ORDER BY 1142 date_heure 1143 LIMIT 1) > '0001-01-01' THEN 1144 ( SELECT DISTINCT 1145 date_heure 1146 FROM 1147 old_participants 1148 WHERE 1149 id_facture = old_factures.id 1150 ORDER BY 1151 date_heure 1152 LIMIT 1) 1153 ELSE 1154 ( 1155 SELECT 1156 date_debut 1157 FROM 1158 old_camps 1159 WHERE 1160 old_camps.id = ( 1161 SELECT 1162 id_camp 1163 FROM 1164 old_participants 1165 WHERE 1166 old_participants.id_facture = old_factures.id 1167 LIMIT 1)) 1168 END, 1169 NULL, 1170 TRUE 1171 FROM 1172 old_factures 1173 WHERE 1174 coalesce(info, '') <> ''; 1175 1176 -- comme il n'y qu'une info par facture, on se sert de la facture 1177 -- pour retrouver le message 1178 1179 INSERT INTO message_messages (id_message, contenu, guard_kind) 1180 SELECT 1181 messages.id, 1182 old_factures.info, 1183 m_responsable () 1184 FROM 1185 messages 1186 JOIN old_factures ON old_factures.id = messages.id_facture 1187 WHERE 1188 messages.kind = m_responsable (); 1189 1190 -- champ etat.accuse reception 1191 INSERT INTO messages (id_facture, kind, created, modified, vu) 1192 SELECT 1193 id, 1194 m_accuse_reception (), 1195 (etat ->> 'accuse_reception')::timestamp WITH time zone AS ti, 1196 NULL, 1197 TRUE 1198 FROM 1199 old_factures 1200 WHERE 1201 coalesce(etat ->> 'accuse_reception', '0001-01-01T00:00:00Z') <> '0001-01-01T00:00:00Z' 1202 ORDER BY 1203 ti; 1204 1205 -- champ etat.facture 1206 INSERT INTO messages (id_facture, kind, created, modified, vu) 1207 SELECT 1208 id, 1209 m_facture (), 1210 (etat ->> 'facture')::timestamp AS ti, 1211 NULL, 1212 TRUE 1213 FROM 1214 old_factures 1215 WHERE 1216 coalesce(etat ->> 'facture', '0001-01-01T00:00:00Z') <> '0001-01-01T00:00:00Z' 1217 ORDER BY 1218 ti; 1219 1220 -- champ etat.facture_acquittee 1221 INSERT INTO messages (id_facture, kind, created, modified, vu) 1222 SELECT 1223 id, 1224 m_facture_acquittee (), 1225 (etat ->> 'facture_acquittee')::timestamp AS ti, 1226 NULL, 1227 TRUE 1228 FROM 1229 old_factures 1230 WHERE 1231 coalesce(etat ->> 'facture_acquittee', '0001-01-01T00:00:00Z') <> '0001-01-01T00:00:00Z' 1232 ORDER BY 1233 ti; 1234 1235 -- champ etat.attestation_presence 1236 INSERT INTO messages (id_facture, kind, created, modified, vu) 1237 SELECT 1238 id, 1239 m_attestation_presence (), 1240 (etat ->> 'attestation_presence')::timestamp AS ti, 1241 NULL, 1242 TRUE 1243 FROM 1244 old_factures 1245 WHERE 1246 coalesce(etat ->> 'attestation_presence', '0001-01-01T00:00:00Z') <> '0001-01-01T00:00:00Z' 1247 ORDER BY 1248 ti; 1249 1250 -- toutes les attestations ont été envoyées par mail 1251 INSERT INTO message_attestations (id_message, distribution, guard_kind) 1252 SELECT 1253 id, 1254 d_mail (), 1255 kind 1256 FROM 1257 messages 1258 WHERE 1259 kind = m_attestation_presence () 1260 OR kind = m_facture_acquittee (); 1261 1262 -- table temporaire 1263 CREATE TABLE tmp_messages_camps ( 1264 id_message integer, 1265 -- target to fill 1266 id_facture integer, 1267 id_camp integer, 1268 created timestamp 1269 ); 1270 1271 INSERT INTO tmp_messages_camps (id_facture, id_camp, created) 1272 SELECT 1273 old_factures.id, 1274 docs.key::int, 1275 (docs.value::text)::timestamp 1276 FROM 1277 old_factures 1278 JOIN LATERAL jsonb_each(old_factures.etat -> 'documents') AS docs ON TRUE 1279 WHERE 1280 coalesce(old_factures.etat -> 'documents', 'null') <> 'null' 1281 AND coalesce(docs.value, 'null') <> 'null' 1282 AND ((docs.value)::text)::date <> '0001-01-01'; 1283 1284 -- on pré-calcule les ids messages et on les sauvegarde dans la table temporaire 1285 UPDATE 1286 tmp_messages_camps 1287 SET 1288 id_message = nextval(pg_get_serial_sequence('messages', 'id')); 1289 1290 -- pour éviter que tous les anciens messages camps soit dans un groupe 1291 -- on ajoute une petite variation dans le temps 1292 1293 INSERT INTO messages (id, id_facture, kind, created, modified, vu) 1294 SELECT 1295 id_message, 1296 id_facture, 1297 m_documents (), 1298 created + id_camp * interval '1 second', 1299 NULL, 1300 TRUE 1301 FROM 1302 tmp_messages_camps; 1303 1304 INSERT INTO message_documents (id_message, id_camp, guardKind) 1305 SELECT 1306 id_message, 1307 id_camp, 1308 m_documents () 1309 FROM 1310 tmp_messages_camps; 1311 1312 -- cleanup 1313 DROP TABLE tmp_messages_camps; 1314 1315 -- type 'id' 1316 INSERT INTO users (label, mdp, is_admin, modules) 1317 SELECT 1318 label, 1319 mdp, 1320 is_admin, 1321 modules 1322 FROM 1323 old_users; 1324 1325 -- convertion des dates stockées en JSON comme des time 1326 CREATE FUNCTION time_to_date (text) 1327 RETURNS jsonb 1328 AS $$ 1329 SELECT 1330 to_jsonb (to_date($1, 'YYYY-MM-DD"T"HH24:MI:SS"Z"')::date) 1331 $$ 1332 LANGUAGE SQL; 1333 1334 UPDATE 1335 camps 1336 SET 1337 option_prix = jsonb_set(option_prix, '{semaine, plage_1, from}', time_to_date (option_prix -> 'semaine' -> 'plage_1' ->> 'from')) 1338 WHERE 1339 option_prix -> 'semaine' -> 'plage_1' -> 'from' IS NOT NULL; 1340 1341 UPDATE 1342 camps 1343 SET 1344 option_prix = jsonb_set(option_prix, '{semaine, plage_1, to}', time_to_date (option_prix -> 'semaine' -> 'plage_1' ->> 'to')) 1345 WHERE 1346 option_prix -> 'semaine' -> 'plage_1' -> 'to' IS NOT NULL; 1347 1348 UPDATE 1349 camps 1350 SET 1351 option_prix = jsonb_set(option_prix, '{semaine, plage_2, from}', time_to_date (option_prix -> 'semaine' -> 'plage_2' ->> 'from')) 1352 WHERE 1353 option_prix -> 'semaine' -> 'plage_2' -> 'from' IS NOT NULL; 1354 1355 UPDATE 1356 camps 1357 SET 1358 option_prix = jsonb_set(option_prix, '{semaine, plage_2, to}', time_to_date (option_prix -> 'semaine' -> 'plage_2' ->> 'to')) 1359 WHERE 1360 option_prix -> 'semaine' -> 'plage_2' -> 'to' IS NOT NULL; 1361 1362 UPDATE 1363 equipiers 1364 SET 1365 presence = jsonb_set(presence, '{from}', time_to_date (presence ->> 'from')) 1366 WHERE 1367 presence -> 'from' IS NOT NULL; 1368 1369 UPDATE 1370 equipiers 1371 SET 1372 presence = jsonb_set(presence, '{to}', time_to_date (presence ->> 'to')) 1373 WHERE 1374 presence -> 'to' IS NOT NULL; 1375 1376 UPDATE 1377 groupes 1378 SET 1379 plage = jsonb_set(plage, '{from}', time_to_date (plage ->> 'from')) 1380 WHERE 1381 plage -> 'from' IS NOT NULL; 1382 1383 UPDATE 1384 groupes 1385 SET 1386 plage = jsonb_set(plage, '{to}', time_to_date (plage ->> 'to')) 1387 WHERE 1388 plage -> 'to' IS NOT NULL; 1389 1390 UPDATE 1391 inscriptions 1392 SET 1393 responsable = jsonb_set(responsable, '{date_naissance}', time_to_date (responsable ->> 'date_naissance')) 1394 WHERE 1395 responsable -> 'date_naissance' IS NOT NULL; 1396 1397 UPDATE 1398 inscriptions 1399 SET 1400 participants = ( 1401 SELECT 1402 jsonb_agg(jsonb_set(value, '{date_naissance}', time_to_date (value ->> 'date_naissance'))) 1403 FROM 1404 jsonb_array_elements(participants)); 1405 1406 DROP FUNCTION time_to_date (text); 1407 1408 -- on peut supprimer les anciennes tables 1409 DROP TABLE old_personnes CASCADE; 1410 1411 DROP TABLE old_aides CASCADE; 1412 1413 DROP TABLE old_camps CASCADE; 1414 1415 DROP TABLE old_documents CASCADE; 1416 1417 DROP TABLE old_dons CASCADE; 1418 1419 DROP TABLE old_factures CASCADE; 1420 1421 DROP TABLE old_inscriptions CASCADE; 1422 1423 DROP TABLE old_paiements CASCADE; 1424 1425 DROP TABLE old_participants CASCADE; 1426 1427 DROP TABLE old_structures_aides CASCADE; 1428 1429 DROP TABLE old_users CASCADE; 1430 1431 -- ajuste les serials 1432 SELECT 1433 setval( 1434 pg_get_serial_sequence('aides', 'id'), 1435 coalesce(max(id), 0) + 1, 1436 FALSE 1437 ) 1438 FROM 1439 aides; 1440 1441 SELECT 1442 setval( 1443 pg_get_serial_sequence('camps', 'id'), 1444 coalesce(max(id), 0) + 1, 1445 FALSE 1446 ) 1447 FROM 1448 camps; 1449 1450 SELECT 1451 setval( 1452 pg_get_serial_sequence('contraintes', 'id'), 1453 coalesce(max(id), 0) + 1, 1454 FALSE 1455 ) 1456 FROM 1457 contraintes; 1458 1459 SELECT 1460 setval( 1461 pg_get_serial_sequence('documents', 'id'), 1462 coalesce(max(id), 0) + 1, 1463 FALSE 1464 ) 1465 FROM 1466 documents; 1467 1468 SELECT 1469 setval( 1470 pg_get_serial_sequence('dons', 'id'), 1471 coalesce(max(id), 0) + 1, 1472 FALSE 1473 ) 1474 FROM 1475 dons; 1476 1477 SELECT 1478 setval( 1479 pg_get_serial_sequence('equipiers', 'id'), 1480 coalesce(max(id), 0) + 1, 1481 FALSE 1482 ) 1483 FROM 1484 equipiers; 1485 1486 SELECT 1487 setval( 1488 pg_get_serial_sequence('factures', 'id'), 1489 coalesce(max(id), 0) + 1, 1490 FALSE 1491 ) 1492 FROM 1493 factures; 1494 1495 SELECT 1496 setval( 1497 pg_get_serial_sequence('groupes', 'id'), 1498 coalesce(max(id), 0) + 1, 1499 FALSE 1500 ) 1501 FROM 1502 groupes; 1503 1504 SELECT 1505 setval( 1506 pg_get_serial_sequence('messages', 'id'), 1507 coalesce(max(id), 0) + 1, 1508 FALSE 1509 ) 1510 FROM 1511 messages; 1512 1513 SELECT 1514 setval( 1515 pg_get_serial_sequence('paiements', 'id'), 1516 coalesce(max(id), 0) + 1, 1517 FALSE 1518 ) 1519 FROM 1520 paiements; 1521 1522 SELECT 1523 setval( 1524 pg_get_serial_sequence('participants', 'id'), 1525 coalesce(max(id), 0) + 1, 1526 FALSE 1527 ) 1528 FROM 1529 participants; 1530 1531 SELECT 1532 setval( 1533 pg_get_serial_sequence('participantsimples', 'id'), 1534 coalesce(max(id), 0) + 1, 1535 FALSE 1536 ) 1537 FROM 1538 participantsimples; 1539 1540 SELECT 1541 setval( 1542 pg_get_serial_sequence('personnes', 'id'), 1543 coalesce(max(id), 0) + 1, 1544 FALSE 1545 ) 1546 FROM 1547 personnes; 1548 1549 SELECT 1550 setval( 1551 pg_get_serial_sequence('sondages', 'id'), 1552 coalesce(max(id), 0) + 1, 1553 FALSE 1554 ) 1555 FROM 1556 sondages; 1557 1558 SELECT 1559 setval( 1560 pg_get_serial_sequence('structureaides', 'id'), 1561 coalesce(max(id), 0) + 1, 1562 FALSE 1563 ) 1564 FROM 1565 structureaides; 1566 1567 SELECT 1568 setval( 1569 pg_get_serial_sequence('users', 'id'), 1570 coalesce(max(id), 0) + 1, 1571 FALSE 1572 ) 1573 FROM 1574 users; 1575 1576 DROP FUNCTION IF EXISTS m_responsable(); 1577 1578 DROP FUNCTION IF EXISTS m_centre(); 1579 1580 DROP FUNCTION IF EXISTS m_accuse_reception(); 1581 1582 DROP FUNCTION IF EXISTS m_facture(); 1583 1584 DROP FUNCTION IF EXISTS m_documents(); 1585 1586 DROP FUNCTION IF EXISTS m_facture_aquittee(); 1587 1588 DROP FUNCTION IF EXISTS m_attestation_presence(); 1589 1590 DROP FUNCTION IF EXISTS m_sondage(); 1591 1592 DROP FUNCTION IF EXISTS m_inscription(); 1593 1594 DROP FUNCTION IF EXISTS m_place_liberee(); 1595 1596 DROP FUNCTION IF EXISTS d_mail();