From ac58f6992bfdec0f4b2d8f66109b72a7dd55e31a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 20 May 2020 20:41:47 +0200 Subject: [PATCH] Use getTenantKey() instead of ->id references --- src/Commands/MigrateFresh.php | 2 +- src/Contracts/Tenant.php | 2 +- src/Database/Concerns/GeneratesIds.php | 4 ++-- src/Database/Models/Tenant.php | 2 +- src/DatabaseConfig.php | 2 +- src/Jobs/MigrateDatabase.php | 8 ++++---- src/Jobs/SeedDatabase.php | 8 ++++---- tests/CommandsTest.php | 2 +- 8 files changed, 15 insertions(+), 15 deletions(-) diff --git a/src/Commands/MigrateFresh.php b/src/Commands/MigrateFresh.php index dec6c415..c4fb3a3f 100644 --- a/src/Commands/MigrateFresh.php +++ b/src/Commands/MigrateFresh.php @@ -43,7 +43,7 @@ final class MigrateFresh extends Command $this->info('Migrating.'); $this->callSilent('tenants:migrate', [ - '--tenants' => [$tenant->id], + '--tenants' => [$tenant->getTenantKey()], '--force' => true, ]); }); diff --git a/src/Contracts/Tenant.php b/src/Contracts/Tenant.php index 377fb2e6..b93ed014 100644 --- a/src/Contracts/Tenant.php +++ b/src/Contracts/Tenant.php @@ -11,7 +11,7 @@ interface Tenant public function getTenantKeyName(): string; /** Get the value of the key used for identifying the tenant. */ - public function getTenantKey(): string; + public function getTenantKey(): ?string; /** Get the value of an internal key. */ public function getInternal(string $key); diff --git a/src/Database/Concerns/GeneratesIds.php b/src/Database/Concerns/GeneratesIds.php index 4b194fb8..501b0a86 100644 --- a/src/Database/Concerns/GeneratesIds.php +++ b/src/Database/Concerns/GeneratesIds.php @@ -9,8 +9,8 @@ trait GeneratesIds public static function bootGeneratesIds() { static::creating(function (self $model) { - if (! $model->id && app()->bound(UniqueIdentifierGenerator::class)) { - $model->id = app(UniqueIdentifierGenerator::class)->generate($model); + if (! $model->getTenantKey() && app()->bound(UniqueIdentifierGenerator::class)) { + $model->setAttribute($model->getTenantKeyName(), app(UniqueIdentifierGenerator::class)->generate($model)); } }); } diff --git a/src/Database/Models/Tenant.php b/src/Database/Models/Tenant.php index 5fc7fe68..b75f028e 100644 --- a/src/Database/Models/Tenant.php +++ b/src/Database/Models/Tenant.php @@ -32,7 +32,7 @@ class Tenant extends Model implements Contracts\Tenant return 'id'; } - public function getTenantKey(): string + public function getTenantKey(): ?string { return $this->getAttribute($this->getTenantKeyName()); } diff --git a/src/DatabaseConfig.php b/src/DatabaseConfig.php index e56d25b9..799f5e67 100644 --- a/src/DatabaseConfig.php +++ b/src/DatabaseConfig.php @@ -37,7 +37,7 @@ class DatabaseConfig }; static::$databaseNameGenerator = static::$databaseNameGenerator ?? function (Tenant $tenant) { - return config('tenancy.database.prefix') . $tenant->id . config('tenancy.database.suffix'); + return config('tenancy.database.prefix') . $tenant->getTenantKey() . config('tenancy.database.suffix'); }; } diff --git a/src/Jobs/MigrateDatabase.php b/src/Jobs/MigrateDatabase.php index 5fdd0ede..ad590062 100644 --- a/src/Jobs/MigrateDatabase.php +++ b/src/Jobs/MigrateDatabase.php @@ -10,16 +10,16 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Artisan; -use Stancl\Tenancy\Database\Models\Tenant; +use Stancl\Tenancy\Contracts\TenantWithDatabase; class MigrateDatabase implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - /** @var Tenant */ + /** @var TenantWithDatabase */ protected $tenant; - public function __construct(Tenant $tenant) + public function __construct(TenantWithDatabase $tenant) { $this->tenant = $tenant; } @@ -32,7 +32,7 @@ class MigrateDatabase implements ShouldQueue public function handle() { Artisan::call('tenants:migrate', [ - '--tenants' => [$this->tenant->id], + '--tenants' => [$this->tenant->getTenantKey()], ]); } } diff --git a/src/Jobs/SeedDatabase.php b/src/Jobs/SeedDatabase.php index 6a197cf0..9c1a95f6 100644 --- a/src/Jobs/SeedDatabase.php +++ b/src/Jobs/SeedDatabase.php @@ -10,16 +10,16 @@ use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; use Illuminate\Support\Facades\Artisan; -use Stancl\Tenancy\Database\Models\Tenant; +use Stancl\Tenancy\Contracts\TenantWithDatabase; class SeedDatabase implements ShouldQueue { use Dispatchable, InteractsWithQueue, Queueable, SerializesModels; - /** @var Tenant */ + /** @var TenantWithDatabase */ protected $tenant; - public function __construct(Tenant $tenant) + public function __construct(TenantWithDatabase $tenant) { $this->tenant = $tenant; } @@ -32,7 +32,7 @@ class SeedDatabase implements ShouldQueue public function handle() { Artisan::call('tenants:seed', [ - '--tenants' => [$this->tenant->id], + '--tenants' => [$this->tenant->getTenantKey()], ]); } } diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index c2229945..4ea77f28 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -131,7 +131,7 @@ class CommandsTest extends TestCase /** @test */ public function run_commands_works() { - $id = Tenant::create()->id; + $id = Tenant::create()->getTenantKey(); Artisan::call('tenants:migrate', ['--tenants' => [$id]]);