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,34 +22,42 @@ CREATE TABLE IF NOT EXISTS public.servizio_annunci (
|
||||||
doc_contratto_added boolean DEFAULT FALSE NOT NULL,
|
doc_contratto_added boolean DEFAULT FALSE NOT NULL,
|
||||||
doc_registrazione_added boolean DEFAULT FALSE NOT NULL
|
doc_registrazione_added boolean DEFAULT FALSE NOT NULL
|
||||||
);
|
);
|
||||||
|
|
||||||
-- Unique Constraint
|
-- Unique Constraint
|
||||||
DO $$ BEGIN IF NOT EXISTS (
|
DO $$ BEGIN
|
||||||
SELECT 1
|
IF NOT EXISTS (
|
||||||
FROM pg_constraint
|
SELECT 1 FROM pg_constraint
|
||||||
WHERE conname = 'uniq_annuncio_servizio'
|
WHERE conname = 'uniq_annuncio_servizio'
|
||||||
|
AND conrelid = 'public.servizio_annunci'::regclass
|
||||||
) THEN
|
) THEN
|
||||||
ALTER TABLE public.servizio_annunci
|
ALTER TABLE public.servizio_annunci
|
||||||
ADD CONSTRAINT uniq_annuncio_servizio UNIQUE (annunci_id, servizio_id);
|
ADD CONSTRAINT uniq_annuncio_servizio UNIQUE (annunci_id, servizio_id);
|
||||||
END IF;
|
END IF;
|
||||||
END $$;
|
END $$;
|
||||||
|
|
||||||
-- Foreign Key to Annunci
|
-- Foreign Key to Annunci
|
||||||
DO $$ BEGIN IF NOT EXISTS (
|
DO $$ BEGIN
|
||||||
SELECT 1 FROM pg_constraint
|
IF NOT EXISTS (
|
||||||
WHERE conname = 'servizio_annuncio_J_annuncio'
|
SELECT 1 FROM pg_constraint
|
||||||
|
WHERE conname = 'servizio_annuncio_j_annuncio' -- LOWERCASE
|
||||||
|
AND conrelid = 'public.servizio_annunci'::regclass
|
||||||
) THEN
|
) THEN
|
||||||
ALTER TABLE public.servizio_annunci
|
ALTER TABLE public.servizio_annunci
|
||||||
ADD CONSTRAINT servizio_annuncio_J_annuncio
|
ADD CONSTRAINT servizio_annuncio_j_annuncio -- LOWERCASE
|
||||||
FOREIGN KEY (annunci_id) REFERENCES public.annunci (id)
|
FOREIGN KEY (annunci_id) REFERENCES public.annunci (id)
|
||||||
ON UPDATE CASCADE ON DELETE CASCADE;
|
ON UPDATE CASCADE ON DELETE CASCADE;
|
||||||
END IF;
|
END IF;
|
||||||
END $$;
|
END $$;
|
||||||
|
|
||||||
-- Foreign Key to Servizio
|
-- Foreign Key to Servizio
|
||||||
DO $$ BEGIN IF NOT EXISTS (
|
DO $$ BEGIN
|
||||||
SELECT 1 FROM pg_constraint
|
IF NOT EXISTS (
|
||||||
WHERE conname = 'servizio_annuncio_J_servizio'
|
SELECT 1 FROM pg_constraint
|
||||||
|
WHERE conname = 'servizio_annuncio_j_servizio' -- LOWERCASE
|
||||||
|
AND conrelid = 'public.servizio_annunci'::regclass
|
||||||
) THEN
|
) THEN
|
||||||
ALTER TABLE public.servizio_annunci
|
ALTER TABLE public.servizio_annunci
|
||||||
ADD CONSTRAINT servizio_annuncio_J_servizio
|
ADD CONSTRAINT servizio_annuncio_j_servizio -- LOWERCASE
|
||||||
FOREIGN KEY (servizio_id) REFERENCES public.servizio (servizio_id)
|
FOREIGN KEY (servizio_id) REFERENCES public.servizio (servizio_id)
|
||||||
ON UPDATE CASCADE ON DELETE CASCADE;
|
ON UPDATE CASCADE ON DELETE CASCADE;
|
||||||
END IF;
|
END IF;
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue