babenkoivan / elastic-migrations

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

Multi-tenancy Support #5

Closed fernandocoronatomf closed 4 years ago

fernandocoronatomf commented 4 years ago

Hey mate,

Thanks again for providing such good packages for the elastic stack and laravel.

Here's the thing, my application is multi-tenant https://github.com/tenancy/multi-tenant and I need the indexes to be prefixed and run for every tenant.

For database migrations the package did: php artisan tenancy:migrate

I am thinking to do the same thing for the elastic migrations, like: tenancy:elastic:migrate for example. Do you plan to get this working and/or have any recommendations on how to implement it?

Otherwise I will be just investigating the package code and probably swap some classes from my ServiceProvider.

babenkoivan commented 4 years ago

Hi @fernandocoronatomf, I do not plan to support https://github.com/tenancy/multi-tenant, but the package already supports index prefixes. Even though there is only one configuration for the whole project, it can be used to make the tenancy:elastic:migrate command.

You just need to extend the basic command as following:

class TenancyMigrateCommand extends \ElasticMigrations\Console\MigrateCommand
{
    protected $signature = 'tenancy:elastic:migrate 
        {website_id: Website id}
        {fileName? : The name of the migration file} 
        {--force : Force the operation to run when in production}';

    public function handle()
    {
         // todo get a prefix using website_id argument
         // $webSitePrefix = ...

         // change the configuration
         config(['elastic.migrations.index_name_prefix' => $webSitePrefix]);

         // run the command
         parent::handle();

         // set the default value back 
         config(['elastic.migrations.index_name_prefix' => '']);
    }   
}

I didn't test it, but it should work. The only obstacle here is that \ElasticMigrations\Console\MigrateCommand is defined as final so you can extend it, but if you decide to go this way I can make a fix to remove final from all the commands.

github-actions[bot] commented 4 years ago

This issue is stale because it has been open 30 days with no activity. Remove stale label or comment or this will be closed in 7 days