infoalloggi-monorepo/apps/db/migrations/007_chats.up.sql
Marco Pedone 66ef4a7fbd feat(database): Add migrations for chat labels, messages, services, interests, announcements, pricing, orders, payments, and miscellaneous data
- Created tables for `chats_etichette`, `messages`, `servizio`, `servizio_interessi`, `servizio_annunci`, `prezziario`, `ordini`, `payments`, and `banlist`.
- Established primary and foreign key constraints for data integrity.
- Introduced enums for various types including `TipologiaPosizioneEnum`, `OrderTypeEnum`, and `PaymentStatusEnum`.
- Seeded initial data for banners, labels, flags, pricing, strings, and users.
- Implemented unique constraints where necessary to prevent duplicate entries.
2025-10-29 16:59:20 +01:00

26 lines
No EOL
764 B
SQL

-- Chats
CREATE TABLE IF NOT EXISTS public.chats (
chatid UUID DEFAULT gen_random_uuid () NOT NULL,
created_at TIMESTAMP WITHOUT TIME ZONE NOT NULL,
user_ref UUID NOT NULL,
tags TEXT []
);
-- Primary Key
DO $$ BEGIN
ALTER TABLE public.chats
ADD CONSTRAINT chats_pkey PRIMARY KEY (chatid);
EXCEPTION
WHEN invalid_table_definition THEN
RAISE NOTICE 'Primary key already exists. Ignoring...';
END $$;
-- Foreign Key to Users
DO $$ BEGIN IF NOT EXISTS (
SELECT 1 FROM pg_constraint
WHERE conname = 'userchats'
) THEN
ALTER TABLE public.chats
ADD CONSTRAINT userchats
FOREIGN KEY (user_ref) REFERENCES public.users (id)
ON UPDATE CASCADE ON DELETE CASCADE;
END IF;
END $$;