33 lines
1.3 KiB
MySQL
33 lines
1.3 KiB
MySQL
|
|
-- Migration: Add Messaging System
|
||
|
|
-- Creates tables for customer-admin conversations and messages
|
||
|
|
|
||
|
|
-- Conversations table
|
||
|
|
CREATE TABLE IF NOT EXISTS conversations (
|
||
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||
|
|
user_id INT NOT NULL,
|
||
|
|
product_id INT NULL,
|
||
|
|
subject VARCHAR(255) NOT NULL,
|
||
|
|
status ENUM('open', 'closed') DEFAULT 'open',
|
||
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||
|
|
updated_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP,
|
||
|
|
FOREIGN KEY (user_id) REFERENCES users(id) ON DELETE CASCADE,
|
||
|
|
FOREIGN KEY (product_id) REFERENCES products(id) ON DELETE SET NULL,
|
||
|
|
INDEX idx_user_status (user_id, status),
|
||
|
|
INDEX idx_updated (updated_at DESC)
|
||
|
|
);
|
||
|
|
|
||
|
|
-- Messages table
|
||
|
|
CREATE TABLE IF NOT EXISTS messages (
|
||
|
|
id INT AUTO_INCREMENT PRIMARY KEY,
|
||
|
|
conversation_id INT NOT NULL,
|
||
|
|
sender_id INT NOT NULL,
|
||
|
|
sender_role ENUM('customer', 'admin') NOT NULL,
|
||
|
|
content TEXT NOT NULL,
|
||
|
|
is_read BOOLEAN DEFAULT FALSE,
|
||
|
|
created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
||
|
|
FOREIGN KEY (conversation_id) REFERENCES conversations(id) ON DELETE CASCADE,
|
||
|
|
FOREIGN KEY (sender_id) REFERENCES users(id) ON DELETE CASCADE,
|
||
|
|
INDEX idx_conversation (conversation_id, created_at),
|
||
|
|
INDEX idx_unread (conversation_id, is_read)
|
||
|
|
);
|