veza/veza-backend-api/migrations/047_notifications.sql

37 lines
1.5 KiB
SQL

-- 047_notifications.sql
-- User Notifications (BE-DB-008: Create migration for notifications table)
-- Add table for user notifications
-- === NOTIFICATIONS ===
-- Table for storing user notifications
CREATE TABLE IF NOT EXISTS public.notifications (
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
user_id UUID NOT NULL REFERENCES public.users(id) ON DELETE CASCADE,
-- Notification Content
type VARCHAR(50) NOT NULL,
title VARCHAR(255) NOT NULL,
content TEXT,
link VARCHAR(500),
-- Status
read BOOLEAN NOT NULL DEFAULT FALSE,
-- Timestamps
created_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
updated_at TIMESTAMPTZ NOT NULL DEFAULT NOW(),
read_at TIMESTAMPTZ
);
-- Indexes
CREATE INDEX IF NOT EXISTS idx_notifications_user_id ON public.notifications(user_id);
CREATE INDEX IF NOT EXISTS idx_notifications_user_id_read ON public.notifications(user_id, read) WHERE read = FALSE;
CREATE INDEX IF NOT EXISTS idx_notifications_created_at_desc ON public.notifications(created_at DESC);
CREATE INDEX IF NOT EXISTS idx_notifications_type ON public.notifications(type);
-- Comments
COMMENT ON TABLE public.notifications IS 'User notifications for various events';
COMMENT ON COLUMN public.notifications.type IS 'Type of notification (e.g., follow, like, comment, message)';
COMMENT ON COLUMN public.notifications.read IS 'Whether the notification has been read';
COMMENT ON COLUMN public.notifications.read_at IS 'Timestamp when the notification was marked as read';