1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2025-12-13 00:14:04 +00:00

Simplify DeleteAllTenantMappings (code and docblocks)

This commit is contained in:
lukinovec 2025-11-05 09:57:17 +01:00
parent cb91083024
commit 9c16b7d53e
2 changed files with 16 additions and 36 deletions

View file

@ -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();
}

View file

@ -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();