Skip to content

Commit d45fa47

Browse files
Fix: SQLite "too many SQL variables" error in Importer (#97)
* Optimize batch processing in Importer by chunking items into smaller batches of 500 for improved performance during import operations. * Enhance batch processing in Importer to track current and total batches, ensuring UpdateCollectionTreeJob is dispatched only after the final batch is processed. * Keep track of multiple batch IDs * Chunk items into batches & dispatch `UpdateCollectionTreeJob` once all batches have finished * Chunks of 500 * Code formatting --------- Co-authored-by: Duncan McClean <[email protected]>
1 parent dba698e commit d45fa47

File tree

3 files changed

+27
-19
lines changed

3 files changed

+27
-19
lines changed

src/Importer.php

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@
66
use Illuminate\Support\Facades\Bus;
77
use Illuminate\Support\Facades\Cache;
88
use Illuminate\Support\Facades\Schema;
9+
use Illuminate\Support\LazyCollection;
910
use Statamic\Importer\Exceptions\JobBatchesTableMissingException;
1011
use Statamic\Importer\Imports\Import;
1112
use Statamic\Importer\Jobs\ImportItemJob;
@@ -19,6 +20,7 @@ class Importer
1920

2021
public static function run(Import $import): void
2122
{
23+
$import->batchIds([])->save();
2224
Cache::forget("importer.{$import->id}.parents");
2325

2426
if (! Schema::connection(config('queue.batching.database', env('DB_CONNECTION', 'sqlite')))->hasTable(config('queue.batching.table', 'job_batches'))) {
@@ -30,14 +32,18 @@ public static function run(Import $import): void
3032
'xml' => (new Xml($import))->getItems($import->get('path')),
3133
};
3234

33-
Bus::batch($items->map(fn (array $item) => new ImportItemJob($import, $item)))
34-
->before(fn (Batch $batch) => $import->batchId($batch->id)->save())
35-
->finally(function (Batch $batch) use ($import) {
36-
if ($import->get('destination.type') === 'entries') {
37-
UpdateCollectionTreeJob::dispatch($import);
38-
}
39-
})
40-
->dispatch();
35+
$items
36+
->chunk(500)
37+
->each(function (LazyCollection $chunk) use ($import) {
38+
Bus::batch($chunk->map(fn (array $item) => new ImportItemJob($import, $item)))
39+
->before(fn (Batch $batch) => $import->batchIds([...$import->batchIds(), $batch->id])->save())
40+
->finally(function (Batch $batch) use ($import) {
41+
if ($import->allBatchesHaveFinished() && $import->get('destination.type') === 'entries') {
42+
UpdateCollectionTreeJob::dispatch($import);
43+
}
44+
})
45+
->dispatch();
46+
});
4147
}
4248

4349
public static function getTransformer(string $fieldtype): ?string

src/Imports/Import.php

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,8 @@
22

33
namespace Statamic\Importer\Imports;
44

5-
use Illuminate\Bus\PendingBatch;
5+
use Illuminate\Bus\Batch;
6+
use Illuminate\Support\Collection as SupportCollection;
67
use Illuminate\Support\Facades\Bus;
78
use Statamic\Facades\Collection;
89
use Statamic\Facades\Taxonomy;
@@ -20,7 +21,7 @@ class Import
2021
public $id;
2122
public $name;
2223
public $config;
23-
public $batchId;
24+
public $batchIds;
2425

2526
public function __construct()
2627
{
@@ -59,26 +60,27 @@ public function get(string $key, ?string $default = null): mixed
5960
return data_get($this->config, $key, $default);
6061
}
6162

62-
public function batchId($batchId = null)
63+
public function batchIds($batchIds = null)
6364
{
64-
return $this->fluentlyGetOrSet('batchId')->args(func_get_args());
65+
return $this->fluentlyGetOrSet('batchIds')->args(func_get_args());
6566
}
6667

67-
public function batch(): ?PendingBatch
68+
public function batches(): SupportCollection
6869
{
69-
if (! $this->batchId()) {
70-
return null;
71-
}
70+
return collect($this->batchIds())->map(fn ($id): Batch => Bus::findBatch($id));
71+
}
7272

73-
return Bus::batch($this->batchId());
73+
public function allBatchesHaveFinished(): bool
74+
{
75+
return $this->batches()->every(fn (Batch $batch) => $batch->finished());
7476
}
7577

7678
public function fileData(): array
7779
{
7880
return collect([
7981
'name' => $this->name(),
8082
'config' => $this->config()->all(),
81-
'batch_id' => $this->batchId(),
83+
'batch_ids' => $this->batchIds(),
8284
])->filter()->all();
8385
}
8486

src/Imports/ImportRepository.php

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ public function all(): Collection
2323
return $this->make()
2424
->id($id)
2525
->name(Arr::pull($data, 'name'))
26-
->batchId(Arr::pull($data, 'batch_id'))
26+
->batchIds(Arr::pull($data, 'batch_ids'))
2727
->config(Arr::pull($data, 'config'));
2828
})
2929
->sortBy->name()

0 commit comments

Comments
 (0)