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

Create and drop schema in tests

This commit is contained in:
lukinovec 2023-04-26 18:09:50 +02:00
parent 4eea9f1031
commit e169cf49fa
2 changed files with 28 additions and 3 deletions

View file

@ -43,8 +43,6 @@ class CreateRLSPoliciesForTables
) )
)"); )");
dump(DB::select('select CURRENT_USER'));
DB::statement("ALTER TABLE {$table} FORCE ROW LEVEL SECURITY"); DB::statement("ALTER TABLE {$table} FORCE ROW LEVEL SECURITY");
return Command::SUCCESS; return Command::SUCCESS;
@ -60,6 +58,7 @@ class CreateRLSPoliciesForTables
DB::statement("ALTER TABLE {$table} FORCE ROW LEVEL SECURITY"); DB::statement("ALTER TABLE {$table} FORCE ROW LEVEL SECURITY");
// $this->components->info("Created RLS policy for table '$table'"); // $this->components->info("Created RLS policy for table '$table'");
} }

View file

@ -4,6 +4,8 @@ declare(strict_types=1);
use Illuminate\Support\Facades\DB; use Illuminate\Support\Facades\DB;
use Stancl\Tenancy\Tests\Etc\Tenant; use Stancl\Tenancy\Tests\Etc\Tenant;
use Illuminate\Support\Facades\Schema;
use Illuminate\Database\Schema\Blueprint;
use Stancl\Tenancy\Jobs\DeleteTenantsPostgresUser; use Stancl\Tenancy\Jobs\DeleteTenantsPostgresUser;
use Stancl\Tenancy\Jobs\CreatePostgresUserForTenant; use Stancl\Tenancy\Jobs\CreatePostgresUserForTenant;
use Stancl\Tenancy\Actions\CreateRLSPoliciesForTables; use Stancl\Tenancy\Actions\CreateRLSPoliciesForTables;
@ -53,6 +55,27 @@ test('correct rls policies get created', function(bool $action) {
], ],
]); ]);
Schema::dropIfExists('comments');
Schema::dropIfExists('posts');
Schema::create('posts', function (Blueprint $table) {
$table->increments('id');
$table->string('text');
$table->string('tenant_id');
$table->foreign('tenant_id')->references('id')->on('tenants')->onUpdate('cascade')->onDelete('cascade');
});
Schema::create('comments', function (Blueprint $table) {
$table->increments('id');
$table->string('text');
$table->unsignedInteger('post_id');
$table->foreign('post_id')->references('id')->on('posts')->onUpdate('cascade')->onDelete('cascade');
});
$getRlsPolicies = fn () => DB::select('select * from pg_policies'); $getRlsPolicies = fn () => DB::select('select * from pg_policies');
$getModelTables = fn () => collect(config('tenancy.models.rls'))->map(fn (string $model) => (new $model)->getTable()); $getModelTables = fn () => collect(config('tenancy.models.rls'))->map(fn (string $model) => (new $model)->getTable());
$getRlsTables = fn() => $getModelTables()->map(fn ($table) => DB::select('select relname, relrowsecurity, relforcerowsecurity from pg_class WHERE oid = ' . "'$table'::regclass"))->collapse(); $getRlsTables = fn() => $getModelTables()->map(fn ($table) => DB::select('select relname, relrowsecurity, relforcerowsecurity from pg_class WHERE oid = ' . "'$table'::regclass"))->collapse();
@ -94,4 +117,7 @@ test('correct rls policies get created', function(bool $action) {
expect($getModelTables())->toContain($table->relname); expect($getModelTables())->toContain($table->relname);
expect($table->relforcerowsecurity)->toBeTrue(); expect($table->relforcerowsecurity)->toBeTrue();
} }
})->with(['action' => true, 'command' => false]); })->with([
'action' => true,
'command' => false
]);