Skip to content

Commit

Permalink
Merge pull request #5 from macbookandrew/feature/create-index
Browse files Browse the repository at this point in the history
Add index:create and index:delete commands
  • Loading branch information
FrittenKeeZ authored Dec 4, 2022
2 parents 3a9a991 + 185d12a commit 66be7e3
Show file tree
Hide file tree
Showing 17 changed files with 599 additions and 10 deletions.
11 changes: 7 additions & 4 deletions .github/workflows/lint.yml
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,23 @@ on:
push:
branches:
- main
paths:
- '**.php'
pull_request:
branches:
- main
paths:
- '**.php'

jobs:
php-cs-fixer:
laravel-pint:
name: PHP Linting (Pint)

runs-on: ubuntu-latest

steps:
- name: Checkout code
uses: actions/checkout@v3
with:
# Make sure the actual branch is checked out when running on pull requests.
ref: ${{ github.head_ref }}

- name: Laravel Pint
uses: aglipanci/laravel-pint-action@0.1.0
Expand Down
8 changes: 5 additions & 3 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
@@ -1,12 +1,17 @@
name: Tests

on:
workflow_dispatch:
push:
branches:
- main
paths:
- '**.php'
pull_request:
branches:
- main
paths:
- '**.php'
schedule:
- cron: '0 1 * * 1' # Run at 01:00 on Mondays.

Expand Down Expand Up @@ -34,9 +39,6 @@ jobs:
steps:
- name: Checkout code
uses: actions/checkout@v3
with:
# Make sure the actual branch is checked out when running on pull requests.
ref: ${{ github.head_ref }}

- name: Install SQLite 3
run: |
Expand Down
14 changes: 13 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ use Laravel\Scout\Searchable;
class Article extends Model implements MeiliSettings
{
use Searchable;

/**
* {@inheritdoc}
*/
Expand All @@ -67,6 +67,18 @@ A full list of available index settings can be found [here](https://docs.meilise

### Commands
The following commands are available:

#### `meili:index:create` - Create a new MeiliSearch index
**Arguments:**
- `index` : Index name

**Options:**
- `--force` : Force the operation to run

#### `meili:index:delete` - Delete a MeiliSearch index
**Arguments:**
- `index` : Index name

#### `meili:index:details` - Get details for a MeiliSearch index
**Arguments:**
- `index` : Index name
Expand Down
50 changes: 50 additions & 0 deletions src/Actions/CreateIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
<?php

declare(strict_types=1);

namespace Dwarf\MeiliTools\Actions;

use Dwarf\MeiliTools\Contracts\Actions\CreatesIndex;
use Dwarf\MeiliTools\Helpers;
use Laravel\Scout\EngineManager;

/**
* Create index.
*/
class CreateIndex implements CreatesIndex
{
use Concerns\ExtractsIndexInformation;

/**
* Scout engine manager.
*
* @var \Laravel\Scout\EngineManager
*/
protected EngineManager $manager;

/**
* Constructor.
*
* @param \Laravel\Scout\EngineManager $manager Scout engine manager.
*/
public function __construct(EngineManager $manager)
{
$this->manager = $manager;
}

/**
* {@inheritDoc}
*
* @throws \Dwarf\MeiliTools\Exceptions\MeiliToolsException When not using the MeiliSearch Scout driver.
* @throws \MeiliSearch\Exceptions\CommunicationException When connection to MeiliSearch fails.
*/
public function __invoke(string $index, array $options = []): array
{
Helpers::throwUnlessMeiliSearch();
$engine = $this->manager->engine();
$task = $engine->createIndex($index, $options);
$engine->waitForTask($task['taskUid']);

return $this->getIndexData($engine->getIndex($index));
}
}
48 changes: 48 additions & 0 deletions src/Actions/DeleteIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
<?php

declare(strict_types=1);

namespace Dwarf\MeiliTools\Actions;

use Dwarf\MeiliTools\Contracts\Actions\DeletesIndex;
use Dwarf\MeiliTools\Helpers;
use Laravel\Scout\EngineManager;

/**
* Delete index.
*/
class DeleteIndex implements DeletesIndex
{
/**
* Scout engine manager.
*
* @var \Laravel\Scout\EngineManager
*/
protected EngineManager $manager;

/**
* Constructor.
*
* @param \Laravel\Scout\EngineManager $manager Scout engine manager.
*/
public function __construct(EngineManager $manager)
{
$this->manager = $manager;
}

/**
* {@inheritDoc}
*
* @throws \Dwarf\MeiliTools\Exceptions\MeiliToolsException When not using the MeiliSearch Scout driver.
* @throws \MeiliSearch\Exceptions\CommunicationException When connection to MeiliSearch fails.
* @throws \MeiliSearch\Exceptions\ApiException When index is not found.
*/
public function __invoke(string $index): void
{
Helpers::throwUnlessMeiliSearch();

$engine = $this->manager->engine();
$task = $engine->deleteIndex($index);
$engine->waitForTask($task['taskUid']);
}
}
41 changes: 41 additions & 0 deletions src/Console/Commands/IndexCreate.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
<?php

declare(strict_types=1);

namespace Dwarf\MeiliTools\Console\Commands;

use Dwarf\MeiliTools\Contracts\Actions\CreatesIndex;
use Illuminate\Console\Command;

class IndexCreate extends Command
{
use Concerns\RequiresIndex;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'meili:index:create {index? : Index name}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Create a MeiliSearch index';

/**
* Execute the console command.
*
* @param \Dwarf\MeiliTools\Contracts\Actions\CreatesIndex $createIndex
*
* @return int
*/
public function handle(CreatesIndex $createIndex)
{
$createIndex($this->getIndex());

return Command::SUCCESS;
}
}
51 changes: 51 additions & 0 deletions src/Console/Commands/IndexDelete.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
<?php

declare(strict_types=1);

namespace Dwarf\MeiliTools\Console\Commands;

use Dwarf\MeiliTools\Contracts\Actions\DeletesIndex;
use Illuminate\Console\Command;
use Illuminate\Console\ConfirmableTrait;

class IndexDelete extends Command
{
use Concerns\RequiresIndex;
use ConfirmableTrait;

/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'meili:index:delete
{index? : Index name}
{--force : Force the operation to run}';

/**
* The console command description.
*
* @var string
*/
protected $description = 'Delete a MeiliSearch index';

/**
* Execute the console command.
*
* @param \Dwarf\MeiliTools\Contracts\Actions\DeletesIndex $deleteIndex
*
* @return int
*/
public function handle(DeletesIndex $deleteIndex)
{
// Confirm execution.
$index = $this->getIndex();
if (!$this->confirmToProceed("Index '{$index}' is about to be deleted", fn () => true)) {
return Command::FAILURE;
}

$deleteIndex($index);

return Command::SUCCESS;
}
}
2 changes: 1 addition & 1 deletion src/Console/Commands/ModelsSynchronize.php
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ public function handle(ListsClasses $listClasses, SynchronizesModels $synchroniz
{
// Confirm execution if not pretending and in production.
if (!$this->option('pretend') && !$this->confirmToProceed()) {
return Command::SUCCESS;
return Command::FAILURE;
}

$paths = config('meilitools.paths');
Expand Down
21 changes: 21 additions & 0 deletions src/Contracts/Actions/CreatesIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php

declare(strict_types=1);

namespace Dwarf\MeiliTools\Contracts\Actions;

/**
* Creates index.
*/
interface CreatesIndex
{
/**
* Create index.
*
* @param string $index Index name.
* @param array $options Index options.
*
* @return array
*/
public function __invoke(string $index, array $options = []): array;
}
20 changes: 20 additions & 0 deletions src/Contracts/Actions/DeletesIndex.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
<?php

declare(strict_types=1);

namespace Dwarf\MeiliTools\Contracts\Actions;

/**
* Deletes index.
*/
interface DeletesIndex
{
/**
* Delete index.
*
* @param string $index Index name.
*
* @return void
*/
public function __invoke(string $index): void;
}
3 changes: 2 additions & 1 deletion src/Contracts/Actions/EnsuresIndexExists.php
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ interface EnsuresIndexExists
/**
* Ensure that the given index exists.
*
* @param string $index Index name.
* @param string $index Index name.
* @param array $options Index options.
*
* @return void
*/
Expand Down
10 changes: 10 additions & 0 deletions src/MeiliToolsServiceProvider.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@

namespace Dwarf\MeiliTools;

use Dwarf\MeiliTools\Actions\CreateIndex;
use Dwarf\MeiliTools\Actions\DeleteIndex;
use Dwarf\MeiliTools\Actions\DetailIndex;
use Dwarf\MeiliTools\Actions\DetailModel;
use Dwarf\MeiliTools\Actions\EnsureIndexExists;
Expand All @@ -17,6 +19,8 @@
use Dwarf\MeiliTools\Actions\ValidateIndexSettings;
use Dwarf\MeiliTools\Actions\ViewIndex;
use Dwarf\MeiliTools\Actions\ViewModel;
use Dwarf\MeiliTools\Console\Commands\IndexCreate;
use Dwarf\MeiliTools\Console\Commands\IndexDelete;
use Dwarf\MeiliTools\Console\Commands\IndexDetails;
use Dwarf\MeiliTools\Console\Commands\IndexesList;
use Dwarf\MeiliTools\Console\Commands\IndexReset;
Expand All @@ -26,6 +30,8 @@
use Dwarf\MeiliTools\Console\Commands\ModelsSynchronize;
use Dwarf\MeiliTools\Console\Commands\ModelSynchronize;
use Dwarf\MeiliTools\Console\Commands\ModelView;
use Dwarf\MeiliTools\Contracts\Actions\CreatesIndex;
use Dwarf\MeiliTools\Contracts\Actions\DeletesIndex;
use Dwarf\MeiliTools\Contracts\Actions\DetailsIndex;
use Dwarf\MeiliTools\Contracts\Actions\DetailsModel;
use Dwarf\MeiliTools\Contracts\Actions\EnsuresIndexExists;
Expand All @@ -52,6 +58,8 @@ class MeiliToolsServiceProvider extends ServiceProvider
*/
public array $bindings = [
ArrayAssocRule::class => ArrayAssoc::class,
CreatesIndex::class => CreateIndex::class,
DeletesIndex::class => DeleteIndex::class,
DetailsIndex::class => DetailIndex::class,
DetailsModel::class => DetailModel::class,
EnsuresIndexExists::class => EnsureIndexExists::class,
Expand Down Expand Up @@ -93,6 +101,8 @@ public function boot(): void
{
if ($this->app->runningInConsole()) {
$this->commands([
IndexCreate::class,
IndexDelete::class,
IndexDetails::class,
IndexReset::class,
IndexView::class,
Expand Down
Loading

0 comments on commit 66be7e3

Please sign in to comment.