1
0
Fork 0
mirror of https://github.com/archtechx/tenancy.git synced 2026-02-05 10:34:04 +00:00

Use DB transactions

This commit is contained in:
lukinovec 2023-05-24 14:57:06 +02:00
parent ea9690f915
commit 47fe86c21e
2 changed files with 29 additions and 29 deletions

View file

@ -39,9 +39,7 @@ class CreatePostgresUserForTenant implements ShouldQueue
// Create the user only if it doesn't already exist // Create the user only if it doesn't already exist
if (! count(DB::select("SELECT usename FROM pg_user WHERE usename = '$name';")) > 0) { if (! count(DB::select("SELECT usename FROM pg_user WHERE usename = '$name';")) > 0) {
DB::transaction(function () use ($name, $password) { DB::transaction(fn () => DB::statement("CREATE USER \"$name\" LOGIN PASSWORD '$password';"));
DB::statement("CREATE USER \"$name\" LOGIN PASSWORD '$password';");
});
} }
$this->grantPermissions((string) $name); $this->grantPermissions((string) $name);

View file

@ -42,37 +42,39 @@ beforeEach(function () {
$tenantTable = $tenantModel->getTable(); $tenantTable = $tenantModel->getTable();
// Drop all existing policies // Drop all existing policies
foreach (DB::select('select * from pg_policies') as $policy) { DB::transaction(function () use ($tenantTable, $primaryModel, $secondaryModel) {
DB::statement("DROP POLICY IF EXISTS {$policy->policyname} ON {$policy->tablename}"); foreach (DB::select('select * from pg_policies') as $policy) {
} DB::statement("DROP POLICY IF EXISTS {$policy->policyname} ON {$policy->tablename}");
}
Schema::dropIfExists('domains'); Schema::dropIfExists('domains');
DB::statement("DROP TABLE IF EXISTS {$secondaryModel->getTable()} CASCADE"); DB::statement("DROP TABLE IF EXISTS {$secondaryModel->getTable()} CASCADE");
DB::statement("DROP TABLE IF EXISTS {$primaryModel->getTable()} CASCADE"); DB::statement("DROP TABLE IF EXISTS {$primaryModel->getTable()} CASCADE");
DB::statement("DROP TABLE IF EXISTS $tenantTable CASCADE"); DB::statement("DROP TABLE IF EXISTS $tenantTable CASCADE");
Schema::create($tenantTable, function (Blueprint $table) { Schema::create($tenantTable, function (Blueprint $table) {
$table->string('id')->primary(); $table->string('id')->primary();
$table->timestamps(); $table->timestamps();
$table->json('data')->nullable(); $table->json('data')->nullable();
}); });
Schema::create($primaryModel->getTable(), function (Blueprint $table) { Schema::create($primaryModel->getTable(), function (Blueprint $table) {
$table->id(); $table->id();
$table->string('text'); $table->string('text');
$table->string($tenantKeyColumn = config('tenancy.models.tenant_key_column')); $table->string($tenantKeyColumn = config('tenancy.models.tenant_key_column'));
$table->timestamps(); $table->timestamps();
$table->foreign($tenantKeyColumn)->references(tenancy()->model()->getKeyName())->on(tenancy()->model()->getTable())->onUpdate('cascade')->onDelete('cascade'); $table->foreign($tenantKeyColumn)->references(tenancy()->model()->getKeyName())->on(tenancy()->model()->getTable())->onUpdate('cascade')->onDelete('cascade');
}); });
Schema::create($secondaryModel->getTable(), function (Blueprint $table) use ($primaryModel) { Schema::create($secondaryModel->getTable(), function (Blueprint $table) use ($primaryModel) {
$table->id(); $table->id();
$table->string('text'); $table->string('text');
$table->unsignedBigInteger($primaryModel->getForeignKey()); $table->unsignedBigInteger($primaryModel->getForeignKey());
$table->timestamps(); $table->timestamps();
$table->foreign($primaryModel->getForeignKey())->references($primaryModel->getKeyName())->on($primaryModel->getTable())->onUpdate('cascade')->onDelete('cascade'); $table->foreign($primaryModel->getForeignKey())->references($primaryModel->getKeyName())->on($primaryModel->getTable())->onUpdate('cascade')->onDelete('cascade');
});
}); });
}); });
@ -112,7 +114,7 @@ test('correct rls policies get created', function () {
// Drop all existing policies to check if the command creates policies for multiple tables // Drop all existing policies to check if the command creates policies for multiple tables
foreach ($getRlsPolicies() as $policy) { foreach ($getRlsPolicies() as $policy) {
DB::statement("DROP POLICY IF EXISTS {$policy->policyname} ON {$policy->tablename}"); DB::transaction(fn () => DB::statement("DROP POLICY IF EXISTS {$policy->policyname} ON {$policy->tablename}"));
} }
expect($getRlsPolicies())->toHaveCount(0); expect($getRlsPolicies())->toHaveCount(0);