From 1cfb85e756c5319c8efcb90657285a0983ed1ed8 Mon Sep 17 00:00:00 2001 From: lukinovec Date: Wed, 2 Jul 2025 17:53:15 +0200 Subject: [PATCH] Update key names of the formatted constraints --- src/RLS/PolicyManagers/TableRLSManager.php | 40 +++++----- tests/RLS/TableManagerTest.php | 92 +++++++++++----------- 2 files changed, 66 insertions(+), 66 deletions(-) diff --git a/src/RLS/PolicyManagers/TableRLSManager.php b/src/RLS/PolicyManagers/TableRLSManager.php index b8dcf6cc..5d5c5161 100644 --- a/src/RLS/PolicyManagers/TableRLSManager.php +++ b/src/RLS/PolicyManagers/TableRLSManager.php @@ -43,14 +43,14 @@ use Stancl\Tenancy\RLS\Exceptions\RLSCommentConstraintException; * // 'foo_table' => [...$stepsLeadingToTenantsTable], * // 'bar_table' => [ * // [ - * // 'foreignKey' => 'post_id', + * // 'localColumn' => 'post_id', * // 'foreignTable' => 'posts', - * // 'foreignId' => 'id' + * // 'foreignColumn' => 'id' * // ], * // [ - * // 'foreignKey' => 'tenant_id', + * // 'localColumn' => 'tenant_id', * // 'foreignTable' => 'tenants', - * // 'foreignId' => 'id' + * // 'foreignColumn' => 'id' * // ], * // ], * // This is used in the CreateUserWithRLSPolicies command. @@ -103,21 +103,21 @@ class TableRLSManager implements RLSPolicyManager * * 'posts' => [ * [ - * 'foreignKey' => 'tenant_id', + * 'localColumn' => 'tenant_id', * 'foreignTable' => 'tenants', - * 'foreignId' => 'id' + * 'foreignColumn' => 'id' * ], * ], * 'comments' => [ * [ - * 'foreignKey' => 'post_id', + * 'localColumn' => 'post_id', * 'foreignTable' => 'posts', - * 'foreignId' => 'id' + * 'foreignColumn' => 'id' * ], * [ - * 'foreignKey' => 'tenant_id', + * 'localColumn' => 'tenant_id', * 'foreignTable' => 'tenants', - * 'foreignId' => 'id' + * 'foreignColumn' => 'id' * ], * ], * @@ -136,9 +136,9 @@ class TableRLSManager implements RLSPolicyManager if ($this->isValidPath($shortestPath)) { // Format path steps to a more readable format (keep only the needed data) $shortestPaths[$tableName] = array_map(fn (array $step) => [ - 'foreignKey' => $step['foreignKey'], + 'localColumn' => $step['localColumn'], 'foreignTable' => $step['foreignTable'], - 'foreignId' => $step['foreignId'], + 'foreignColumn' => $step['foreignColumn'], ], $shortestPath['steps']); } @@ -187,9 +187,9 @@ class TableRLSManager implements RLSPolicyManager * before returning the shortest paths in shortestPath(). * * [ - * 'foreignKey' => 'tenant_id', + * 'localColumn' => 'tenant_id', * 'foreignTable' => 'tenants', - * 'foreignId' => 'id', + * 'foreignColumn' => 'id', * 'comment' => 'no-rls', // Used to explicitly enable/disable RLS or to create a comment constraint (internal metadata) * 'nullable' => false, // Used to determine if the constraint is nullable (internal metadata) * ]. @@ -229,9 +229,9 @@ class TableRLSManager implements RLSPolicyManager bool $nullable ): array { return [ - 'foreignKey' => $foreignKey, + 'localColumn' => $foreignKey, 'foreignTable' => $foreignTable, - 'foreignId' => $foreignId, + 'foreignColumn' => $foreignId, // Internal metadata omitted in shortestPaths() 'comment' => $comment, 'nullable' => $nullable, @@ -362,9 +362,9 @@ class TableRLSManager implements RLSPolicyManager * and that method uses formatConstraint(), which serves as a single source of truth * for our constraint formatting. A formatted constraint looks like this: * [ - * 'foreignKey' => 'tenant_id', + * 'localColumn' => 'tenant_id', * 'foreignTable' => 'tenants', - * 'foreignId' => 'id', + * 'foreignColumn' => 'id', * 'comment' => 'no-rls', * 'nullable' => false * ] @@ -505,9 +505,9 @@ class TableRLSManager implements RLSPolicyManager $sessionTenantKey = config('tenancy.rls.session_variable_name'); foreach ($path as $index => $relation) { - $column = $relation['foreignKey']; + $column = $relation['localColumn']; $table = $relation['foreignTable']; - $foreignKey = $relation['foreignId']; + $foreignKey = $relation['foreignColumn']; $indentation = str_repeat(' ', ($index + 1) * 4); diff --git a/tests/RLS/TableManagerTest.php b/tests/RLS/TableManagerTest.php index ef972f3b..c5c8d254 100644 --- a/tests/RLS/TableManagerTest.php +++ b/tests/RLS/TableManagerTest.php @@ -361,38 +361,38 @@ test('table rls manager generates shortest paths that lead to the tenants table $expectedShortestPaths = [ 'authors' => [ [ - 'foreignKey' => 'tenant_id', + 'localColumn' => 'tenant_id', 'foreignTable' => 'tenants', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], ], 'posts' => [ [ - 'foreignKey' => 'author_id', + 'localColumn' => 'author_id', 'foreignTable' => 'authors', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], [ - 'foreignKey' => 'tenant_id', + 'localColumn' => 'tenant_id', 'foreignTable' => 'tenants', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], ], 'comments' => [ [ - 'foreignKey' => 'post_id', + 'localColumn' => 'post_id', 'foreignTable' => 'posts', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], [ - 'foreignKey' => 'author_id', + 'localColumn' => 'author_id', 'foreignTable' => 'authors', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], [ - 'foreignKey' => 'tenant_id', + 'localColumn' => 'tenant_id', 'foreignTable' => 'tenants', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], ], // When scoping by default is enabled (implicit scoping), @@ -401,25 +401,25 @@ test('table rls manager generates shortest paths that lead to the tenants table // the shortest path leads through post_id. 'ratings' => $scopeByDefault ? [ [ - 'foreignKey' => 'tenant_id', + 'localColumn' => 'tenant_id', 'foreignTable' => 'tenants', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], ] : [ [ - 'foreignKey' => 'post_id', + 'localColumn' => 'post_id', 'foreignTable' => 'posts', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], [ - 'foreignKey' => 'author_id', + 'localColumn' => 'author_id', 'foreignTable' => 'authors', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], [ - 'foreignKey' => 'tenant_id', + 'localColumn' => 'tenant_id', 'foreignTable' => 'tenants', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], ], // Articles table is ignored because it's not related to the tenant table in any way @@ -443,28 +443,28 @@ test('table rls manager generates shortest paths that lead to the tenants table // Non-nullable paths are preferred over nullable paths $expectedShortestPaths['ratings'] = [ [ - 'foreignKey' => 'comment_id', + 'localColumn' => 'comment_id', 'foreignTable' => 'comments', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], [ - 'foreignKey' => 'post_id', + 'localColumn' => 'post_id', 'foreignTable' => 'posts', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], [ // Importantly, the best path goes through authors // since ratings -> posts is nullable, as well as // posts -> tenants directly (without going through // authors first). - 'foreignKey' => 'author_id', + 'localColumn' => 'author_id', 'foreignTable' => 'authors', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], [ - 'foreignKey' => 'tenant_id', + 'localColumn' => 'tenant_id', 'foreignTable' => 'tenants', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], ]; @@ -613,38 +613,38 @@ test('table rls manager generates queries correctly', function() { $paths = [ 'primaries' => [ [ - 'foreignKey' => 'tenant_id', + 'localColumn' => 'tenant_id', 'foreignTable' => 'tenants', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], ], 'secondaries' => [ [ - 'foreignKey' => 'primary_id', + 'localColumn' => 'primary_id', 'foreignTable' => 'primaries', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], [ - 'foreignKey' => 'tenant_id', + 'localColumn' => 'tenant_id', 'foreignTable' => 'tenants', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], ], 'foo' => [ [ - 'foreignKey' => 'secondary_id', + 'localColumn' => 'secondary_id', 'foreignTable' => 'secondaries', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], [ - 'foreignKey' => 'primary_id', + 'localColumn' => 'primary_id', 'foreignTable' => 'primaries', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], [ - 'foreignKey' => 'tenant_id', + 'localColumn' => 'tenant_id', 'foreignTable' => 'tenants', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], ], ]; @@ -782,21 +782,21 @@ test('table manager can generate paths leading through comment constraint column $expectedPaths = [ 'non_constrained_posts' => [ [ - 'foreignKey' => 'author_id', + 'localColumn' => 'author_id', 'foreignTable' => 'non_constrained_users', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], [ - 'foreignKey' => 'tenant_id', + 'localColumn' => 'tenant_id', 'foreignTable' => 'tenants', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], ], 'non_constrained_users' => [ [ - 'foreignKey' => 'tenant_id', + 'localColumn' => 'tenant_id', 'foreignTable' => 'tenants', - 'foreignId' => 'id', + 'foreignColumn' => 'id', ], ], ];