-- Migration V1.2: Création de la table organisations -- Auteur: UnionFlow Team -- Date: 2025-01-15 -- Description: Création de la table organisations avec toutes les colonnes nécessaires -- Création de la table organisations CREATE TABLE organisations ( id BIGSERIAL PRIMARY KEY, -- Informations de base nom VARCHAR(200) NOT NULL, nom_court VARCHAR(50), type_organisation VARCHAR(50) NOT NULL DEFAULT 'ASSOCIATION', statut VARCHAR(50) NOT NULL DEFAULT 'ACTIVE', description TEXT, date_fondation DATE, numero_enregistrement VARCHAR(100) UNIQUE, -- Informations de contact email VARCHAR(255) NOT NULL UNIQUE, telephone VARCHAR(20), telephone_secondaire VARCHAR(20), email_secondaire VARCHAR(255), -- Adresse adresse VARCHAR(500), ville VARCHAR(100), code_postal VARCHAR(20), region VARCHAR(100), pays VARCHAR(100), -- Coordonnées géographiques latitude DECIMAL(9,6) CHECK (latitude >= -90 AND latitude <= 90), longitude DECIMAL(9,6) CHECK (longitude >= -180 AND longitude <= 180), -- Web et réseaux sociaux site_web VARCHAR(500), logo VARCHAR(500), reseaux_sociaux VARCHAR(1000), -- Hiérarchie organisation_parente_id UUID, niveau_hierarchique INTEGER NOT NULL DEFAULT 0, -- Statistiques nombre_membres INTEGER NOT NULL DEFAULT 0, nombre_administrateurs INTEGER NOT NULL DEFAULT 0, -- Finances budget_annuel DECIMAL(14,2) CHECK (budget_annuel >= 0), devise VARCHAR(3) DEFAULT 'XOF', cotisation_obligatoire BOOLEAN NOT NULL DEFAULT FALSE, montant_cotisation_annuelle DECIMAL(12,2) CHECK (montant_cotisation_annuelle >= 0), -- Informations complémentaires objectifs TEXT, activites_principales TEXT, certifications VARCHAR(500), partenaires VARCHAR(1000), notes VARCHAR(1000), -- Paramètres organisation_publique BOOLEAN NOT NULL DEFAULT TRUE, accepte_nouveaux_membres BOOLEAN NOT NULL DEFAULT TRUE, -- Métadonnées actif BOOLEAN NOT NULL DEFAULT TRUE, date_creation TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, date_modification TIMESTAMP, cree_par VARCHAR(100), modifie_par VARCHAR(100), version BIGINT NOT NULL DEFAULT 0, -- Contraintes CONSTRAINT chk_organisation_statut CHECK (statut IN ('ACTIVE', 'SUSPENDUE', 'DISSOUTE', 'EN_ATTENTE')), CONSTRAINT chk_organisation_type CHECK (type_organisation IN ( 'ASSOCIATION', 'LIONS_CLUB', 'ROTARY_CLUB', 'COOPERATIVE', 'FONDATION', 'ONG', 'SYNDICAT', 'AUTRE' )), CONSTRAINT chk_organisation_devise CHECK (devise IN ('XOF', 'EUR', 'USD', 'GBP', 'CHF')), CONSTRAINT chk_organisation_niveau CHECK (niveau_hierarchique >= 0 AND niveau_hierarchique <= 10), CONSTRAINT chk_organisation_membres CHECK (nombre_membres >= 0), CONSTRAINT chk_organisation_admins CHECK (nombre_administrateurs >= 0) ); -- Création des index pour optimiser les performances CREATE INDEX idx_organisation_nom ON organisations(nom); CREATE INDEX idx_organisation_email ON organisations(email); CREATE INDEX idx_organisation_statut ON organisations(statut); CREATE INDEX idx_organisation_type ON organisations(type_organisation); CREATE INDEX idx_organisation_ville ON organisations(ville); CREATE INDEX idx_organisation_pays ON organisations(pays); CREATE INDEX idx_organisation_parente ON organisations(organisation_parente_id); CREATE INDEX idx_organisation_numero_enregistrement ON organisations(numero_enregistrement); CREATE INDEX idx_organisation_actif ON organisations(actif); CREATE INDEX idx_organisation_date_creation ON organisations(date_creation); CREATE INDEX idx_organisation_publique ON organisations(organisation_publique); CREATE INDEX idx_organisation_accepte_membres ON organisations(accepte_nouveaux_membres); -- Index composites pour les recherches fréquentes CREATE INDEX idx_organisation_statut_actif ON organisations(statut, actif); CREATE INDEX idx_organisation_type_ville ON organisations(type_organisation, ville); CREATE INDEX idx_organisation_pays_region ON organisations(pays, region); CREATE INDEX idx_organisation_publique_actif ON organisations(organisation_publique, actif); -- Index pour les recherches textuelles CREATE INDEX idx_organisation_nom_lower ON organisations(LOWER(nom)); CREATE INDEX idx_organisation_nom_court_lower ON organisations(LOWER(nom_court)); CREATE INDEX idx_organisation_ville_lower ON organisations(LOWER(ville)); -- Ajout de la colonne organisation_id à la table membres (si elle n'existe pas déjà) DO $$ BEGIN IF NOT EXISTS ( SELECT 1 FROM information_schema.columns WHERE table_name = 'membres' AND column_name = 'organisation_id' ) THEN ALTER TABLE membres ADD COLUMN organisation_id BIGINT; ALTER TABLE membres ADD CONSTRAINT fk_membre_organisation FOREIGN KEY (organisation_id) REFERENCES organisations(id); CREATE INDEX idx_membre_organisation ON membres(organisation_id); END IF; END $$; -- IMPORTANT: Aucune donnée fictive n'est insérée dans ce script de migration. -- Les données doivent être insérées manuellement via l'interface d'administration -- ou via des scripts de migration séparés si nécessaire pour la production. -- Mise à jour des statistiques de la base de données ANALYZE organisations; -- Commentaires sur la table et les colonnes principales COMMENT ON TABLE organisations IS 'Table des organisations (Lions Clubs, Associations, Coopératives, etc.)'; COMMENT ON COLUMN organisations.nom IS 'Nom officiel de l''organisation'; COMMENT ON COLUMN organisations.nom_court IS 'Nom court ou sigle de l''organisation'; COMMENT ON COLUMN organisations.type_organisation IS 'Type d''organisation (LIONS_CLUB, ASSOCIATION, etc.)'; COMMENT ON COLUMN organisations.statut IS 'Statut actuel de l''organisation (ACTIVE, SUSPENDUE, etc.)'; COMMENT ON COLUMN organisations.organisation_parente_id IS 'ID de l''organisation parente pour la hiérarchie'; COMMENT ON COLUMN organisations.niveau_hierarchique IS 'Niveau dans la hiérarchie (0 = racine)'; COMMENT ON COLUMN organisations.nombre_membres IS 'Nombre total de membres actifs'; COMMENT ON COLUMN organisations.organisation_publique IS 'Si l''organisation est visible publiquement'; COMMENT ON COLUMN organisations.accepte_nouveaux_membres IS 'Si l''organisation accepte de nouveaux membres'; COMMENT ON COLUMN organisations.version IS 'Version pour le contrôle de concurrence optimiste';