mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-13 02:04:03 +00:00
Compare commits
2 commits
53c7d4988c
...
cf01ce92bd
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
cf01ce92bd | ||
|
|
4f3c973858 |
2 changed files with 3 additions and 8 deletions
|
|
@ -5,7 +5,6 @@ declare(strict_types=1);
|
||||||
namespace Stancl\Tenancy\ResourceSyncing\Listeners;
|
namespace Stancl\Tenancy\ResourceSyncing\Listeners;
|
||||||
|
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Illuminate\Support\Facades\Schema;
|
|
||||||
use Stancl\Tenancy\Events\TenantDeleted;
|
use Stancl\Tenancy\Events\TenantDeleted;
|
||||||
use Stancl\Tenancy\Listeners\QueueableListener;
|
use Stancl\Tenancy\Listeners\QueueableListener;
|
||||||
|
|
||||||
|
|
@ -37,17 +36,13 @@ class DeleteAllTenantMappings extends QueueableListener
|
||||||
* 'tenant_users' => 'tenant_id',
|
* 'tenant_users' => 'tenant_id',
|
||||||
* // You can also add more pivot tables here
|
* // You can also add more pivot tables here
|
||||||
* ];
|
* ];
|
||||||
*
|
|
||||||
* Non-existent tables specified in the property will be skipped.
|
|
||||||
*/
|
*/
|
||||||
public static array $pivotTables = ['tenant_resources' => 'tenant_id'];
|
public static array $pivotTables = ['tenant_resources' => 'tenant_id'];
|
||||||
|
|
||||||
public function handle(TenantDeleted $event): void
|
public function handle(TenantDeleted $event): void
|
||||||
{
|
{
|
||||||
foreach (static::$pivotTables as $table => $tenantKeyColumn) {
|
foreach (static::$pivotTables as $table => $tenantKeyColumn) {
|
||||||
if (Schema::hasTable($table)) {
|
DB::table($table)->where($tenantKeyColumn, $event->tenant->getTenantKey())->delete();
|
||||||
DB::table($table)->where($tenantKeyColumn, $event->tenant->getTenantKey())->delete();
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -978,10 +978,10 @@ test('DeleteAllTenantMappings handles incorrect configuration correctly', functi
|
||||||
// Should throw an exception when tenant is deleted
|
// Should throw an exception when tenant is deleted
|
||||||
expect(fn() => $tenant1->delete())->toThrow(QueryException::class, "Unknown column 'non_existent_column' in 'where clause'");
|
expect(fn() => $tenant1->delete())->toThrow(QueryException::class, "Unknown column 'non_existent_column' in 'where clause'");
|
||||||
|
|
||||||
// Non-existent table, the listener skips it, no exception to throw
|
// Non-existent table
|
||||||
DeleteAllTenantMappings::$pivotTables = ['nonexistent_pivot' => 'non_existent_column'];
|
DeleteAllTenantMappings::$pivotTables = ['nonexistent_pivot' => 'non_existent_column'];
|
||||||
|
|
||||||
expect(fn() => $tenant2->delete())->not()->toThrow(Exception::class);
|
expect(fn() => $tenant2->delete())->toThrow(QueryException::class, "Table 'main.nonexistent_pivot' doesn't exist");
|
||||||
});
|
});
|
||||||
|
|
||||||
test('trashed resources are synced correctly', function () {
|
test('trashed resources are synced correctly', function () {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue