mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 14:34:04 +00:00
[2.x] Queued post-creation automigration (#154)
* Queued post-creation automigration * Add shouldQueueMigration() * Automigration test, config key, QueueTenancyBootstrapper support for QueueFake * Apply fixes from StyleCI * Fix if statement
This commit is contained in:
parent
b66574b1ab
commit
abd0b8f04e
7 changed files with 88 additions and 6 deletions
|
|
@ -90,6 +90,7 @@ return [
|
||||||
],
|
],
|
||||||
'home_url' => '/app',
|
'home_url' => '/app',
|
||||||
'migrate_after_creation' => false, // run migrations after creating a tenant
|
'migrate_after_creation' => false, // run migrations after creating a tenant
|
||||||
|
'queue_automatic_migration' => false, // queue the automatic post-tenant-creation migrations
|
||||||
'delete_database_after_tenant_deletion' => false, // delete the tenant's database after deleting the tenant
|
'delete_database_after_tenant_deletion' => false, // delete the tenant's database after deleting the tenant
|
||||||
'queue_database_creation' => false,
|
'queue_database_creation' => false,
|
||||||
'queue_database_deletion' => false,
|
'queue_database_deletion' => false,
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,10 @@ class QueuedTenantDatabaseCreator implements ShouldQueue
|
||||||
{
|
{
|
||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/** @var TenantDatabaseManager */
|
||||||
protected $databaseManager;
|
protected $databaseManager;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
protected $databaseName;
|
protected $databaseName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
|
|
@ -15,7 +15,10 @@ class QueuedTenantDatabaseDeleter implements ShouldQueue
|
||||||
{
|
{
|
||||||
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/** @var TenantDatabaseManager */
|
||||||
protected $databaseManager;
|
protected $databaseManager;
|
||||||
|
|
||||||
|
/** @var string */
|
||||||
protected $databaseName;
|
protected $databaseName;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
|
|
|
||||||
38
src/Jobs/QueuedTenantDatabaseMigrator.php
Normal file
38
src/Jobs/QueuedTenantDatabaseMigrator.php
Normal file
|
|
@ -0,0 +1,38 @@
|
||||||
|
<?php
|
||||||
|
|
||||||
|
declare(strict_types=1);
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Jobs;
|
||||||
|
|
||||||
|
use Illuminate\Bus\Queueable;
|
||||||
|
use Illuminate\Contracts\Queue\ShouldQueue;
|
||||||
|
use Illuminate\Foundation\Bus\Dispatchable;
|
||||||
|
use Illuminate\Queue\InteractsWithQueue;
|
||||||
|
use Illuminate\Queue\SerializesModels;
|
||||||
|
use Illuminate\Support\Facades\Artisan;
|
||||||
|
use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
|
class QueuedTenantDatabaseMigrator implements ShouldQueue
|
||||||
|
{
|
||||||
|
use Dispatchable, InteractsWithQueue, Queueable, SerializesModels;
|
||||||
|
|
||||||
|
/** @var Tenant */
|
||||||
|
protected $tenant;
|
||||||
|
|
||||||
|
public function __construct(Tenant $tenant)
|
||||||
|
{
|
||||||
|
$this->tenant = $tenant;
|
||||||
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Execute the job.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
Artisan::call('tenants:migrate', [
|
||||||
|
'--tenants' => [$this->tenant->id],
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
@ -5,6 +5,7 @@ declare(strict_types=1);
|
||||||
namespace Stancl\Tenancy\TenancyBootstrappers;
|
namespace Stancl\Tenancy\TenancyBootstrappers;
|
||||||
|
|
||||||
use Illuminate\Contracts\Foundation\Application;
|
use Illuminate\Contracts\Foundation\Application;
|
||||||
|
use Illuminate\Support\Testing\Fakes\QueueFake;
|
||||||
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
use Stancl\Tenancy\Contracts\TenancyBootstrapper;
|
||||||
use Stancl\Tenancy\Tenant;
|
use Stancl\Tenancy\Tenant;
|
||||||
|
|
||||||
|
|
@ -21,9 +22,13 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper
|
||||||
$this->app = $app;
|
$this->app = $app;
|
||||||
|
|
||||||
$bootstrapper = &$this;
|
$bootstrapper = &$this;
|
||||||
$this->app['queue']->createPayloadUsing(function () use (&$bootstrapper) {
|
|
||||||
return $bootstrapper->getPayload();
|
$queue = $this->app['queue'];
|
||||||
});
|
if (! $queue instanceof QueueFake) {
|
||||||
|
$queue->createPayloadUsing(function () use (&$bootstrapper) {
|
||||||
|
return $bootstrapper->getPayload();
|
||||||
|
});
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
public function start(Tenant $tenant)
|
public function start(Tenant $tenant)
|
||||||
|
|
|
||||||
|
|
@ -9,6 +9,7 @@ use Illuminate\Foundation\Application;
|
||||||
use Illuminate\Support\Collection;
|
use Illuminate\Support\Collection;
|
||||||
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
|
use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException;
|
||||||
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
use Stancl\Tenancy\Exceptions\TenantCouldNotBeIdentifiedException;
|
||||||
|
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseMigrator;
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @internal Class is subject to breaking changes in minor and patch versions.
|
* @internal Class is subject to breaking changes in minor and patch versions.
|
||||||
|
|
@ -64,9 +65,13 @@ class TenantManager
|
||||||
$this->database->createDatabase($tenant);
|
$this->database->createDatabase($tenant);
|
||||||
|
|
||||||
if ($this->shouldMigrateAfterCreation()) {
|
if ($this->shouldMigrateAfterCreation()) {
|
||||||
$this->artisan->call('tenants:migrate', [
|
if ($this->shouldQueueMigration()) {
|
||||||
'--tenants' => [$tenant['id']],
|
QueuedTenantDatabaseMigrator::dispatch($tenant);
|
||||||
]);
|
} else {
|
||||||
|
$this->artisan->call('tenants:migrate', [
|
||||||
|
'--tenants' => [$tenant['id']],
|
||||||
|
]);
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return $this;
|
return $this;
|
||||||
|
|
@ -306,6 +311,11 @@ class TenantManager
|
||||||
return $this->app['config']['tenancy.migrate_after_creation'] ?? false;
|
return $this->app['config']['tenancy.migrate_after_creation'] ?? false;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
public function shouldQueueMigration(): bool
|
||||||
|
{
|
||||||
|
return $this->app['config']['tenancy.queue_automatic_migration'] ?? false;
|
||||||
|
}
|
||||||
|
|
||||||
public function shouldDeleteDatabase(): bool
|
public function shouldDeleteDatabase(): bool
|
||||||
{
|
{
|
||||||
return $this->app['config']['tenancy.delete_database_after_tenant_deletion'] ?? false;
|
return $this->app['config']['tenancy.delete_database_after_tenant_deletion'] ?? false;
|
||||||
|
|
|
||||||
|
|
@ -5,9 +5,11 @@ declare(strict_types=1);
|
||||||
namespace Stancl\Tenancy\Tests;
|
namespace Stancl\Tenancy\Tests;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Queue;
|
||||||
use Illuminate\Support\Facades\Storage;
|
use Illuminate\Support\Facades\Storage;
|
||||||
use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException;
|
use Stancl\Tenancy\Exceptions\DomainsOccupiedByOtherTenantException;
|
||||||
use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException;
|
use Stancl\Tenancy\Exceptions\TenantWithThisIdAlreadyExistsException;
|
||||||
|
use Stancl\Tenancy\Jobs\QueuedTenantDatabaseMigrator;
|
||||||
use Stancl\Tenancy\Tenant;
|
use Stancl\Tenancy\Tenant;
|
||||||
use Stancl\Tenancy\TenantManager;
|
use Stancl\Tenancy\TenantManager;
|
||||||
|
|
||||||
|
|
@ -244,4 +246,24 @@ class TenantManagerTest extends TestCase
|
||||||
$this->expectException(TenantWithThisIdAlreadyExistsException::class);
|
$this->expectException(TenantWithThisIdAlreadyExistsException::class);
|
||||||
Tenant::create(['bar.localhost'], ['id' => $id]);
|
Tenant::create(['bar.localhost'], ['id' => $id]);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function automigration_can_be_queued()
|
||||||
|
{
|
||||||
|
Queue::fake();
|
||||||
|
|
||||||
|
config([
|
||||||
|
'tenancy.migrate_after_creation' => true,
|
||||||
|
'tenancy.queue_automatic_migration' => true,
|
||||||
|
]);
|
||||||
|
|
||||||
|
$tenant = Tenant::new()->save();
|
||||||
|
tenancy()->initialize($tenant);
|
||||||
|
|
||||||
|
Queue::assertPushed(QueuedTenantDatabaseMigrator::class);
|
||||||
|
|
||||||
|
$this->assertFalse(\Schema::hasTable('users'));
|
||||||
|
(new QueuedTenantDatabaseMigrator($tenant))->handle();
|
||||||
|
$this->assertTrue(\Schema::hasTable('users'));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue