Make complaints master migration idempotent for existing codes

This commit is contained in:
DevSoftTech Server 2026-03-21 16:03:12 +05:30
parent 3e810dec74
commit d2400fc5c2

View File

@ -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);
}
}
}