Skip to content

Commit

Permalink
fix: fix meilisearch indexes import (monicahq/chandler#378)
Browse files Browse the repository at this point in the history
  • Loading branch information
asbiin committed Jan 5, 2023
1 parent 77f51be commit 55b4bbb
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 13 deletions.
2 changes: 1 addition & 1 deletion app/Console/Commands/SetupDummyAccount.php
Expand Up @@ -91,7 +91,7 @@ private function wipeAndMigrateDB(): void
} else {
$this->artisan('☐ Migration of the database', 'migrate:fresh', ['--force' => true]);
}
$this->artisan('☐ Reset search engine', 'scout:setup', ['--force' => true]);
$this->artisan('☐ Reset search engine', 'scout:setup', ['--force' => true, '--flush' => true]);
}

private function stop(): void
Expand Down
51 changes: 39 additions & 12 deletions app/Console/Commands/SetupScout.php
Expand Up @@ -2,9 +2,6 @@

namespace App\Console\Commands;

use App\Models\Contact;
use App\Models\Group;
use App\Models\Note;
use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;
use Symfony\Component\Console\Output\OutputInterface;
Expand All @@ -22,6 +19,8 @@ class SetupScout extends Command
* @var string
*/
protected $signature = 'scout:setup
{--flush : Flush the indexes.}
{--import : Import the models into the search index.}
{--force : Force the operation to run when in production.}';

/**
Expand All @@ -37,7 +36,9 @@ class SetupScout extends Command
public function handle(): void
{
if ($this->confirmToProceed()) {
$this->scout();
$this->scoutConfigure();
$this->scoutFlush();
$this->scoutImport();
}
}

Expand All @@ -46,19 +47,45 @@ public function handle(): void
*
* @return void
*/
protected function scout(): void
protected function scoutConfigure(): void
{
if (config('scout.driver') !== null) {
$this->artisan('☐ Flush search engine', 'scout:flush', ['model' => Note::class, '--verbose' => true]);
$this->artisan('☐ Flush search engine', 'scout:flush', ['model' => Contact::class, '--verbose' => true]);
$this->artisan('☐ Flush search engine', 'scout:flush', ['model' => Group::class, '--verbose' => true]);
if (config('scout.driver') === 'meilisearch' && (config('scout.meilisearch.host')) !== '') {
$this->artisan('☐ Updating indexes on Meilisearch', 'scout:sync-index-settings', ['--verbose' => true]);
}
}

if (config('scout.driver') === 'meilisearch' && (config('scout.meilisearch.host')) !== '') {
$this->artisan('☐ Creating indexes on Meilisearch', 'scout:sync-index-settings', ['--verbose' => true]);
/**
* Import models.
*
* @return void
*/
protected function scoutFlush(): void
{
if (config('scout.driver') !== null && $this->option('flush')) {
foreach (config('scout.meilisearch.index-settings') as $index => $settings) {
$name = (new $index)->getTable();
$this->artisan("☐ Flush {$name} index", 'scout:flush', ['model' => $index, '--verbose' => true]);
}

$this->info('✓ Indexes flushed');
}
}

/**
* Import models.
*
* @return void
*/
protected function scoutImport(): void
{
if (config('scout.driver') !== null && $this->option('import')) {
foreach (config('scout.meilisearch.index-settings') as $index => $settings) {
$name = (new $index)->getTable();
$this->artisan("☐ Import {$name}", 'scout:import', ['model' => $index, '--verbose' => true]);
}

$this->info('✓ Indexes created');
$this->info('✓ Indexes imported');
}
}

private function artisan(string $message, string $command, array $options = [])
Expand Down

0 comments on commit 55b4bbb

Please sign in to comment.