-- Notification deliveries table (tracks per-device delivery attempts) CREATE TABLE notification_deliveries ( id UUID PRIMARY KEY DEFAULT gen_random_uuid(), notification_receiver_id UUID NOT NULL REFERENCES notification_receivers(id) ON DELETE CASCADE, user_device_id UUID NOT NULL REFERENCES user_devices(id) ON DELETE CASCADE, channel VARCHAR(50) NOT NULL DEFAULT 'push' CHECK (channel IN ('push', 'websocket', 'email')), delivery_status VARCHAR(50) NOT NULL DEFAULT 'pending' CHECK (delivery_status IN ('pending', 'sent', 'delivered', 'failed')), provider VARCHAR(50) CHECK (provider IN ('firebase', 'onesignal')), provider_message_id VARCHAR(255), sent_at TIMESTAMP WITH TIME ZONE, delivered_at TIMESTAMP WITH TIME ZONE, failed_at TIMESTAMP WITH TIME ZONE, failure_reason TEXT, retry_count INT NOT NULL DEFAULT 0, created_at TIMESTAMP WITH TIME ZONE DEFAULT NOW(), updated_at TIMESTAMP WITH TIME ZONE DEFAULT NOW() ); -- Indexes CREATE INDEX idx_notification_deliveries_receiver_id ON notification_deliveries(notification_receiver_id); CREATE INDEX idx_notification_deliveries_device_id ON notification_deliveries(user_device_id); CREATE INDEX idx_notification_deliveries_status ON notification_deliveries(delivery_status); CREATE INDEX idx_notification_deliveries_provider ON notification_deliveries(provider);