refactor: clean up SQL migration for servizio_annunci table constraints and formatting
This commit is contained in:
parent
63397923c4
commit
f8e8017572
1 changed files with 25 additions and 17 deletions
|
|
@ -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 $$;
|
||||
Loading…
Add table
Reference in a new issue