Skip to content

Commit

Permalink
Merge pull request #34 from lapaliv/develop
Browse files Browse the repository at this point in the history
Added method `Bulk::withTrashed()`
  • Loading branch information
lapaliv committed Jun 4, 2023
2 parents f1839ae + 9c52cf3 commit 1944084
Show file tree
Hide file tree
Showing 9 changed files with 45 additions and 5 deletions.
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -528,6 +528,13 @@ class Bulk {
*/
public function updateAllExcept(array $attributes): static;

/**
* Enables soft deleted rows into select.
*
* @return $this
*/
public function withTrashed(): static;

/**
* Creates the rows.
* @param iterable<int|string, Model|stdClass|array<string, mixed>|object> $rows
Expand Down
15 changes: 15 additions & 0 deletions src/Bulk.php
Original file line number Diff line number Diff line change
Expand Up @@ -95,6 +95,7 @@ class Bulk
* @var string[]
*/
private array $updateExcept = [];
private bool $withTrashed = false;

/**
* @param class-string<TModel>|TModel $model
Expand Down Expand Up @@ -642,6 +643,18 @@ public function saveAccumulated(): static
->upsertAccumulated();
}

/**
* Adds soft deleted rows to queries.
*
* @return $this
*/
public function withTrashed(): static
{
$this->withTrashed = true;

return $this;
}

/**
* Accumulates the rows to the storage.
*
Expand Down Expand Up @@ -865,6 +878,7 @@ private function runUpdateScenario(BulkAccumulationEntity $accumulation, array $
$this->getDateFields(),
$this->getSelectColumns($columns, $accumulation->uniqueBy),
$this->getDeletedAtColumn(),
$this->withTrashed,
);

unset($scenario);
Expand Down Expand Up @@ -899,6 +913,7 @@ private function runUpsertScenario(BulkAccumulationEntity $accumulation, array $
$this->getDateFields(),
$this->getSelectColumns($columns, $accumulation->uniqueBy),
$this->getDeletedAtColumn(),
$this->withTrashed,
);

unset($scenario);
Expand Down
2 changes: 2 additions & 0 deletions src/Features/MarkNonexistentRowsAsSkippedFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ public function handle(
BulkAccumulationEntity $data,
array $selectColumns,
?string $deletedAtColumn,
bool $withTrashed,
): void {
$nonexistent = new BulkAccumulationEntity($data->uniqueBy);

Expand All @@ -43,6 +44,7 @@ public function handle(
$data->uniqueBy,
$selectColumns,
$deletedAtColumn,
$withTrashed,
)
);
}
Expand Down
3 changes: 2 additions & 1 deletion src/Features/SelectExistingRowsFeature.php
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,13 @@ public function handle(
array $uniqueBy,
array $selectColumns,
?string $deletedAtColumn = null,
bool $withTrashed = false,
): Collection {
$builder = $eloquent->newQuery()
->select($selectColumns)
->limit($collection->count());

if ($deletedAtColumn !== null) {
if ($withTrashed && $deletedAtColumn !== null) {
call_user_func([$builder, 'withTrashed']);
}

Expand Down
11 changes: 10 additions & 1 deletion src/Scenarios/CreateScenario.php
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@
*/
class CreateScenario
{
private bool $hasDeletingRows = false;

public function __construct(
private GetInsertBuilderFeature $getInsertBuilderFeature,
private BulkDriverManager $driverManager,
Expand Down Expand Up @@ -112,7 +114,8 @@ public function handle(
$data->getNotSkippedModels('skipCreating'),
$data->uniqueBy,
$selectColumns,
$deletedAtColumn
$deletedAtColumn,
withTrashed: $this->hasDeletingRows,
);
}

Expand Down Expand Up @@ -220,6 +223,8 @@ private function dispatchDeletingEvents(
BulkEventDispatcher $eventDispatcher,
string $deletedAtColumn
): void {
$this->hasDeletingRows = false;

if (!$eventDispatcher->hasListeners(BulkEventEnum::delete())) {
return;
}
Expand All @@ -237,6 +242,7 @@ private function dispatchDeletingEvents(
}

$accumulatedRow->isDeleting = true;
$this->hasDeletingRows = true;

if ($eventDispatcher->dispatch(BulkEventEnum::DELETING, $accumulatedRow->model) === false) {
$accumulatedRow->skipDeleting = true;
Expand Down Expand Up @@ -304,6 +310,9 @@ function (Model $model) use ($data): string {
) {
$row->model->wasRecentlyCreated = $startedAt < $existingRow->getAttribute($createdAtColumn);
}
} else {
$row->skipSaving = true;
$row->skipCreating = true;
}
}
}
Expand Down
3 changes: 2 additions & 1 deletion src/Scenarios/UpdateScenario.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,13 @@ public function handle(
array $dateFields,
array $selectColumns,
?string $deletedAtColumn,
bool $withTrashed,
): void {
if (empty($data->rows)) {
return;
}

$this->markNonexistentRowsAsSkipped->handle($eloquent, $data, $selectColumns, $deletedAtColumn);
$this->markNonexistentRowsAsSkipped->handle($eloquent, $data, $selectColumns, $deletedAtColumn, $withTrashed);

if ($eventDispatcher->hasListeners(BulkEventEnum::saving())
|| $eventDispatcher->hasListeners(BulkEventEnum::updating())
Expand Down
4 changes: 3 additions & 1 deletion src/Scenarios/UpsertScenario.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,12 +27,13 @@ public function handle(
array $dateFields,
array $selectColumns,
?string $deletedAtColumn,
bool $withTrashed,
): void {
if (empty($data->rows)) {
return;
}

$this->markNonexistentRowsAsSkipped->handle($eloquent, $data, $selectColumns, $deletedAtColumn);
$this->markNonexistentRowsAsSkipped->handle($eloquent, $data, $selectColumns, $deletedAtColumn, $withTrashed);

$this->create($eloquent, $data, $eventDispatcher, $dateFields, $selectColumns, $deletedAtColumn);

Expand All @@ -43,6 +44,7 @@ public function handle(
$dateFields,
$selectColumns,
$deletedAtColumn,
$withTrashed
);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ final class CreateBeforeWritingEventDependenciesTest extends TestCase
use UserTestTrait;

/**
* When one of model events sometimes returns false then its dependencies have not been called.
* When one of model's events return false then its dependencies have not been called.
*
* @param class-string<User> $model
* @param Closure $data
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,7 @@ public function testModelEventReturnsFalseSometimes(

$sut = $model::query()
->bulk()
->withTrashed()
->uniqueBy(['id']);

// act
Expand Down Expand Up @@ -143,6 +144,7 @@ public function testModelEventReturnsFalseAlways(

$sut = $model::query()
->bulk()
->withTrashed()
->uniqueBy(['id']);

// act
Expand Down Expand Up @@ -200,6 +202,7 @@ public function testCollectionEventReturnsFalse(

$sut = $model::query()
->bulk()
->withTrashed()
->uniqueBy(['id']);

// act
Expand Down

0 comments on commit 1944084

Please sign in to comment.