infoalloggi-monorepo/apps/db/migrations/5_email.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

45 lines
No EOL
1.3 KiB
SQL

-- Emails
CREATE TABLE IF NOT EXISTS public.emails (
id_email integer NOT NULL,
user_id UUID NOT NULL,
created_at TIMESTAMP WITHOUT TIME ZONE DEFAULT now() NOT NULL,
DATA json NOT NULL
);
-- Primary Key
DO $$ BEGIN
ALTER TABLE public.emails
ADD CONSTRAINT emails_pkey PRIMARY KEY (id_email);
EXCEPTION
WHEN invalid_table_definition THEN
RAISE NOTICE 'Primary key already exists. Ignoring...';
END $$;
-- Sequence for id_email
DO $$ BEGIN IF NOT EXISTS (
SELECT 1 FROM pg_sequences
WHERE schemaname = 'public' AND sequencename = 'emails_id_email_seq'
) THEN
CREATE SEQUENCE public.emails_id_email_seq
AS integer
START WITH 1 INCREMENT BY 1 NO MINVALUE NO MAXVALUE CACHE 1;
END IF;
END $$;
ALTER SEQUENCE public.emails_id_email_seq OWNED BY public.emails.id_email;
-- Set default for id_email
ALTER TABLE public.emails
ALTER COLUMN id_email
SET DEFAULT nextval(
'public.emails_id_email_seq'::regclass
);
-- Foreign Key to Users
DO $$ BEGIN IF NOT EXISTS (
SELECT 1 FROM pg_constraint
WHERE conname = 'email_user'
) THEN
ALTER TABLE public.emails
ADD CONSTRAINT email_user
FOREIGN KEY (user_id) REFERENCES public.users (id)
ON UPDATE CASCADE ON DELETE CASCADE;
END IF;
END $$;