20 lines
806 B
MySQL
20 lines
806 B
MySQL
|
|
-- 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';
|