mirror of
https://github.com/archtechx/tenancy.git
synced 2025-12-12 20:14:03 +00:00
DatabasePreparationTest
This commit is contained in:
parent
86a98b2bc8
commit
3c4d2189dc
4 changed files with 148 additions and 25 deletions
|
|
@ -201,7 +201,7 @@ return [
|
||||||
* See the documentation page for each class to
|
* See the documentation page for each class to
|
||||||
* understand which ones you want to enable.
|
* understand which ones you want to enable.
|
||||||
*/
|
*/
|
||||||
'features' => [
|
'features' => [ // todo test features
|
||||||
// Stancl\Tenancy\Features\Timestamps::class, // https://tenancy.samuelstancl.me/docs/v2/features/timestamps/
|
// Stancl\Tenancy\Features\Timestamps::class, // https://tenancy.samuelstancl.me/docs/v2/features/timestamps/
|
||||||
// Stancl\Tenancy\Features\TenantConfig::class, // https://tenancy.samuelstancl.me/docs/v2/features/tenant-config/
|
// Stancl\Tenancy\Features\TenantConfig::class, // https://tenancy.samuelstancl.me/docs/v2/features/tenant-config/
|
||||||
// Stancl\Tenancy\Features\TelescopeTags::class, // https://tenancy.samuelstancl.me/docs/v2/telescope/
|
// Stancl\Tenancy\Features\TelescopeTags::class, // https://tenancy.samuelstancl.me/docs/v2/telescope/
|
||||||
|
|
@ -211,32 +211,15 @@ return [
|
||||||
/**
|
/**
|
||||||
* The URL to which users will be redirected when they try to acceess a central route on a tenant domain.
|
* The URL to which users will be redirected when they try to acceess a central route on a tenant domain.
|
||||||
*/
|
*/
|
||||||
'home_url' => '/app',
|
'home_url' => '/app', // todo move this to static
|
||||||
|
|
||||||
/**
|
|
||||||
* Should tenant databases be created asynchronously in a queued job.
|
|
||||||
*/
|
|
||||||
'queue_database_creation' => false, // todo make this a static property
|
|
||||||
|
|
||||||
'migration_parameters' => [
|
'migration_parameters' => [
|
||||||
'--force' => true, // Set this to true to be able to run migrations in production
|
'--force' => true, // Set this to true to be able to run migrations in production
|
||||||
// '--path' => [database_path('migrations/tenant')], // If you need to customize paths to tenant migrations
|
'--path' => [database_path('migrations/tenant')],
|
||||||
],
|
],
|
||||||
|
|
||||||
'seeder_parameters' => [
|
'seeder_parameters' => [
|
||||||
'--class' => 'DatabaseSeeder', // root seeder class, e.g.: 'DatabaseSeeder'
|
'--class' => 'DatabaseSeeder', // root seeder class, e.g.: 'DatabaseSeeder'
|
||||||
// '--force' => true,
|
// '--force' => true,
|
||||||
],
|
],
|
||||||
|
|
||||||
/**
|
|
||||||
* Should tenant databases be deleted asynchronously in a queued job.
|
|
||||||
*/
|
|
||||||
'queue_database_deletion' => false,
|
|
||||||
|
|
||||||
/**
|
|
||||||
* Middleware pushed to the global middleware stack.
|
|
||||||
*/
|
|
||||||
'global_middleware' => [ // todo get rid of this
|
|
||||||
// Stancl\Tenancy\Middleware\InitializeTenancy::class,
|
|
||||||
],
|
|
||||||
];
|
];
|
||||||
|
|
|
||||||
|
|
@ -6,6 +6,7 @@ namespace Stancl\Tenancy\Commands;
|
||||||
|
|
||||||
use Illuminate\Database\ConnectionResolverInterface;
|
use Illuminate\Database\ConnectionResolverInterface;
|
||||||
use Illuminate\Database\Console\Seeds\SeedCommand;
|
use Illuminate\Database\Console\Seeds\SeedCommand;
|
||||||
|
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||||
use Stancl\Tenancy\DatabaseManager;
|
use Stancl\Tenancy\DatabaseManager;
|
||||||
use Stancl\Tenancy\Events\DatabaseSeeded;
|
use Stancl\Tenancy\Events\DatabaseSeeded;
|
||||||
use Stancl\Tenancy\Traits\HasATenantsOption;
|
use Stancl\Tenancy\Traits\HasATenantsOption;
|
||||||
|
|
@ -54,7 +55,12 @@ class Seed extends SeedCommand
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
|
||||||
tenancy()->all($this->option('tenants'))->each(function ($tenant) {
|
tenancy()
|
||||||
|
->query()
|
||||||
|
->when($this->option('tenants'), function ($query) {
|
||||||
|
$query->whereIn(tenancy()->model()->getTenantKeyName(), $this->option('tenants'));
|
||||||
|
})
|
||||||
|
->each(function (TenantWithDatabase $tenant) {
|
||||||
$this->line("Tenant: {$tenant['id']}");
|
$this->line("Tenant: {$tenant['id']}");
|
||||||
|
|
||||||
$tenant->run(function () {
|
$tenant->run(function () {
|
||||||
|
|
|
||||||
|
|
@ -9,7 +9,7 @@ use Illuminate\Database\Connection;
|
||||||
use Illuminate\Support\Facades\DB;
|
use Illuminate\Support\Facades\DB;
|
||||||
use Stancl\Tenancy\Contracts\Future\CanSetConnection;
|
use Stancl\Tenancy\Contracts\Future\CanSetConnection;
|
||||||
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
use Stancl\Tenancy\Contracts\TenantDatabaseManager;
|
||||||
use Stancl\Tenancy\Tenant;
|
use Stancl\Tenancy\Contracts\TenantWithDatabase;
|
||||||
|
|
||||||
class MySQLDatabaseManager implements TenantDatabaseManager, CanSetConnection
|
class MySQLDatabaseManager implements TenantDatabaseManager, CanSetConnection
|
||||||
{
|
{
|
||||||
|
|
@ -36,7 +36,7 @@ class MySQLDatabaseManager implements TenantDatabaseManager, CanSetConnection
|
||||||
$this->connection = $connection;
|
$this->connection = $connection;
|
||||||
}
|
}
|
||||||
|
|
||||||
public function createDatabase(Tenant $tenant): bool
|
public function createDatabase(TenantWithDatabase $tenant): bool
|
||||||
{
|
{
|
||||||
$database = $tenant->database()->getName();
|
$database = $tenant->database()->getName();
|
||||||
$charset = $this->database()->getConfig('charset');
|
$charset = $this->database()->getConfig('charset');
|
||||||
|
|
@ -45,7 +45,7 @@ class MySQLDatabaseManager implements TenantDatabaseManager, CanSetConnection
|
||||||
return $this->database()->statement("CREATE DATABASE `{$database}` CHARACTER SET `$charset` COLLATE `$collation`");
|
return $this->database()->statement("CREATE DATABASE `{$database}` CHARACTER SET `$charset` COLLATE `$collation`");
|
||||||
}
|
}
|
||||||
|
|
||||||
public function deleteDatabase(Tenant $tenant): bool
|
public function deleteDatabase(TenantWithDatabase $tenant): bool
|
||||||
{
|
{
|
||||||
return $this->database()->statement("DROP DATABASE `{$tenant->database()->getName()}`");
|
return $this->database()->statement("DROP DATABASE `{$tenant->database()->getName()}`");
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1 +1,135 @@
|
||||||
// test DB creation, migration, seeding
|
<?php
|
||||||
|
|
||||||
|
namespace Stancl\Tenancy\Tests\v3;
|
||||||
|
|
||||||
|
use Illuminate\Database\Seeder;
|
||||||
|
use Illuminate\Foundation\Auth\User as Authenticable;
|
||||||
|
use Illuminate\Support\Facades\DB;
|
||||||
|
use Illuminate\Support\Facades\Event;
|
||||||
|
use Illuminate\Support\Facades\Schema;
|
||||||
|
use Stancl\Tenancy\Database\Models\Tenant;
|
||||||
|
use Stancl\Tenancy\Events\Listeners\JobPipeline;
|
||||||
|
use Stancl\Tenancy\Events\TenantCreated;
|
||||||
|
use Stancl\Tenancy\Jobs\CreateDatabase;
|
||||||
|
use Stancl\Tenancy\Jobs\MigrateDatabase;
|
||||||
|
use Stancl\Tenancy\Jobs\SeedDatabase;
|
||||||
|
use Stancl\Tenancy\TenantDatabaseManagers\MySQLDatabaseManager;
|
||||||
|
use Stancl\Tenancy\Tests\TestCase;
|
||||||
|
|
||||||
|
class DatabasePreparationTest extends TestCase
|
||||||
|
{
|
||||||
|
/** @test */
|
||||||
|
public function database_can_be_created_after_tenant_creation()
|
||||||
|
{
|
||||||
|
config(['tenancy.template_tenant_connection' => 'mysql']);
|
||||||
|
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([CreateDatabase::class])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
|
||||||
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
|
$this->assertTrue(app(MySQLDatabaseManager::class)->databaseExists($tenant->database()->getName()));
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function database_can_be_migrated_after_tenant_creation()
|
||||||
|
{
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([
|
||||||
|
CreateDatabase::class,
|
||||||
|
MigrateDatabase::class,
|
||||||
|
])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
|
||||||
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
|
$tenant->run(function () {
|
||||||
|
$this->assertTrue(Schema::hasTable('users'));
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function database_can_be_seeded_after_tenant_creation()
|
||||||
|
{
|
||||||
|
config(['tenancy.seeder_parameters' => [
|
||||||
|
'--class' => TestSeeder::class,
|
||||||
|
]]);
|
||||||
|
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([
|
||||||
|
CreateDatabase::class,
|
||||||
|
MigrateDatabase::class,
|
||||||
|
SeedDatabase::class,
|
||||||
|
])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
|
||||||
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
|
$tenant->run(function () {
|
||||||
|
$this->assertSame('Seeded User', User::first()->name);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
|
||||||
|
/** @test */
|
||||||
|
public function custom_job_can_be_added_to_the_pipeline()
|
||||||
|
{
|
||||||
|
config(['tenancy.seeder_parameters' => [
|
||||||
|
'--class' => TestSeeder::class,
|
||||||
|
]]);
|
||||||
|
|
||||||
|
Event::listen(TenantCreated::class, JobPipeline::make([
|
||||||
|
CreateDatabase::class,
|
||||||
|
MigrateDatabase::class,
|
||||||
|
SeedDatabase::class,
|
||||||
|
CreateSuperuser::class,
|
||||||
|
])->send(function (TenantCreated $event) {
|
||||||
|
return $event->tenant;
|
||||||
|
})->toListener());
|
||||||
|
|
||||||
|
$tenant = Tenant::create();
|
||||||
|
|
||||||
|
$tenant->run(function () {
|
||||||
|
$this->assertSame('Foo', User::all()[1]->name);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class User extends Authenticable
|
||||||
|
{
|
||||||
|
protected $guarded = [];
|
||||||
|
}
|
||||||
|
|
||||||
|
class TestSeeder extends Seeder
|
||||||
|
{
|
||||||
|
/**
|
||||||
|
* Run the database seeds.
|
||||||
|
*
|
||||||
|
* @return void
|
||||||
|
*/
|
||||||
|
public function run()
|
||||||
|
{
|
||||||
|
DB::table('users')->insert([
|
||||||
|
'name' => 'Seeded User',
|
||||||
|
'email' => 'seeded@user',
|
||||||
|
'password' => bcrypt('password'),
|
||||||
|
]);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
class CreateSuperuser
|
||||||
|
{
|
||||||
|
protected $tenant;
|
||||||
|
|
||||||
|
public function __construct(Tenant $tenant)
|
||||||
|
{
|
||||||
|
$this->tenant = $tenant;
|
||||||
|
}
|
||||||
|
|
||||||
|
public function handle()
|
||||||
|
{
|
||||||
|
$this->tenant->run(function () {
|
||||||
|
User::create(['name' => 'Foo', 'email' => 'foo@bar.com', 'password' => 'secret']);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
}
|
||||||
Loading…
Add table
Add a link
Reference in a new issue