mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 17:44:04 +00:00
[2.x] Migrate fresh (#148)
* Remove comment * migrate-fresh first draft * Final * DB name -> DB connection * Add array_filter for consistency with Laravel * Add test for migrate-fresh * Apply fixes from StyleCI
This commit is contained in:
parent
0515b0b5b5
commit
d0b1729258
3 changed files with 81 additions and 0 deletions
62
src/Commands/MigrateFresh.php
Normal file
62
src/Commands/MigrateFresh.php
Normal file
|
|
@ -0,0 +1,62 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Commands;
|
||||||
|
|
||||||
|
use Illuminate\Console\Command;
|
||||||
|
use Stancl\Tenancy\Traits\DealsWithMigrations;
|
||||||
|
use Stancl\Tenancy\Traits\HasATenantsOption;
|
||||||
|
|
||||||
|
final class MigrateFresh extends Command
|
||||||
|
{
|
||||||
|
use HasATenantsOption, DealsWithMigrations;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* The console command description.
|
||||||
|
*
|
||||||
|
* @var string
|
||||||
|
*/
|
||||||
|
protected $description = 'Drop all tables and re-run all migrations for tenant(s)';
|
||||||
|
|
||||||
|
public function __construct()
|
||||||
|
{
|
||||||
|
parent::__construct();
|
||||||
|
|
||||||
|
$this->setName('tenants:migrate-fresh');
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the console command.
|
||||||
|
*
|
||||||
|
* @return mixed
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$originalTenant = tenancy()->getTenant();
|
||||||
|
$this->info('Dropping tables.');
|
||||||
|
|
||||||
|
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
|
||||||
|
$this->line("Tenant: {$tenant->id}");
|
||||||
|
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
$this->call('db:wipe', array_filter([
|
||||||
|
'--database' => $tenant->getConnectionName(),
|
||||||
|
'--force' => true,
|
||||||
|
]));
|
||||||
|
|
||||||
|
$this->call('tenants:migrate', [
|
||||||
|
'--tenants' => [$tenant->id],
|
||||||
|
]);
|
||||||
|
|
||||||
|
tenancy()->end();
|
||||||
|
});
|
||||||
|
|
||||||
|
$this->info('Done.');
|
||||||
|
|
||||||
|
if ($originalTenant) {
|
||||||
|
tenancy()->initialize($originalTenant);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -65,6 +65,7 @@ class TenancyServiceProvider extends ServiceProvider
|
||||||
Commands\Migrate::class,
|
Commands\Migrate::class,
|
||||||
Commands\Rollback::class,
|
Commands\Rollback::class,
|
||||||
Commands\TenantList::class,
|
Commands\TenantList::class,
|
||||||
|
Commands\MigrateFresh::class,
|
||||||
]);
|
]);
|
||||||
|
|
||||||
$this->publishes([
|
$this->publishes([
|
||||||
|
|
|
||||||
|
|
@ -138,4 +138,22 @@ class CommandsTest extends TestCase
|
||||||
$this->assertDirectoryExists(database_path('migrations/tenant'));
|
$this->assertDirectoryExists(database_path('migrations/tenant'));
|
||||||
$this->assertSame(file_get_contents(__DIR__ . '/Etc/modifiedHttpKernel.stub'), file_get_contents(app_path('Http/Kernel.php')));
|
$this->assertSame(file_get_contents(__DIR__ . '/Etc/modifiedHttpKernel.stub'), file_get_contents(app_path('Http/Kernel.php')));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function migrate_fresh_command_works()
|
||||||
|
{
|
||||||
|
$this->assertFalse(Schema::hasTable('users'));
|
||||||
|
Artisan::call('tenants:migrate-fresh');
|
||||||
|
$this->assertFalse(Schema::hasTable('users'));
|
||||||
|
tenancy()->init('test.localhost');
|
||||||
|
$this->assertTrue(Schema::hasTable('users'));
|
||||||
|
|
||||||
|
$this->assertFalse(DB::table('users')->exists());
|
||||||
|
DB::table('users')->insert(['name' => 'xxx', 'password' => bcrypt('password'), 'email' => 'foo@bar.xxx']);
|
||||||
|
$this->assertTrue(DB::table('users')->exists());
|
||||||
|
|
||||||
|
// test that db is wiped
|
||||||
|
Artisan::call('tenants:migrate-fresh');
|
||||||
|
$this->assertFalse(DB::table('users')->exists());
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue