gohorsejobs/backend/migrations/041_create_notifications_table_v2.sql
2026-02-15 16:03:40 +00:00

54 lines
1.9 KiB
SQL

-- Migration: Create notifications table
-- Description: Stores user notifications for in-app and push notifications
CREATE TABLE IF NOT EXISTS notifications (
id SERIAL PRIMARY KEY,
user_id INT REFERENCES users(id) ON DELETE CASCADE,
tenant_id VARCHAR(36),
-- Notification content
title VARCHAR(255) NOT NULL,
message TEXT NOT NULL,
type VARCHAR(50) NOT NULL DEFAULT 'info', -- info, success, warning, error, application, job, message
-- Action/Link
action_url VARCHAR(500),
action_label VARCHAR(100),
-- Status
read BOOLEAN DEFAULT false,
read_at TIMESTAMP,
-- Push notification
push_sent BOOLEAN DEFAULT false,
push_sent_at TIMESTAMP,
-- Metadata
metadata JSONB,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);
-- FCM device tokens for push notifications
CREATE TABLE IF NOT EXISTS fcm_tokens (
id SERIAL PRIMARY KEY,
user_id INT NOT NULL REFERENCES users(id) ON DELETE CASCADE,
token VARCHAR(500) NOT NULL,
device_type VARCHAR(20), -- web, android, ios
device_name VARCHAR(100),
active BOOLEAN DEFAULT true,
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
UNIQUE(user_id, token)
);
-- Indexes
CREATE INDEX idx_notifications_user ON notifications(user_id);
CREATE INDEX idx_notifications_tenant ON notifications(tenant_id);
CREATE INDEX idx_notifications_read ON notifications(user_id, read);
CREATE INDEX idx_notifications_type ON notifications(type);
CREATE INDEX idx_notifications_created ON notifications(created_at DESC);
CREATE INDEX idx_fcm_tokens_user ON fcm_tokens(user_id);
CREATE INDEX idx_fcm_tokens_active ON fcm_tokens(user_id, active);
COMMENT ON TABLE notifications IS 'User notifications for in-app display and push notifications';
COMMENT ON TABLE fcm_tokens IS 'Firebase Cloud Messaging device tokens for push notifications';