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

[4.x] Drop tenant databases on migrate:fresh (#971)

* Test that `migrate:fresh` deletes tenant databases

* Delete tenants on `migrate:fresh`

* Fix code style (php-cs-fixer)

* Add config key for dropping tenant databases on `migrate:fresh`

* Add MigrateFreshOverride

* Try to override `migrate:fresh` in TenancyServiceProvider

* Update `migrate:fresh` test

* Fix code style (php-cs-fixer)

* Drop tenant databases by default

* Change `migrate:fresh` test to test if the tenant DBs are dropped by default

* Override `migrate:fresh` by extending `FreshCommand` in TenancyServiceProvider

* Update MigrateFreshOverride

* Fix code style (php-cs-fixer)

* Fix commands test

* Simplify handle method

* Fix code style (php-cs-fixer)

* Don't drop tenant DBs on migrate:fresh by default

* Change command overriding

* Update migrate:fresh test

* always register MigrateFreshOverride

* misc

Co-authored-by: PHP CS Fixer <phpcsfixer@example.com>
Co-authored-by: Samuel Štancl <samuel.stancl@gmail.com>
This commit is contained in:
lukinovec 2022-10-17 15:19:30 +02:00 committed by GitHub
parent 42dab2985a
commit 080b271bb3
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
7 changed files with 95 additions and 21 deletions

View file

@ -116,18 +116,21 @@ return [
'pgsql' => Stancl\Tenancy\Database\TenantDatabaseManagers\PostgreSQLDatabaseManager::class, 'pgsql' => Stancl\Tenancy\Database\TenantDatabaseManagers\PostgreSQLDatabaseManager::class,
'sqlsrv' => Stancl\Tenancy\Database\TenantDatabaseManagers\MicrosoftSQLDatabaseManager::class, 'sqlsrv' => Stancl\Tenancy\Database\TenantDatabaseManagers\MicrosoftSQLDatabaseManager::class,
/** /**
* Use this database manager for MySQL to have a DB user created for each tenant database. * Use this database manager for MySQL to have a DB user created for each tenant database.
* You can customize the grants given to these users by changing the $grants property. * You can customize the grants given to these users by changing the $grants property.
*/ */
// 'mysql' => Stancl\Tenancy\Database\TenantDatabaseManagers\PermissionControlledMySQLDatabaseManager::class, // 'mysql' => Stancl\Tenancy\Database\TenantDatabaseManagers\PermissionControlledMySQLDatabaseManager::class,
/** /**
* Disable the pgsql manager above, and enable the one below if you * Disable the pgsql manager above, and enable the one below if you
* want to separate tenant DBs by schemas rather than databases. * want to separate tenant DBs by schemas rather than databases.
*/ */
// 'pgsql' => Stancl\Tenancy\Database\TenantDatabaseManagers\PostgreSQLSchemaManager::class, // Separate by schema instead of database // 'pgsql' => Stancl\Tenancy\Database\TenantDatabaseManagers\PostgreSQLSchemaManager::class, // Separate by schema instead of database
], ],
// todo docblock
'drop_tenant_databases_on_migrate_fresh' => false,
], ],
/** /**

View file

@ -30,7 +30,8 @@
"doctrine/dbal": "^2.10", "doctrine/dbal": "^2.10",
"spatie/valuestore": "^1.2.5", "spatie/valuestore": "^1.2.5",
"pestphp/pest": "^1.21", "pestphp/pest": "^1.21",
"nunomaduro/larastan": "^1.0" "nunomaduro/larastan": "^1.0",
"spatie/invade": "^1.1"
}, },
"autoload": { "autoload": {
"psr-4": { "psr-4": {

View file

@ -1,5 +1,6 @@
includes: includes:
- ./vendor/nunomaduro/larastan/extension.neon - ./vendor/nunomaduro/larastan/extension.neon
- ./vendor/spatie/invade/phpstan-extension.neon
parameters: parameters:
paths: paths:

View file

@ -0,0 +1,19 @@
<?php
declare(strict_types=1);
namespace Stancl\Tenancy\Commands;
use Illuminate\Database\Console\Migrations\FreshCommand;
class MigrateFreshOverride extends FreshCommand
{
public function handle()
{
if (config('tenancy.database.drop_tenant_databases_on_migrate_fresh')) {
tenancy()->model()::cursor()->each->delete();
}
return parent::handle();
}
}

View file

@ -7,7 +7,7 @@ namespace Stancl\Tenancy\Events\Contracts;
use Illuminate\Queue\SerializesModels; use Illuminate\Queue\SerializesModels;
use Stancl\Tenancy\Contracts\Tenant; use Stancl\Tenancy\Contracts\Tenant;
abstract class TenantEvent abstract class TenantEvent // todo we could add a feature to JobPipeline that automatically gets data for the send() from here
{ {
use SerializesModels; use SerializesModels;

View file

@ -5,6 +5,7 @@ declare(strict_types=1);
namespace Stancl\Tenancy; namespace Stancl\Tenancy;
use Illuminate\Cache\CacheManager; use Illuminate\Cache\CacheManager;
use Illuminate\Database\Console\Migrations\FreshCommand;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Illuminate\Support\ServiceProvider; use Illuminate\Support\ServiceProvider;
use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper; use Stancl\Tenancy\Bootstrappers\FilesystemTenancyBootstrapper;
@ -90,6 +91,10 @@ class TenancyServiceProvider extends ServiceProvider
Commands\Up::class, Commands\Up::class,
]); ]);
$this->app->extend(FreshCommand::class, function () {
return new Commands\MigrateFreshOverride;
});
$this->publishes([ $this->publishes([
__DIR__ . '/../assets/config.php' => config_path('tenancy.php'), __DIR__ . '/../assets/config.php' => config_path('tenancy.php'),
], 'config'); ], 'config');

View file

@ -2,24 +2,28 @@
declare(strict_types=1); declare(strict_types=1);
use Illuminate\Database\DatabaseManager;
use Illuminate\Support\Carbon;
use Illuminate\Support\Facades\Artisan;
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Stancl\Tenancy\Tests\Etc\User;
use Stancl\JobPipeline\JobPipeline;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Event; use Illuminate\Support\Facades\Event;
use Illuminate\Support\Facades\Schema; use Illuminate\Support\Facades\Schema;
use Stancl\JobPipeline\JobPipeline; use Stancl\Tenancy\Jobs\DeleteDomains;
use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper; use Illuminate\Support\Facades\Artisan;
use Stancl\Tenancy\Events\TenancyEnded; use Stancl\Tenancy\Events\TenancyEnded;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Jobs\CreateDatabase; use Stancl\Tenancy\Jobs\CreateDatabase;
use Stancl\Tenancy\Jobs\DeleteDatabase;
use Illuminate\Database\DatabaseManager;
use Stancl\Tenancy\Events\TenantCreated;
use Stancl\Tenancy\Events\TenantDeleted;
use Stancl\Tenancy\Tests\Etc\TestSeeder;
use Stancl\Tenancy\Events\DeletingTenant;
use Stancl\Tenancy\Tests\Etc\ExampleSeeder;
use Stancl\Tenancy\Events\TenancyInitialized;
use Stancl\Tenancy\Listeners\BootstrapTenancy; use Stancl\Tenancy\Listeners\BootstrapTenancy;
use Stancl\Tenancy\Listeners\RevertToCentralContext; use Stancl\Tenancy\Listeners\RevertToCentralContext;
use Stancl\Tenancy\Tests\Etc\ExampleSeeder; use Stancl\Tenancy\Bootstrappers\DatabaseTenancyBootstrapper;
use Stancl\Tenancy\Tests\Etc\Tenant;
use Stancl\Tenancy\Tests\Etc\TestSeeder;
use Stancl\Tenancy\Tests\Etc\User;
beforeEach(function () { beforeEach(function () {
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) { Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
@ -267,6 +271,47 @@ test('run command works when sub command asks questions and accepts arguments',
expect($user->email)->toBe('email@localhost'); expect($user->email)->toBe('email@localhost');
}); });
test('migrate fresh command only deletes tenant databases if drop_tenant_databases_on_migrate_fresh is true', function (bool $dropTenantDBsOnMigrateFresh) {
Event::listen(DeletingTenant::class,
JobPipeline::make([DeleteDomains::class])->send(function (DeletingTenant $event) {
return $event->tenant;
})->shouldBeQueued(false)->toListener()
);
Event::listen(
TenantDeleted::class,
JobPipeline::make([DeleteDatabase::class])->send(function (TenantDeleted $event) {
return $event->tenant;
})->shouldBeQueued(false)->toListener()
);
config(['tenancy.database.drop_tenant_databases_on_migrate_fresh' => $dropTenantDBsOnMigrateFresh]);
$shouldHaveDBAfterMigrateFresh = ! $dropTenantDBsOnMigrateFresh;
/** @var Tenant[] $tenants */
$tenants = [
Tenant::create(),
Tenant::create(),
Tenant::create(),
];
$tenantHasDatabase = fn (Tenant $tenant) => $tenant->database()->manager()->databaseExists($tenant->database()->getName());
foreach ($tenants as $tenant) {
expect($tenantHasDatabase($tenant))->toBeTrue();
}
pest()->artisan('migrate:fresh', [
'--force' => true,
'--path' => __DIR__ . '/../assets/migrations',
'--realpath' => true,
]);
foreach ($tenants as $tenant) {
expect($tenantHasDatabase($tenant))->toBe($shouldHaveDBAfterMigrateFresh);
}
})->with([true, false]);
// todo@tests // todo@tests
function runCommandWorks(): void function runCommandWorks(): void
{ {