Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[4.x] Optionally delete storage after tenant deletion #938

Merged
merged 7 commits into from
Sep 20, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions assets/TenancyServiceProvider.stub.php
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,8 @@ public function events()
])->send(function (Events\DeletingTenant $event) {
return $event->tenant;
})->shouldBeQueued(false),

// Listeners\DeleteTenantStorage::class,
],
Events\TenantDeleted::class => [
JobPipeline::make([
Expand Down
16 changes: 16 additions & 0 deletions src/Listeners/DeleteTenantStorage.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<?php

declare(strict_types=1);

namespace Stancl\Tenancy\Listeners;

use Illuminate\Support\Facades\File;
use Stancl\Tenancy\Events\DeletingTenant;

class DeleteTenantStorage
{
public function handle(DeletingTenant $event): void
{
File::deleteDirectory($event->tenant->run(fn () => storage_path()));
}
}
28 changes: 27 additions & 1 deletion tests/BootstrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@

declare(strict_types=1);

use Illuminate\Filesystem\FilesystemAdapter;
use Illuminate\Support\Str;
use Illuminate\Support\Facades\DB;
use Stancl\JobPipeline\JobPipeline;
use Illuminate\Support\Facades\File;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\Event;
Expand All @@ -14,13 +14,16 @@
use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Jobs\CreateDatabase;
use Stancl\Tenancy\Events\TenantCreated;
use Illuminate\Filesystem\FilesystemAdapter;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Bootstrappers\CacheTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\RedisTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
use Stancl\Tenancy\Events\DeletingTenant;
use Stancl\Tenancy\Listeners\DeleteTenantStorage;

beforeEach(function () {
$this->mockConsoleOutput = false;
Expand Down Expand Up @@ -184,6 +187,29 @@
expect($new_storage_path)->toEqual($expected_storage_path);
});

test('tenant storage can get deleted after the tenant when DeletingTenant listens to DeleteTenantStorage', function () {
config([
'tenancy.bootstrappers' => [
FilesystemTenancyBootstrapper::class,
],
]);

Event::listen(DeletingTenant::class, DeleteTenantStorage::class);

tenancy()->initialize(Tenant::create());
$tenantStoragePath = storage_path();

Storage::fake('test');

expect(File::isDirectory($tenantStoragePath))->toBeTrue();

Storage::put('test.txt', 'testing file');

tenant()->delete();

expect(File::isDirectory($tenantStoragePath))->toBeFalse();
});

function getDiskPrefix(string $disk): string
{
/** @var FilesystemAdapter $disk */
Expand Down