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

Minor shortestPathToTenantsTable improvement

This commit is contained in:
lukinovec 2025-06-23 14:47:07 +02:00
parent 7a16df80aa
commit 8505045e00

View file

@ -223,16 +223,17 @@ class TableRLSManager implements RLSPolicyManager
$hasValidPaths = false; $hasValidPaths = false;
foreach ($constraints as $constraint) { foreach ($constraints as $constraint) {
// Check if the constraint would lead to recursion $foreignTable = $constraint['foreignTable'];
if (in_array($constraint['foreignTable'], $visitedTables)) {
// This constraint leads to a table we've already visited - skip it // Skip constraints that would create loops
if (in_array($foreignTable, $visitedTables)) {
$hasRecursiveRelationships = true; $hasRecursiveRelationships = true;
continue; continue;
} }
// Recursive call // Recursive call
$pathThroughConstraint = $this->shortestPathToTenantsTable( $pathThroughConstraint = $this->shortestPathToTenantsTable(
$constraint['foreignTable'], $foreignTable,
$cachedPaths, $cachedPaths,
$visitedTables $visitedTables
); );
@ -242,18 +243,20 @@ class TableRLSManager implements RLSPolicyManager
continue; continue;
} }
if (! $pathThroughConstraint['dead_end']) { // Skip dead ends
$hasValidPaths = true; if ($pathThroughConstraint['dead_end']) {
continue;
}
// Build the full path with the current constraint as the first step $hasValidPaths = true;
$path = $this->buildPath(steps: array_merge([$constraint], $pathThroughConstraint['steps'])); $path = $this->buildPath(steps: array_merge([$constraint], $pathThroughConstraint['steps']));
if ($this->isPathPreferable($path, $shortestPath)) { if ($this->isPathPreferable($path, $shortestPath)) {
$shortestPath = $path; $shortestPath = $path;
}
} }
} }
// Handle tables with only recursive relationships
if ($hasRecursiveRelationships && ! $hasValidPaths) { if ($hasRecursiveRelationships && ! $hasValidPaths) {
// Don't cache paths that cause recursion - return right away. // Don't cache paths that cause recursion - return right away.
// This allows tables with recursive relationships to be processed again. // This allows tables with recursive relationships to be processed again.
@ -263,13 +266,11 @@ class TableRLSManager implements RLSPolicyManager
// - comments table also has tenant_id which leads to the tenants table (a valid path). // - comments table also has tenant_id which leads to the tenants table (a valid path).
// If the recursive path got cached first, the path leading directly through tenants would never be found. // If the recursive path got cached first, the path leading directly through tenants would never be found.
return $this->buildPath(recursive: true); return $this->buildPath(recursive: true);
} else {
$finalPath = $shortestPath ?: $this->buildPath(deadEnd: true);
} }
$cachedPaths[$table] = $finalPath; $cachedPaths[$table] = $shortestPath ?: $this->buildPath(deadEnd: true);
return $finalPath; return $cachedPaths[$table];
} }
/** /**