mirror of
https://github.com/archtechx/tenancy.git
synced 2026-02-05 07:14:05 +00:00
Add tenancy:migrate-fresh command
This commit is contained in:
parent
bd1c829520
commit
86c5d17196
2 changed files with 81 additions and 0 deletions
77
src/Commands/MigrateFresh.php
Normal file
77
src/Commands/MigrateFresh.php
Normal file
|
|
@ -0,0 +1,77 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Illuminate\Database\Console\Migrations\MigrateCommand;
|
||||||
|
use Illuminate\Database\Migrations\Migrator;
|
||||||
|
use Stancl\Tenancy\DatabaseManager;
|
||||||
|
use Stancl\Tenancy\Traits\DealsWithMigrations;
|
||||||
|
use Stancl\Tenancy\Traits\HasATenantsOption;
|
||||||
|
|
||||||
|
class MigrateFresh extends MigrateCommand
|
||||||
|
{
|
||||||
|
use HasATenantsOption, DealsWithMigrations;
|
||||||
|
|
||||||
|
protected $database;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Drop all tables and re-run all migrations for tenant(s)';
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Create a new command instance.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function __construct(Migrator $migrator, DatabaseManager $database)
|
||||||
|
{
|
||||||
|
parent::__construct($migrator);
|
||||||
|
$this->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();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -23,6 +23,7 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
Commands\Seed::class,
|
Commands\Seed::class,
|
||||||
Commands\Install::class,
|
Commands\Install::class,
|
||||||
Commands\Migrate::class,
|
Commands\Migrate::class,
|
||||||
|
Commands\MigrateFresh::class,
|
||||||
Commands\Rollback::class,
|
Commands\Rollback::class,
|
||||||
Commands\TenantList::class,
|
Commands\TenantList::class,
|
||||||
]);
|
]);
|
||||||
|
|
@ -82,6 +83,9 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
$this->app->singleton(Commands\Migrate::class, function ($app) {
|
$this->app->singleton(Commands\Migrate::class, function ($app) {
|
||||||
return new Commands\Migrate($app['migrator'], $app[DatabaseManager::class]);
|
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) {
|
$this->app->singleton(Commands\Rollback::class, function ($app) {
|
||||||
return new Commands\Rollback($app['migrator'], $app[DatabaseManager::class]);
|
return new Commands\Rollback($app['migrator'], $app[DatabaseManager::class]);
|
||||||
});
|
});
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue