59 lines
2.8 KiB
MySQL
59 lines
2.8 KiB
MySQL
|
|
-- Migration: Table read_receipts pour le système de read receipts
|
||
|
|
-- Création: 2025-12-05
|
||
|
|
-- Version: 1.0.0
|
||
|
|
|
||
|
|
-- ================================================================
|
||
|
|
-- TABLE READ RECEIPTS
|
||
|
|
-- ================================================================
|
||
|
|
|
||
|
|
-- Table pour tracker les read receipts (marquage de messages comme lus)
|
||
|
|
CREATE TABLE IF NOT EXISTS read_receipts (
|
||
|
|
id UUID PRIMARY KEY DEFAULT gen_random_uuid(),
|
||
|
|
message_id UUID NOT NULL REFERENCES messages(id) ON DELETE CASCADE,
|
||
|
|
user_id UUID NOT NULL REFERENCES users(id) ON DELETE CASCADE,
|
||
|
|
conversation_id UUID NOT NULL REFERENCES conversations(id) ON DELETE CASCADE,
|
||
|
|
read_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
|
||
|
|
created_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
|
||
|
|
updated_at TIMESTAMP WITH TIME ZONE NOT NULL DEFAULT NOW(),
|
||
|
|
|
||
|
|
-- Un utilisateur ne peut avoir qu'un seul read receipt par message
|
||
|
|
UNIQUE(message_id, user_id)
|
||
|
|
);
|
||
|
|
|
||
|
|
-- ================================================================
|
||
|
|
-- INDEX POUR PERFORMANCE
|
||
|
|
-- ================================================================
|
||
|
|
|
||
|
|
-- Index pour rechercher les read receipts par message
|
||
|
|
CREATE INDEX IF NOT EXISTS idx_read_receipts_message_id ON read_receipts(message_id);
|
||
|
|
|
||
|
|
-- Index pour rechercher les read receipts par utilisateur
|
||
|
|
CREATE INDEX IF NOT EXISTS idx_read_receipts_user_id ON read_receipts(user_id);
|
||
|
|
|
||
|
|
-- Index pour rechercher les read receipts par conversation
|
||
|
|
CREATE INDEX IF NOT EXISTS idx_read_receipts_conversation_id ON read_receipts(conversation_id);
|
||
|
|
|
||
|
|
-- Index composite pour les requêtes fréquentes (dernière lecture dans une conversation)
|
||
|
|
CREATE INDEX IF NOT EXISTS idx_read_receipts_conversation_user ON read_receipts(conversation_id, user_id, read_at DESC);
|
||
|
|
|
||
|
|
-- Index pour les requêtes de comptage de messages non lus
|
||
|
|
CREATE INDEX IF NOT EXISTS idx_read_receipts_message_user ON read_receipts(message_id, user_id);
|
||
|
|
|
||
|
|
-- ================================================================
|
||
|
|
-- TRIGGERS POUR MISE À JOUR AUTOMATIQUE
|
||
|
|
-- ================================================================
|
||
|
|
|
||
|
|
CREATE TRIGGER update_read_receipts_updated_at BEFORE UPDATE ON read_receipts
|
||
|
|
FOR EACH ROW EXECUTE FUNCTION update_updated_at_column();
|
||
|
|
|
||
|
|
-- ================================================================
|
||
|
|
-- COMMENTAIRES POUR DOCUMENTATION
|
||
|
|
-- ================================================================
|
||
|
|
|
||
|
|
COMMENT ON TABLE read_receipts IS 'Table des read receipts pour tracker quels messages ont été lus par quels utilisateurs';
|
||
|
|
COMMENT ON COLUMN read_receipts.message_id IS 'ID du message marqué comme lu';
|
||
|
|
COMMENT ON COLUMN read_receipts.user_id IS 'ID de l''utilisateur qui a lu le message';
|
||
|
|
COMMENT ON COLUMN read_receipts.conversation_id IS 'ID de la conversation (pour optimiser les requêtes)';
|
||
|
|
COMMENT ON COLUMN read_receipts.read_at IS 'Timestamp de la lecture du message';
|
||
|
|
|