From f8e80175726c59b638cb052f8bc92fe456091b18 Mon Sep 17 00:00:00 2001 From: Marco Pedone Date: Fri, 14 Nov 2025 18:23:38 +0100 Subject: [PATCH] refactor: clean up SQL migration for servizio_annunci table constraints and formatting --- apps/db/migrations/14_servizioannunci.up.sql | 42 ++++++++++++-------- 1 file changed, 25 insertions(+), 17 deletions(-) diff --git a/apps/db/migrations/14_servizioannunci.up.sql b/apps/db/migrations/14_servizioannunci.up.sql index d90c419..4af04c4 100644 --- a/apps/db/migrations/14_servizioannunci.up.sql +++ b/apps/db/migrations/14_servizioannunci.up.sql @@ -22,35 +22,43 @@ CREATE TABLE IF NOT EXISTS public.servizio_annunci ( doc_contratto_added boolean DEFAULT FALSE NOT NULL, doc_registrazione_added boolean DEFAULT FALSE NOT NULL ); + -- Unique Constraint -DO $$ BEGIN IF NOT EXISTS ( - SELECT 1 - FROM pg_constraint - WHERE conname = 'uniq_annuncio_servizio' +DO $$ BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'uniq_annuncio_servizio' + AND conrelid = 'public.servizio_annunci'::regclass ) THEN - ALTER TABLE public.servizio_annunci + ALTER TABLE public.servizio_annunci ADD CONSTRAINT uniq_annuncio_servizio UNIQUE (annunci_id, servizio_id); END IF; END $$; + -- Foreign Key to Annunci -DO $$ BEGIN IF NOT EXISTS ( - SELECT 1 FROM pg_constraint - WHERE conname = 'servizio_annuncio_J_annuncio' +DO $$ BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'servizio_annuncio_j_annuncio' -- LOWERCASE + AND conrelid = 'public.servizio_annunci'::regclass ) THEN - ALTER TABLE public.servizio_annunci - ADD CONSTRAINT servizio_annuncio_J_annuncio - FOREIGN KEY (annunci_id) REFERENCES public.annunci (id) + ALTER TABLE public.servizio_annunci + ADD CONSTRAINT servizio_annuncio_j_annuncio -- LOWERCASE + FOREIGN KEY (annunci_id) REFERENCES public.annunci (id) ON UPDATE CASCADE ON DELETE CASCADE; END IF; END $$; + -- Foreign Key to Servizio -DO $$ BEGIN IF NOT EXISTS ( - SELECT 1 FROM pg_constraint - WHERE conname = 'servizio_annuncio_J_servizio' +DO $$ BEGIN + IF NOT EXISTS ( + SELECT 1 FROM pg_constraint + WHERE conname = 'servizio_annuncio_j_servizio' -- LOWERCASE + AND conrelid = 'public.servizio_annunci'::regclass ) THEN - ALTER TABLE public.servizio_annunci - ADD CONSTRAINT servizio_annuncio_J_servizio - FOREIGN KEY (servizio_id) REFERENCES public.servizio (servizio_id) + ALTER TABLE public.servizio_annunci + ADD CONSTRAINT servizio_annuncio_j_servizio -- LOWERCASE + FOREIGN KEY (servizio_id) REFERENCES public.servizio (servizio_id) ON UPDATE CASCADE ON DELETE CASCADE; END IF; END $$; \ No newline at end of file