Files
opensourcepos/app/Models/ImportBatchContext.php
objecttothis c365e6e6dd feat(jobs): add batch-level import context and queue pause support
- Add import_batch_contexts table plus ImportBatchContext model so
  per-batch data (e.g. Items CSV attribute definitions) is stored
  once and referenced by context_id, instead of duplicated into
  every queued row's job payload.
- Extend queue_jobs, queue_jobs_failed, and new queue_paused_jobs
  tables with batch_id/context_id columns and FKs (ON DELETE
  RESTRICT) back to import_batch_contexts, so context rows cannot
  be purged while any job still references them.
- Introduce ImportBatchService.pushRow() to centralize queuing a
  batch row with batch/context stamping and priority; update
  Customers and Items controllers to use it instead of calling
  importBatch->create() and queue->push() separately.
- Add queue_paused_jobs table and Jobs::postPauseJobs()/pauseQueues()
  to move pending jobs out of queue_jobs into a paused table per
  queue (all core queues or a selected subset), leaving in-flight
  jobs untouched (issue #3833 pause/resume design).
- Change Jobs::getEditJob()/postSaveJob() routes to accept separate
  source/id path segments instead of a combined uid string.
- Add test coverage for ImportBatchContext, ImportBatchService, and
  ImportBatch (including purge-retains-while-referenced guards).

Signed-off-by: objecttothis <17935339+objecttothis@users.noreply.github.com>
2026-10-07 17:55:22 +04:00

56 lines
1.5 KiB
PHP

<?php
namespace App\Models;
use CodeIgniter\Model;
/**
* Stores a one-time-per-batch context snapshot (e.g. attribute definitions
* for an Items CSV import), so row-level jobs read it once by batch_id
* instead of carrying a duplicate copy in every row's queued job payload.
* queue_jobs/queue_jobs_failed/queue_paused_jobs all FK their context_id
* (ON DELETE RESTRICT) here, so a row can only be deleted once no job
* anywhere still references it.
*/
class ImportBatchContext extends Model
{
protected $table = 'import_batch_contexts';
protected $primaryKey = 'id';
protected $useTimestamps = false;
protected $allowedFields = [
'batch_id',
'context',
'created_at',
];
public function create(string $batchId, array $context): int
{
$this->insert([
'batch_id' => $batchId,
'context' => json_encode($context),
'created_at' => date('Y-m-d H:i:s'),
]);
return $this->getInsertID();
}
public function getByBatchId(string $batchId): ?array
{
$row = $this->where('batch_id', $batchId)->first();
return $row === null ? null : json_decode($row['context'], true);
}
public function getIdForBatch(string $batchId): ?int
{
$row = $this->where('batch_id', $batchId)->select('id')->first();
return $row === null ? null : (int)$row['id'];
}
public function deleteForBatch(string $batchId): void
{
$this->where('batch_id', $batchId)->delete();
}
}