Skip to content

Commit

Permalink
Merge pull request #310 from driesvints/refactor-flush-command
Browse files Browse the repository at this point in the history
[6.0] Use engine to flush records of model
  • Loading branch information
taylorotwell authored Oct 8, 2018
2 parents f8c5ff4 + fb507b3 commit 17f2dd1
Show file tree
Hide file tree
Showing 7 changed files with 51 additions and 14 deletions.
6 changes: 6 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# Release Notes

## [v6.0.0 (unreleased)](https://github.com/laravel/scout/compare/v5.0.3...v6.0.0)

### Changed
- Flush records of a model using the engine. **This removes the emitting of the `ModelsFlushed` event.** ([#310](https://github.com/laravel/scout/pull/310))
11 changes: 1 addition & 10 deletions src/Console/FlushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
namespace Laravel\Scout\Console;

use Illuminate\Console\Command;
use Laravel\Scout\Events\ModelsFlushed;
use Illuminate\Contracts\Events\Dispatcher;

class FlushCommand extends Command
Expand Down Expand Up @@ -34,16 +33,8 @@ public function handle(Dispatcher $events)

$model = new $class;

$events->listen(ModelsFlushed::class, function ($event) use ($class) {
$key = $event->models->last()->getScoutKey();

$this->line('<comment>Flushed ['.$class.'] models up to ID:</comment> '.$key);
});

$model::removeAllFromSearch();

$events->forget(ModelsFlushed::class);


$this->info('All ['.$class.'] records have been flushed.');
}
}
13 changes: 13 additions & 0 deletions src/Engines/AlgoliaEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,19 @@ public function getTotalCount($results)
return $results['nbHits'];
}

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

$index->clearIndex();
}

/**
* Determine if the given model uses soft deletes.
*
Expand Down
8 changes: 8 additions & 0 deletions src/Engines/Engine.php
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ abstract public function map(Builder $builder, $results, $model);
*/
abstract public function getTotalCount($results);

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

/**
* Get the results of the query as a Collection of primary keys.
*
Expand Down
11 changes: 11 additions & 0 deletions src/Engines/NullEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -87,4 +87,15 @@ public function getTotalCount($results)
{
return count($results);
}

/**
* Flush all of the model's records from the engine.
*
* @param \Illuminate\Database\Eloquent\Model $model
* @return void
*/
public function flush($model)
{
//
}
}
6 changes: 2 additions & 4 deletions src/Searchable.php
Original file line number Diff line number Diff line change
Expand Up @@ -148,9 +148,7 @@ public static function removeAllFromSearch()
{
$self = new static();

$self->newQuery()
->orderBy($self->getKeyName())
->unsearchable();
$self->searchableUsing()->flush($self);
}

/**
Expand Down Expand Up @@ -264,7 +262,7 @@ public function syncWithSearchUsing()
/**
* Get the queue that should be used with syncing
*
* @return string
* @return string
*/
public function syncWithSearchUsingQueue()
{
Expand Down
10 changes: 10 additions & 0 deletions tests/AlgoliaEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -89,4 +89,14 @@ public function test_a_model_is_removed_with_a_custom_algolia_key()
$engine = new AlgoliaEngine($client);
$engine->delete(Collection::make([new AlgoliaEngineTestCustomKeyModel()]));
}

public function test_flush_a_model()
{
$client = Mockery::mock('AlgoliaSearch\Client');
$client->shouldReceive('initIndex')->with('table')->andReturn($index = Mockery::mock('StdClass'));
$index->shouldReceive('clearIndex');

$engine = new AlgoliaEngine($client);
$engine->flush(new AlgoliaEngineTestCustomKeyModel());
}
}

0 comments on commit 17f2dd1

Please sign in to comment.