infoalloggi-monorepo/apps/db/migrations/4_usersstorage.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

36 lines
No EOL
1.1 KiB
SQL

-- User Storage
CREATE TABLE IF NOT EXISTS public.users_storage (
"userId" UUID NOT NULL,
"storageId" TEXT NOT NULL,
from_admin boolean DEFAULT FALSE NOT NULL,
user_storage_id UUID DEFAULT gen_random_uuid () NOT NULL
);
-- Primary Key
DO $$
BEGIN
ALTER TABLE public.users_storage
ADD CONSTRAINT users_storage_pkey PRIMARY KEY (user_storage_id);
EXCEPTION
WHEN invalid_table_definition THEN
RAISE NOTICE 'Primary key already exists. Ignoring...';
END $$;
-- Unique UserId and StorageId
DO $$ BEGIN IF NOT EXISTS (
SELECT 1 FROM pg_constraint
WHERE conname = 'users_storage_user_storage_key'
) THEN
ALTER TABLE public.users_storage
ADD CONSTRAINT users_storage_user_storage_key UNIQUE ("userId", "storageId");
END IF;
END $$;
-- Foreign Key to Users
DO $$ BEGIN IF NOT EXISTS (
SELECT 1 FROM pg_constraint
WHERE conname = 'userstorage'
) THEN
ALTER TABLE public.users_storage
ADD CONSTRAINT userstorage
FOREIGN KEY ("userId") REFERENCES public.users (id)
ON UPDATE CASCADE ON DELETE CASCADE;
END IF;
END $$;