Files
saqel/backend/migrations/20260909_watch_sessions.sql
T

16 lines
2.2 KiB
SQL

-- Must run before revenue periods because verified ratings reference watch_sessions.
CREATE TABLE IF NOT EXISTS `watch_sessions` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `uuid` CHAR(36) NOT NULL, `student_id` BIGINT UNSIGNED NOT NULL, `video_version_id` BIGINT UNSIGNED NOT NULL,
`funding_source` ENUM('school','marketplace','none') NOT NULL, `server_started_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `server_ended_at` TIMESTAMP NULL, `status` ENUM('active','ended','invalidated') NOT NULL DEFAULT 'active',
PRIMARY KEY (`id`), UNIQUE KEY `uq_watch_session_uuid` (`uuid`), KEY `idx_watch_student_version` (`student_id`,`video_version_id`,`status`),
CONSTRAINT `fk_watch_student` FOREIGN KEY (`student_id`) REFERENCES `students` (`id`) ON DELETE RESTRICT, CONSTRAINT `fk_watch_version` FOREIGN KEY (`video_version_id`) REFERENCES `video_versions` (`id`) ON DELETE RESTRICT
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `watch_events` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `watch_session_id` BIGINT UNSIGNED NOT NULL, `sequence_no` INT UNSIGNED NOT NULL, `event_type` ENUM('start','heartbeat','pause','seek','resume','end','buffer') NOT NULL, `client_position_ms` BIGINT UNSIGNED NOT NULL, `server_received_at` TIMESTAMP NOT NULL DEFAULT CURRENT_TIMESTAMP, `payload_json` JSON NULL,
PRIMARY KEY (`id`), UNIQUE KEY `uq_watch_event_sequence` (`watch_session_id`,`sequence_no`), CONSTRAINT `fk_watch_event_session` FOREIGN KEY (`watch_session_id`) REFERENCES `watch_sessions` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;
CREATE TABLE IF NOT EXISTS `eligible_watch_intervals` (
`id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `watch_session_id` BIGINT UNSIGNED NOT NULL, `start_ms` BIGINT UNSIGNED NOT NULL, `end_ms` BIGINT UNSIGNED NOT NULL, `eligible_seconds` INT UNSIGNED NOT NULL, `reason_code` VARCHAR(80) NOT NULL, `policy_version` VARCHAR(64) NOT NULL, `status` ENUM('measured','excluded','review') NOT NULL DEFAULT 'measured',
PRIMARY KEY (`id`), UNIQUE KEY `uq_eligible_interval` (`watch_session_id`,`start_ms`,`end_ms`,`policy_version`), CONSTRAINT `fk_eligible_session` FOREIGN KEY (`watch_session_id`) REFERENCES `watch_sessions` (`id`) ON DELETE CASCADE
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4;