Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.0] Use engine to flush records of model #310

Merged
merged 5 commits into from
Oct 8, 2018
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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/framework/compare/v5.0.3...v6.0.0)
Copy link
Member

@nunomaduro nunomaduro Oct 5, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be: ## [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 ([#310](https://github.com/laravel/scout/pull/310))
6 changes: 0 additions & 6 deletions src/Console/FlushCommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -34,12 +34,6 @@ public function handle(Dispatcher $events)

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@driesvints This class no longer needs the line use Laravel\Scout\Events\ModelsFlushed;.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done!

$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);
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

You also don't need this line.

Expand Down
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();
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I confirm. This is good. 👍

}

/**
* 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 @@ -86,4 +86,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());
}
}