From 20e1fa1959426498489eecffbfdd0bdc397cd906 Mon Sep 17 00:00:00 2001 From: Ralfs Garkaklis Date: Thu, 8 Jul 2021 14:32:17 +0300 Subject: [PATCH 01/45] Fixed typo (#684) --- src/Exceptions/ModelNotSyncMasterException.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Exceptions/ModelNotSyncMasterException.php b/src/Exceptions/ModelNotSyncMasterException.php index abe8904b..ee5feb9a 100644 --- a/src/Exceptions/ModelNotSyncMasterException.php +++ b/src/Exceptions/ModelNotSyncMasterException.php @@ -10,6 +10,6 @@ class ModelNotSyncMasterException extends Exception { public function __construct(string $class) { - parent::__construct("Model of $class class is not an SyncMaster model. Make sure you're using the central model to make changes to synced resouces when you're in the central context"); + parent::__construct("Model of $class class is not an SyncMaster model. Make sure you're using the central model to make changes to synced resources when you're in the central context"); } } From b58b068434718c00679ee0dffad371b13fc33e13 Mon Sep 17 00:00:00 2001 From: Jasper Zonneveld Date: Wed, 3 Nov 2021 10:21:51 +0100 Subject: [PATCH 02/45] Add missing import for Domain model (#745) --- src/TenancyServiceProvider.php | 1 + 1 file changed, 1 insertion(+) diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index cf326792..4faaccf3 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -7,6 +7,7 @@ namespace Stancl\Tenancy; use Illuminate\Cache\CacheManager; use Illuminate\Support\ServiceProvider; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; +use Stancl\Tenancy\Contracts\Domain; use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Resolvers\DomainTenantResolver; From f12c826df52c91fa5a024e8b1982b1056baed728 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Thu, 18 Nov 2021 01:45:44 +0500 Subject: [PATCH 03/45] Use GitHub forms for issues template. (#755) * Create bug-report_new.md * wip * Delete bug-report.md * Update bug-report.yml --- .github/ISSUE_TEMPLATE/bug-report.md | 21 ------------ .github/ISSUE_TEMPLATE/bug-report.yml | 48 +++++++++++++++++++++++++++ 2 files changed, 48 insertions(+), 21 deletions(-) delete mode 100644 .github/ISSUE_TEMPLATE/bug-report.md create mode 100644 .github/ISSUE_TEMPLATE/bug-report.yml diff --git a/.github/ISSUE_TEMPLATE/bug-report.md b/.github/ISSUE_TEMPLATE/bug-report.md deleted file mode 100644 index 9f3d2e65..00000000 --- a/.github/ISSUE_TEMPLATE/bug-report.md +++ /dev/null @@ -1,21 +0,0 @@ ---- -name: "\U0001F41B Bug Report" -about: Report unexpected behavior with stancl/tenancy. -title: '' -labels: bug -assignees: stancl - ---- - -#### Describe the bug - - -#### Steps to reproduce - - -#### Expected behavior -A clear and concise description of what you expected to happen. - -#### Your setup - - Laravel version: [e.g. 8.2.0] - - stancl/tenancy version: [e.g. 3.1.0] diff --git a/.github/ISSUE_TEMPLATE/bug-report.yml b/.github/ISSUE_TEMPLATE/bug-report.yml new file mode 100644 index 00000000..75e345b2 --- /dev/null +++ b/.github/ISSUE_TEMPLATE/bug-report.yml @@ -0,0 +1,48 @@ +name: 🐛 Bug Report +description: Report unexpected behavior with stancl/tenancy. +labels: ["bug"] +assignees: + - stancl +body: + - type: markdown + attributes: + value: | + Before opening a bug report, please search for the behaviour in the existing issues. + --- + Thank you for taking the time to file a bug report. To address this bug as fast as possible, we need some information. + - type: textarea + id: bug-description + attributes: + label: Bug description + description: A clear and concise description of what the bug is. + validations: + required: true + - type: textarea + id: steps + attributes: + label: Steps to reproduce + description: Step-by-step guide for reproducing the bug in a fresh Laravel application. + validations: + required: true + - type: textarea + id: logs + attributes: + label: Expected behavior + description: A clear and concise description of what you expected to happen. + validations: + required: true + + - type: input + id: laravel-version + attributes: + label: Laravel version + placeholder: "e.g. 8.2.0" + validations: + required: true + - type: input + id: tenancy-version + attributes: + label: stancl/tenancy version + placeholder: "e.g. 3.1.0" + validations: + required: true From 98832195448eb781235d9e1abcfa1de4b44104c5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Mon, 13 Dec 2021 13:09:16 +0100 Subject: [PATCH 04/45] wip --- .github/workflows/ci.yml | 2 +- composer.json | 18 ++++++++++++------ .../FilesystemTenancyBootstrapper.php | 3 +++ tests/Etc/ConsoleKernel.php | 7 +------ 4 files changed, 17 insertions(+), 13 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index efb8ad02..37e3d449 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,7 +15,7 @@ jobs: strategy: matrix: - php: ["7.4", "8.0"] + php: ["7.4", "8.0.11"] laravel: ["^6.0", "^8.0"] steps: diff --git a/composer.json b/composer.json index b10f2d16..faeb3d5e 100644 --- a/composer.json +++ b/composer.json @@ -11,17 +11,17 @@ ], "require": { "ext-json": "*", - "illuminate/support": "^6.0|^7.0|^8.0", + "illuminate/support": "dev-master", "facade/ignition-contracts": "^1.0", "ramsey/uuid": "^3.7|^4.0", - "stancl/jobpipeline": "^1.0", - "stancl/virtualcolumn": "^1.0" + "stancl/jobpipeline": "dev-master", + "stancl/virtualcolumn": "dev-master" }, "require-dev": { "vlucas/phpdotenv": "^3.3|^4.0|^5.0", - "laravel/framework": "^6.0|^7.0|^8.0", - "orchestra/testbench-browser-kit": "^4.0|^5.0|^6.0", - "league/flysystem-aws-s3-v3": "~1.0", + "laravel/framework": "dev-master", + "orchestra/testbench-browser-kit": "dev-master", + "league/flysystem-aws-s3-v3": "*", "doctrine/dbal": "^2.10", "spatie/valuestore": "^1.2.5" }, @@ -49,6 +49,12 @@ } } }, + "scripts": { + "docker-up": "PHP_VERSION=8.0.11 docker-compose up -d", + "docker-down": "PHP_VERSION=8.0.11 docker-compose down", + "docker-rebuild": "PHP_VERSION=8.0.11 docker-compose up -d --no-deps --build", + "test": "./test" + }, "minimum-stability": "dev", "prefer-stable": true } diff --git a/src/Bootstrappers/FilesystemTenancyBootstrapper.php b/src/Bootstrappers/FilesystemTenancyBootstrapper.php index d5ae2d50..157a1337 100644 --- a/src/Bootstrappers/FilesystemTenancyBootstrapper.php +++ b/src/Bootstrappers/FilesystemTenancyBootstrapper.php @@ -57,6 +57,9 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) { /** @var FilesystemAdapter $filesystemDisk */ $filesystemDisk = Storage::disk($disk); + + // todo0 @v4 \League\Flysystem\PathPrefixer is making this a lot more painful in flysystem v2 + $this->originalPaths['disks'][$disk] = $filesystemDisk->getAdapter()->getPathPrefix(); if ($root = str_replace( diff --git a/tests/Etc/ConsoleKernel.php b/tests/Etc/ConsoleKernel.php index 1bc66365..a548f113 100644 --- a/tests/Etc/ConsoleKernel.php +++ b/tests/Etc/ConsoleKernel.php @@ -4,15 +4,10 @@ declare(strict_types=1); namespace Stancl\Tenancy\Tests\Etc; -use Orchestra\Testbench\Console\Kernel; +use Orchestra\Testbench\Foundation\Console\Kernel; class ConsoleKernel extends Kernel { - /** - * The Artisan commands provided by your application. - * - * @var array - */ protected $commands = [ ExampleCommand::class, AddUserCommand::class, From 2726f07bcaecca6351ad2716a04df591042f4972 Mon Sep 17 00:00:00 2001 From: Frederic Habich <53627251+CodeAdminDe@users.noreply.github.com> Date: Wed, 22 Dec 2021 13:24:07 +0100 Subject: [PATCH 05/45] fixed typo (#766) fixed typo within description 'searhced' => 'searched' --- src/Features/UniversalRoutes.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Features/UniversalRoutes.php b/src/Features/UniversalRoutes.php index 970dcd7b..6b729962 100644 --- a/src/Features/UniversalRoutes.php +++ b/src/Features/UniversalRoutes.php @@ -40,7 +40,7 @@ class UniversalRoutes implements Feature } // Loop one level deep and check if the route's middleware - // groups have the searhced middleware group inside them + // groups have the searched middleware group inside them $middlewareGroups = Router::getMiddlewareGroups(); foreach ($route->gatherMiddleware() as $inner) { if (! $inner instanceof Closure && isset($middlewareGroups[$inner]) && in_array($middleware, $middlewareGroups[$inner], true)) { From 08bfd6f9bb04e5364f6d9855e216c9c1bfcf54de Mon Sep 17 00:00:00 2001 From: sort72 <47725212+sort72@users.noreply.github.com> Date: Sat, 25 Dec 2021 09:24:34 -0500 Subject: [PATCH 06/45] Use tenant key on console commands instead of id (#768) --- src/Commands/Migrate.php | 2 +- src/Commands/Rollback.php | 2 +- src/Commands/Run.php | 2 +- src/Commands/Seed.php | 2 +- src/Commands/TenantList.php | 4 ++-- 5 files changed, 6 insertions(+), 6 deletions(-) diff --git a/src/Commands/Migrate.php b/src/Commands/Migrate.php index 4bf8408c..bf92dfcd 100644 --- a/src/Commands/Migrate.php +++ b/src/Commands/Migrate.php @@ -55,7 +55,7 @@ class Migrate extends MigrateCommand } tenancy()->runForMultiple($this->option('tenants'), function ($tenant) { - $this->line("Tenant: {$tenant['id']}"); + $this->line("Tenant: {$tenant->getTenantKey()}"); event(new MigratingDatabase($tenant)); diff --git a/src/Commands/Rollback.php b/src/Commands/Rollback.php index ec9cc461..081872c8 100644 --- a/src/Commands/Rollback.php +++ b/src/Commands/Rollback.php @@ -53,7 +53,7 @@ class Rollback extends RollbackCommand } tenancy()->runForMultiple($this->option('tenants'), function ($tenant) { - $this->line("Tenant: {$tenant['id']}"); + $this->line("Tenant: {$tenant->getTenantKey()}"); event(new RollingBackDatabase($tenant)); diff --git a/src/Commands/Run.php b/src/Commands/Run.php index c2770825..4216d1c6 100644 --- a/src/Commands/Run.php +++ b/src/Commands/Run.php @@ -33,7 +33,7 @@ class Run extends Command public function handle() { tenancy()->runForMultiple($this->option('tenants'), function ($tenant) { - $this->line("Tenant: {$tenant['id']}"); + $this->line("Tenant: {$tenant->getTenantKey()}"); tenancy()->initialize($tenant); $callback = function ($prefix = '') { diff --git a/src/Commands/Seed.php b/src/Commands/Seed.php index 43038107..dc97ae71 100644 --- a/src/Commands/Seed.php +++ b/src/Commands/Seed.php @@ -51,7 +51,7 @@ class Seed extends SeedCommand } tenancy()->runForMultiple($this->option('tenants'), function ($tenant) { - $this->line("Tenant: {$tenant['id']}"); + $this->line("Tenant: {$tenant->getTenantKey()}"); event(new SeedingDatabase($tenant)); diff --git a/src/Commands/TenantList.php b/src/Commands/TenantList.php index 493b5a93..d01afcb9 100644 --- a/src/Commands/TenantList.php +++ b/src/Commands/TenantList.php @@ -36,9 +36,9 @@ class TenantList extends Command ->cursor() ->each(function (Tenant $tenant) { if ($tenant->domains) { - $this->line("[Tenant] id: {$tenant['id']} @ " . implode('; ', $tenant->domains->pluck('domain')->toArray() ?? [])); + $this->line("[Tenant] {$tenant->getTenantKeyName()}: {$tenant->getTenantKey()} @ " . implode('; ', $tenant->domains->pluck('domain')->toArray() ?? [])); } else { - $this->line("[Tenant] id: {$tenant['id']}"); + $this->line("[Tenant] {$tenant->getTenantKeyName()}: {$tenant->getTenantKey()}"); } }); } From 435d8528a75acdbdc2994962bc47e90f4288aed1 Mon Sep 17 00:00:00 2001 From: Stefan Ninic Date: Sat, 25 Dec 2021 22:10:34 +0100 Subject: [PATCH 07/45] Fixed array to string conversion (#718) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Fixed array to string conversion Previous code would give this warning before actually showing exception message `PHP Warning: Array to string conversion in .../vendor/stancl/tenancy/src/CacheManager.php on line 24` * Update variable & syntax Co-authored-by: Samuel Štancl --- src/CacheManager.php | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/src/CacheManager.php b/src/CacheManager.php index f7190842..88428353 100644 --- a/src/CacheManager.php +++ b/src/CacheManager.php @@ -20,8 +20,10 @@ class CacheManager extends BaseCacheManager $tags = [config('tenancy.cache.tag_base') . tenant()->getTenantKey()]; if ($method === 'tags') { - if (count($parameters) !== 1) { - throw new \Exception("Method tags() takes exactly 1 argument. {count($parameters)} passed."); + $count = count($parameters); + + if ($count !== 1) { + throw new \Exception("Method tags() takes exactly 1 argument. $count passed."); } $names = $parameters[0]; From 5980c46449482169890618ca697dfc8809e321be Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 30 Dec 2021 19:46:48 +0100 Subject: [PATCH 08/45] wip --- composer.json | 2 +- src/Exceptions/TenantCouldNotBeIdentifiedById.php | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index faeb3d5e..1eeed2bf 100644 --- a/composer.json +++ b/composer.json @@ -53,7 +53,7 @@ "docker-up": "PHP_VERSION=8.0.11 docker-compose up -d", "docker-down": "PHP_VERSION=8.0.11 docker-compose down", "docker-rebuild": "PHP_VERSION=8.0.11 docker-compose up -d --no-deps --build", - "test": "./test" + "test": "PHP_VERSION=8.0.11 ./test" }, "minimum-stability": "dev", "prefer-stable": true diff --git a/src/Exceptions/TenantCouldNotBeIdentifiedById.php b/src/Exceptions/TenantCouldNotBeIdentifiedById.php index 8fa103ea..5c2e562c 100644 --- a/src/Exceptions/TenantCouldNotBeIdentifiedById.php +++ b/src/Exceptions/TenantCouldNotBeIdentifiedById.php @@ -9,7 +9,7 @@ use Facade\IgnitionContracts\ProvidesSolution; use Facade\IgnitionContracts\Solution; use Stancl\Tenancy\Contracts\TenantCouldNotBeIdentifiedException; -// todo: in v3 this should be suffixed with Exception +// todo: in v4 this should be suffixed with Exception class TenantCouldNotBeIdentifiedById extends TenantCouldNotBeIdentifiedException implements ProvidesSolution { public function __construct($tenant_id) From 73a4a3018cadca2ba0fb5f2130fca1718a2b3670 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 31 Dec 2021 18:10:03 +0100 Subject: [PATCH 09/45] Improve queue tenancy --- docker-compose.yml | 2 +- .../DatabaseTenancyBootstrapper.php | 1 + .../QueueTenancyBootstrapper.php | 80 +++++++-- src/Database/DatabaseManager.php | 23 ++- .../TenantCouldNotBeIdentifiedById.php | 2 +- src/Tenancy.php | 4 +- tests/QueueTest.php | 152 ++++++++++++++++-- tests/TenantDatabaseManagerTest.php | 51 ++++++ 8 files changed, 279 insertions(+), 36 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index 30d87dfd..e8e8d418 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -4,7 +4,7 @@ services: build: context: . args: - PHP_VERSION: ${PHP_VERSION} + PHP_VERSION: ${PHP_VERSION:-8.1} depends_on: mysql: condition: service_healthy diff --git a/src/Bootstrappers/DatabaseTenancyBootstrapper.php b/src/Bootstrappers/DatabaseTenancyBootstrapper.php index a107fc0d..59ee0aec 100644 --- a/src/Bootstrappers/DatabaseTenancyBootstrapper.php +++ b/src/Bootstrappers/DatabaseTenancyBootstrapper.php @@ -6,6 +6,7 @@ namespace Stancl\Tenancy\Bootstrappers; use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\Tenant; +use Stancl\Tenancy\Contracts\TenantWithDatabase; use Stancl\Tenancy\Database\DatabaseManager; use Stancl\Tenancy\Exceptions\TenantDatabaseDoesNotExistException; diff --git a/src/Bootstrappers/QueueTenancyBootstrapper.php b/src/Bootstrappers/QueueTenancyBootstrapper.php index 6fefaad2..5706963e 100644 --- a/src/Bootstrappers/QueueTenancyBootstrapper.php +++ b/src/Bootstrappers/QueueTenancyBootstrapper.php @@ -7,7 +7,10 @@ namespace Stancl\Tenancy\Bootstrappers; use Illuminate\Config\Repository; use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Contracts\Foundation\Application; +use Illuminate\Queue\Events\JobFailed; +use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; +use Illuminate\Queue\Events\JobRetryRequested; use Illuminate\Queue\QueueManager; use Illuminate\Support\Testing\Fakes\QueueFake; use Stancl\Tenancy\Contracts\TenancyBootstrapper; @@ -28,7 +31,7 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper */ public static function __constructStatic(Application $app) { - static::setUpJobListener($app->make(Dispatcher::class)); + static::setUpJobListener($app->make(Dispatcher::class), $app->runningUnitTests()); } public function __construct(Repository $config, QueueManager $queue) @@ -39,25 +42,70 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper $this->setUpPayloadGenerator(); } - protected static function setUpJobListener($dispatcher) + protected static function setUpJobListener($dispatcher, $runningTests) { - $dispatcher->listen(JobProcessing::class, function ($event) { - $tenantId = $event->job->payload()['tenant_id'] ?? null; + $previousTenant = null; - // The job is not tenant-aware - if (! $tenantId) { - return; - } + $dispatcher->listen(JobProcessing::class, function ($event) use (&$previousTenant) { + $previousTenant = tenant(); - // Tenancy is already initialized for the tenant (e.g. dispatchNow was used) - if (tenancy()->initialized && tenant()->getTenantKey() === $tenantId) { - return; - } - - // Tenancy was either not initialized, or initialized for a different tenant. - // Therefore, we initialize it for the correct tenant. - tenancy()->initialize(tenancy()->find($tenantId)); + static::initializeTenancyForQueue($event->job->payload()['tenant_id'] ?? null); }); + + $dispatcher->listen(JobRetryRequested::class, function ($event) use (&$previousTenant) { + $previousTenant = tenant(); + + static::initializeTenancyForQueue($event->payload()['tenant_id'] ?? null); + }); + + // If we're running tests, we make sure to clean up after any artisan('queue:work') calls + $revertToPreviousState = function ($event) use (&$previousTenant, $runningTests) { + if ($runningTests) { + static::revertToPreviousState($event, $previousTenant); + } + }; + + $dispatcher->listen(JobProcessed::class, $revertToPreviousState); // artisan('queue:work') which succeeds + $dispatcher->listen(JobFailed::class, $revertToPreviousState); // artisan('queue:work') which fails + } + + protected static function initializeTenancyForQueue($tenantId) + { + // The job is not tenant-aware + if (! $tenantId) { + return; + } + + if (tenancy()->initialized) { + if (tenant()->getTenantKey() === $tenantId) { + // Tenancy is already initialized for the tenant (e.g. dispatchNow was used) + return; + } + } + + // Tenancy was either not initialized, or initialized for a different tenant. + // Therefore, we initialize it for the correct tenant. + tenancy()->initialize(tenancy()->find($tenantId)); + } + + protected static function revertToPreviousState($event, &$previousTenant) + { + $tenantId = $event->job->payload()['tenant_id'] ?? null; + + // The job was not tenant-aware + if (! $tenantId) { + return; + } + + // Revert back to the previous tenant + if (tenant() && $previousTenant && $previousTenant->isNot(tenant())) { + tenancy()->initialize($previousTenant); + } + + // End tenancy + if (tenant() && (! $previousTenant)) { + tenancy()->end(); + } } protected function setUpPayloadGenerator() diff --git a/src/Database/DatabaseManager.php b/src/Database/DatabaseManager.php index dd30f443..e85fd659 100644 --- a/src/Database/DatabaseManager.php +++ b/src/Database/DatabaseManager.php @@ -38,7 +38,7 @@ class DatabaseManager */ public function connectToTenant(TenantWithDatabase $tenant) { - $this->database->purge('tenant'); + $this->purgeTenantConnection(); $this->createTenantConnection($tenant); $this->setDefaultConnection('tenant'); } @@ -48,10 +48,7 @@ class DatabaseManager */ public function reconnectToCentral() { - if (tenancy()->initialized) { - $this->database->purge('tenant'); - } - + $this->purgeTenantConnection(); $this->setDefaultConnection($this->config->get('tenancy.database.central_connection')); } @@ -60,7 +57,7 @@ class DatabaseManager */ public function setDefaultConnection(string $connection) { - $this->app['config']['database.default'] = $connection; + $this->config['database.default'] = $connection; $this->database->setDefaultConnection($connection); } @@ -69,7 +66,19 @@ class DatabaseManager */ public function createTenantConnection(TenantWithDatabase $tenant) { - $this->app['config']['database.connections.tenant'] = $tenant->database()->connection(); + $this->config['database.connections.tenant'] = $tenant->database()->connection(); + } + + /** + * Purge the tenant database connection. + */ + public function purgeTenantConnection() + { + if (array_key_exists('tenant', $this->database->getConnections())) { + $this->database->purge('tenant'); + } + + unset($this->config['database.connections.tenant']); } /** diff --git a/src/Exceptions/TenantCouldNotBeIdentifiedById.php b/src/Exceptions/TenantCouldNotBeIdentifiedById.php index 8fa103ea..5c2e562c 100644 --- a/src/Exceptions/TenantCouldNotBeIdentifiedById.php +++ b/src/Exceptions/TenantCouldNotBeIdentifiedById.php @@ -9,7 +9,7 @@ use Facade\IgnitionContracts\ProvidesSolution; use Facade\IgnitionContracts\Solution; use Stancl\Tenancy\Contracts\TenantCouldNotBeIdentifiedException; -// todo: in v3 this should be suffixed with Exception +// todo: in v4 this should be suffixed with Exception class TenantCouldNotBeIdentifiedById extends TenantCouldNotBeIdentifiedException implements ProvidesSolution { public function __construct($tenant_id) diff --git a/src/Tenancy.php b/src/Tenancy.php index 864c00f0..30f138e3 100644 --- a/src/Tenancy.php +++ b/src/Tenancy.php @@ -66,10 +66,10 @@ class Tenancy return; } - $this->initialized = false; - event(new Events\TenancyEnded($this)); + $this->initialized = false; + $this->tenant = null; } diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 41d71320..75c727ce 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -4,18 +4,30 @@ declare(strict_types=1); namespace Stancl\Tenancy\Tests; +use Exception; use Illuminate\Bus\Queueable; use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Database\Schema\Blueprint; use Illuminate\Foundation\Bus\Dispatchable; +use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; use Illuminate\Queue\InteractsWithQueue; use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Event; +use Illuminate\Support\Facades\Schema; use Spatie\Valuestore\Valuestore; +use Stancl\JobPipeline\JobPipeline; +use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper; +use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyInitialized; +use Stancl\Tenancy\Events\TenantCreated; +use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Listeners\BootstrapTenancy; +use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Tests\Etc\Tenant; +use Stancl\Tenancy\Tests\Etc\User; class QueueTest extends TestCase { @@ -31,15 +43,49 @@ class QueueTest extends TestCase config([ 'tenancy.bootstrappers' => [ QueueTenancyBootstrapper::class, + DatabaseTenancyBootstrapper::class, ], 'queue.default' => 'redis', ]); Event::listen(TenancyInitialized::class, BootstrapTenancy::class); + Event::listen(TenancyEnded::class, RevertToCentralContext::class); $this->valuestore = Valuestore::make(__DIR__ . '/Etc/tmp/queuetest.json')->flush(); } + protected function withFailedJobs() + { + Schema::connection('central')->create('failed_jobs', function (Blueprint $table) { + $table->id(); + $table->string('uuid')->unique(); + $table->text('connection'); + $table->text('queue'); + $table->longText('payload'); + $table->longText('exception'); + $table->timestamp('failed_at')->useCurrent(); + }); + } + + protected function withUsers() + { + Schema::create('users', function (Blueprint $table) { + $table->increments('id'); + $table->string('name'); + $table->string('email')->unique(); + $table->string('password'); + $table->rememberToken(); + $table->timestamps(); + }); + } + + protected function withTenantDatabases() + { + Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) { + return $event->tenant; + })->toListener()); + } + /** @test */ public function tenant_id_is_passed_to_tenant_queues() { @@ -49,7 +95,7 @@ class QueueTest extends TestCase tenancy()->initialize($tenant); - Event::fake([JobProcessing::class]); + Event::fake([JobProcessing::class, JobProcessed::class]); dispatch(new TestJob($this->valuestore)); @@ -79,21 +125,91 @@ class QueueTest extends TestCase }); } - /** @test */ - public function tenancy_is_initialized_inside_queues() + /** + * @test + * + * @testWith [true] + * [false] + */ + public function tenancy_is_initialized_inside_queues(bool $shouldEndTenancy) { - $tenant = Tenant::create([ - 'id' => 'acme', - ]); + $this->withTenantDatabases(); + $this->withFailedJobs(); + + $tenant = Tenant::create(); tenancy()->initialize($tenant); - dispatch(new TestJob($this->valuestore)); + $this->withUsers(); + + $user = User::create(['name' => 'Foo', 'email' => 'foo@bar.com', 'password' => 'secret']); + + $this->valuestore->put('userName', 'Bar'); + + dispatch(new TestJob($this->valuestore, $user)); $this->assertFalse($this->valuestore->has('tenant_id')); + + if ($shouldEndTenancy) { + tenancy()->end(); + } + $this->artisan('queue:work --once'); - $this->assertSame('The current tenant id is: acme', $this->valuestore->get('tenant_id')); + $this->assertSame(0, DB::connection('central')->table('failed_jobs')->count()); + + $this->assertSame('The current tenant id is: ' . $tenant->id, $this->valuestore->get('tenant_id')); + + $tenant->run(function () use ($user) { + $this->assertSame('Bar', $user->fresh()->name); + }); + } + + /** + * @test + * + * @testWith [true] + * [false] + */ + public function tenancy_is_initialized_when_retrying_jobs(bool $shouldEndTenancy) + { + $this->withFailedJobs(); + $this->withTenantDatabases(); + + $tenant = Tenant::create(); + + tenancy()->initialize($tenant); + + $this->withUsers(); + + $user = User::create(['name' => 'Foo', 'email' => 'foo@bar.com', 'password' => 'secret']); + + $this->valuestore->put('userName', 'Bar'); + $this->valuestore->put('shouldFail', true); + + dispatch(new TestJob($this->valuestore, $user)); + + $this->assertFalse($this->valuestore->has('tenant_id')); + + if ($shouldEndTenancy) { + tenancy()->end(); + } + + $this->artisan('queue:work --once'); + + $this->assertSame(1, DB::connection('central')->table('failed_jobs')->count()); + $this->assertNull($this->valuestore->get('tenant_id')); // job failed + + $this->artisan('queue:retry all'); + $this->artisan('queue:work --once'); + + $this->assertSame(0, DB::connection('central')->table('failed_jobs')->count()); + + $this->assertSame('The current tenant id is: ' . $tenant->id, $this->valuestore->get('tenant_id')); // job succeeded + + $tenant->run(function () use ($user) { + $this->assertSame('Bar', $user->fresh()->name); + }); } /** @test */ @@ -127,13 +243,31 @@ class TestJob implements ShouldQueue /** @var Valuestore */ protected $valuestore; - public function __construct(Valuestore $valuestore) + /** @var User|null */ + protected $user; + + public function __construct(Valuestore $valuestore, User $user = null) { $this->valuestore = $valuestore; + $this->user = $user; } public function handle() { + if ($this->valuestore->get('shouldFail')) { + $this->valuestore->put('shouldFail', false); + + throw new Exception('failing'); + } + + if ($this->user) { + assert($this->user->getConnectionName() === 'tenant'); + } + $this->valuestore->put('tenant_id', 'The current tenant id is: ' . tenant('id')); + + if ($userName = $this->valuestore->get('userName')) { + $this->user->update(['name' => $userName]); + } } } diff --git a/tests/TenantDatabaseManagerTest.php b/tests/TenantDatabaseManagerTest.php index ead2bba8..f64770b1 100644 --- a/tests/TenantDatabaseManagerTest.php +++ b/tests/TenantDatabaseManagerTest.php @@ -4,17 +4,21 @@ declare(strict_types=1); namespace Stancl\Tenancy\Tests; +use Illuminate\Database\Schema\Blueprint; use Illuminate\Support\Facades\Event; +use Illuminate\Support\Facades\Schema; use Illuminate\Support\Str; use PDO; use Stancl\JobPipeline\JobPipeline; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Stancl\Tenancy\Database\DatabaseManager; +use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Events\TenantCreated; use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException; use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Listeners\BootstrapTenancy; +use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager; use Stancl\Tenancy\TenantDatabaseManagers\PermissionControlledMySQLDatabaseManager; use Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager; @@ -102,6 +106,52 @@ class TenantDatabaseManagerTest extends TestCase ]; } + /** @test */ + public function the_tenant_connection_is_fully_removed() + { + config([ + 'tenancy.boostrappers' => [ + DatabaseTenancyBootstrapper::class, + ], + ]); + + Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) { + return $event->tenant; + })->toListener()); + + Event::listen(TenancyInitialized::class, BootstrapTenancy::class); + Event::listen(TenancyEnded::class, RevertToCentralContext::class); + + $tenant = Tenant::create(); + + $this->assertSame(['central'], array_keys(app('db')->getConnections())); + $this->assertArrayNotHasKey('tenant', config('database.connections')); + + tenancy()->initialize($tenant); + + $this->createUsersTable(); + + $this->assertSame(['central', 'tenant'], array_keys(app('db')->getConnections())); + $this->assertArrayHasKey('tenant', config('database.connections')); + + tenancy()->end(); + + $this->assertSame(['central'], array_keys(app('db')->getConnections())); + $this->assertNull(config('database.connections.tenant')); + } + + protected function createUsersTable() + { + Schema::create('users', function (Blueprint $table) { + $table->increments('id'); + $table->string('name'); + $table->string('email')->unique(); + $table->string('password'); + $table->rememberToken(); + $table->timestamps(); + }); + } + /** @test */ public function db_name_is_prefixed_with_db_path_when_sqlite_is_used() { @@ -217,5 +267,6 @@ class TenantDatabaseManagerTest extends TestCase /** @test */ public function path_used_by_sqlite_manager_can_be_customized() { + $this->markTestIncomplete(); } } From 49ef28da059bb0423f9ccd8f9f88b43cd58cfb34 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 31 Dec 2021 18:19:53 +0100 Subject: [PATCH 10/45] 6.x support --- tests/QueueTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 75c727ce..9758c9c8 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -57,7 +57,7 @@ class QueueTest extends TestCase protected function withFailedJobs() { Schema::connection('central')->create('failed_jobs', function (Blueprint $table) { - $table->id(); + $table->increments('id'); $table->string('uuid')->unique(); $table->text('connection'); $table->text('queue'); From a83568ded280ebca364ad898f28db2ec047f87a6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 31 Dec 2021 18:28:37 +0100 Subject: [PATCH 11/45] Only use JobRetryRequested in Laravel 8 --- .../QueueTenancyBootstrapper.php | 20 ++++++---- tests/Etc/tmp/queuetest.json | 2 +- tests/QueueTest.php | 37 +++++++++++-------- 3 files changed, 34 insertions(+), 25 deletions(-) diff --git a/src/Bootstrappers/QueueTenancyBootstrapper.php b/src/Bootstrappers/QueueTenancyBootstrapper.php index 5706963e..c94d6749 100644 --- a/src/Bootstrappers/QueueTenancyBootstrapper.php +++ b/src/Bootstrappers/QueueTenancyBootstrapper.php @@ -4,17 +4,18 @@ declare(strict_types=1); namespace Stancl\Tenancy\Bootstrappers; +use Illuminate\Support\Str; use Illuminate\Config\Repository; -use Illuminate\Contracts\Events\Dispatcher; -use Illuminate\Contracts\Foundation\Application; +use Illuminate\Queue\QueueManager; +use Stancl\Tenancy\Contracts\Tenant; use Illuminate\Queue\Events\JobFailed; use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; +use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Queue\Events\JobRetryRequested; -use Illuminate\Queue\QueueManager; use Illuminate\Support\Testing\Fakes\QueueFake; +use Illuminate\Contracts\Foundation\Application; use Stancl\Tenancy\Contracts\TenancyBootstrapper; -use Stancl\Tenancy\Contracts\Tenant; class QueueTenancyBootstrapper implements TenancyBootstrapper { @@ -52,11 +53,14 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper static::initializeTenancyForQueue($event->job->payload()['tenant_id'] ?? null); }); - $dispatcher->listen(JobRetryRequested::class, function ($event) use (&$previousTenant) { - $previousTenant = tenant(); + if (Str::startsWith(app()->version(), '8')) { + // queue:retry tenancy is only supported in Laravel 8 + $dispatcher->listen(JobRetryRequested::class, function ($event) use (&$previousTenant) { + $previousTenant = tenant(); - static::initializeTenancyForQueue($event->payload()['tenant_id'] ?? null); - }); + static::initializeTenancyForQueue($event->payload()['tenant_id'] ?? null); + }); + } // If we're running tests, we make sure to clean up after any artisan('queue:work') calls $revertToPreviousState = function ($event) use (&$previousTenant, $runningTests) { diff --git a/tests/Etc/tmp/queuetest.json b/tests/Etc/tmp/queuetest.json index 00cf7c37..dc158c6b 100644 --- a/tests/Etc/tmp/queuetest.json +++ b/tests/Etc/tmp/queuetest.json @@ -1 +1 @@ -{"tenant_id":"The current tenant id is: acme"} \ No newline at end of file +{"userName":"Bar","shouldFail":false,"tenant_id":"The current tenant id is: a7f73c10-9879-40ae-b7b0-1ded7c1f7b1b"} \ No newline at end of file diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 9758c9c8..158ad56b 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -5,29 +5,30 @@ declare(strict_types=1); namespace Stancl\Tenancy\Tests; use Exception; +use Illuminate\Support\Str; use Illuminate\Bus\Queueable; -use Illuminate\Contracts\Queue\ShouldQueue; +use Spatie\Valuestore\Valuestore; +use Illuminate\Support\Facades\DB; +use Stancl\Tenancy\Tests\Etc\User; +use Stancl\JobPipeline\JobPipeline; +use Stancl\Tenancy\Tests\Etc\Tenant; +use Illuminate\Support\Facades\Event; +use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\Schema; +use Stancl\Tenancy\Events\TenancyEnded; +use Stancl\Tenancy\Jobs\CreateDatabase; +use Illuminate\Queue\InteractsWithQueue; +use Stancl\Tenancy\Events\TenantCreated; use Illuminate\Database\Schema\Blueprint; -use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; -use Illuminate\Queue\InteractsWithQueue; -use Illuminate\Queue\SerializesModels; -use Illuminate\Support\Facades\DB; -use Illuminate\Support\Facades\Event; -use Illuminate\Support\Facades\Schema; -use Spatie\Valuestore\Valuestore; -use Stancl\JobPipeline\JobPipeline; -use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; -use Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper; -use Stancl\Tenancy\Events\TenancyEnded; +use Illuminate\Contracts\Queue\ShouldQueue; +use Illuminate\Foundation\Bus\Dispatchable; use Stancl\Tenancy\Events\TenancyInitialized; -use Stancl\Tenancy\Events\TenantCreated; -use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\RevertToCentralContext; -use Stancl\Tenancy\Tests\Etc\Tenant; -use Stancl\Tenancy\Tests\Etc\User; +use Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper; +use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; class QueueTest extends TestCase { @@ -173,6 +174,10 @@ class QueueTest extends TestCase */ public function tenancy_is_initialized_when_retrying_jobs(bool $shouldEndTenancy) { + if (! Str::startsWith(app()->version(), '8')) { + $this->markTestSkipped('queue:retry tenancy is only supported in Laravel 8'); + } + $this->withFailedJobs(); $this->withTenantDatabases(); From e442bdb64419038758d4fd6e10946735050b5a7d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Fri, 31 Dec 2021 18:29:05 +0100 Subject: [PATCH 12/45] Only use JobRetryRequested in Laravel 8 --- src/Bootstrappers/QueueTenancyBootstrapper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bootstrappers/QueueTenancyBootstrapper.php b/src/Bootstrappers/QueueTenancyBootstrapper.php index c94d6749..d2ce52d0 100644 --- a/src/Bootstrappers/QueueTenancyBootstrapper.php +++ b/src/Bootstrappers/QueueTenancyBootstrapper.php @@ -54,7 +54,7 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper }); if (Str::startsWith(app()->version(), '8')) { - // queue:retry tenancy is only supported in Laravel 8 + // JobRetryRequested only exists since Laravel 8 $dispatcher->listen(JobRetryRequested::class, function ($event) use (&$previousTenant) { $previousTenant = tenant(); From 96d9ad13d821b1619062e930c2bce54642b1ffd3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 6 Jan 2022 16:57:01 +0100 Subject: [PATCH 13/45] Add a note about 'tenant' connection being reserved (fixes #774) --- assets/config.php | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/assets/config.php b/assets/config.php index 029591ad..85592d14 100644 --- a/assets/config.php +++ b/assets/config.php @@ -42,7 +42,8 @@ return [ 'central_connection' => env('DB_CONNECTION', 'central'), /** - * Connection used as a "template" for the tenant database connection. + * Connection used as a "template" for the dynamically created tenant database connection. + * Note: don't name your template connection tenant. That name is reserved by package. */ 'template_tenant_connection' => null, From f08e33afd80d14d99eb0595261cb100f981d4b52 Mon Sep 17 00:00:00 2001 From: Jori Stein <44996807+stein-j@users.noreply.github.com> Date: Thu, 6 Jan 2022 21:35:56 +0100 Subject: [PATCH 14/45] Remove redondant initialization (#775) --- src/Commands/Run.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/Commands/Run.php b/src/Commands/Run.php index 4216d1c6..aa518d7a 100644 --- a/src/Commands/Run.php +++ b/src/Commands/Run.php @@ -34,7 +34,6 @@ class Run extends Command { tenancy()->runForMultiple($this->option('tenants'), function ($tenant) { $this->line("Tenant: {$tenant->getTenantKey()}"); - tenancy()->initialize($tenant); $callback = function ($prefix = '') { return function ($arguments, $argument) use ($prefix) { From 9c79267e2444b5df4c32123f62387b71953f0a26 Mon Sep 17 00:00:00 2001 From: Erik Gaal Date: Mon, 14 Feb 2022 14:31:01 +0100 Subject: [PATCH 15/45] Fix .env loading in development (#799) * Upgrade vlucas/phpdotenv to ^5.0 `Dotenv::create($paths)` was the syntax for releases before v4 * Remove vlucas/phpdotenv dependency and make it work with newer versions. --- composer.json | 1 - tests/TestCase.php | 6 +++++- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/composer.json b/composer.json index b10f2d16..bf66e1f2 100644 --- a/composer.json +++ b/composer.json @@ -18,7 +18,6 @@ "stancl/virtualcolumn": "^1.0" }, "require-dev": { - "vlucas/phpdotenv": "^3.3|^4.0|^5.0", "laravel/framework": "^6.0|^7.0|^8.0", "orchestra/testbench-browser-kit": "^4.0|^5.0|^6.0", "league/flysystem-aws-s3-v3": "~1.0", diff --git a/tests/TestCase.php b/tests/TestCase.php index bbc42489..d3e42ea1 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -48,7 +48,11 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase protected function getEnvironmentSetUp($app) { if (file_exists(__DIR__ . '/../.env')) { - \Dotenv\Dotenv::create(__DIR__ . '/..')->load(); + if (method_exists(\Dotenv\Dotenv::class, 'createImmutable')) { + \Dotenv\Dotenv::createImmutable(__DIR__ . '/..')->load(); + } else { + \Dotenv\Dotenv::create(__DIR__ . '/..')->load(); + } } $app['config']->set([ From 27f916c3239fb4dbad44f1cfa74fbde4bd9d656d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sat, 19 Feb 2022 16:12:38 +0100 Subject: [PATCH 16/45] end tenancy in queue if the next job is not tenant aware --- src/Bootstrappers/QueueTenancyBootstrapper.php | 10 ++++++++-- tests/Etc/tmp/queuetest.json | 1 - tests/QueueTest.php | 5 +++++ 3 files changed, 13 insertions(+), 3 deletions(-) delete mode 100644 tests/Etc/tmp/queuetest.json diff --git a/src/Bootstrappers/QueueTenancyBootstrapper.php b/src/Bootstrappers/QueueTenancyBootstrapper.php index d2ce52d0..2e9aa051 100644 --- a/src/Bootstrappers/QueueTenancyBootstrapper.php +++ b/src/Bootstrappers/QueueTenancyBootstrapper.php @@ -75,14 +75,20 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper protected static function initializeTenancyForQueue($tenantId) { - // The job is not tenant-aware if (! $tenantId) { + // The job is not tenant-aware + if (tenancy()->initialized) { + // Tenancy was initialized, so we revert back to the central context + tenancy()->end(); + } + return; } if (tenancy()->initialized) { + // Tenancy is already initialized if (tenant()->getTenantKey() === $tenantId) { - // Tenancy is already initialized for the tenant (e.g. dispatchNow was used) + // It's initialized for the same tenant (e.g. dispatchNow was used, or the previous job also ran for this tenant) return; } } diff --git a/tests/Etc/tmp/queuetest.json b/tests/Etc/tmp/queuetest.json deleted file mode 100644 index dc158c6b..00000000 --- a/tests/Etc/tmp/queuetest.json +++ /dev/null @@ -1 +0,0 @@ -{"userName":"Bar","shouldFail":false,"tenant_id":"The current tenant id is: a7f73c10-9879-40ae-b7b0-1ded7c1f7b1b"} \ No newline at end of file diff --git a/tests/QueueTest.php b/tests/QueueTest.php index 158ad56b..afe64fea 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -55,6 +55,11 @@ class QueueTest extends TestCase $this->valuestore = Valuestore::make(__DIR__ . '/Etc/tmp/queuetest.json')->flush(); } + public function tearDown(): void + { + $this->valuestore->flush(); + } + protected function withFailedJobs() { Schema::connection('central')->create('failed_jobs', function (Blueprint $table) { From 368d3cc99f7916af5e8290cddabd4968eeec73ab Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sat, 19 Feb 2022 16:21:27 +0100 Subject: [PATCH 17/45] add forceRefresh option to QueueTenancyBootstrapper --- .../QueueTenancyBootstrapper.php | 19 +++++++++++++++++++ 1 file changed, 19 insertions(+) diff --git a/src/Bootstrappers/QueueTenancyBootstrapper.php b/src/Bootstrappers/QueueTenancyBootstrapper.php index 2e9aa051..6a88f701 100644 --- a/src/Bootstrappers/QueueTenancyBootstrapper.php +++ b/src/Bootstrappers/QueueTenancyBootstrapper.php @@ -25,6 +25,14 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper /** @var QueueManager */ protected $queue; + /** + * Don't persist the same tenant across multiple jobs even if they have the same tenant ID. + * + * This is useful when you're changing the tenant's state (e.g. properties in the `data` column) and want the next job to initialize tenancy again + * with the new data. Features like the Tenant Config are only executed when tenancy is initialized, so the re-initialization is needed in some cases. + */ + public static bool $forceRefresh = false; + /** * The normal constructor is only executed after tenancy is bootstrapped. * However, we're registering a hook to initialize tenancy. Therefore, @@ -85,6 +93,17 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper return; } + if (static::$forceRefresh) { + // Re-initialize tenancy between all jobs + if (tenancy()->initialized) { + tenancy()->end(); + } + + tenancy()->initialize(tenancy()->find($tenantId)); + + return; + } + if (tenancy()->initialized) { // Tenancy is already initialized if (tenant()->getTenantKey() === $tenantId) { From 8e9485f9b1f51d066a791d5cf94834738383ab76 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sat, 19 Feb 2022 16:31:31 +0100 Subject: [PATCH 18/45] add empty queuetest.json --- tests/Etc/tmp/queuetest.json | 0 1 file changed, 0 insertions(+), 0 deletions(-) create mode 100644 tests/Etc/tmp/queuetest.json diff --git a/tests/Etc/tmp/queuetest.json b/tests/Etc/tmp/queuetest.json new file mode 100644 index 00000000..e69de29b From 5249ec7c82013a5122f076d9fac6ed2a04b8f532 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sat, 19 Feb 2022 16:31:45 +0100 Subject: [PATCH 19/45] ignore changes to queuetest.json --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 1d03dbec..b3223156 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ psysh phpunit_var_*.xml coverage/ clover.xml +tests/Etc/tmp/queuetest.json From 5b9b3845261fcd5f24181f2a92911db62b26364f Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sat, 19 Feb 2022 16:33:59 +0100 Subject: [PATCH 20/45] Remove codecov --- .github/workflows/ci.yml | 4 ---- README.md | 1 - 2 files changed, 5 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index efb8ad02..f7b64d2d 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -26,7 +26,3 @@ jobs: run: docker-compose exec -T test composer require --no-interaction "laravel/framework:${{ matrix.laravel }}" - name: Run tests run: ./test - - name: Send code coverage to codecov - env: - CODECOV_TOKEN: 24382d15-84e7-4a55-bea4-c4df96a24a9b - run: bash <(curl -s https://codecov.io/bash) diff --git a/README.md b/README.md index 46f1b097..f4d28288 100644 --- a/README.md +++ b/README.md @@ -6,7 +6,6 @@ Laravel 6.x/7.x/8.x Latest Stable Version GitHub Actions CI status - codecov Donate

From b4a4eab949481d2f96b0b2c1c21aa8db92a2147e Mon Sep 17 00:00:00 2001 From: masiorama Date: Tue, 22 Feb 2022 16:26:07 +0100 Subject: [PATCH 21/45] Add drop of db views on migrate fresh command (#812) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Optionally handle drop of table views on MigrateFresh @stancl I managed to make the modification discussed here #811 Afaik (and I can understand) this is the easiest way to handle it, but I'm open to discuss. * Remove redundant store variable * code style Co-authored-by: Samuel Å tancl --- src/Commands/MigrateFresh.php | 4 ++++ 1 file changed, 4 insertions(+) diff --git a/src/Commands/MigrateFresh.php b/src/Commands/MigrateFresh.php index f50e2f5f..4d003db0 100644 --- a/src/Commands/MigrateFresh.php +++ b/src/Commands/MigrateFresh.php @@ -7,6 +7,7 @@ namespace Stancl\Tenancy\Commands; use Illuminate\Console\Command; use Stancl\Tenancy\Concerns\DealsWithMigrations; use Stancl\Tenancy\Concerns\HasATenantsOption; +use Symfony\Component\Console\Input\InputOption; final class MigrateFresh extends Command { @@ -22,6 +23,8 @@ final class MigrateFresh extends Command public function __construct() { parent::__construct(); + + $this->addOption('--drop-views', null, InputOption::VALUE_NONE, 'Drop views along with tenant tables.', null); $this->setName('tenants:migrate-fresh'); } @@ -37,6 +40,7 @@ final class MigrateFresh extends Command $this->info('Dropping tables.'); $this->call('db:wipe', array_filter([ '--database' => 'tenant', + '--drop-views' => $this->option('drop-views'), '--force' => true, ])); From 79e3d53b06f33aa6e30ee4454177e1d798918704 Mon Sep 17 00:00:00 2001 From: Erik Gaal Date: Tue, 8 Mar 2022 01:50:25 +0100 Subject: [PATCH 22/45] [3.x] Compatibility with Laravel 9 (#802) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Test on Laravel 9 * Don't extend final Kernel class * Make FilesystemTenancyBootstrapper compatible with Flysystem v3 Co-authored-by: George * Update tenant maintenance mode te be in line with Laravel * Exclude PHP 7.4 <> L9 combination from testing * add root_override-related assertions * getPrefix -> getPathPrefix * handle / inconsistency in s3 prefix * Refactor Storage facade changes Co-authored-by: George Co-authored-by: Samuel Å tancl --- .github/workflows/ci.yml | 5 +- composer.json | 8 +-- .../FilesystemTenancyBootstrapper.php | 31 ++++----- .../CheckTenantForMaintenanceMode.php | 9 ++- tests/BootstrapperTest.php | 69 +++++++++++-------- tests/Etc/ConsoleKernel.php | 2 +- tests/MaintenanceModeTest.php | 4 +- tests/TestCase.php | 1 + 8 files changed, 74 insertions(+), 55 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index f7b64d2d..1303061a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -16,7 +16,10 @@ jobs: strategy: matrix: php: ["7.4", "8.0"] - laravel: ["^6.0", "^8.0"] + laravel: ["^6.0", "^8.0", "^9.0"] + exclude: + - laravel: "^9.0" + php: "7.4" steps: - uses: actions/checkout@v2 diff --git a/composer.json b/composer.json index bf66e1f2..88bfea29 100644 --- a/composer.json +++ b/composer.json @@ -11,16 +11,16 @@ ], "require": { "ext-json": "*", - "illuminate/support": "^6.0|^7.0|^8.0", + "illuminate/support": "^6.0|^7.0|^8.0|^9.0", "facade/ignition-contracts": "^1.0", "ramsey/uuid": "^3.7|^4.0", "stancl/jobpipeline": "^1.0", "stancl/virtualcolumn": "^1.0" }, "require-dev": { - "laravel/framework": "^6.0|^7.0|^8.0", - "orchestra/testbench-browser-kit": "^4.0|^5.0|^6.0", - "league/flysystem-aws-s3-v3": "~1.0", + "laravel/framework": "^6.0|^7.0|^8.0|^9.0", + "orchestra/testbench": "^4.0|^5.0|^6.0|^7.0", + "league/flysystem-aws-s3-v3": "^1.0|^3.0", "doctrine/dbal": "^2.10", "spatie/valuestore": "^1.2.5" }, diff --git a/src/Bootstrappers/FilesystemTenancyBootstrapper.php b/src/Bootstrappers/FilesystemTenancyBootstrapper.php index d5ae2d50..418be93f 100644 --- a/src/Bootstrappers/FilesystemTenancyBootstrapper.php +++ b/src/Bootstrappers/FilesystemTenancyBootstrapper.php @@ -54,20 +54,20 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper } // Storage facade - foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) { - /** @var FilesystemAdapter $filesystemDisk */ - $filesystemDisk = Storage::disk($disk); - $this->originalPaths['disks'][$disk] = $filesystemDisk->getAdapter()->getPathPrefix(); + Storage::forgetDisk($this->app['config']['tenancy.filesystem.disks']); - if ($root = str_replace( + foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) { + $originalRoot = $this->app['config']["filesystems.disks.{$disk}.root"]; + $this->originalPaths['disks'][$disk] = $originalRoot; + + $finalPrefix = str_replace( '%storage_path%', storage_path(), - $this->app['config']["tenancy.filesystem.root_override.{$disk}"] ?? '' - )) { - $filesystemDisk->getAdapter()->setPathPrefix($finalPrefix = $root); - } else { - $root = $this->app['config']["filesystems.disks.{$disk}.root"]; - $filesystemDisk->getAdapter()->setPathPrefix($finalPrefix = $root . "/{$suffix}"); + $this->app['config']["tenancy.filesystem.root_override.{$disk}"] ?? '', + ); + + if (! $finalPrefix) { + $finalPrefix = $originalRoot . '/'. $suffix; } $this->app['config']["filesystems.disks.{$disk}.root"] = $finalPrefix; @@ -84,14 +84,9 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper $this->app['url']->setAssetRoot($this->app['config']['app.asset_url']); // Storage facade + Storage::forgetDisk($this->app['config']['tenancy.filesystem.disks']); foreach ($this->app['config']['tenancy.filesystem.disks'] as $disk) { - /** @var FilesystemAdapter $filesystemDisk */ - $filesystemDisk = Storage::disk($disk); - - $root = $this->originalPaths['disks'][$disk]; - - $filesystemDisk->getAdapter()->setPathPrefix($root); - $this->app['config']["filesystems.disks.{$disk}.root"] = $root; + $this->app['config']["filesystems.disks.{$disk}.root"] = $this->originalPaths['disks'][$disk]; } } } diff --git a/src/Middleware/CheckTenantForMaintenanceMode.php b/src/Middleware/CheckTenantForMaintenanceMode.php index 5554663f..8e29a31e 100644 --- a/src/Middleware/CheckTenantForMaintenanceMode.php +++ b/src/Middleware/CheckTenantForMaintenanceMode.php @@ -5,7 +5,7 @@ declare(strict_types=1); namespace Stancl\Tenancy\Middleware; use Closure; -use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException; +use Symfony\Component\HttpKernel\Exception\HttpException; use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode; use Stancl\Tenancy\Exceptions\TenancyNotInitializedException; use Symfony\Component\HttpFoundation\IpUtils; @@ -29,7 +29,12 @@ class CheckTenantForMaintenanceMode extends CheckForMaintenanceMode return $next($request); } - throw new MaintenanceModeException($data['time'], $data['retry'], $data['message']); + throw new HttpException( + 503, + 'Service Unavailable', + null, + isset($data['retry']) ? ['Retry-After' => $data['retry']] : [] + ); } return $next($request); diff --git a/tests/BootstrapperTest.php b/tests/BootstrapperTest.php index 1b0c880d..29aa7dc9 100644 --- a/tests/BootstrapperTest.php +++ b/tests/BootstrapperTest.php @@ -4,23 +4,27 @@ declare(strict_types=1); namespace Stancl\Tenancy\Tests; -use Illuminate\Support\Facades\Cache; +use Illuminate\Filesystem\FilesystemAdapter; +use ReflectionObject; +use ReflectionProperty; +use Illuminate\Support\Str; use Illuminate\Support\Facades\DB; +use Stancl\JobPipeline\JobPipeline; +use Stancl\Tenancy\Tests\Etc\Tenant; +use Illuminate\Support\Facades\Cache; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Storage; -use Stancl\JobPipeline\JobPipeline; -use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper; -use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; -use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; -use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper; use Stancl\Tenancy\Events\TenancyEnded; -use Stancl\Tenancy\Events\TenancyInitialized; -use Stancl\Tenancy\Events\TenantCreated; use Stancl\Tenancy\Jobs\CreateDatabase; +use Stancl\Tenancy\Events\TenantCreated; +use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\RevertToCentralContext; -use Stancl\Tenancy\Tests\Etc\Tenant; +use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper; +use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper; +use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; +use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; class BootstrapperTest extends TestCase { @@ -165,6 +169,7 @@ class BootstrapperTest extends TestCase $tenant2 = Tenant::create(); tenancy()->initialize($tenant1); + Storage::disk('public')->put('foo', 'bar'); $this->assertSame('bar', Storage::disk('public')->get('foo')); @@ -184,30 +189,38 @@ class BootstrapperTest extends TestCase $this->assertFalse(Storage::disk('public')->exists('foo')); $this->assertFalse(Storage::disk('public')->exists('abc')); + $expected_storage_path = $old_storage_path . '/tenant' . tenant('id'); // /tenant = suffix base + + // Check that disk prefixes respect the root_override logic + $this->assertSame($expected_storage_path . '/app/', $this->getDiskPrefix('local')); + $this->assertSame($expected_storage_path . '/app/public/', $this->getDiskPrefix('public')); + $this->assertSame('tenant' . tenant('id') . '/', ltrim($this->getDiskPrefix('s3'), '/')); + // Check suffixing logic $new_storage_path = storage_path(); - $this->assertEquals($old_storage_path . '/' . config('tenancy.filesystem.suffix_base') . tenant('id'), $new_storage_path); + $this->assertEquals($expected_storage_path, $new_storage_path); + } - foreach (config('tenancy.filesystem.disks') as $disk) { - $suffix = config('tenancy.filesystem.suffix_base') . tenant('id'); + protected function getDiskPrefix(string $disk): string + { + /** @var FilesystemAdapter $disk */ + $disk = Storage::disk($disk); + $adapter = $disk->getAdapter(); - /** @var FilesystemAdapter $filesystemDisk */ - $filesystemDisk = Storage::disk($disk); - - $current_path_prefix = $filesystemDisk->getAdapter()->getPathPrefix(); - - if ($override = config("tenancy.filesystem.root_override.{$disk}")) { - $correct_path_prefix = str_replace('%storage_path%', storage_path(), $override); - } else { - if ($base = $old_storage_facade_roots[$disk]) { - $correct_path_prefix = $base . "/$suffix/"; - } else { - $correct_path_prefix = "$suffix/"; - } - } - - $this->assertSame($correct_path_prefix, $current_path_prefix); + if (! Str::startsWith(app()->version(), '9.')) { + return $adapter->getPathPrefix(); } + + $prefixer = (new ReflectionObject($adapter))->getProperty('prefixer'); + $prefixer->setAccessible(true); + + // reflection -> instance + $prefixer = $prefixer->getValue($adapter); + + $prefix = (new ReflectionProperty($prefixer, 'prefix')); + $prefix->setAccessible(true); + + return $prefix->getValue($prefixer); } // for queues see QueueTest diff --git a/tests/Etc/ConsoleKernel.php b/tests/Etc/ConsoleKernel.php index 1bc66365..9d37d3c6 100644 --- a/tests/Etc/ConsoleKernel.php +++ b/tests/Etc/ConsoleKernel.php @@ -4,7 +4,7 @@ declare(strict_types=1); namespace Stancl\Tenancy\Tests\Etc; -use Orchestra\Testbench\Console\Kernel; +use Orchestra\Testbench\Foundation\Console\Kernel; class ConsoleKernel extends Kernel { diff --git a/tests/MaintenanceModeTest.php b/tests/MaintenanceModeTest.php index a8ecb064..4a8d8d0c 100644 --- a/tests/MaintenanceModeTest.php +++ b/tests/MaintenanceModeTest.php @@ -4,12 +4,14 @@ declare(strict_types=1); namespace Stancl\Tenancy\Tests; +use Symfony\Component\HttpKernel\Exception\HttpException; use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException; use Illuminate\Support\Facades\Route; use Stancl\Tenancy\Database\Concerns\MaintenanceMode; use Stancl\Tenancy\Middleware\CheckTenantForMaintenanceMode; use Stancl\Tenancy\Middleware\InitializeTenancyByDomain; use Stancl\Tenancy\Tests\Etc\Tenant; +use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException; class MaintenanceModeTest extends TestCase { @@ -32,7 +34,7 @@ class MaintenanceModeTest extends TestCase $tenant->putDownForMaintenance(); - $this->expectException(MaintenanceModeException::class); + $this->expectException(HttpException::class); $this->withoutExceptionHandling() ->get('http://acme.localhost/foo'); } diff --git a/tests/TestCase.php b/tests/TestCase.php index d3e42ea1..cea669a1 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -87,6 +87,7 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase 'public', 's3', ], + 'filesystems.disks.s3.bucket' => 'foo', 'tenancy.redis.tenancy' => env('TENANCY_TEST_REDIS_TENANCY', true), 'database.redis.client' => env('TENANCY_TEST_REDIS_CLIENT', 'phpredis'), 'tenancy.redis.prefixed_connections' => ['default'], From eb1a2ebe32a3dd9e5941c81602d55aab2268db06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 17 Mar 2022 12:24:57 +0100 Subject: [PATCH 23/45] Use 7.2 instead of 7.4 --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 1303061a..3d1698b4 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,11 +15,11 @@ jobs: strategy: matrix: - php: ["7.4", "8.0"] + php: ["7.2", "8.0"] laravel: ["^6.0", "^8.0", "^9.0"] exclude: - laravel: "^9.0" - php: "7.4" + php: "7.2" steps: - uses: actions/checkout@v2 From fa2a61fcd74126587312567d5e07e273c8ea43b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 17 Mar 2022 12:30:14 +0100 Subject: [PATCH 24/45] Use PHP 7.3 instead of 7.2 --- .github/workflows/ci.yml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 3d1698b4..cc8ad985 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -15,11 +15,11 @@ jobs: strategy: matrix: - php: ["7.2", "8.0"] + php: ["7.3", "8.0"] laravel: ["^6.0", "^8.0", "^9.0"] exclude: - laravel: "^9.0" - php: "7.2" + php: "7.3" steps: - uses: actions/checkout@v2 From 49ebb75f007d649465fc3f847ddc76f6396b0337 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Thu, 17 Mar 2022 12:46:49 +0100 Subject: [PATCH 25/45] Fixes #827 --- src/Bootstrappers/QueueTenancyBootstrapper.php | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/Bootstrappers/QueueTenancyBootstrapper.php b/src/Bootstrappers/QueueTenancyBootstrapper.php index 6a88f701..666e29ed 100644 --- a/src/Bootstrappers/QueueTenancyBootstrapper.php +++ b/src/Bootstrappers/QueueTenancyBootstrapper.php @@ -30,8 +30,10 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper * * This is useful when you're changing the tenant's state (e.g. properties in the `data` column) and want the next job to initialize tenancy again * with the new data. Features like the Tenant Config are only executed when tenancy is initialized, so the re-initialization is needed in some cases. + * + * @var bool */ - public static bool $forceRefresh = false; + public static $forceRefresh = false; /** * The normal constructor is only executed after tenancy is bootstrapped. From 5026f54a6d4482226951d3a8196218ea41434db4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 23 Mar 2022 20:48:55 +0100 Subject: [PATCH 26/45] fix path prefixing --- CONTRIBUTING.md | 13 +++++++++++++ Dockerfile | 2 +- docker-compose.override.yml | 5 +++++ src/Bootstrappers/FilesystemTenancyBootstrapper.php | 4 +++- tests/BootstrapperTest.php | 2 +- tests/Etc/tmp/queuetest.json | 0 6 files changed, 23 insertions(+), 3 deletions(-) create mode 100644 docker-compose.override.yml delete mode 100644 tests/Etc/tmp/queuetest.json diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7dce1b82..a5a6ec3f 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -9,3 +9,16 @@ StyleCI will flag code style violations in your pull requests. Run `docker-compose up -d` to start the containers. Then run `./test` to run the tests. When you're done testing, run `docker-compose down` to shut down the containers. + +### Docker on M1 + +You can add: +```yaml +services: + mysql: + platform: linux/amd64 + mysql2: + platform: linux/amd64 +``` + +to `docker-compose.override.yml` to make `docker-compose up-d` work on M1. diff --git a/Dockerfile b/Dockerfile index 06d97aea..36f52d6a 100644 --- a/Dockerfile +++ b/Dockerfile @@ -30,7 +30,7 @@ RUN docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \ # && if [ "${PHP_VERSION}" = "7.4" ]; then docker-php-ext-configure gd --with-freetype --with-jpeg; else docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/; fi \ && docker-php-ext-install -j$(nproc) gd pdo pdo_mysql pdo_pgsql pdo_sqlite pgsql zip gmp bcmath pcntl ldap sysvmsg exif \ # install the redis php extension - && pecl install redis-5.3.2 \ + && pecl install redis-5.3.7 \ && docker-php-ext-enable redis \ # install the pcov extention && pecl install pcov \ diff --git a/docker-compose.override.yml b/docker-compose.override.yml new file mode 100644 index 00000000..29e9fb37 --- /dev/null +++ b/docker-compose.override.yml @@ -0,0 +1,5 @@ +services: + mysql: + platform: linux/amd64 + mysql2: + platform: linux/amd64 diff --git a/src/Bootstrappers/FilesystemTenancyBootstrapper.php b/src/Bootstrappers/FilesystemTenancyBootstrapper.php index 418be93f..dcd7299e 100644 --- a/src/Bootstrappers/FilesystemTenancyBootstrapper.php +++ b/src/Bootstrappers/FilesystemTenancyBootstrapper.php @@ -67,7 +67,9 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper ); if (! $finalPrefix) { - $finalPrefix = $originalRoot . '/'. $suffix; + $finalPrefix = $originalRoot + ? $originalRoot . '/'. $suffix + : $suffix; } $this->app['config']["filesystems.disks.{$disk}.root"] = $finalPrefix; diff --git a/tests/BootstrapperTest.php b/tests/BootstrapperTest.php index 29aa7dc9..588fadd8 100644 --- a/tests/BootstrapperTest.php +++ b/tests/BootstrapperTest.php @@ -194,7 +194,7 @@ class BootstrapperTest extends TestCase // Check that disk prefixes respect the root_override logic $this->assertSame($expected_storage_path . '/app/', $this->getDiskPrefix('local')); $this->assertSame($expected_storage_path . '/app/public/', $this->getDiskPrefix('public')); - $this->assertSame('tenant' . tenant('id') . '/', ltrim($this->getDiskPrefix('s3'), '/')); + $this->assertSame('tenant' . tenant('id') . '/', $this->getDiskPrefix('s3'), '/'); // Check suffixing logic $new_storage_path = storage_path(); diff --git a/tests/Etc/tmp/queuetest.json b/tests/Etc/tmp/queuetest.json deleted file mode 100644 index e69de29b..00000000 From 600bb823de185ef02c3ff5aeba0c75f41409bafa Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 23 Mar 2022 20:49:25 +0100 Subject: [PATCH 27/45] avoid double // in prefix --- src/Bootstrappers/FilesystemTenancyBootstrapper.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/Bootstrappers/FilesystemTenancyBootstrapper.php b/src/Bootstrappers/FilesystemTenancyBootstrapper.php index dcd7299e..da1e5e2a 100644 --- a/src/Bootstrappers/FilesystemTenancyBootstrapper.php +++ b/src/Bootstrappers/FilesystemTenancyBootstrapper.php @@ -68,7 +68,7 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper if (! $finalPrefix) { $finalPrefix = $originalRoot - ? $originalRoot . '/'. $suffix + ? rtrim($originalRoot, '/') . '/'. $suffix : $suffix; } From e1ae6f4380bf2062adfb44bc1acb83e603b95ed9 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 23 Mar 2022 20:57:15 +0100 Subject: [PATCH 28/45] re-add queuetest.json --- .gitignore | 1 - tests/Etc/tmp/queuetest.json | 0 2 files changed, 1 deletion(-) create mode 100644 tests/Etc/tmp/queuetest.json diff --git a/.gitignore b/.gitignore index b3223156..1d03dbec 100644 --- a/.gitignore +++ b/.gitignore @@ -8,4 +8,3 @@ psysh phpunit_var_*.xml coverage/ clover.xml -tests/Etc/tmp/queuetest.json diff --git a/tests/Etc/tmp/queuetest.json b/tests/Etc/tmp/queuetest.json new file mode 100644 index 00000000..e69de29b From 4e717236f9c1aa5d4e4aa588bace783d4d7bcde2 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 23 Mar 2022 20:57:32 +0100 Subject: [PATCH 29/45] revert gitignore --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 1d03dbec..b3223156 100644 --- a/.gitignore +++ b/.gitignore @@ -8,3 +8,4 @@ psysh phpunit_var_*.xml coverage/ clover.xml +tests/Etc/tmp/queuetest.json From 4f196097979653fa7b7522ac339116d88b5baae4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 23 Mar 2022 20:58:47 +0100 Subject: [PATCH 30/45] remove docker-compose.override.yml --- .gitignore | 1 + docker-compose.override.yml | 5 ----- 2 files changed, 1 insertion(+), 5 deletions(-) delete mode 100644 docker-compose.override.yml diff --git a/.gitignore b/.gitignore index b3223156..f470ba75 100644 --- a/.gitignore +++ b/.gitignore @@ -9,3 +9,4 @@ phpunit_var_*.xml coverage/ clover.xml tests/Etc/tmp/queuetest.json +docker-compose.override.yml diff --git a/docker-compose.override.yml b/docker-compose.override.yml deleted file mode 100644 index 29e9fb37..00000000 --- a/docker-compose.override.yml +++ /dev/null @@ -1,5 +0,0 @@ -services: - mysql: - platform: linux/amd64 - mysql2: - platform: linux/amd64 From 349125c02ebe71216bfbbb4ea0c3b955e03ba474 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 30 Mar 2022 18:00:55 +0200 Subject: [PATCH 31/45] Merge hotfix branch (#834) * try specifying the signature in __construct * constructor doesn't work since Reflection is used, try specifying getDefaultName() instead * Fixed: make migration commands compatible * Fix failing tests * Fix username generation * Re-create tmp dir as well if needed * wip --- src/Commands/Migrate.php | 20 ++++++---------- src/Commands/MigrateFresh.php | 2 +- src/Commands/Rollback.php | 11 ++++++--- src/Concerns/ExtendsLaravelCommand.php | 23 +++++++++++++++++++ src/Database/DatabaseManager.php | 10 +++++++- src/Jobs/CreateDatabase.php | 2 +- ...rmissionControlledMySQLDatabaseManager.php | 5 ---- tests/DatabaseUsersTest.php | 9 ++++++-- tests/Etc/tmp/queuetest.json | 0 tests/QueueTest.php | 20 +++++++++++++++- 10 files changed, 75 insertions(+), 27 deletions(-) create mode 100644 src/Concerns/ExtendsLaravelCommand.php delete mode 100644 tests/Etc/tmp/queuetest.json diff --git a/src/Commands/Migrate.php b/src/Commands/Migrate.php index bf92dfcd..c67d3598 100644 --- a/src/Commands/Migrate.php +++ b/src/Commands/Migrate.php @@ -8,32 +8,26 @@ use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Database\Console\Migrations\MigrateCommand; use Illuminate\Database\Migrations\Migrator; use Stancl\Tenancy\Concerns\DealsWithMigrations; +use Stancl\Tenancy\Concerns\ExtendsLaravelCommand; use Stancl\Tenancy\Concerns\HasATenantsOption; use Stancl\Tenancy\Events\DatabaseMigrated; use Stancl\Tenancy\Events\MigratingDatabase; class Migrate extends MigrateCommand { - use HasATenantsOption, DealsWithMigrations; + use HasATenantsOption, DealsWithMigrations, ExtendsLaravelCommand; - /** - * The console command description. - * - * @var string - */ protected $description = 'Run migrations for tenant(s)'; - /** - * Create a new command instance. - * - * @param Migrator $migrator - * @param Dispatcher $dispatcher - */ + protected static function getTenantCommandName(): string + { + return 'tenants:migrate'; + } + public function __construct(Migrator $migrator, Dispatcher $dispatcher) { parent::__construct($migrator, $dispatcher); - $this->setName('tenants:migrate'); $this->specifyParameters(); } diff --git a/src/Commands/MigrateFresh.php b/src/Commands/MigrateFresh.php index 4d003db0..283d70b0 100644 --- a/src/Commands/MigrateFresh.php +++ b/src/Commands/MigrateFresh.php @@ -23,7 +23,7 @@ final class MigrateFresh extends Command public function __construct() { parent::__construct(); - + $this->addOption('--drop-views', null, InputOption::VALUE_NONE, 'Drop views along with tenant tables.', null); $this->setName('tenants:migrate-fresh'); diff --git a/src/Commands/Rollback.php b/src/Commands/Rollback.php index 081872c8..e60d974b 100644 --- a/src/Commands/Rollback.php +++ b/src/Commands/Rollback.php @@ -7,13 +7,19 @@ namespace Stancl\Tenancy\Commands; use Illuminate\Database\Console\Migrations\RollbackCommand; use Illuminate\Database\Migrations\Migrator; use Stancl\Tenancy\Concerns\DealsWithMigrations; +use Stancl\Tenancy\Concerns\ExtendsLaravelCommand; use Stancl\Tenancy\Concerns\HasATenantsOption; use Stancl\Tenancy\Events\DatabaseRolledBack; use Stancl\Tenancy\Events\RollingBackDatabase; class Rollback extends RollbackCommand { - use HasATenantsOption, DealsWithMigrations; + use HasATenantsOption, DealsWithMigrations, ExtendsLaravelCommand; + + protected static function getTenantCommandName(): string + { + return 'tenants:rollback'; + } /** * The console command description. @@ -31,8 +37,7 @@ class Rollback extends RollbackCommand { parent::__construct($migrator); - $this->setName('tenants:rollback'); - $this->specifyParameters(); + $this->specifyTenantSignature(); } /** diff --git a/src/Concerns/ExtendsLaravelCommand.php b/src/Concerns/ExtendsLaravelCommand.php new file mode 100644 index 00000000..bdafc8f7 --- /dev/null +++ b/src/Concerns/ExtendsLaravelCommand.php @@ -0,0 +1,23 @@ +specifyParameters(); + } + + public function getName(): ?string + { + return static::getTenantCommandName(); + } + + public static function getDefaultName(): ?string + { + return static::getTenantCommandName(); + } + + abstract protected static function getTenantCommandName(): string; +} diff --git a/src/Database/DatabaseManager.php b/src/Database/DatabaseManager.php index e85fd659..6242ffa9 100644 --- a/src/Database/DatabaseManager.php +++ b/src/Database/DatabaseManager.php @@ -7,10 +7,12 @@ namespace Stancl\Tenancy\Database; use Illuminate\Config\Repository; use Illuminate\Contracts\Foundation\Application; use Illuminate\Database\DatabaseManager as BaseDatabaseManager; +use Stancl\Tenancy\Contracts\ManagesDatabaseUsers; use Stancl\Tenancy\Contracts\TenantCannotBeCreatedException; use Stancl\Tenancy\Contracts\TenantWithDatabase; use Stancl\Tenancy\Exceptions\DatabaseManagerNotRegisteredException; use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException; +use Stancl\Tenancy\Exceptions\TenantDatabaseUserAlreadyExistsException; /** * @internal Class is subject to breaking changes in minor and patch versions. @@ -90,8 +92,14 @@ class DatabaseManager */ public function ensureTenantCanBeCreated(TenantWithDatabase $tenant): void { - if ($tenant->database()->manager()->databaseExists($database = $tenant->database()->getName())) { + $manager = $tenant->database()->manager(); + + if ($manager->databaseExists($database = $tenant->database()->getName())) { throw new TenantDatabaseAlreadyExistsException($database); } + + if ($manager instanceof ManagesDatabaseUsers && $manager->userExists($username = $tenant->database()->getUsername())) { + throw new TenantDatabaseUserAlreadyExistsException($username); + } } } diff --git a/src/Jobs/CreateDatabase.php b/src/Jobs/CreateDatabase.php index 3a74534d..3cb2a6b4 100644 --- a/src/Jobs/CreateDatabase.php +++ b/src/Jobs/CreateDatabase.php @@ -36,8 +36,8 @@ class CreateDatabase implements ShouldQueue return false; } - $databaseManager->ensureTenantCanBeCreated($this->tenant); $this->tenant->database()->makeCredentials(); + $databaseManager->ensureTenantCanBeCreated($this->tenant); $this->tenant->database()->manager()->createDatabase($this->tenant); event(new DatabaseCreated($this->tenant)); diff --git a/src/TenantDatabaseManagers/PermissionControlledMySQLDatabaseManager.php b/src/TenantDatabaseManagers/PermissionControlledMySQLDatabaseManager.php index f8bedc97..918601a8 100644 --- a/src/TenantDatabaseManagers/PermissionControlledMySQLDatabaseManager.php +++ b/src/TenantDatabaseManagers/PermissionControlledMySQLDatabaseManager.php @@ -7,7 +7,6 @@ namespace Stancl\Tenancy\TenantDatabaseManagers; use Stancl\Tenancy\Concerns\CreatesDatabaseUsers; use Stancl\Tenancy\Contracts\ManagesDatabaseUsers; use Stancl\Tenancy\DatabaseConfig; -use Stancl\Tenancy\Exceptions\TenantDatabaseUserAlreadyExistsException; class PermissionControlledMySQLDatabaseManager extends MySQLDatabaseManager implements ManagesDatabaseUsers { @@ -26,10 +25,6 @@ class PermissionControlledMySQLDatabaseManager extends MySQLDatabaseManager impl $hostname = $databaseConfig->connection()['host']; $password = $databaseConfig->getPassword(); - if ($this->userExists($username)) { - throw new TenantDatabaseUserAlreadyExistsException($username); - } - $this->database()->statement("CREATE USER `{$username}`@`%` IDENTIFIED BY '{$password}'"); $grants = implode(', ', static::$grants); diff --git a/tests/DatabaseUsersTest.php b/tests/DatabaseUsersTest.php index 0b095024..344239d1 100644 --- a/tests/DatabaseUsersTest.php +++ b/tests/DatabaseUsersTest.php @@ -10,6 +10,7 @@ use Illuminate\Support\Str; use Stancl\JobPipeline\JobPipeline; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Stancl\Tenancy\Contracts\ManagesDatabaseUsers; +use Stancl\Tenancy\Events\DatabaseCreated; use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Events\TenantCreated; use Stancl\Tenancy\Exceptions\TenantDatabaseUserAlreadyExistsException; @@ -67,14 +68,18 @@ class DatabaseUsersTest extends TestCase $this->assertTrue($manager->databaseExists($tenant->database()->getName())); $this->expectException(TenantDatabaseUserAlreadyExistsException::class); + Event::fake([DatabaseCreated::class]); + $tenant2 = Tenant::create([ 'tenancy_db_username' => $username, ]); /** @var ManagesDatabaseUsers $manager */ - $manager = $tenant2->database()->manager(); + $manager2 = $tenant2->database()->manager(); + // database was not created because of DB transaction - $this->assertFalse($manager->databaseExists($tenant2->database()->getName())); + $this->assertFalse($manager2->databaseExists($tenant2->database()->getName())); + Event::assertNotDispatched(DatabaseCreated::class); } /** @test */ diff --git a/tests/Etc/tmp/queuetest.json b/tests/Etc/tmp/queuetest.json deleted file mode 100644 index e69de29b..00000000 diff --git a/tests/QueueTest.php b/tests/QueueTest.php index afe64fea..a3df9cd7 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -4,6 +4,7 @@ declare(strict_types=1); namespace Stancl\Tenancy\Tests; +use Closure; use Exception; use Illuminate\Support\Str; use Illuminate\Bus\Queueable; @@ -24,6 +25,7 @@ use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Foundation\Bus\Dispatchable; +use PDO; use Stancl\Tenancy\Events\TenancyInitialized; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\RevertToCentralContext; @@ -52,7 +54,7 @@ class QueueTest extends TestCase Event::listen(TenancyInitialized::class, BootstrapTenancy::class); Event::listen(TenancyEnded::class, RevertToCentralContext::class); - $this->valuestore = Valuestore::make(__DIR__ . '/Etc/tmp/queuetest.json')->flush(); + $this->createValueStore(); } public function tearDown(): void @@ -60,6 +62,22 @@ class QueueTest extends TestCase $this->valuestore->flush(); } + protected function createValueStore(): void + { + $valueStorePath = __DIR__ . '/Etc/tmp/queuetest.json'; + + if (! file_exists($valueStorePath)) { + // The directory sometimes goes missing as well when the file is deleted in git + if (! is_dir(__DIR__ . '/Etc/tmp')) { + mkdir(__DIR__ . '/Etc/tmp'); + } + + file_put_contents($valueStorePath, ''); + } + + $this->valuestore = Valuestore::make($valueStorePath)->flush(); + } + protected function withFailedJobs() { Schema::connection('central')->create('failed_jobs', function (Blueprint $table) { From f065ea60b0e56c6a22cd75c476ca448649f8ffe2 Mon Sep 17 00:00:00 2001 From: Roy de Vos Burchart Date: Fri, 1 Apr 2022 22:53:09 +0200 Subject: [PATCH 32/45] Update QueueTenancyBootstrapper.php (#836) --- src/Bootstrappers/QueueTenancyBootstrapper.php | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/Bootstrappers/QueueTenancyBootstrapper.php b/src/Bootstrappers/QueueTenancyBootstrapper.php index 666e29ed..790e1344 100644 --- a/src/Bootstrappers/QueueTenancyBootstrapper.php +++ b/src/Bootstrappers/QueueTenancyBootstrapper.php @@ -63,8 +63,8 @@ class QueueTenancyBootstrapper implements TenancyBootstrapper static::initializeTenancyForQueue($event->job->payload()['tenant_id'] ?? null); }); - if (Str::startsWith(app()->version(), '8')) { - // JobRetryRequested only exists since Laravel 8 + if (version_compare(app()->version(), '8.64', '>=')) { + // JobRetryRequested only exists since Laravel 8.64 $dispatcher->listen(JobRetryRequested::class, function ($event) use (&$previousTenant) { $previousTenant = tenant(); From 40bf576e0087f3350fa5f91c2c73962a7dcdff50 Mon Sep 17 00:00:00 2001 From: Nathan Dunn Date: Fri, 8 Apr 2022 02:13:29 +0100 Subject: [PATCH 33/45] [3.x] Update PostgreSQLSchemaManager to set correct config key value (#840) * Update PostgreSQLSchemaManager to set correct config key value * Update to use version_compare * Update TenantDatabaseManagerTest * Improve TenantDatabaseManagerTest * Update TenantDatabaseManager --- src/TenantDatabaseManagers/PostgreSQLSchemaManager.php | 6 +++++- tests/TenantDatabaseManagerTest.php | 6 +++++- 2 files changed, 10 insertions(+), 2 deletions(-) diff --git a/src/TenantDatabaseManagers/PostgreSQLSchemaManager.php b/src/TenantDatabaseManagers/PostgreSQLSchemaManager.php index 9d815b25..55f049d0 100644 --- a/src/TenantDatabaseManagers/PostgreSQLSchemaManager.php +++ b/src/TenantDatabaseManagers/PostgreSQLSchemaManager.php @@ -46,7 +46,11 @@ class PostgreSQLSchemaManager implements TenantDatabaseManager public function makeConnectionConfig(array $baseConfig, string $databaseName): array { - $baseConfig['schema'] = $databaseName; + if (version_compare(app()->version(), '9.0', '>=')) { + $baseConfig['search_path'] = $databaseName; + } else { + $baseConfig['schema'] = $databaseName; + } return $baseConfig; } diff --git a/tests/TenantDatabaseManagerTest.php b/tests/TenantDatabaseManagerTest.php index f64770b1..3d45d96f 100644 --- a/tests/TenantDatabaseManagerTest.php +++ b/tests/TenantDatabaseManagerTest.php @@ -194,7 +194,11 @@ class TenantDatabaseManagerTest extends TestCase ]); tenancy()->initialize($tenant); - $this->assertSame($tenant->database()->getName(), config('database.connections.' . config('database.default') . '.schema')); + $schemaConfig = version_compare(app()->version(), '9.0', '>=') ? + config('database.connections.' . config('database.default') . '.search_path') : + config('database.connections.' . config('database.default') . '.schema'); + + $this->assertSame($tenant->database()->getName(), $schemaConfig); $this->assertSame($originalDatabaseName, config(['database.connections.pgsql.database'])); } From 0569bf5a3495a194079540f8c7096ab6aac68117 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 1 May 2022 12:56:25 +0200 Subject: [PATCH 34/45] Update README.md --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index f4d28288..95fb7c60 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@

- Laravel 6.x/7.x/8.x + Laravel 9.x Latest Stable Version GitHub Actions CI status Donate From a1c34421488b8eb4c5776cbf457f0719c79d742e Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 15 May 2022 13:32:09 +0200 Subject: [PATCH 35/45] Resolve #854 --- src/Database/Concerns/BelongsToTenant.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Database/Concerns/BelongsToTenant.php b/src/Database/Concerns/BelongsToTenant.php index 5410758d..fc899411 100644 --- a/src/Database/Concerns/BelongsToTenant.php +++ b/src/Database/Concerns/BelongsToTenant.php @@ -16,7 +16,7 @@ trait BelongsToTenant public function tenant() { - return $this->belongsTo(config('tenancy.tenant_model'), BelongsToTenant::$tenantIdColumn); + return $this->belongsTo(config('tenancy.tenant_model'), static::$tenantIdColumn); } public static function bootBelongsToTenant() @@ -24,9 +24,9 @@ trait BelongsToTenant static::addGlobalScope(new TenantScope); static::creating(function ($model) { - if (! $model->getAttribute(BelongsToTenant::$tenantIdColumn) && ! $model->relationLoaded('tenant')) { + if (! $model->getAttribute(static::$tenantIdColumn) && ! $model->relationLoaded('tenant')) { if (tenancy()->initialized) { - $model->setAttribute(BelongsToTenant::$tenantIdColumn, tenant()->getTenantKey()); + $model->setAttribute(static::$tenantIdColumn, tenant()->getTenantKey()); $model->setRelation('tenant', tenant()); } } From 4d95e88e272d5c3f4beebd10a8e549d17259a079 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Sun, 15 May 2022 13:45:54 +0200 Subject: [PATCH 36/45] Revert "Resolve #854" This reverts commit a1c34421488b8eb4c5776cbf457f0719c79d742e. --- src/Database/Concerns/BelongsToTenant.php | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/Database/Concerns/BelongsToTenant.php b/src/Database/Concerns/BelongsToTenant.php index fc899411..5410758d 100644 --- a/src/Database/Concerns/BelongsToTenant.php +++ b/src/Database/Concerns/BelongsToTenant.php @@ -16,7 +16,7 @@ trait BelongsToTenant public function tenant() { - return $this->belongsTo(config('tenancy.tenant_model'), static::$tenantIdColumn); + return $this->belongsTo(config('tenancy.tenant_model'), BelongsToTenant::$tenantIdColumn); } public static function bootBelongsToTenant() @@ -24,9 +24,9 @@ trait BelongsToTenant static::addGlobalScope(new TenantScope); static::creating(function ($model) { - if (! $model->getAttribute(static::$tenantIdColumn) && ! $model->relationLoaded('tenant')) { + if (! $model->getAttribute(BelongsToTenant::$tenantIdColumn) && ! $model->relationLoaded('tenant')) { if (tenancy()->initialized) { - $model->setAttribute(static::$tenantIdColumn, tenant()->getTenantKey()); + $model->setAttribute(BelongsToTenant::$tenantIdColumn, tenant()->getTenantKey()); $model->setRelation('tenant', tenant()); } } From 51228defc68a6362e31f486a04e3117105abf3e0 Mon Sep 17 00:00:00 2001 From: Vincent GS Date: Thu, 26 May 2022 04:51:27 -0500 Subject: [PATCH 37/45] [3.x][Filesystem] Provide an additional argument for tenant name path (#817) * Let the user pass the tenant suffix by %tenant% In this PR we let the user pass an additional parameter using `%tenant%` so the user can additionally pass the folder corresponding to each tenant. This is my proposal, because if I try to use %storage_path% within Linux, I get the full path to the project when I use Google Cloud Storage * Missing missing updates Moving from $subject to $root when %storage_path% has been replaced --- src/Bootstrappers/FilesystemTenancyBootstrapper.php | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/Bootstrappers/FilesystemTenancyBootstrapper.php b/src/Bootstrappers/FilesystemTenancyBootstrapper.php index da1e5e2a..346892b3 100644 --- a/src/Bootstrappers/FilesystemTenancyBootstrapper.php +++ b/src/Bootstrappers/FilesystemTenancyBootstrapper.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace Stancl\Tenancy\Bootstrappers; use Illuminate\Contracts\Foundation\Application; -use Illuminate\Filesystem\FilesystemAdapter; use Illuminate\Support\Facades\Storage; use Stancl\Tenancy\Contracts\TenancyBootstrapper; use Stancl\Tenancy\Contracts\Tenant; @@ -61,8 +60,8 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper $this->originalPaths['disks'][$disk] = $originalRoot; $finalPrefix = str_replace( - '%storage_path%', - storage_path(), + ['%storage_path%', '%tenant%'], + [storage_path(), $tenant->getTenantKey()], $this->app['config']["tenancy.filesystem.root_override.{$disk}"] ?? '', ); From d0de09aa5360b17033b81726d38b18272462f6b4 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Wed, 1 Jun 2022 15:36:46 +0200 Subject: [PATCH 38/45] remove old versions from CI --- .github/workflows/ci.yml | 11 ++++------- 1 file changed, 4 insertions(+), 7 deletions(-) diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index cc8ad985..81d37af5 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -5,9 +5,9 @@ env: on: push: - branches: [ 3.x, 2.x, master ] + branches: [ master ] pull_request: - branches: [ 3.x, 2.x, master ] + branches: [ master ] jobs: tests: @@ -15,11 +15,8 @@ jobs: strategy: matrix: - php: ["7.3", "8.0"] - laravel: ["^6.0", "^8.0", "^9.0"] - exclude: - - laravel: "^9.0" - php: "7.3" + php: ["8.1"] + laravel: ["^9.0"] steps: - uses: actions/checkout@v2 From 7d98ebb5d177a1ac30ec0a7d248c4cd254abce4a Mon Sep 17 00:00:00 2001 From: Victor R <39545521+viicslen@users.noreply.github.com> Date: Wed, 1 Jun 2022 10:12:59 -0400 Subject: [PATCH 39/45] [4.x] Add tenant schema dump command (#807) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * Add tenant dump command * Register tenant schema dump command * Added tests for tenant schema dump command * remove docblocks, fix tenant() logic * trigger ci * Install mysql-client * mysql-client -> mariadb-client * add tenant-schema-test.dump to .gitignore Co-authored-by: Samuel Å tancl Co-authored-by: Samuel Å tancl --- .gitignore | 1 + Dockerfile | 2 +- src/Commands/TenantDump.php | 54 ++++++++++++++++++++++++++++ src/TenancyServiceProvider.php | 1 + tests/CommandsTest.php | 32 +++++++++++++++++ tests/Etc/tenant-schema.dump | 66 ++++++++++++++++++++++++++++++++++ 6 files changed, 155 insertions(+), 1 deletion(-) create mode 100644 src/Commands/TenantDump.php create mode 100644 tests/Etc/tenant-schema.dump diff --git a/.gitignore b/.gitignore index f470ba75..95522c34 100644 --- a/.gitignore +++ b/.gitignore @@ -8,5 +8,6 @@ psysh phpunit_var_*.xml coverage/ clover.xml +tenant-schema-test.dump tests/Etc/tmp/queuetest.json docker-compose.override.yml diff --git a/Dockerfile b/Dockerfile index 36f52d6a..a636bc45 100644 --- a/Dockerfile +++ b/Dockerfile @@ -24,7 +24,7 @@ ENV LANG=en_GB.UTF-8 # install some OS packages we need RUN apt-get update -RUN apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev libgmp-dev libldap2-dev netcat curl sqlite3 libsqlite3-dev libpq-dev libzip-dev unzip vim-tiny gosu git +RUN apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev libgmp-dev libldap2-dev netcat curl mariadb-client sqlite3 libsqlite3-dev libpq-dev libzip-dev unzip vim-tiny gosu git # install php extensions RUN docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \ # && if [ "${PHP_VERSION}" = "7.4" ]; then docker-php-ext-configure gd --with-freetype --with-jpeg; else docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/; fi \ diff --git a/src/Commands/TenantDump.php b/src/Commands/TenantDump.php new file mode 100644 index 00000000..557c6975 --- /dev/null +++ b/src/Commands/TenantDump.php @@ -0,0 +1,54 @@ +setName('tenants:dump'); + $this->specifyParameters(); + } + + + public function handle(ConnectionResolverInterface $connections, Dispatcher $dispatcher): int + { + $this->tenant()->run(fn() => parent::handle($connections, $dispatcher)); + + return Command::SUCCESS; + } + + public function tenant(): Tenant + { + $tenant = $this->option('tenant') + ?? tenant() + ?? $this->ask('What tenant do you want to dump the schema for?') + ?? tenancy()->query()->first(); + + if (! $tenant instanceof Tenant) { + $tenant = tenancy()->find($tenant); + } + + throw_if(! $tenant, 'Could not identify the tenant to use for dumping the schema.'); + + return $tenant; + } + + protected function getOptions(): array + { + return array_merge([ + ['tenant', null, InputOption::VALUE_OPTIONAL, '', null], + ], parent::getOptions()); + } +} diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index 4faaccf3..dd061af3 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -88,6 +88,7 @@ class TenancyServiceProvider extends ServiceProvider Commands\Migrate::class, Commands\Rollback::class, Commands\TenantList::class, + Commands\TenantDump::class, Commands\MigrateFresh::class, ]); diff --git a/tests/CommandsTest.php b/tests/CommandsTest.php index d7da0cab..145a93c5 100644 --- a/tests/CommandsTest.php +++ b/tests/CommandsTest.php @@ -91,6 +91,38 @@ class CommandsTest extends TestCase $this->assertTrue(Schema::hasTable('users')); } + /** @test */ + public function migrate_command_loads_schema_state() + { + $tenant = Tenant::create(); + + $this->assertFalse(Schema::hasTable('schema_users')); + $this->assertFalse(Schema::hasTable('users')); + + Artisan::call('tenants:migrate --schema-path="tests/Etc/tenant-schema.dump"'); + + $this->assertFalse(Schema::hasTable('schema_users')); + $this->assertFalse(Schema::hasTable('users')); + + tenancy()->initialize($tenant); + + // Check for both tables to see if missing migrations also get executed + $this->assertTrue(Schema::hasTable('schema_users')); + $this->assertTrue(Schema::hasTable('users')); + } + + /** @test */ + public function dump_command_works() + { + $tenant = Tenant::create(); + Artisan::call('tenants:migrate'); + + tenancy()->initialize($tenant); + + Artisan::call('tenants:dump --path="tests/Etc/tenant-schema-test.dump"'); + $this->assertFileExists('tests/Etc/tenant-schema-test.dump'); + } + /** @test */ public function rollback_command_works() { diff --git a/tests/Etc/tenant-schema.dump b/tests/Etc/tenant-schema.dump new file mode 100644 index 00000000..6af9f019 --- /dev/null +++ b/tests/Etc/tenant-schema.dump @@ -0,0 +1,66 @@ +/*!40103 SET @OLD_TIME_ZONE=@@TIME_ZONE */; +/*!40103 SET TIME_ZONE='+00:00' */; +/*!40014 SET @OLD_UNIQUE_CHECKS=@@UNIQUE_CHECKS, UNIQUE_CHECKS=0 */; +/*!40014 SET @OLD_FOREIGN_KEY_CHECKS=@@FOREIGN_KEY_CHECKS, FOREIGN_KEY_CHECKS=0 */; +/*!40101 SET @OLD_SQL_MODE=@@SQL_MODE, SQL_MODE='NO_AUTO_VALUE_ON_ZERO' */; +/*!40111 SET @OLD_SQL_NOTES=@@SQL_NOTES, SQL_NOTES=0 */; +DROP TABLE IF EXISTS `failed_jobs`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `failed_jobs` ( + `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, + `uuid` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `connection` text COLLATE utf8mb4_unicode_ci NOT NULL, + `queue` text COLLATE utf8mb4_unicode_ci NOT NULL, + `payload` longtext COLLATE utf8mb4_unicode_ci NOT NULL, + `exception` longtext COLLATE utf8mb4_unicode_ci NOT NULL, + `failed_at` timestamp NOT NULL DEFAULT CURRENT_TIMESTAMP, + PRIMARY KEY (`id`), + UNIQUE KEY `failed_jobs_uuid_unique` (`uuid`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; +DROP TABLE IF EXISTS `migrations`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `migrations` ( + `id` int(10) unsigned NOT NULL AUTO_INCREMENT, + `migration` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `batch` int(11) NOT NULL, + PRIMARY KEY (`id`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; +DROP TABLE IF EXISTS `password_resets`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `password_resets` ( + `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `token` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `created_at` timestamp NULL DEFAULT NULL, + KEY `password_resets_email_index` (`email`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; +DROP TABLE IF EXISTS `users`; +/*!40101 SET @saved_cs_client = @@character_set_client */; +/*!40101 SET character_set_client = utf8 */; +CREATE TABLE `schema_users` ( + `id` bigint(20) unsigned NOT NULL AUTO_INCREMENT, + `name` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `email` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `email_verified_at` timestamp NULL DEFAULT NULL, + `password` varchar(255) COLLATE utf8mb4_unicode_ci NOT NULL, + `remember_token` varchar(100) COLLATE utf8mb4_unicode_ci DEFAULT NULL, + `created_at` timestamp NULL DEFAULT NULL, + `updated_at` timestamp NULL DEFAULT NULL, + PRIMARY KEY (`id`), + UNIQUE KEY `users_email_unique` (`email`) +) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4 COLLATE=utf8mb4_unicode_ci; +/*!40101 SET character_set_client = @saved_cs_client */; +/*!40103 SET TIME_ZONE=@OLD_TIME_ZONE */; + +/*!40101 SET SQL_MODE=@OLD_SQL_MODE */; +/*!40014 SET FOREIGN_KEY_CHECKS=@OLD_FOREIGN_KEY_CHECKS */; +/*!40014 SET UNIQUE_CHECKS=@OLD_UNIQUE_CHECKS */; +/*!40111 SET SQL_NOTES=@OLD_SQL_NOTES */; + +INSERT INTO `migrations` VALUES (2,'2014_10_12_100000_testbench_create_password_resets_table',1); +INSERT INTO `migrations` VALUES (3,'2019_08_19_000000_testbench_create_failed_jobs_table',1); From 72c41ea9938eef4964ef9469d829c16536f69e43 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Mon, 13 Jun 2022 19:16:35 +0200 Subject: [PATCH 40/45] Discord link --- SUPPORT.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SUPPORT.md b/SUPPORT.md index b7caaa5c..24be468b 100644 --- a/SUPPORT.md +++ b/SUPPORT.md @@ -1,5 +1,5 @@ # Get Support -If you need help with implementing the package, you can join our community [Discord server](https://discord.gg/7cpgPxv) and ask in `#help`. +If you need help with implementing the package, you can join our community [Discord server](https://archte.ch/discord) and ask in `#help`. If you're interested in paid consulting from the maintainer, see the [contact page](https://tenancyforlaravel.com/contact/) on our website. From cc6d4fe0ddcec405f645fbd2d8e90ab71afb1bec Mon Sep 17 00:00:00 2001 From: Nick Kitchen <61636526+nickakitch@users.noreply.github.com> Date: Thu, 23 Jun 2022 21:04:53 +1000 Subject: [PATCH 41/45] [4.x] Added support for Microsoft Sql Server (#715) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * added support for microsoft sql server database * added support for microsoft sql server database * trigger ci * revert change * trigger ci * Try installing pdo_sqlsrv * different approach for installing sqlsrv via pecl * add dependencies * add gnupg2 * Update Dockerfile * try skipping msodbcsql17 * Update Dockerfile * add dependency back * update before installing * try to add mssql * mssql host * TENANCY_TEST_MSSQL_HOST env var * add env vars for mssql * add sqlsrv vars to TestCase * rename vars to SQLSRV [skip ci] * MSSQL -> SQLSRV Co-authored-by: Samuel Å tancl Co-authored-by: Samuel Å tancl --- Dockerfile | 21 +++++-- assets/config.php | 1 + docker-compose.yml | 11 ++++ .../MicrosoftSQLDatabaseManager.php | 57 +++++++++++++++++++ tests/TenantDatabaseManagerTest.php | 2 + tests/TestCase.php | 4 ++ 6 files changed, 90 insertions(+), 6 deletions(-) create mode 100644 src/TenantDatabaseManagers/MicrosoftSQLDatabaseManager.php diff --git a/Dockerfile b/Dockerfile index a636bc45..fb63afe3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -1,7 +1,7 @@ ARG PHP_VERSION=7.4 ARG PHP_TARGET=php:${PHP_VERSION}-cli -FROM ${PHP_TARGET} +FROM --platform=linux/amd64 ${PHP_TARGET} ARG COMPOSER_TARGET=2.0.3 @@ -22,10 +22,16 @@ ENV LANG=en_GB.UTF-8 # Dockerfile _and pin the versions_! Eg: # RUN pecl install memcached-2.2.0 && docker-php-ext-enable memcached -# install some OS packages we need -RUN apt-get update -RUN apt-get install -y --no-install-recommends libfreetype6-dev libjpeg62-turbo-dev libpng-dev libgmp-dev libldap2-dev netcat curl mariadb-client sqlite3 libsqlite3-dev libpq-dev libzip-dev unzip vim-tiny gosu git - # install php extensions + +RUN apt-get update \ + && apt-get install -y gnupg2 \ + && curl https://packages.microsoft.com/keys/microsoft.asc | apt-key add - \ + && curl https://packages.microsoft.com/config/debian/11/prod.list > /etc/apt/sources.list.d/mssql-release.list \ + && apt-get update \ + && ACCEPT_EULA=Y apt-get install -y unixodbc-dev msodbcsql17 + +RUN apt-get install -y --no-install-recommends locales apt-transport-https libfreetype6-dev libjpeg62-turbo-dev libpng-dev libgmp-dev libldap2-dev netcat curl mariadb-client sqlite3 libsqlite3-dev libpq-dev libzip-dev unzip vim-tiny gosu git + RUN docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \ # && if [ "${PHP_VERSION}" = "7.4" ]; then docker-php-ext-configure gd --with-freetype --with-jpeg; else docker-php-ext-configure gd --with-freetype-dir=/usr/include/ --with-jpeg-dir=/usr/include/; fi \ && docker-php-ext-install -j$(nproc) gd pdo pdo_mysql pdo_pgsql pdo_sqlite pgsql zip gmp bcmath pcntl ldap sysvmsg exif \ @@ -35,7 +41,10 @@ RUN docker-php-ext-configure pgsql -with-pgsql=/usr/local/pgsql \ # install the pcov extention && pecl install pcov \ && docker-php-ext-enable pcov \ - && echo "pcov.enabled = 1" > /usr/local/etc/php/conf.d/pcov.ini + && echo "pcov.enabled = 1" > /usr/local/etc/php/conf.d/pcov.ini \ + # install sqlsrv + && pecl install sqlsrv pdo_sqlsrv \ + && docker-php-ext-enable sqlsrv pdo_sqlsrv # clear the apt cache RUN rm -rf /var/lib/apt/lists/* \ && rm -rf /var/lib/apt/lists/* \ diff --git a/assets/config.php b/assets/config.php index 85592d14..e1c82e6b 100644 --- a/assets/config.php +++ b/assets/config.php @@ -61,6 +61,7 @@ return [ 'sqlite' => Stancl\Tenancy\TenantDatabaseManagers\SQLiteDatabaseManager::class, 'mysql' => Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager::class, 'pgsql' => Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager::class, + 'sqlsrv' => Stancl\Tenancy\TenantDatabaseManagers\MicrosoftSQLDatabaseManager::class, /** * Use this database manager for MySQL to have a DB user created for each tenant database. diff --git a/docker-compose.yml b/docker-compose.yml index e8e8d418..7b635637 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -22,6 +22,9 @@ services: TENANCY_TEST_REDIS_HOST: redis TENANCY_TEST_MYSQL_HOST: mysql TENANCY_TEST_PGSQL_HOST: postgres + TENANCY_TEST_SQLSRV_HOST: mssql + TENANCY_TEST_SQLSRV_USERNAME: sa + TENANCY_TEST_SQLSRV_PASSWORD: P@ssword stdin_open: true tty: true mysql: @@ -64,3 +67,11 @@ services: interval: 1s timeout: 3s retries: 30 + mssql: + image: mcr.microsoft.com/mssql/server:2019-latest + ports: + - 1433:1433 + environment: + - ACCEPT_EULA=Y + - SA_PASSWORD=P@ssword # todo reuse values from env above + # todo missing health check diff --git a/src/TenantDatabaseManagers/MicrosoftSQLDatabaseManager.php b/src/TenantDatabaseManagers/MicrosoftSQLDatabaseManager.php new file mode 100644 index 00000000..0bc34623 --- /dev/null +++ b/src/TenantDatabaseManagers/MicrosoftSQLDatabaseManager.php @@ -0,0 +1,57 @@ +connection === null) { + throw new NoConnectionSetException(static::class); + } + + return DB::connection($this->connection); + } + + public function setConnection(string $connection): void + { + $this->connection = $connection; + } + + public function createDatabase(TenantWithDatabase $tenant): bool + { + $database = $tenant->database()->getName(); + $charset = $this->database()->getConfig('charset'); + $collation = $this->database()->getConfig('collation'); + + return $this->database()->statement("CREATE DATABASE [{$database}]"); + } + + public function deleteDatabase(TenantWithDatabase $tenant): bool + { + return $this->database()->statement("DROP DATABASE [{$tenant->database()->getName()}]"); + } + + public function databaseExists(string $name): bool + { + return (bool) $this->database()->select("SELECT name FROM master.sys.databases WHERE name = '$name'"); + } + + public function makeConnectionConfig(array $baseConfig, string $databaseName): array + { + $baseConfig['database'] = $databaseName; + + return $baseConfig; + } +} diff --git a/tests/TenantDatabaseManagerTest.php b/tests/TenantDatabaseManagerTest.php index 3d45d96f..0e1464c0 100644 --- a/tests/TenantDatabaseManagerTest.php +++ b/tests/TenantDatabaseManagerTest.php @@ -19,6 +19,7 @@ use Stancl\Tenancy\Exceptions\TenantDatabaseAlreadyExistsException; use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\RevertToCentralContext; +use Stancl\Tenancy\TenantDatabaseManagers\MicrosoftSQLDatabaseManager; use Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager; use Stancl\Tenancy\TenantDatabaseManagers\PermissionControlledMySQLDatabaseManager; use Stancl\Tenancy\TenantDatabaseManagers\PostgreSQLDatabaseManager; @@ -103,6 +104,7 @@ class TenantDatabaseManagerTest extends TestCase ['sqlite', SQLiteDatabaseManager::class], ['pgsql', PostgreSQLDatabaseManager::class], ['pgsql', PostgreSQLSchemaManager::class], + ['sqlsrv', MicrosoftSQLDatabaseManager::class] ]; } diff --git a/tests/TestCase.php b/tests/TestCase.php index cea669a1..75fe51fd 100644 --- a/tests/TestCase.php +++ b/tests/TestCase.php @@ -81,6 +81,10 @@ abstract class TestCase extends \Orchestra\Testbench\TestCase ], 'database.connections.sqlite.database' => ':memory:', 'database.connections.mysql.host' => env('TENANCY_TEST_MYSQL_HOST', '127.0.0.1'), + 'database.connections.sqlsrv.username' => env('TENANCY_TEST_SQLSRV_USERNAME', 'sa'), + 'database.connections.sqlsrv.password' => env('TENANCY_TEST_SQLSRV_PASSWORD', 'P@ssword'), + 'database.connections.sqlsrv.host' => env('TENANCY_TEST_SQLSRV_HOST', '127.0.0.1'), + 'database.connections.sqlsrv.database' => null, 'database.connections.pgsql.host' => env('TENANCY_TEST_PGSQL_HOST', '127.0.0.1'), 'tenancy.filesystem.disks' => [ 'local', From 4aec6bfda20e659c4e62d2527ead99fbf44926cd Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Samuel=20=C5=A0tancl?= Date: Tue, 28 Jun 2022 21:47:33 +0200 Subject: [PATCH 42/45] add phpunit dependency --- composer.json | 1 + 1 file changed, 1 insertion(+) diff --git a/composer.json b/composer.json index 8e932658..8123944a 100644 --- a/composer.json +++ b/composer.json @@ -20,6 +20,7 @@ "require-dev": { "laravel/framework": "^6.0|^7.0|^8.0|^9.0", "orchestra/testbench": "^4.0|^5.0|^6.0|^7.0", + "phpunit/phpunit": "*", "league/flysystem-aws-s3-v3": "^1.0|^3.0", "doctrine/dbal": "^2.10", "spatie/valuestore": "^1.2.5" From 627233d07aa173893cc2afd6484d44f8455c7362 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Wed, 20 Jul 2022 16:02:33 +0500 Subject: [PATCH 43/45] [4.x] Don't use onDeleteCascade in the migrations (#883) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * removed `cascade` on delete for domains * removed only `onDelete` cascade * keep impersonation migrations unchanged * domains set null on delete * Update 2019_09_15_000020_create_domains_table.php * Added DeleteDomain Job while deleting tenant. * Update assets/TenancyServiceProvider.stub.php Co-authored-by: Samuel Å tancl * renamed class * Update DeleteDomains.php * onDelete restrict * revert nullable * removed `shouldQueue` interface * Update TenancyServiceProvider.stub.php * fetch and delete domains individually * Update DeleteDomains.php * tests for `DeleteDomains` job Co-authored-by: Samuel Å tancl --- assets/TenancyServiceProvider.stub.php | 8 +++- ...2019_09_15_000020_create_domains_table.php | 2 +- src/Jobs/DeleteDomains.php | 35 ++++++++++++++++ tests/DeleteDomainsJobTest.php | 42 +++++++++++++++++++ 4 files changed, 85 insertions(+), 2 deletions(-) create mode 100644 src/Jobs/DeleteDomains.php create mode 100644 tests/DeleteDomainsJobTest.php diff --git a/assets/TenancyServiceProvider.stub.php b/assets/TenancyServiceProvider.stub.php index 1d15f418..865bb93d 100644 --- a/assets/TenancyServiceProvider.stub.php +++ b/assets/TenancyServiceProvider.stub.php @@ -40,7 +40,13 @@ class TenancyServiceProvider extends ServiceProvider Events\TenantSaved::class => [], Events\UpdatingTenant::class => [], Events\TenantUpdated::class => [], - Events\DeletingTenant::class => [], + Events\DeletingTenant::class => [ + JobPipeline::make([ + Jobs\DeleteDomains::class, + ])->send(function (Events\DeletingTenant $event) { + return $event->tenant; + })->shouldBeQueued(false), + ], Events\TenantDeleted::class => [ JobPipeline::make([ Jobs\DeleteDatabase::class, diff --git a/assets/migrations/2019_09_15_000020_create_domains_table.php b/assets/migrations/2019_09_15_000020_create_domains_table.php index 77c1b88a..17f706c2 100644 --- a/assets/migrations/2019_09_15_000020_create_domains_table.php +++ b/assets/migrations/2019_09_15_000020_create_domains_table.php @@ -21,7 +21,7 @@ class CreateDomainsTable extends Migration $table->string('tenant_id'); $table->timestamps(); - $table->foreign('tenant_id')->references('id')->on('tenants')->onUpdate('cascade')->onDelete('cascade'); + $table->foreign('tenant_id')->references('id')->on('tenants')->onUpdate('cascade'); }); } diff --git a/src/Jobs/DeleteDomains.php b/src/Jobs/DeleteDomains.php new file mode 100644 index 00000000..fac60e43 --- /dev/null +++ b/src/Jobs/DeleteDomains.php @@ -0,0 +1,35 @@ +tenant = $tenant; + } + + public function handle() + { + $this->tenant->domains->each->delete(); + } +} diff --git a/tests/DeleteDomainsJobTest.php b/tests/DeleteDomainsJobTest.php new file mode 100644 index 00000000..7fce9cf3 --- /dev/null +++ b/tests/DeleteDomainsJobTest.php @@ -0,0 +1,42 @@ + DatabaseAndDomainTenant::class]); + } + + /** @test */ + public function job_delete_domains_successfully() + { + $tenant = DatabaseAndDomainTenant::create(); + + $tenant->domains()->create([ + 'domain' => 'foo.localhost', + ]); + $tenant->domains()->create([ + 'domain' => 'bar.localhost', + ]); + + $this->assertSame($tenant->domains()->count(), 2); + + (new DeleteDomains($tenant))->handle(); + + $this->assertSame($tenant->refresh()->domains()->count(), 0); + } +} + +class DatabaseAndDomainTenant extends Etc\Tenant +{ + use HasDomains; +} From 97ab483173b38d9233082b349a9f73f268784449 Mon Sep 17 00:00:00 2001 From: Abrar Ahmad Date: Wed, 20 Jul 2022 18:28:45 +0500 Subject: [PATCH 44/45] Completing PR #881 (#902) * install PHP CS Fixer * Fix styling * remove StyleCI config * use config from archtechx/template * Fix styling * added `php-cs-fixer` * Update .php-cs-fixer.php * added GitHub token * Update ci.yml * Update ci.yml * Update ci.yml * php-cs-fixer workflow same as template Co-authored-by: Erik Gaal Co-authored-by: erikgaal --- .github/workflows/ci.yml | 18 +++ .gitignore | 1 + .php-cs-fixer.php | 141 ++++++++++++++++++ .styleci.yml | 7 - .../FilesystemTenancyBootstrapper.php | 2 +- .../QueueTenancyBootstrapper.php | 9 +- src/CacheManager.php | 3 +- src/Commands/Install.php | 2 - src/Commands/Migrate.php | 2 - src/Commands/MigrateFresh.php | 2 - src/Commands/Rollback.php | 2 - src/Commands/Run.php | 2 - src/Commands/Seed.php | 2 - src/Commands/TenantDump.php | 3 +- src/Commands/TenantList.php | 2 - src/Concerns/ExtendsLaravelCommand.php | 2 + src/Concerns/HasATenantsOption.php | 2 +- src/Contracts/TenantDatabaseManager.php | 10 -- src/Database/Concerns/TenantRun.php | 3 - src/Database/Models/ImpersonationToken.php | 5 + src/Database/Models/Tenant.php | 2 + src/DatabaseConfig.php | 5 +- src/Features/UserImpersonation.php | 1 - src/Listeners/UpdateSyncedResource.php | 3 +- .../CheckTenantForMaintenanceMode.php | 2 +- src/Middleware/InitializeTenancyByDomain.php | 6 +- .../InitializeTenancyByDomainOrSubdomain.php | 2 - src/Middleware/InitializeTenancyByPath.php | 4 +- .../InitializeTenancyByRequestData.php | 2 - .../InitializeTenancyBySubdomain.php | 2 - .../Contracts/CachedTenantResolver.php | 1 - src/Tenancy.php | 5 - src/TenancyServiceProvider.php | 4 - tests/BootstrapperTest.php | 26 ++-- tests/Etc/ExampleSeeder.php | 2 +- tests/EventListenerTest.php | 1 - tests/MaintenanceModeTest.php | 4 +- tests/QueueTest.php | 36 +++-- tests/ResourceSyncingTest.php | 4 + tests/SingleDatabaseTenancyTest.php | 3 + tests/TenantDatabaseManagerTest.php | 4 +- tests/TenantModelTest.php | 1 + tests/TenantUserImpersonationTest.php | 2 + 43 files changed, 231 insertions(+), 111 deletions(-) create mode 100644 .php-cs-fixer.php delete mode 100644 .styleci.yml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 81d37af5..3cbda814 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -2,6 +2,8 @@ name: CI env: COMPOSE_INTERACTIVE_NO_CLI: 1 + PHP_CS_FIXER_IGNORE_ENV: 1 + GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }} on: push: @@ -26,3 +28,19 @@ jobs: run: docker-compose exec -T test composer require --no-interaction "laravel/framework:${{ matrix.laravel }}" - name: Run tests run: ./test + + php-cs-fixer: + name: Code style (php-cs-fixer) + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v2 + - name: Install php-cs-fixer + run: composer global require friendsofphp/php-cs-fixer + - name: Run php-cs-fixer + run: $HOME/.composer/vendor/bin/php-cs-fixer fix --config=.php-cs-fixer.php + - name: Commit changes from php-cs-fixer + uses: EndBug/add-and-commit@v5 + with: + author_name: "PHP CS Fixer" + author_email: "phpcsfixer@example.com" + message: Fix code style (php-cs-fixer) diff --git a/.gitignore b/.gitignore index 95522c34..64d9dc21 100644 --- a/.gitignore +++ b/.gitignore @@ -11,3 +11,4 @@ clover.xml tenant-schema-test.dump tests/Etc/tmp/queuetest.json docker-compose.override.yml +.php-cs-fixer.cache diff --git a/.php-cs-fixer.php b/.php-cs-fixer.php new file mode 100644 index 00000000..589838bc --- /dev/null +++ b/.php-cs-fixer.php @@ -0,0 +1,141 @@ + ['syntax' => 'short'], + 'binary_operator_spaces' => [ + 'default' => 'single_space', + 'operators' => [ + '=>' => null, + '|' => 'no_space', + ] + ], + 'blank_line_after_namespace' => true, + 'blank_line_after_opening_tag' => true, + 'no_superfluous_phpdoc_tags' => true, + 'blank_line_before_statement' => [ + 'statements' => ['return'] + ], + 'braces' => true, + 'cast_spaces' => true, + 'class_definition' => true, + 'concat_space' => [ + 'spacing' => 'one' + ], + 'declare_equal_normalize' => true, + 'elseif' => true, + 'encoding' => true, + 'full_opening_tag' => true, + 'declare_strict_types' => true, + 'fully_qualified_strict_types' => true, // added by Shift + 'function_declaration' => true, + 'function_typehint_space' => true, + 'heredoc_to_nowdoc' => true, + 'include' => true, + 'increment_style' => ['style' => 'post'], + 'indentation_type' => true, + 'linebreak_after_opening_tag' => true, + 'line_ending' => true, + 'lowercase_cast' => true, + 'constant_case' => true, + 'lowercase_keywords' => true, + 'lowercase_static_reference' => true, // added from Symfony + 'magic_method_casing' => true, // added from Symfony + 'magic_constant_casing' => true, + 'method_argument_space' => true, + 'native_function_casing' => true, + 'no_alias_functions' => true, + 'no_extra_blank_lines' => [ + 'tokens' => [ + 'extra', + 'throw', + 'use', + 'use_trait', + ] + ], + 'no_blank_lines_after_class_opening' => true, + 'no_blank_lines_after_phpdoc' => true, + 'no_closing_tag' => true, + 'no_empty_phpdoc' => true, + 'no_empty_statement' => true, + 'no_leading_import_slash' => true, + 'no_leading_namespace_whitespace' => true, + 'no_mixed_echo_print' => [ + 'use' => 'echo' + ], + 'no_multiline_whitespace_around_double_arrow' => true, + 'multiline_whitespace_before_semicolons' => [ + 'strategy' => 'no_multi_line' + ], + 'no_short_bool_cast' => true, + 'no_singleline_whitespace_before_semicolons' => true, + 'no_spaces_after_function_name' => true, + 'no_spaces_around_offset' => true, + 'no_spaces_inside_parenthesis' => true, + 'no_trailing_comma_in_list_call' => true, + 'no_trailing_comma_in_singleline_array' => true, + 'no_trailing_whitespace' => true, + 'no_trailing_whitespace_in_comment' => true, + 'no_unneeded_control_parentheses' => true, + 'no_unreachable_default_argument_value' => true, + 'no_useless_return' => true, + 'no_whitespace_before_comma_in_array' => true, + 'no_whitespace_in_blank_line' => true, + 'normalize_index_brace' => true, + 'not_operator_with_successor_space' => true, + 'object_operator_without_whitespace' => true, + 'ordered_imports' => ['sort_algorithm' => 'alpha'], + 'phpdoc_indent' => true, + 'general_phpdoc_tag_rename' => true, + 'phpdoc_no_access' => true, + 'phpdoc_no_package' => true, + 'phpdoc_no_useless_inheritdoc' => true, + 'phpdoc_scalar' => true, + 'phpdoc_single_line_var_spacing' => true, + 'phpdoc_summary' => true, + 'phpdoc_to_comment' => false, + 'phpdoc_trim' => true, + 'phpdoc_types' => true, + 'phpdoc_var_without_name' => true, + 'psr_autoloading' => true, + 'self_accessor' => true, + 'short_scalar_cast' => true, + 'simplified_null_return' => false, // disabled by Shift + 'single_blank_line_at_eof' => true, + 'single_blank_line_before_namespace' => true, + 'single_class_element_per_statement' => true, + 'single_import_per_statement' => true, + 'single_line_after_imports' => true, + 'no_unused_imports' => true, + 'single_line_comment_style' => [ + 'comment_types' => ['hash'] + ], + 'single_quote' => true, + 'space_after_semicolon' => true, + 'standardize_not_equals' => true, + 'switch_case_semicolon_to_colon' => true, + 'switch_case_space' => true, + 'ternary_operator_spaces' => true, + 'trailing_comma_in_multiline' => true, + 'trim_array_spaces' => true, + 'unary_operator_spaces' => true, + 'whitespace_after_comma_in_array' => true, +]; + +$project_path = getcwd(); +$finder = Finder::create() + ->in([ + $project_path . '/src', + ]) + ->name('*.php') + ->notName('*.blade.php') + ->ignoreDotFiles(true) + ->ignoreVCS(true); + +return (new Config()) + ->setFinder($finder) + ->setRules($rules) + ->setRiskyAllowed(true) + ->setUsingCache(true); diff --git a/.styleci.yml b/.styleci.yml deleted file mode 100644 index e6d2b2c1..00000000 --- a/.styleci.yml +++ /dev/null @@ -1,7 +0,0 @@ -risky: true -preset: laravel -enabled: -- declare_strict_types -disabled: -- concat_without_spaces -- ternary_operator_spaces diff --git a/src/Bootstrappers/FilesystemTenancyBootstrapper.php b/src/Bootstrappers/FilesystemTenancyBootstrapper.php index 2b4f8dfe..6f720e7c 100644 --- a/src/Bootstrappers/FilesystemTenancyBootstrapper.php +++ b/src/Bootstrappers/FilesystemTenancyBootstrapper.php @@ -69,7 +69,7 @@ class FilesystemTenancyBootstrapper implements TenancyBootstrapper if (! $finalPrefix) { $finalPrefix = $originalRoot - ? rtrim($originalRoot, '/') . '/'. $suffix + ? rtrim($originalRoot, '/') . '/' . $suffix : $suffix; } diff --git a/src/Bootstrappers/QueueTenancyBootstrapper.php b/src/Bootstrappers/QueueTenancyBootstrapper.php index 790e1344..2f859ecd 100644 --- a/src/Bootstrappers/QueueTenancyBootstrapper.php +++ b/src/Bootstrappers/QueueTenancyBootstrapper.php @@ -4,18 +4,17 @@ declare(strict_types=1); namespace Stancl\Tenancy\Bootstrappers; -use Illuminate\Support\Str; use Illuminate\Config\Repository; -use Illuminate\Queue\QueueManager; -use Stancl\Tenancy\Contracts\Tenant; +use Illuminate\Contracts\Events\Dispatcher; +use Illuminate\Contracts\Foundation\Application; use Illuminate\Queue\Events\JobFailed; use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; -use Illuminate\Contracts\Events\Dispatcher; use Illuminate\Queue\Events\JobRetryRequested; +use Illuminate\Queue\QueueManager; use Illuminate\Support\Testing\Fakes\QueueFake; -use Illuminate\Contracts\Foundation\Application; use Stancl\Tenancy\Contracts\TenancyBootstrapper; +use Stancl\Tenancy\Contracts\Tenant; class QueueTenancyBootstrapper implements TenancyBootstrapper { diff --git a/src/CacheManager.php b/src/CacheManager.php index 88428353..09581201 100644 --- a/src/CacheManager.php +++ b/src/CacheManager.php @@ -13,7 +13,6 @@ class CacheManager extends BaseCacheManager * * @param string $method * @param array $parameters - * @return mixed */ public function __call($method, $parameters) { @@ -21,7 +20,7 @@ class CacheManager extends BaseCacheManager if ($method === 'tags') { $count = count($parameters); - + if ($count !== 1) { throw new \Exception("Method tags() takes exactly 1 argument. $count passed."); } diff --git a/src/Commands/Install.php b/src/Commands/Install.php index dd2dd280..41492b26 100644 --- a/src/Commands/Install.php +++ b/src/Commands/Install.php @@ -24,8 +24,6 @@ class Install extends Command /** * Execute the console command. - * - * @return mixed */ public function handle() { diff --git a/src/Commands/Migrate.php b/src/Commands/Migrate.php index c67d3598..52ecd47f 100644 --- a/src/Commands/Migrate.php +++ b/src/Commands/Migrate.php @@ -33,8 +33,6 @@ class Migrate extends MigrateCommand /** * Execute the console command. - * - * @return mixed */ public function handle() { diff --git a/src/Commands/MigrateFresh.php b/src/Commands/MigrateFresh.php index 283d70b0..63860153 100644 --- a/src/Commands/MigrateFresh.php +++ b/src/Commands/MigrateFresh.php @@ -31,8 +31,6 @@ final class MigrateFresh extends Command /** * Execute the console command. - * - * @return mixed */ public function handle() { diff --git a/src/Commands/Rollback.php b/src/Commands/Rollback.php index e60d974b..1c434189 100644 --- a/src/Commands/Rollback.php +++ b/src/Commands/Rollback.php @@ -42,8 +42,6 @@ class Rollback extends RollbackCommand /** * Execute the console command. - * - * @return mixed */ public function handle() { diff --git a/src/Commands/Run.php b/src/Commands/Run.php index aa518d7a..2b20d9c3 100644 --- a/src/Commands/Run.php +++ b/src/Commands/Run.php @@ -27,8 +27,6 @@ class Run extends Command /** * Execute the console command. - * - * @return mixed */ public function handle() { diff --git a/src/Commands/Seed.php b/src/Commands/Seed.php index dc97ae71..8c525208 100644 --- a/src/Commands/Seed.php +++ b/src/Commands/Seed.php @@ -35,8 +35,6 @@ class Seed extends SeedCommand /** * Execute the console command. - * - * @return mixed */ public function handle() { diff --git a/src/Commands/TenantDump.php b/src/Commands/TenantDump.php index 557c6975..9c8698c6 100644 --- a/src/Commands/TenantDump.php +++ b/src/Commands/TenantDump.php @@ -21,10 +21,9 @@ class TenantDump extends DumpCommand $this->specifyParameters(); } - public function handle(ConnectionResolverInterface $connections, Dispatcher $dispatcher): int { - $this->tenant()->run(fn() => parent::handle($connections, $dispatcher)); + $this->tenant()->run(fn () => parent::handle($connections, $dispatcher)); return Command::SUCCESS; } diff --git a/src/Commands/TenantList.php b/src/Commands/TenantList.php index d01afcb9..13775676 100644 --- a/src/Commands/TenantList.php +++ b/src/Commands/TenantList.php @@ -25,8 +25,6 @@ class TenantList extends Command /** * Execute the console command. - * - * @return mixed */ public function handle() { diff --git a/src/Concerns/ExtendsLaravelCommand.php b/src/Concerns/ExtendsLaravelCommand.php index bdafc8f7..d08ad6b6 100644 --- a/src/Concerns/ExtendsLaravelCommand.php +++ b/src/Concerns/ExtendsLaravelCommand.php @@ -1,5 +1,7 @@ manager()->makeConnectionConfig( - array_merge($templateConnection, $this->tenantConfig()), $this->getName() + array_merge($templateConnection, $this->tenantConfig()), + $this->getName() ); } diff --git a/src/Features/UserImpersonation.php b/src/Features/UserImpersonation.php index 48d65bb9..f96465ff 100644 --- a/src/Features/UserImpersonation.php +++ b/src/Features/UserImpersonation.php @@ -33,7 +33,6 @@ class UserImpersonation implements Feature * * @param string|ImpersonationToken $token * @param int $ttl - * @return RedirectResponse */ public static function makeResponse($token, int $ttl = null): RedirectResponse { diff --git a/src/Listeners/UpdateSyncedResource.php b/src/Listeners/UpdateSyncedResource.php index 40d4d644..9be290f0 100644 --- a/src/Listeners/UpdateSyncedResource.php +++ b/src/Listeners/UpdateSyncedResource.php @@ -48,8 +48,7 @@ class UpdateSyncedResource extends QueueableListener protected function updateResourceInCentralDatabaseAndGetTenants($event, $syncedAttributes) { /** @var Model|SyncMaster $centralModel */ - $centralModel = $event->model->getCentralModelName() - ::where($event->model->getGlobalIdentifierKeyName(), $event->model->getGlobalIdentifierKey()) + $centralModel = $event->model->getCentralModelName()::where($event->model->getGlobalIdentifierKeyName(), $event->model->getGlobalIdentifierKey()) ->first(); // We disable events for this call, to avoid triggering this event & listener again. diff --git a/src/Middleware/CheckTenantForMaintenanceMode.php b/src/Middleware/CheckTenantForMaintenanceMode.php index 8e29a31e..c1c734f5 100644 --- a/src/Middleware/CheckTenantForMaintenanceMode.php +++ b/src/Middleware/CheckTenantForMaintenanceMode.php @@ -5,10 +5,10 @@ declare(strict_types=1); namespace Stancl\Tenancy\Middleware; use Closure; -use Symfony\Component\HttpKernel\Exception\HttpException; use Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode; use Stancl\Tenancy\Exceptions\TenancyNotInitializedException; use Symfony\Component\HttpFoundation\IpUtils; +use Symfony\Component\HttpKernel\Exception\HttpException; class CheckTenantForMaintenanceMode extends CheckForMaintenanceMode { diff --git a/src/Middleware/InitializeTenancyByDomain.php b/src/Middleware/InitializeTenancyByDomain.php index 24a1abb7..5a07112d 100644 --- a/src/Middleware/InitializeTenancyByDomain.php +++ b/src/Middleware/InitializeTenancyByDomain.php @@ -29,13 +29,13 @@ class InitializeTenancyByDomain extends IdentificationMiddleware * Handle an incoming request. * * @param \Illuminate\Http\Request $request - * @param \Closure $next - * @return mixed */ public function handle($request, Closure $next) { return $this->initializeTenancy( - $request, $next, $request->getHost() + $request, + $next, + $request->getHost() ); } } diff --git a/src/Middleware/InitializeTenancyByDomainOrSubdomain.php b/src/Middleware/InitializeTenancyByDomainOrSubdomain.php index 94217bba..9b153db3 100644 --- a/src/Middleware/InitializeTenancyByDomainOrSubdomain.php +++ b/src/Middleware/InitializeTenancyByDomainOrSubdomain.php @@ -13,8 +13,6 @@ class InitializeTenancyByDomainOrSubdomain * Handle an incoming request. * * @param \Illuminate\Http\Request $request - * @param \Closure $next - * @return mixed */ public function handle($request, Closure $next) { diff --git a/src/Middleware/InitializeTenancyByPath.php b/src/Middleware/InitializeTenancyByPath.php index 6289199b..e66400c5 100644 --- a/src/Middleware/InitializeTenancyByPath.php +++ b/src/Middleware/InitializeTenancyByPath.php @@ -38,7 +38,9 @@ class InitializeTenancyByPath extends IdentificationMiddleware // simply injected into some route controller action. if ($route->parameterNames()[0] === PathTenantResolver::$tenantParameterName) { return $this->initializeTenancy( - $request, $next, $route + $request, + $next, + $route ); } else { throw new RouteIsMissingTenantParameterException; diff --git a/src/Middleware/InitializeTenancyByRequestData.php b/src/Middleware/InitializeTenancyByRequestData.php index de75d8c5..4e1d33ff 100644 --- a/src/Middleware/InitializeTenancyByRequestData.php +++ b/src/Middleware/InitializeTenancyByRequestData.php @@ -36,8 +36,6 @@ class InitializeTenancyByRequestData extends IdentificationMiddleware * Handle an incoming request. * * @param \Illuminate\Http\Request $request - * @param \Closure $next - * @return mixed */ public function handle($request, Closure $next) { diff --git a/src/Middleware/InitializeTenancyBySubdomain.php b/src/Middleware/InitializeTenancyBySubdomain.php index 55d76b05..76389df7 100644 --- a/src/Middleware/InitializeTenancyBySubdomain.php +++ b/src/Middleware/InitializeTenancyBySubdomain.php @@ -28,8 +28,6 @@ class InitializeTenancyBySubdomain extends InitializeTenancyByDomain * Handle an incoming request. * * @param \Illuminate\Http\Request $request - * @param \Closure $next - * @return mixed */ public function handle($request, Closure $next) { diff --git a/src/Resolvers/Contracts/CachedTenantResolver.php b/src/Resolvers/Contracts/CachedTenantResolver.php index 968ac794..e84f1fb1 100644 --- a/src/Resolvers/Contracts/CachedTenantResolver.php +++ b/src/Resolvers/Contracts/CachedTenantResolver.php @@ -75,7 +75,6 @@ abstract class CachedTenantResolver implements TenantResolver /** * Get all the arg combinations for resolve() that can be used to find this tenant. * - * @param Tenant $tenant * @return array[] */ abstract public function getArgsForTenant(Tenant $tenant): array; diff --git a/src/Tenancy.php b/src/Tenancy.php index 30f138e3..6873f93b 100644 --- a/src/Tenancy.php +++ b/src/Tenancy.php @@ -27,7 +27,6 @@ class Tenancy /** * Initializes the tenant. * @param Tenant|int|string $tenant - * @return void */ public function initialize($tenant): void { @@ -106,9 +105,6 @@ class Tenancy /** * Run a callback in the central context. * Atomic, safely reverts to previous context. - * - * @param callable $callback - * @return mixed */ public function central(callable $callback) { @@ -132,7 +128,6 @@ class Tenancy * More performant than running $tenant->run() one by one. * * @param Tenant[]|\Traversable|string[]|null $tenants - * @param callable $callback * @return void */ public function runForMultiple($tenants, callable $callback) diff --git a/src/TenancyServiceProvider.php b/src/TenancyServiceProvider.php index dd061af3..e23200a6 100644 --- a/src/TenancyServiceProvider.php +++ b/src/TenancyServiceProvider.php @@ -15,8 +15,6 @@ class TenancyServiceProvider extends ServiceProvider { /** * Register services. - * - * @return void */ public function register(): void { @@ -76,8 +74,6 @@ class TenancyServiceProvider extends ServiceProvider /** * Bootstrap services. - * - * @return void */ public function boot(): void { diff --git a/tests/BootstrapperTest.php b/tests/BootstrapperTest.php index 588fadd8..a0320282 100644 --- a/tests/BootstrapperTest.php +++ b/tests/BootstrapperTest.php @@ -5,26 +5,26 @@ declare(strict_types=1); namespace Stancl\Tenancy\Tests; use Illuminate\Filesystem\FilesystemAdapter; -use ReflectionObject; -use ReflectionProperty; -use Illuminate\Support\Str; -use Illuminate\Support\Facades\DB; -use Stancl\JobPipeline\JobPipeline; -use Stancl\Tenancy\Tests\Etc\Tenant; use Illuminate\Support\Facades\Cache; +use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Redis; use Illuminate\Support\Facades\Storage; -use Stancl\Tenancy\Events\TenancyEnded; -use Stancl\Tenancy\Jobs\CreateDatabase; -use Stancl\Tenancy\Events\TenantCreated; -use Stancl\Tenancy\Events\TenancyInitialized; -use Stancl\Tenancy\Listeners\BootstrapTenancy; -use Stancl\Tenancy\Listeners\RevertToCentralContext; +use Illuminate\Support\Str; +use ReflectionObject; +use ReflectionProperty; +use Stancl\JobPipeline\JobPipeline; use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper; -use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; +use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper; +use Stancl\Tenancy\Events\TenancyEnded; +use Stancl\Tenancy\Events\TenancyInitialized; +use Stancl\Tenancy\Events\TenantCreated; +use Stancl\Tenancy\Jobs\CreateDatabase; +use Stancl\Tenancy\Listeners\BootstrapTenancy; +use Stancl\Tenancy\Listeners\RevertToCentralContext; +use Stancl\Tenancy\Tests\Etc\Tenant; class BootstrapperTest extends TestCase { diff --git a/tests/Etc/ExampleSeeder.php b/tests/Etc/ExampleSeeder.php index a3e36123..2f97787e 100644 --- a/tests/Etc/ExampleSeeder.php +++ b/tests/Etc/ExampleSeeder.php @@ -19,7 +19,7 @@ class ExampleSeeder extends Seeder { DB::table('users')->insert([ 'name' => Str::random(10), - 'email' => Str::random(10).'@gmail.com', + 'email' => Str::random(10) . '@gmail.com', 'password' => bcrypt('password'), ]); } diff --git a/tests/EventListenerTest.php b/tests/EventListenerTest.php index 4a45205c..02ed8b3b 100644 --- a/tests/EventListenerTest.php +++ b/tests/EventListenerTest.php @@ -20,7 +20,6 @@ use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Jobs\MigrateDatabase; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\QueueableListener; -use Stancl\Tenancy\Tenancy; use Stancl\Tenancy\Tests\Etc\Tenant; class EventListenerTest extends TestCase diff --git a/tests/MaintenanceModeTest.php b/tests/MaintenanceModeTest.php index 4a8d8d0c..90232932 100644 --- a/tests/MaintenanceModeTest.php +++ b/tests/MaintenanceModeTest.php @@ -4,14 +4,12 @@ declare(strict_types=1); namespace Stancl\Tenancy\Tests; -use Symfony\Component\HttpKernel\Exception\HttpException; -use Illuminate\Foundation\Http\Exceptions\MaintenanceModeException; use Illuminate\Support\Facades\Route; use Stancl\Tenancy\Database\Concerns\MaintenanceMode; use Stancl\Tenancy\Middleware\CheckTenantForMaintenanceMode; use Stancl\Tenancy\Middleware\InitializeTenancyByDomain; use Stancl\Tenancy\Tests\Etc\Tenant; -use Symfony\Component\HttpKernel\Exception\ServiceUnavailableHttpException; +use Symfony\Component\HttpKernel\Exception\HttpException; class MaintenanceModeTest extends TestCase { diff --git a/tests/QueueTest.php b/tests/QueueTest.php index a3df9cd7..fe34ba92 100644 --- a/tests/QueueTest.php +++ b/tests/QueueTest.php @@ -4,33 +4,31 @@ declare(strict_types=1); namespace Stancl\Tenancy\Tests; -use Closure; use Exception; -use Illuminate\Support\Str; use Illuminate\Bus\Queueable; -use Spatie\Valuestore\Valuestore; -use Illuminate\Support\Facades\DB; -use Stancl\Tenancy\Tests\Etc\User; -use Stancl\JobPipeline\JobPipeline; -use Stancl\Tenancy\Tests\Etc\Tenant; -use Illuminate\Support\Facades\Event; -use Illuminate\Queue\SerializesModels; -use Illuminate\Support\Facades\Schema; -use Stancl\Tenancy\Events\TenancyEnded; -use Stancl\Tenancy\Jobs\CreateDatabase; -use Illuminate\Queue\InteractsWithQueue; -use Stancl\Tenancy\Events\TenantCreated; +use Illuminate\Contracts\Queue\ShouldQueue; use Illuminate\Database\Schema\Blueprint; +use Illuminate\Foundation\Bus\Dispatchable; use Illuminate\Queue\Events\JobProcessed; use Illuminate\Queue\Events\JobProcessing; -use Illuminate\Contracts\Queue\ShouldQueue; -use Illuminate\Foundation\Bus\Dispatchable; -use PDO; +use Illuminate\Queue\InteractsWithQueue; +use Illuminate\Queue\SerializesModels; +use Illuminate\Support\Facades\DB; +use Illuminate\Support\Facades\Event; +use Illuminate\Support\Facades\Schema; +use Illuminate\Support\Str; +use Spatie\Valuestore\Valuestore; +use Stancl\JobPipeline\JobPipeline; +use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; +use Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper; +use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyInitialized; +use Stancl\Tenancy\Events\TenantCreated; +use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\RevertToCentralContext; -use Stancl\Tenancy\Bootstrappers\QueueTenancyBootstrapper; -use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; +use Stancl\Tenancy\Tests\Etc\Tenant; +use Stancl\Tenancy\Tests\Etc\User; class QueueTest extends TestCase { diff --git a/tests/ResourceSyncingTest.php b/tests/ResourceSyncingTest.php index 570448d1..0ff95a52 100644 --- a/tests/ResourceSyncingTest.php +++ b/tests/ResourceSyncingTest.php @@ -589,7 +589,9 @@ class CentralUser extends Model implements SyncMaster use ResourceSyncing, CentralConnection; protected $guarded = []; + public $timestamps = false; + public $table = 'users'; public function tenants(): BelongsToMany @@ -633,7 +635,9 @@ class ResourceUser extends Model implements Syncable use ResourceSyncing; protected $table = 'users'; + protected $guarded = []; + public $timestamps = false; public function getGlobalIdentifierKey() diff --git a/tests/SingleDatabaseTenancyTest.php b/tests/SingleDatabaseTenancyTest.php index b64478cc..d0425dd9 100644 --- a/tests/SingleDatabaseTenancyTest.php +++ b/tests/SingleDatabaseTenancyTest.php @@ -329,6 +329,7 @@ class Post extends Model use BelongsToTenant; protected $guarded = []; + public $timestamps = false; public function comments() @@ -345,6 +346,7 @@ class Post extends Model class Comment extends Model { protected $guarded = []; + public $timestamps = false; public function post() @@ -368,5 +370,6 @@ class ScopedComment extends Comment class GlobalResource extends Model { protected $guarded = []; + public $timestamps = false; } diff --git a/tests/TenantDatabaseManagerTest.php b/tests/TenantDatabaseManagerTest.php index 0e1464c0..12273c85 100644 --- a/tests/TenantDatabaseManagerTest.php +++ b/tests/TenantDatabaseManagerTest.php @@ -104,7 +104,7 @@ class TenantDatabaseManagerTest extends TestCase ['sqlite', SQLiteDatabaseManager::class], ['pgsql', PostgreSQLDatabaseManager::class], ['pgsql', PostgreSQLSchemaManager::class], - ['sqlsrv', MicrosoftSQLDatabaseManager::class] + ['sqlsrv', MicrosoftSQLDatabaseManager::class], ]; } @@ -196,7 +196,7 @@ class TenantDatabaseManagerTest extends TestCase ]); tenancy()->initialize($tenant); - $schemaConfig = version_compare(app()->version(), '9.0', '>=') ? + $schemaConfig = version_compare(app()->version(), '9.0', '>=') ? config('database.connections.' . config('database.default') . '.search_path') : config('database.connections.' . config('database.default') . '.schema'); diff --git a/tests/TenantModelTest.php b/tests/TenantModelTest.php index 46dc6a00..2d46c233 100644 --- a/tests/TenantModelTest.php +++ b/tests/TenantModelTest.php @@ -172,6 +172,7 @@ class MyTenant extends Tenant class AnotherTenant extends Model implements Contracts\Tenant { protected $guarded = []; + protected $table = 'tenants'; public function getTenantKeyName(): string diff --git a/tests/TenantUserImpersonationTest.php b/tests/TenantUserImpersonationTest.php index c5e83853..b50db84b 100644 --- a/tests/TenantUserImpersonationTest.php +++ b/tests/TenantUserImpersonationTest.php @@ -274,11 +274,13 @@ class TenantUserImpersonationTest extends TestCase class ImpersonationUser extends Authenticable { protected $guarded = []; + protected $table = 'users'; } class AnotherImpersonationUser extends Authenticable { protected $guarded = []; + protected $table = 'users'; } From c0f97fa04ea2c7fb2ab6e353842d5e30dcd545e2 Mon Sep 17 00:00:00 2001 From: PHP CS Fixer Date: Wed, 20 Jul 2022 13:29:11 +0000 Subject: [PATCH 45/45] Fix code style (php-cs-fixer) --- src/Jobs/DeleteDomains.php | 6 ------ 1 file changed, 6 deletions(-) diff --git a/src/Jobs/DeleteDomains.php b/src/Jobs/DeleteDomains.php index fac60e43..4ea92b7f 100644 --- a/src/Jobs/DeleteDomains.php +++ b/src/Jobs/DeleteDomains.php @@ -5,16 +5,10 @@ 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 Stancl\Tenancy\Contracts\TenantWithDatabase; -use Stancl\Tenancy\Database\Models\Domain; -use Stancl\Tenancy\Events\DatabaseDeleted; -use Stancl\Tenancy\Events\DeletingDatabase; -use Stancl\Tenancy\Events\DeletingDomain; -use Stancl\Tenancy\Events\DomainDeleted; class DeleteDomains {