64 lines
No EOL
2.2 KiB
SQL
64 lines
No EOL
2.2 KiB
SQL
-- Servizio Annunci
|
|
CREATE TABLE IF NOT EXISTS public.servizio_annunci (
|
|
servizio_id UUID NOT NULL,
|
|
annunci_id integer NOT NULL,
|
|
created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT now() NOT NULL,
|
|
open_contatti_at TIMESTAMP WITHOUT TIME ZONE,
|
|
doc_conferma_ref TEXT,
|
|
user_confirmed_at TIMESTAMP WITHOUT TIME ZONE,
|
|
accettato_conferma_at TIMESTAMP WITHOUT TIME ZONE,
|
|
caparra_causale TEXT,
|
|
caparra_iban TEXT,
|
|
caparra_importo TEXT,
|
|
caparra_intestazione TEXT,
|
|
"hasConfermaAdmin" boolean DEFAULT FALSE NOT NULL,
|
|
doc_contratto_ref TEXT,
|
|
gestionale_id integer,
|
|
contratto_decorrenza date,
|
|
contratto_scadenza date,
|
|
contratto_tipo TEXT,
|
|
doc_registrazione_ref TEXT,
|
|
doc_conferma_added boolean DEFAULT FALSE NOT NULL,
|
|
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'
|
|
AND conrelid = 'public.servizio_annunci'::regclass
|
|
) THEN
|
|
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' -- LOWERCASE
|
|
AND conrelid = 'public.servizio_annunci'::regclass
|
|
) THEN
|
|
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' -- LOWERCASE
|
|
AND conrelid = 'public.servizio_annunci'::regclass
|
|
) THEN
|
|
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 $$; |