-- ============================================================================ -- Migration : Table fiches_horaires et configuration Storage Fiches_Horaires -- ============================================================================ -- 1. Création de la table fiches_horaires CREATE TABLE IF NOT EXISTS public.fiches_horaires ( "id" UUID PRIMARY KEY DEFAULT gen_random_uuid(), "titre" VARCHAR(255) NOT NULL, "ligne_id" VARCHAR(100), "ligne_nom" VARCHAR(255), "code_ligne" VARCHAR(50), "service_annuel_id" VARCHAR(100), "service_annuel_nom" VARCHAR(255), "code_service_annuel" VARCHAR(50), "direction" VARCHAR(50) DEFAULT 'aller', -- mot-clé SQL réservé, doit être entouré de guillemets doubles "statut" VARCHAR(50) DEFAULT 'Projet (Brouillon)', -- 'Projet (Brouillon)', 'En cours de production', 'Validée' "pdf_url" TEXT, "pdf_filename" VARCHAR(255), "pdf_storage_path" TEXT, "config" JSONB DEFAULT '{}'::jsonb, "circulations_count" INT DEFAULT 0, "gares_count" INT DEFAULT 0, "region_id" VARCHAR(100), "created_by" VARCHAR(255), "created_at" TIMESTAMP WITH TIME ZONE DEFAULT NOW(), "updated_at" TIMESTAMP WITH TIME ZONE DEFAULT NOW() ); -- Index pour accélérer les recherches CREATE INDEX IF NOT EXISTS idx_fiches_horaires_ligne_id ON public.fiches_horaires("ligne_id"); CREATE INDEX IF NOT EXISTS idx_fiches_horaires_service_id ON public.fiches_horaires("service_annuel_id"); CREATE INDEX IF NOT EXISTS idx_fiches_horaires_statut ON public.fiches_horaires("statut"); CREATE INDEX IF NOT EXISTS idx_fiches_horaires_region ON public.fiches_horaires("region_id"); -- RLS Policies pour la table fiches_horaires ALTER TABLE public.fiches_horaires ENABLE ROW LEVEL SECURITY; DROP POLICY IF EXISTS "fiches_horaires_lecture_publique" ON public.fiches_horaires; DROP POLICY IF EXISTS "fiches_horaires_ecriture" ON public.fiches_horaires; CREATE POLICY "fiches_horaires_lecture_publique" ON public.fiches_horaires FOR SELECT USING (true); CREATE POLICY "fiches_horaires_ecriture" ON public.fiches_horaires FOR ALL USING (true) WITH CHECK (true); -- 2. Création et configuration du bucket de stockage 'Fiches_Horaires' INSERT INTO storage.buckets ("id", "name", "public") VALUES ('Fiches_Horaires', 'Fiches_Horaires', true) ON CONFLICT ("id") DO UPDATE SET "public" = true; -- Configuration des règles Storage pour 'Fiches_Horaires' DROP POLICY IF EXISTS "Fiches_Horaires - lecture publique" ON storage.objects; DROP POLICY IF EXISTS "Fiches_Horaires - upload" ON storage.objects; DROP POLICY IF EXISTS "Fiches_Horaires - update" ON storage.objects; DROP POLICY IF EXISTS "Fiches_Horaires - delete" ON storage.objects; CREATE POLICY "Fiches_Horaires - lecture publique" ON storage.objects FOR SELECT USING (bucket_id = 'Fiches_Horaires' OR bucket_id = 'fiches-horaires'); CREATE POLICY "Fiches_Horaires - upload" ON storage.objects FOR INSERT WITH CHECK (bucket_id = 'Fiches_Horaires' OR bucket_id = 'fiches-horaires'); CREATE POLICY "Fiches_Horaires - update" ON storage.objects FOR UPDATE USING (bucket_id = 'Fiches_Horaires' OR bucket_id = 'fiches-horaires'); CREATE POLICY "Fiches_Horaires - delete" ON storage.objects FOR DELETE USING (bucket_id = 'Fiches_Horaires' OR bucket_id = 'fiches-horaires');