From 4f3c97385810d5e92426089df13e0aa054cc2e08 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Fri, 14 Nov 2025 12:10:17 +0100 Subject: [PATCH 1/2] Throw exception on nonexistent table in DeleteAllTenantMappings --- src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php | 6 +----- tests/ResourceSyncingTest.php | 4 ++-- 2 files changed, 3 insertions(+), 7 deletions(-) diff --git a/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php b/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php index 3a572dd9..24943177 100644 --- a/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php +++ b/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php @@ -37,17 +37,13 @@ class DeleteAllTenantMappings extends QueueableListener * 'tenant_users' => 'tenant_id', * // You can also add more pivot tables here * ]; - * - * Non-existent tables specified in the property will be skipped. */ public static array $pivotTables = ['tenant_resources' => 'tenant_id']; public function handle(TenantDeleted $event): void { foreach (static::$pivotTables as $table => $tenantKeyColumn) { - if (Schema::hasTable($table)) { - DB::table($table)->where($tenantKeyColumn, $event->tenant->getTenantKey())->delete(); - } + DB::table($table)->where($tenantKeyColumn, $event->tenant->getTenantKey())->delete(); } } } diff --git a/tests/ResourceSyncingTest.php b/tests/ResourceSyncingTest.php index ac6b18fd..a4736ce7 100644 --- a/tests/ResourceSyncingTest.php +++ b/tests/ResourceSyncingTest.php @@ -978,10 +978,10 @@ test('DeleteAllTenantMappings handles incorrect configuration correctly', functi // Should throw an exception when tenant is deleted expect(fn() => $tenant1->delete())->toThrow(QueryException::class, "Unknown column 'non_existent_column' in 'where clause'"); - // Non-existent table, the listener skips it, no exception to throw + // Non-existent table DeleteAllTenantMappings::$pivotTables = ['nonexistent_pivot' => 'non_existent_column']; - expect(fn() => $tenant2->delete())->not()->toThrow(Exception::class); + expect(fn() => $tenant2->delete())->toThrow(QueryException::class, "Table 'main.nonexistent_pivot' doesn't exist"); }); test('trashed resources are synced correctly', function () { From cf01ce92bd5cfb39f3c5fe849fc0371dc8737659 Mon Sep 17 00:00:00 2001 From: "github-actions[bot]" Date: Fri, 14 Nov 2025 11:10:34 +0000 Subject: [PATCH 2/2] Fix code style (php-cs-fixer) --- src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php | 1 - 1 file changed, 1 deletion(-) diff --git a/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php b/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php index 24943177..6ccf51be 100644 --- a/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php +++ b/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php @@ -5,7 +5,6 @@ declare(strict_types=1); namespace Stancl\Tenancy\ResourceSyncing\Listeners; use Illuminate\Support\Facades\DB; -use Illuminate\Support\Facades\Schema; use Stancl\Tenancy\Events\TenantDeleted; use Stancl\Tenancy\Listeners\QueueableListener;