From d2400fc5c268f6f4946020a62fb8cab13750e993 Mon Sep 17 00:00:00 2001 From: DevSoftTech Server Date: Sat, 21 Mar 2026 16:03:12 +0530 Subject: [PATCH] Make complaints master migration idempotent for existing codes --- ...-21-000003_CreateComplaintsMasterTable.php | 19 ++++++++++++++++++- 1 file changed, 18 insertions(+), 1 deletion(-) diff --git a/app/Database/Migrations/2026-02-21-000003_CreateComplaintsMasterTable.php b/app/Database/Migrations/2026-02-21-000003_CreateComplaintsMasterTable.php index f82a4fa..4bc46d3 100644 --- a/app/Database/Migrations/2026-02-21-000003_CreateComplaintsMasterTable.php +++ b/app/Database/Migrations/2026-02-21-000003_CreateComplaintsMasterTable.php @@ -124,7 +124,24 @@ class CreateComplaintsMasterTable extends Migration unset($row); if (! empty($rows)) { - $this->db->table('complaints_master')->insertBatch($rows); + $existingCodes = $this->db->table('complaints_master') + ->select('Code') + ->get() + ->getResultArray(); + + $existingCodeMap = []; + foreach ($existingCodes as $existing) { + $existingCodeMap[(int) ($existing['Code'] ?? 0)] = true; + } + + $newRows = array_values(array_filter( + $rows, + static fn(array $row): bool => ! isset($existingCodeMap[(int) ($row['Code'] ?? 0)]) + )); + + if (! empty($newRows)) { + $this->db->table('complaints_master')->insertBatch($newRows); + } } }