diff --git a/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php b/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php index 6ccf51be..9a206872 100644 --- a/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php +++ b/src/ResourceSyncing/Listeners/DeleteAllTenantMappings.php @@ -9,40 +9,30 @@ use Stancl\Tenancy\Events\TenantDeleted; use Stancl\Tenancy\Listeners\QueueableListener; /** - * Clean up pivot records related to the deleted tenant. + * Cleans 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. - * - * 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. + * and is intended for use with tables that do not have tenant + * foreign key constraints with onDelete('cascade'). */ class DeleteAllTenantMappings extends QueueableListener { /** - * Pivot tables to clean up after a tenant is deleted, - * formatted like ['table_name' => 'tenant_key_column']. + * Pivot tables to clean up after a tenant is deleted, in the + * ['table_name' => 'tenant_key_column'] format. * * Since we cannot automatically detect which pivot tables - * you want to clean up, they have to be specified here. + * are being used, they have to be specified here manually. * - * 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 - * ]; + * The default value follows the polymorphic table used by default. */ 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->getTenantKey())->delete(); + DB::table($table)->where($tenantKeyColumn, $event->tenant->getKey())->delete(); } } } diff --git a/tests/ResourceSyncingTest.php b/tests/ResourceSyncingTest.php index 3a34259d..ebd6ccfd 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 to', function (bool $dbLevelOnCascadeDelete, bool $morphPivot) { +test('tenant pivot records are deleted along with the tenants to which they belong', function (bool $dbLevelOnCascadeDelete, bool $morphPivot) { [$tenant] = createTenantsAndRunMigrations(); if ($morphPivot) {