babenkoivan / elastic-migrations

Elasticsearch migrations for Laravel
MIT License
187 stars 32 forks source link

--import flag #40

Closed oprypkhantc closed 2 years ago

oprypkhantc commented 2 years ago

Hey.

Laravel's migrate command allows you to run the seeds automatically with --seed option. This is very convenient for development. It would be nice to have a similar --import option for elastic:migrate set of commands, which would run a Scout import of all searchable models after creating the indexes.

This is technically achievable in userland code by creating a duplicate set of commands that first call respective commands from this package and then runs the import, however this is obviously not optimal. I believe this is a common enough task for it to be included in the package itself.

Unfortunately there's a caveat: this package doesn't depend on laravel/scout, and --import flag pretty much only makes sense in the context of Laravel Scout.

There are possible solutions to this though, like the use of suggests section of composer.json, in a similar way that laravel/framework does it. The whole feature would take a few lines:

// .. run migrations

if (!$this->option('import')) {
    return 0;
}

assert(trait_exists(Searchable::class));

foreach (trait_uses_recursive(Searchable::class) as $class) {
    $this->call(ImportCommand::class, ['model' => $class]);
}
babenkoivan commented 2 years ago

Hey @oprypkhantc, I would like to keep these two packages isolated, therefore this feature is unlikely to be included in the library. You can create your own command and publish it on GitHub to be able to reuse it in your projects. Thank you for your understanding 🙂