diff --git a/backend/database_schema.sql b/backend/database_schema.sql index 3ce7ee3..1317952 100644 --- a/backend/database_schema.sql +++ b/backend/database_schema.sql @@ -8,13 +8,15 @@ SET FOREIGN_KEY_CHECKS = 0; -- ------------------------------------------------------------------------------ -- DROP ALL EXISTING TABLES IN REVERSE ORDER TO PREVENT FOREIGN KEY CONFLICTS +-- ------------------------------------------------------------------------------ +DROP TABLE IF EXISTS `chat_messages`; +DROP TABLE IF EXISTS `student_mastery_analytics`; DROP TABLE IF EXISTS `student_question_answers`; DROP TABLE IF EXISTS `exam_attempts`; DROP TABLE IF EXISTS `question_options`; DROP TABLE IF EXISTS `questions`; DROP TABLE IF EXISTS `exams`; DROP TABLE IF EXISTS `quizzes`; -DROP TABLE IF EXISTS `chat_messages`; DROP TABLE IF EXISTS `otp_verifications`; DROP TABLE IF EXISTS `user_devices`; DROP TABLE IF EXISTS `voice_notes`; @@ -31,7 +33,7 @@ DROP TABLE IF EXISTS `schools`; -- ------------------------------------------------------------------------------ -- 1. Table: schools (المدارس والجهات الشريكة - B2B مثل الثقافة العسكرية) -- ------------------------------------------------------------------------------ -CREATE TABLE `schools` ( +CREATE TABLE IF NOT EXISTS `schools` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `uuid` CHAR(36) NOT NULL, `name` VARCHAR(255) NOT NULL, @@ -50,7 +52,7 @@ CREATE TABLE `schools` ( -- ------------------------------------------------------------------------------ -- 2. Table: users (المستخدمون الموحدون - طلاب، معلمون، أولياء أمور، إدارة) -- ------------------------------------------------------------------------------ -CREATE TABLE `users` ( +CREATE TABLE IF NOT EXISTS `users` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `uuid` CHAR(36) NOT NULL, `full_name` TEXT NOT NULL, @@ -75,7 +77,7 @@ CREATE TABLE `users` ( -- ------------------------------------------------------------------------------ -- 3. Table: guardian_student (ربط أولياء الأمور بالأبناء - N:M) -- ------------------------------------------------------------------------------ -CREATE TABLE `guardian_student` ( +CREATE TABLE IF NOT EXISTS `guardian_student` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `guardian_id` BIGINT UNSIGNED NOT NULL, `student_id` BIGINT UNSIGNED NOT NULL, @@ -92,7 +94,7 @@ CREATE TABLE `guardian_student` ( -- ------------------------------------------------------------------------------ -- 4. Table: teacher_profiles (ملفات المعلمين ونسب الأرباح) -- ------------------------------------------------------------------------------ -CREATE TABLE `teacher_profiles` ( +CREATE TABLE IF NOT EXISTS `teacher_profiles` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `user_id` BIGINT UNSIGNED NOT NULL, `bio` TEXT DEFAULT NULL, @@ -109,7 +111,7 @@ CREATE TABLE `teacher_profiles` ( -- ------------------------------------------------------------------------------ -- 5. Table: subjects (المواد التعليمية) -- ------------------------------------------------------------------------------ -CREATE TABLE `subjects` ( +CREATE TABLE IF NOT EXISTS `subjects` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `name` VARCHAR(150) NOT NULL, `code` VARCHAR(50) NOT NULL UNIQUE, @@ -122,7 +124,7 @@ CREATE TABLE `subjects` ( -- ------------------------------------------------------------------------------ -- 6. Table: courses (الدورات التدريبية) -- ------------------------------------------------------------------------------ -CREATE TABLE `courses` ( +CREATE TABLE IF NOT EXISTS `courses` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `uuid` CHAR(36) NOT NULL, `subject_id` BIGINT UNSIGNED NOT NULL, @@ -146,7 +148,7 @@ CREATE TABLE `courses` ( -- ------------------------------------------------------------------------------ -- 7. Table: lessons (الدروس والفيديوهات المربوطة بـ Bunny Stream) -- ------------------------------------------------------------------------------ -CREATE TABLE `lessons` ( +CREATE TABLE IF NOT EXISTS `lessons` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `course_id` BIGINT UNSIGNED NOT NULL, `title` VARCHAR(255) NOT NULL, @@ -164,7 +166,7 @@ CREATE TABLE `lessons` ( -- ------------------------------------------------------------------------------ -- 8. Table: exams (الامتحانات الشاملة: درس، وحدة، فصل، ومحاكاة وزاري) -- ------------------------------------------------------------------------------ -CREATE TABLE `exams` ( +CREATE TABLE IF NOT EXISTS `exams` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `uuid` CHAR(36) NOT NULL, `course_id` BIGINT UNSIGNED NOT NULL, @@ -197,7 +199,7 @@ CREATE TABLE `exams` ( -- ------------------------------------------------------------------------------ -- 9. Table: questions (الأسئلة والتحليل المعرفي للذكاء الاصطناعي) -- ------------------------------------------------------------------------------ -CREATE TABLE `questions` ( +CREATE TABLE IF NOT EXISTS `questions` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `uuid` CHAR(36) NOT NULL, `exam_id` BIGINT UNSIGNED NOT NULL, @@ -219,7 +221,7 @@ CREATE TABLE `questions` ( -- ------------------------------------------------------------------------------ -- 10. Table: question_options (خيارات الإجابة والتغذية الراجعة) -- ------------------------------------------------------------------------------ -CREATE TABLE `question_options` ( +CREATE TABLE IF NOT EXISTS `question_options` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `question_id` BIGINT UNSIGNED NOT NULL, `option_text` TEXT NOT NULL, @@ -233,7 +235,7 @@ CREATE TABLE `question_options` ( -- ------------------------------------------------------------------------------ -- 11. Table: exam_attempts (محاولات ونتائج الامتحانات وتحليل الذكاء الاصطناعي) -- ------------------------------------------------------------------------------ -CREATE TABLE `exam_attempts` ( +CREATE TABLE IF NOT EXISTS `exam_attempts` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `uuid` CHAR(36) NOT NULL, `student_id` BIGINT UNSIGNED NOT NULL, @@ -259,7 +261,7 @@ CREATE TABLE `exam_attempts` ( -- ------------------------------------------------------------------------------ -- 12. Table: student_question_answers (تفاصيل إجابات الطالب على كل سؤال) -- ------------------------------------------------------------------------------ -CREATE TABLE `student_question_answers` ( +CREATE TABLE IF NOT EXISTS `student_question_answers` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `attempt_id` BIGINT UNSIGNED NOT NULL, `student_id` BIGINT UNSIGNED NOT NULL, @@ -282,7 +284,7 @@ CREATE TABLE `student_question_answers` ( -- ------------------------------------------------------------------------------ -- 13. Table: student_mastery_analytics (مؤشر الفهم التراكمي والجاهزية للوزاري) -- ------------------------------------------------------------------------------ -CREATE TABLE `student_mastery_analytics` ( +CREATE TABLE IF NOT EXISTS `student_mastery_analytics` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `student_id` BIGINT UNSIGNED NOT NULL, `course_id` BIGINT UNSIGNED NOT NULL, @@ -305,9 +307,9 @@ CREATE TABLE `student_mastery_analytics` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- ------------------------------------------------------------------------------ --- 11. Table: lesson_progress (سجل متابعة ونبض المشاهدة) +-- 14. Table: lesson_progress (سجل متابعة ونبض المشاهدة) -- ------------------------------------------------------------------------------ -CREATE TABLE `lesson_progress` ( +CREATE TABLE IF NOT EXISTS `lesson_progress` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `user_id` BIGINT UNSIGNED NOT NULL, `lesson_id` BIGINT UNSIGNED NOT NULL, @@ -324,9 +326,9 @@ CREATE TABLE `lesson_progress` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- ------------------------------------------------------------------------------ --- 12. Table: voice_notes (الملاحظات الصوتية والتفريغ الذكي) +-- 15. Table: voice_notes (الملاحظات الصوتية والتفريغ الذكي) -- ------------------------------------------------------------------------------ -CREATE TABLE `voice_notes` ( +CREATE TABLE IF NOT EXISTS `voice_notes` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `uuid` CHAR(36) NOT NULL, `user_id` BIGINT UNSIGNED NOT NULL, @@ -344,9 +346,9 @@ CREATE TABLE `voice_notes` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- ------------------------------------------------------------------------------ --- 13. Table: user_devices (بصمة الأجهزة المربوطة بالجلسات) +-- 16. Table: user_devices (بصمة الأجهزة المربوطة بالجلسات) -- ------------------------------------------------------------------------------ -CREATE TABLE `user_devices` ( +CREATE TABLE IF NOT EXISTS `user_devices` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `user_id` BIGINT UNSIGNED NOT NULL, `device_fingerprint` VARCHAR(64) NOT NULL, @@ -361,9 +363,9 @@ CREATE TABLE `user_devices` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- ------------------------------------------------------------------------------ --- 14. Table: otp_verifications (رموز التحقق عبر منصة صقل) +-- 17. Table: otp_verifications (رموز التحقق عبر منصة صقل) -- ------------------------------------------------------------------------------ -CREATE TABLE `otp_verifications` ( +CREATE TABLE IF NOT EXISTS `otp_verifications` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `phone_hash` VARCHAR(64) NOT NULL, `otp_code_hash` VARCHAR(255) NOT NULL, @@ -376,9 +378,9 @@ CREATE TABLE `otp_verifications` ( ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; -- ------------------------------------------------------------------------------ --- 15. Table: chat_messages (المحادثات المباشرة بين الطلاب والمعلمين) +-- 18. Table: chat_messages (المحادثات المباشرة بين الطلاب والمعلمين) -- ------------------------------------------------------------------------------ -CREATE TABLE `chat_messages` ( +CREATE TABLE IF NOT EXISTS `chat_messages` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `uuid` CHAR(36) NOT NULL, `sender_id` BIGINT UNSIGNED NOT NULL,