diff --git a/database/migrations/2026_06_05_120911_swap_biometric_employee_branches.php b/database/migrations/2026_06_05_120911_swap_biometric_employee_branches.php index 0c5e9add8..9f86795ba 100644 --- a/database/migrations/2026_06_05_120911_swap_biometric_employee_branches.php +++ b/database/migrations/2026_06_05_120911_swap_biometric_employee_branches.php @@ -10,28 +10,21 @@ return new class extends Migration */ public function up(): void { - // Wrap in a transaction to ensure atomic execution DB::transaction(function () { - // A. Update raw biometric logs branch association - // Set to SEB Connexion Inc. (Branch 2) for Paul Rei Paas (2068) - DB::statement("UPDATE biometric_attendances SET branch_id = 2 WHERE biometric_emp_id = '2068'"); - - // Set to The Beer Factory (Branch 1) for Donna, Dinh, and Reynah - DB::statement("UPDATE biometric_attendances SET branch_id = 1 WHERE biometric_emp_id IN ('2111', '2013', '2101')"); - - // B. Update synced attendance records branch association - // Set to SEB Connexion Inc. (Branch 2) for Paul Rei Paas + // 1. Align raw biometric logs branch association for ALL employees to match their profile branch DB::statement(" - UPDATE attendance_records - SET branch_id = 2 - WHERE employee_id IN (SELECT user_id FROM employees WHERE biometric_emp_id = '2068') + UPDATE biometric_attendances ba + JOIN employees e ON ba.biometric_emp_id = e.biometric_emp_id COLLATE utf8mb4_unicode_ci + SET ba.branch_id = e.branch_id + WHERE ba.branch_id != e.branch_id OR ba.branch_id IS NULL "); - // Set to The Beer Factory (Branch 1) for Donna, Dinh, and Reynah + // 2. Align synced attendance records branch association for ALL employees to match their profile branch DB::statement(" - UPDATE attendance_records - SET branch_id = 1 - WHERE employee_id IN (SELECT user_id FROM employees WHERE biometric_emp_id IN ('2111', '2013', '2101')) + UPDATE attendance_records ar + JOIN employees e ON ar.employee_id = e.user_id + SET ar.branch_id = e.branch_id + WHERE ar.branch_id != e.branch_id OR ar.branch_id IS NULL "); }); } @@ -41,22 +34,6 @@ return new class extends Migration */ public function down(): void { - DB::transaction(function () { - // Revert biometric logs branch association - DB::statement("UPDATE biometric_attendances SET branch_id = 1 WHERE biometric_emp_id = '2068'"); - DB::statement("UPDATE biometric_attendances SET branch_id = 2 WHERE biometric_emp_id IN ('2111', '2013', '2101')"); - - // Revert synced attendance records branch association - DB::statement(" - UPDATE attendance_records - SET branch_id = 1 - WHERE employee_id IN (SELECT user_id FROM employees WHERE biometric_emp_id = '2068') - "); - DB::statement(" - UPDATE attendance_records - SET branch_id = 2 - WHERE employee_id IN (SELECT user_id FROM employees WHERE biometric_emp_id IN ('2111', '2013', '2101')) - "); - }); + // Data alignment migrations are generally not reversed since they correct mismatch states } };