mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 11:44:03 +00:00
Use DeleteAllTenantMappings on TenantDeleted only in tests where it matters
This commit is contained in:
parent
e78fb46fbe
commit
8deab4041f
1 changed files with 9 additions and 5 deletions
|
|
@ -104,7 +104,6 @@ beforeEach(function () {
|
||||||
Event::listen(SyncMasterRestored::class, RestoreResourcesInTenants::class);
|
Event::listen(SyncMasterRestored::class, RestoreResourcesInTenants::class);
|
||||||
Event::listen(CentralResourceAttachedToTenant::class, CreateTenantResource::class);
|
Event::listen(CentralResourceAttachedToTenant::class, CreateTenantResource::class);
|
||||||
Event::listen(CentralResourceDetachedFromTenant::class, DeleteResourceInTenant::class);
|
Event::listen(CentralResourceDetachedFromTenant::class, DeleteResourceInTenant::class);
|
||||||
Event::listen(TenantDeleted::class, DeleteAllTenantMappings::class);
|
|
||||||
|
|
||||||
// Run migrations on central connection
|
// Run migrations on central connection
|
||||||
pest()->artisan('migrate', [
|
pest()->artisan('migrate', [
|
||||||
|
|
@ -913,12 +912,15 @@ test('tenant pivot records are deleted along with the tenants to which they belo
|
||||||
|
|
||||||
// Custom pivot table
|
// Custom pivot table
|
||||||
$pivotTable = 'tenant_users';
|
$pivotTable = 'tenant_users';
|
||||||
|
|
||||||
DeleteAllTenantMappings::$pivotTables = [$pivotTable => 'tenant_id'];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
if ($dbLevelOnCascadeDelete) {
|
if ($dbLevelOnCascadeDelete) {
|
||||||
addTenantIdConstraintToPivot($pivotTable);
|
addTenantIdConstraintToPivot($pivotTable);
|
||||||
|
} else {
|
||||||
|
// Event-based cleanup
|
||||||
|
Event::listen(TenantDeleted::class, DeleteAllTenantMappings::class);
|
||||||
|
|
||||||
|
DeleteAllTenantMappings::$pivotTables = [$pivotTable => 'tenant_id'];
|
||||||
}
|
}
|
||||||
|
|
||||||
$syncMaster = $centralUserModel::create([
|
$syncMaster = $centralUserModel::create([
|
||||||
|
|
@ -931,9 +933,9 @@ test('tenant pivot records are deleted along with the tenants to which they belo
|
||||||
|
|
||||||
$syncMaster->tenants()->attach($tenant);
|
$syncMaster->tenants()->attach($tenant);
|
||||||
|
|
||||||
|
// Pivot records should be deleted along with the tenant
|
||||||
$tenant->delete();
|
$tenant->delete();
|
||||||
|
|
||||||
// Deleting tenant deletes its pivot records
|
|
||||||
expect(DB::select("SELECT * FROM {$pivotTable} WHERE tenant_id = ?", [$tenant->getTenantKey()]))->toHaveCount(0);
|
expect(DB::select("SELECT * FROM {$pivotTable} WHERE tenant_id = ?", [$tenant->getTenantKey()]))->toHaveCount(0);
|
||||||
})->with([
|
})->with([
|
||||||
'db level on cascade delete' => true,
|
'db level on cascade delete' => true,
|
||||||
|
|
@ -965,10 +967,12 @@ test('pivot record is automatically deleted with the tenant resource', function(
|
||||||
});
|
});
|
||||||
|
|
||||||
test('DeleteAllTenantMappings handles incorrect configuration correctly', function() {
|
test('DeleteAllTenantMappings handles incorrect configuration correctly', function() {
|
||||||
|
Event::listen(TenantDeleted::class, DeleteAllTenantMappings::class);
|
||||||
|
|
||||||
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
[$tenant1, $tenant2] = createTenantsAndRunMigrations();
|
||||||
|
|
||||||
// Existing table, non-existent tenant key column
|
// Existing table, non-existent tenant key column
|
||||||
// The listener should throw an exception
|
// The listener should throw an 'unknown column' exception
|
||||||
DeleteAllTenantMappings::$pivotTables = ['tenant_users' => 'non_existent_column'];
|
DeleteAllTenantMappings::$pivotTables = ['tenant_users' => 'non_existent_column'];
|
||||||
|
|
||||||
// Should throw an exception when tenant is deleted
|
// Should throw an exception when tenant is deleted
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue