diff --git a/src/Commands/MigrateFresh.php b/src/Commands/MigrateFresh.php new file mode 100644 index 00000000..da4b46e7 --- /dev/null +++ b/src/Commands/MigrateFresh.php @@ -0,0 +1,77 @@ +database = $database; + + $this->setName('tenants:migrate-fresh'); + $this->specifyParameters(); + } + + /** + * Execute the console command. + * + * @return mixed + */ + public function handle() + { + if (! $this->confirmToProceed()) { + return; + } + + $originalTenant = tenancy()->getTenant(); + tenancy()->all($this->option('tenants'))->each(function ($tenant) { + $this->line("Tenant: {$tenant['id']}"); + + // See Illuminate\Database\Migrations\DatabaseMigrationRepository::getConnection. + // Database connections are cached by Illuminate\Database\ConnectionResolver. + $this->input->setOption('database', 'tenant'); + tenancy()->initialize($tenant); + + // Fresh + $this->call('db:wipe', array_filter([ + '--database' => 'tenant', + '--force' => true, + ])); + + // Migrate + parent::handle(); + }); + + if ($originalTenant) { + tenancy()->initialize($originalTenant); + } else { + tenancy()->endTenancy(); + } + } +} diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index e93c2d08..c3bb8a95 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -23,6 +23,7 @@ class TenancyServiceProvider extends ServiceProvider Commands\Seed::class, Commands\Install::class, Commands\Migrate::class, + Commands\MigrateFresh::class, Commands\Rollback::class, Commands\TenantList::class, ]); @@ -82,6 +83,9 @@ class TenancyServiceProvider extends ServiceProvider $this->app->singleton(Commands\Migrate::class, function ($app) { return new Commands\Migrate($app['migrator'], $app[DatabaseManager::class]); }); + $this->app->singleton(Commands\MigrateFresh::class, function ($app) { + return new Commands\MigrateFresh($app['migrator'], $app[DatabaseManager::class]); + }); $this->app->singleton(Commands\Rollback::class, function ($app) { return new Commands\Rollback($app['migrator'], $app[DatabaseManager::class]); });