From 9c16b7d53e5be601decfa89e3855181fefe25479 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Wed, 5 Nov 2025 09:57:17 +0100 Subject: [PATCH] Simplify DeleteAllTenantMappings (code and docblocks) --- .../Listeners/DeleteAllTenantMappings.php | 50 ++++++------------- tests/ResourceSyncingTest.php | 2 +- 2 files changed, 16 insertions(+), 36 deletions(-) diff --git a/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php b/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php index a6c3c932..d9d18ad6 100644 --- a/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php +++ b/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php @@ -10,56 +10,36 @@ use Stancl\Tenancy\Events\TenantDeleted; use Stancl\Tenancy\Listeners\QueueableListener; /** - * When a tenant is deleted, clean up pivot records related to that tenant. - * Only use this listener for cleaning up tables without tenant foreign key constraints. + * When a tenant is deleted, clean up pivot records related to that tenant + * in the pivot tables specified in the $pivotTables property (see the property for details). * - * If you're using foreign key constraints on the tenant key columns in your pivot tables, - * you still must include ->onDelete('cascade') in the constraint definition - * for the pivot records to be deleted automatically. Without ->onDelete('cascade'), + * Only use this listener for cleaning up tables without tenant foreign key constraints. + * When using foreign key constraints, you'll have to use ->onDelete('cascade') + * on the constraint definition for the cleanup instead of utilizing this listener because * the constraint will prevent tenant deletion before this listener can clean up the pivot records, * causing an integrity constraint violation. - * - * With ->onDelete('cascade'), the database will handle the cleanup automatically, - * so there's no need to use this listener (it won't break anything, but it's redundant). - * - * By default, this listener only cleans up the 'tenant_resources' polymorphic pivot table, - * and the records to delete are found by the 'tenant_id' column. - * - * To customize which pivot tables to clean up (or which column has the tenant key), - * set DeleteAllTenantMappings::$pivotTables to an array of table names as the keys, - * and their values should be tenant key column names (e.g. 'tenant_id'). - * - * For example (e.g. in TenancyServiceProvider): - * DeleteAllTenantMappings::$pivotTables = [ - * 'tenant_users' => 'tenant_id', - * ]; - * - * Tables that do not exist will be skipped. */ class DeleteAllTenantMappings extends QueueableListener { /** * Pivot tables to clean up after a tenant is deleted, * formatted like ['table_name' => 'tenant_key_column']. - * E.g. ['tenant_users' => 'tenant_id']. * - * If empty, the listener defaults to cleaning only - * the default pivot ('tenant_resources' with 'tenant_id' as the tenant key column). + * By default, the listener only cleans up the default pivot + * ('tenant_resources' with 'tenant_id' as the tenant key column). * - * Set this property, e.g. in your TenancyServiceProvider, - * for this listener to clean up specific pivot tables. + * To customize this, set this property, e.g. in TenancyServiceProvider: + * DeleteAllTenantMappings::$pivotTables = [ + * 'tenant_users' => 'tenant_id', + * ]; + * + * Non-existent tables specified in the property will be skipped. */ - public static array $pivotTables = []; + public static array $pivotTables = ['tenant_resources' => 'tenant_id']; public function handle(TenantDeleted $event): void { - $pivotTables = static::$pivotTables; - - if (! $pivotTables) { - $pivotTables = ['tenant_resources' => 'tenant_id']; - } - - foreach ($pivotTables as $table => $tenantKeyColumn) { + foreach (static::$pivotTables as $table => $tenantKeyColumn) { if (Schema::hasTable($table)) { DB::table($table)->where($tenantKeyColumn, $event->tenant->getTenantKey())->delete(); } diff --git a/tests/ResourceSyncingTest.php b/tests/ResourceSyncingTest.php index 6b9b595d..ac9e9710 100644 --- a/tests/ResourceSyncingTest.php +++ b/tests/ResourceSyncingTest.php @@ -75,7 +75,7 @@ beforeEach(function () { CreateTenantResource::$shouldQueue = false; DeleteResourceInTenant::$shouldQueue = false; UpdateOrCreateSyncedResource::$scopeGetModelQuery = null; - DeleteAllTenantMappings::$pivotTables = []; + DeleteAllTenantMappings::$pivotTables = ['tenant_resources' => 'tenant_id']; // Reset global scopes on models (should happen automatically but to make this more explicit) Model::clearBootedModels();