infoalloggi-monorepo/apps/db/migrations/6_annunci.up.sql
Marco Pedone 3d70810937 feat(migrations): Add multiple database migrations for new tables and data seeding
- Created migration scripts for new tables including banlist, banners, event_queue, flags, testi_e_stringhe, and ratelimiter.
- Added data seeding for banners, etichette, flags, prezziario, and testi_e_stringhe with idempotent inserts.
- Initialized database with necessary extensions and user tables.
- Updated servizio table to include new columns for managing service states.
- Established foreign key relationships and unique constraints across various tables.
- Implemented sequences for auto-incrementing primary keys in several tables.
2025-10-30 19:14:31 +01:00

91 lines
No EOL
2.3 KiB
SQL

-- Annunci
CREATE TABLE IF NOT EXISTS public.annunci (
id integer NOT NULL,
codice TEXT NOT NULL,
locatore TEXT,
numero TEXT,
idlocatore TEXT,
indirizzo TEXT,
civico TEXT,
comune TEXT,
cap TEXT,
provincia TEXT,
regione TEXT,
lat TEXT,
lon TEXT,
indirizzo_secondario TEXT,
civico_secondario TEXT,
lat_secondario TEXT,
lon_secondario TEXT,
tipo TEXT,
categorie TEXT [],
prezzo integer DEFAULT 0 NOT NULL,
anno TEXT,
classe TEXT,
mq numeric,
piano TEXT,
piano_palazzo integer,
unita_condominio integer,
numero_vani integer,
numero_camere integer,
numero_bagni integer,
numero_balconi integer,
numero_terrazzi integer,
numero_box integer,
numero_postiauto integer,
accessori TEXT [],
titolo_it TEXT,
desc_it TEXT,
titolo_en TEXT,
desc_en TEXT,
stato TEXT,
web boolean,
caratteristiche json,
homepage boolean,
url_video TEXT [],
email TEXT,
creato_il TIMESTAMP WITHOUT TIME ZONE,
modificato_il TIMESTAMP WITHOUT TIME ZONE,
consegna integer,
disponibile_da date,
permanenza INTEGER[],
persone TEXT [],
updated_at TIMESTAMP WITHOUT TIME ZONE
);
COMMENT ON COLUMN public.annunci.caratteristiche IS '@type(Caratteristiche, ''src/utils/kanel-types.ts'', false, false, true)';
-- Sequence for id
DO $$ BEGIN
IF NOT EXISTS (
SELECT 1 FROM pg_sequences
WHERE schemaname = 'public' AND sequencename = 'annunci_id_seq'
) THEN
CREATE SEQUENCE public.annunci_id_seq
AS integer START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
END IF;
END $$;
ALTER SEQUENCE public.annunci_id_seq OWNED BY public.annunci.id;
-- Set default for id
ALTER TABLE public.annunci
ALTER COLUMN id
SET DEFAULT nextval(
'public.annunci_id_seq'::regclass
);
-- Primary Key
DO $$ BEGIN ALTER TABLE public.annunci
ADD CONSTRAINT annunci_pkey PRIMARY KEY (id);
EXCEPTION
WHEN invalid_table_definition THEN
RAISE NOTICE 'Primary key already exists. Ignoring...';
END $$;
-- Unique Codice
DO $$ BEGIN IF NOT EXISTS (
SELECT 1
FROM pg_constraint
WHERE conname = 'annunci_codice_key'
) THEN
ALTER TABLE public.annunci
ADD CONSTRAINT annunci_codice_key UNIQUE (codice);
END IF;
END $$;