Skip to content

Commit

Permalink
added transaction
Browse files Browse the repository at this point in the history
  • Loading branch information
ivanomatteo committed May 24, 2022
1 parent acb514d commit a5363a8
Showing 1 changed file with 28 additions and 3 deletions.
31 changes: 28 additions & 3 deletions src/Scout/ScoutEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,19 @@ public function __construct()
*/
public function update($models)
{
$models->each(fn ($m) => $this->ftindexer->addModelToIndex($m));
$count = $models->count();
if ($count === 0) {
return;
}
$connection = $models->first()->getConnection();

if ($count > 1 && $connection->transactionLevel() === 0) {
$connection->transaction(
fn () => $models->each(fn ($m) => $this->ftindexer->addModelToIndex($m))
);
} else {
$models->each(fn ($m) => $this->ftindexer->addModelToIndex($m));
}
}

/**
Expand All @@ -39,7 +51,20 @@ public function update($models)
*/
public function delete($models)
{
$models->each(fn ($m) => $this->ftindexer->removeModelFromIndex($m));
$count = $models->count();

if ($count === 0) {
return;
}
$connection = $models->first()->getConnection();

if ($count > 1 && $connection->transactionLevel() === 0) {
$connection->transaction(
fn () => $models->each(fn ($m) => $this->ftindexer->removeModelFromIndex($m))
);
} else {
$models->each(fn ($m) => $this->ftindexer->removeModelFromIndex($m));
}
}

/**
Expand Down Expand Up @@ -191,6 +216,6 @@ public function deleteIndex($name)

private static function usesSoftDelete($class)
{
return ! empty(class_uses_recursive($class)[SoftDeletes::class]);
return !empty(class_uses_recursive($class)[SoftDeletes::class]);
}
}

0 comments on commit a5363a8

Please sign in to comment.