diff --git a/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php b/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php index 9a206872..6ccf51be 100644 --- a/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php +++ b/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php @@ -9,30 +9,40 @@ use Stancl\Tenancy\Events\TenantDeleted; use Stancl\Tenancy\Listeners\QueueableListener; /** - * Cleans up pivot records related to the deleted tenant. - * + * Clean up pivot records related to the deleted tenant. * The listener only cleans up the pivot tables specified * in the $pivotTables property (see the property for details), - * and is intended for use with tables that do not have tenant - * foreign key constraints with onDelete('cascade'). + * and is intended for use with tables that do not have tenant foreign key constraints. + * + * When using foreign key constraints, you'll still have to use ->onDelete('cascade') + * on the constraint (otherwise, deleting a tenant will throw a foreign key constraint violation). + * That way, the cleanup will happen on the database level, and this listener will essentially + * just perform an extra 'where' query. */ class DeleteAllTenantMappings extends QueueableListener { /** - * Pivot tables to clean up after a tenant is deleted, in the - * ['table_name' => 'tenant_key_column'] format. + * Pivot tables to clean up after a tenant is deleted, + * formatted like ['table_name' => 'tenant_key_column']. * * Since we cannot automatically detect which pivot tables - * are being used, they have to be specified here manually. + * you want to clean up, they have to be specified here. * - * The default value follows the polymorphic table used by default. + * By default, resource syncing uses the tenant_resources table, and the records are associated + * to tenants by the tenant_id column (thus the ['tenant_resources' => 'tenant_id'] default). + * + * To customize this, set this property, e.g. in TenancyServiceProvider: + * DeleteAllTenantMappings::$pivotTables = [ + * 'tenant_users' => 'tenant_id', + * // You can also add more pivot tables here + * ]; */ public static array $pivotTables = ['tenant_resources' => 'tenant_id']; public function handle(TenantDeleted $event): void { foreach (static::$pivotTables as $table => $tenantKeyColumn) { - DB::table($table)->where($tenantKeyColumn, $event->tenant->getKey())->delete(); + DB::table($table)->where($tenantKeyColumn, $event->tenant->getTenantKey())->delete(); } } } diff --git a/tests/ResourceSyncingTest.php b/tests/ResourceSyncingTest.php index ebd6ccfd..3a34259d 100644 --- a/tests/ResourceSyncingTest.php +++ b/tests/ResourceSyncingTest.php @@ -898,7 +898,7 @@ test('deleting SyncMaster automatically deletes its Syncables', function (bool $ 'basic pivot' => false, ]); -test('tenant pivot records are deleted along with the tenants to which they belong', function (bool $dbLevelOnCascadeDelete, bool $morphPivot) { +test('tenant pivot records are deleted along with the tenants to which they belong to', function (bool $dbLevelOnCascadeDelete, bool $morphPivot) { [$tenant] = createTenantsAndRunMigrations(); if ($morphPivot) {