- 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.
39 lines
No EOL
1.2 KiB
SQL
39 lines
No EOL
1.2 KiB
SQL
-- Messages
|
|
CREATE TABLE IF NOT EXISTS public.messages (
|
|
messageid UUID DEFAULT gen_random_uuid () NOT NULL,
|
|
chatid UUID NOT NULL,
|
|
message TEXT,
|
|
"time" TIMESTAMP WITHOUT TIME ZONE NOT NULL,
|
|
isread boolean DEFAULT FALSE NOT NULL,
|
|
sender UUID NOT NULL
|
|
);
|
|
-- Primary Key
|
|
DO $$ BEGIN
|
|
ALTER TABLE public.messages
|
|
ADD CONSTRAINT messages_pkey PRIMARY KEY (messageid);
|
|
EXCEPTION
|
|
WHEN invalid_table_definition THEN
|
|
RAISE NOTICE 'Primary key already exists. Ignoring...';
|
|
END $$;
|
|
-- Foreign Key to Chats
|
|
DO $$ BEGIN IF NOT EXISTS (
|
|
SELECT 1 FROM pg_constraint
|
|
WHERE conname = 'messages_chatid_fkey'
|
|
) THEN
|
|
ALTER TABLE public.messages
|
|
ADD CONSTRAINT messages_chatid_fkey
|
|
FOREIGN KEY (chatid) REFERENCES public.chats (chatid)
|
|
ON UPDATE CASCADE ON DELETE CASCADE;
|
|
END IF;
|
|
END $$;
|
|
-- Foreign Key to Users
|
|
DO $$ BEGIN IF NOT EXISTS (
|
|
SELECT 1 FROM pg_constraint
|
|
WHERE conname = 'messages_user_fkey'
|
|
) THEN
|
|
ALTER TABLE public.messages
|
|
ADD CONSTRAINT messages_user_fkey
|
|
FOREIGN KEY (sender) REFERENCES public.users (id)
|
|
ON UPDATE CASCADE ON DELETE CASCADE;
|
|
END IF;
|
|
END $$; |