Rustelo/resources/user-dashboard/migrations/007_message_status_postgres.sql
Jesús Pérez f1010e9d07
chore: update
2026-07-18 20:16:16 +01:00

19 lines
806 B
SQL
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

-- Migration: 007 Enhance user_messages with status lifecycle and timestamps
-- Database: PostgreSQL
--
-- Adds: status (sent | read | deleted), received_at, deleted_at
-- Existing rows are backfilled to is_read=true → status='read', else status='sent'.
ALTER TABLE user_messages
ADD COLUMN IF NOT EXISTS status TEXT NOT NULL DEFAULT 'sent',
ADD COLUMN IF NOT EXISTS received_at TIMESTAMPTZ,
ADD COLUMN IF NOT EXISTS deleted_at TIMESTAMPTZ;
-- Backfill: rows already marked is_read should be 'read' with received_at = created_at.
UPDATE user_messages
SET status = 'read',
received_at = created_at
WHERE is_read = true AND status = 'sent';
CREATE INDEX IF NOT EXISTS idx_user_messages_status ON user_messages(user_id, status)
WHERE status != 'deleted';