From 313e99402537471383dc53d9da34a726c30cb9e7 Mon Sep 17 00:00:00 2001 From: Hamza-Ayed Date: Wed, 9 Sep 2026 17:07:17 +0300 Subject: [PATCH] Make legacy migrations safe on canonical schema --- .../20260908_runtime_schema_consolidation.sql | 8 -------- .../20260908_secure_identity_and_staff.sql | 20 +++---------------- 2 files changed, 3 insertions(+), 25 deletions(-) diff --git a/backend/migrations/20260908_runtime_schema_consolidation.sql b/backend/migrations/20260908_runtime_schema_consolidation.sql index acb74a3..28e8faa 100644 --- a/backend/migrations/20260908_runtime_schema_consolidation.sql +++ b/backend/migrations/20260908_runtime_schema_consolidation.sql @@ -1,13 +1,6 @@ -- Consolidates additive schema previously created lazily by request handlers. -- Run once on staging/production before disabling runtime DDL. -ALTER TABLE `teachers` - ADD COLUMN `grades_taught` TEXT NULL, - ADD COLUMN `school_name` VARCHAR(255) NULL; - -ALTER TABLE `exam_attempts` - ADD COLUMN `completed_at` TIMESTAMP NULL DEFAULT NULL AFTER `ai_diagnostic_report`; - CREATE TABLE IF NOT EXISTS `teacher_reviews` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT, `teacher_id` BIGINT UNSIGNED NOT NULL, @@ -31,4 +24,3 @@ CREATE TABLE IF NOT EXISTS `teacher_reviews` ( CONSTRAINT `fk_tr_teacher` FOREIGN KEY (`teacher_id`) REFERENCES `teachers` (`id`) ON DELETE CASCADE, CONSTRAINT `fk_tr_student` FOREIGN KEY (`student_id`) REFERENCES `students` (`id`) ON DELETE CASCADE ) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; - diff --git a/backend/migrations/20260908_secure_identity_and_staff.sql b/backend/migrations/20260908_secure_identity_and_staff.sql index 77ac559..1ad77b0 100644 --- a/backend/migrations/20260908_secure_identity_and_staff.sql +++ b/backend/migrations/20260908_secure_identity_and_staff.sql @@ -1,20 +1,6 @@ -ALTER TABLE `students` - DROP INDEX `national_id`, - DROP INDEX `idx_students_national_id`, - MODIFY `national_id` TEXT NOT NULL, - ADD COLUMN `national_id_hash` CHAR(64) NULL AFTER `national_id`, - ADD INDEX `idx_students_national_id_hash` (`national_id_hash`), - MODIFY `readiness_score` DECIMAL(5,2) NOT NULL DEFAULT 0.00; - -ALTER TABLE `school_rosters` - DROP INDEX `idx_school_national_id`, - DROP INDEX `idx_roster_national_id`, - MODIFY `national_id` TEXT NOT NULL, - ADD COLUMN `national_id_hash` CHAR(64) NULL AFTER `national_id`, - ADD INDEX `idx_roster_national_id_hash` (`national_id_hash`); - --- Backfill national_id_hash from the application before making these columns --- NOT NULL and UNIQUE. The encryption key must remain the production key. +-- The canonical schema already contains national_id_hash. Existing installations +-- therefore only need the additive staff/link tables below; no request-time or +-- repeated ALTER is performed here. CREATE TABLE IF NOT EXISTS `staff_accounts` ( `id` BIGINT UNSIGNED NOT NULL AUTO_INCREMENT,