-- Migration: Add notification system tables -- Main notifications table CREATE TABLE IF NOT EXISTS db_notifications ( notification_id INT AUTO_INCREMENT PRIMARY KEY, usr_id INT NOT NULL, type ENUM('comment', 'like', 'subscribe', 'video_upload', 'mention', 'system') NOT NULL, title VARCHAR(255) NOT NULL, message TEXT, link VARCHAR(512), actor_id INT, related_video_id INT, related_comment_id INT, is_read TINYINT(1) DEFAULT 0, is_seen TINYINT(1) DEFAULT 0, email_sent TINYINT(1) DEFAULT 0, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, read_at DATETIME NULL, INDEX idx_user_read (usr_id, is_read), INDEX idx_user_created (usr_id, created_at), INDEX idx_type (type), INDEX idx_actor (actor_id), FOREIGN KEY (usr_id) REFERENCES db_accountuser(usr_id) ON DELETE CASCADE, FOREIGN KEY (actor_id) REFERENCES db_accountuser(usr_id) ON DELETE SET NULL ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- User notification preferences CREATE TABLE IF NOT EXISTS db_notification_preferences ( preference_id INT AUTO_INCREMENT PRIMARY KEY, usr_id INT NOT NULL UNIQUE, email_comments TINYINT(1) DEFAULT 1, email_likes TINYINT(1) DEFAULT 1, email_subscribes TINYINT(1) DEFAULT 1, email_uploads TINYINT(1) DEFAULT 1, email_mentions TINYINT(1) DEFAULT 1, email_digest ENUM('instant', 'hourly', 'daily', 'weekly', 'never') DEFAULT 'instant', push_enabled TINYINT(1) DEFAULT 1, push_comments TINYINT(1) DEFAULT 1, push_likes TINYINT(1) DEFAULT 1, push_subscribes TINYINT(1) DEFAULT 1, updated_at DATETIME DEFAULT CURRENT_TIMESTAMP ON UPDATE CURRENT_TIMESTAMP, FOREIGN KEY (usr_id) REFERENCES db_accountuser(usr_id) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- Email queue for batched/delayed sending CREATE TABLE IF NOT EXISTS db_email_queue ( queue_id INT AUTO_INCREMENT PRIMARY KEY, usr_id INT NOT NULL, email VARCHAR(255) NOT NULL, subject VARCHAR(255) NOT NULL, body TEXT NOT NULL, scheduled_for DATETIME NOT NULL, sent_at DATETIME NULL, attempts INT DEFAULT 0, last_error TEXT NULL, created_at DATETIME DEFAULT CURRENT_TIMESTAMP, INDEX idx_scheduled (scheduled_for, sent_at), INDEX idx_user (usr_id), FOREIGN KEY (usr_id) REFERENCES db_accountuser(usr_id) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci;