Skip to content

Commit

Permalink
use scout prefix when deleting indexes
Browse files Browse the repository at this point in the history
  • Loading branch information
macbookandrew committed Jun 21, 2024
1 parent 86afd4f commit 9ee43c7
Show file tree
Hide file tree
Showing 2 changed files with 30 additions and 0 deletions.
5 changes: 5 additions & 0 deletions src/Engines/MeilisearchEngine.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
namespace Laravel\Scout\Engines;

use BackedEnum;
use Illuminate\Support\Facades\Config;
use Illuminate\Support\LazyCollection;
use Laravel\Scout\Builder;
use Laravel\Scout\Jobs\RemoveableScoutCollection;
Expand Down Expand Up @@ -430,6 +431,10 @@ public function deleteAllIndexes()
$indexes = $this->meilisearch->getIndexes($query);

foreach ($indexes->getResults() as $index) {
if (! str($index->getUid())->startsWith(Config::get('scout.prefix'))) {
continue;
}

$tasks[] = $index->delete();
}

Expand Down
25 changes: 25 additions & 0 deletions tests/Unit/MeilisearchEngineTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,31 @@ public function test_delete_all_indexes_works_with_pagination()
$engine = new MeilisearchEngine($client);
$engine->deleteAllIndexes();
}

public function test_delete_all_indexes_only_deletes_indexes_with_scout_prefix()
{
Config::shouldReceive('get')->with('scout.prefix')->andReturn('app_');

$client = m::mock(Client::class);
$client->shouldReceive('getIndexes')->andReturn($indexesResults = m::mock(IndexesResults::class));

$otherIndex = m::mock(Indexes::class);
$otherIndex->shouldReceive('getUid')->andReturn('users');

$prefixedIndex = m::mock(Indexes::class);
$prefixedIndex->shouldReceive('getUid')->andReturn('app_users');

$indexesResults->shouldReceive('getResults')->zeroOrMoreTimes()->andReturn([
$otherIndex,
$prefixedIndex,
]);

$otherIndex->shouldNotReceive('delete');
$prefixedIndex->shouldReceive('delete')->once()->andReturn([]);

$engine = new MeilisearchEngine($client);
$engine->deleteAllIndexes();
}
}

class MeilisearchCustomKeySearchableModel extends SearchableModel
Expand Down

0 comments on commit 9ee43c7

Please sign in to comment.