Skip to content

Commit

Permalink
Add soft delete support / bump to 6.1 (#3)
Browse files Browse the repository at this point in the history
* Add soft delete support
* Update to 6.1
  • Loading branch information
Jordan Hoff authored and olsgreen committed Nov 24, 2018
1 parent 28b4802 commit e97408d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 5 deletions.
2 changes: 1 addition & 1 deletion composer.json
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
],
"require": {
"php": ">=7.0",
"laravel/scout": "^5.0"
"laravel/scout": "^6.1"
},
"require-dev": {
"mockery/mockery": "~1.0",
Expand Down
37 changes: 35 additions & 2 deletions src/DatabaseEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace BoxedCode\Laravel\Scout;

use Illuminate\Database\DatabaseManager;
use Illuminate\Database\Eloquent\SoftDeletes;
use Illuminate\Support\Collection;
use Laravel\Scout\Builder;
use Laravel\Scout\Engines\Engine;
Expand Down Expand Up @@ -33,9 +34,15 @@ public function __construct(DatabaseManager $database)
*/
public function update($models)
{
if ($this->usesSoftDelete($models->first()) && config('scout.soft_delete', false)) {
$models->each->pushSoftDeleteMetadata();
}

$models
->map(function ($model) {
$array = array_values($model->toSearchableArray());
$array = array_merge(
$model->toSearchableArray(), $model->scoutMetadata()
);

if (empty($array)) {
return;
Expand Down Expand Up @@ -154,6 +161,21 @@ public function getTotalCount($results)
return $results['total'];
}

/**
* Flush all of the model's records from the engine.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function flush($model)
{
$index = $model->searchableAs();

$this->query()
->where('index', $index)
->delete();
}

/**
* Pluck and return the primary keys of the given results.
*
Expand Down Expand Up @@ -203,11 +225,22 @@ protected function performSearch(Builder $builder, array $options = [])
->where('entry', 'like', '%' . $builder->query . '%');

foreach ($builder->wheres as $column => $value) {
$search = sprintf('%%"%s":"%s"%%', $column, $value);
$search = preg_replace('/^\{|\}$/', '%', json_encode([$column => $value]));

$query->where('entry', 'like', $search);
}

return $query;
}

/**
* Determine if the given model uses soft deletes.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return bool
*/
protected function usesSoftDelete($model)
{
return in_array(SoftDeletes::class, class_uses_recursive($model));
}
}
9 changes: 7 additions & 2 deletions tests/DatabaseEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,15 @@ public function test_update_adds_objects_to_index()
$queryBuilder = Mockery::mock('Illuminate\Database\Query\Builder');
$modelReturningEmpty = Mockery::mock('Testing\Fixtures\TestModel');
$modelReturningEmpty->shouldReceive('toSearchableArray')->andReturn([]);
$modelReturningEmpty->shouldReceive('scoutMetadata')->andReturn([]);
$manager->shouldReceive('table')->with('scout_index')->andReturn($queryBuilder);
$queryBuilder->shouldReceive('updateOrInsert')->with([
'index' => 'table',
'objectID' => 1,
], [
'objectID' => 1,
'index' => 'table',
'entry' => '[1]',
'entry' => '{"id":1}',
]);

$engine = new DatabaseEngine($manager);
Expand Down Expand Up @@ -103,14 +104,18 @@ function ($closure) use ($queryBuilder) {
)
->andReturn($queryBuilder)
->shouldReceive('where')
->with('entry', 'like', '%"foo":"1"%')
->with('entry', 'like', '%"foo":1%')
->andReturn($queryBuilder)
->shouldReceive('where')
->with('entry', 'like', '%"bar":"string"%')
->andReturn($queryBuilder)
->shouldReceive('get')
->andReturn([1, 2, 3]);

$engine = new DatabaseEngine($manager);
$builder = new Builder(new TestModel(), 'zonda');
$builder->where('foo', 1);
$builder->where('bar', 'string');
$result = $engine->search($builder);
$this->assertSame($result, [
'results' => [1, 2, 3],
Expand Down

0 comments on commit e97408d

Please sign in to comment.