-
Notifications
You must be signed in to change notification settings - Fork 55
Throw Auth in count and Sum methods - Remove order queries too #769
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Conversation
WalkthroughThe pull request removes vector-based similarity ordering logic from the SQL adapter's find/count/sum operations and introduces documentSecurity-aware authorization checks to the Database class's find/count/sum methods. Additionally, a test is updated to verify authorization exceptions are thrown for unauthorized access instead of returning empty results. Changes
Estimated code review effort🎯 3 (Moderate) | ⏱️ ~25 minutes
Possibly related PRs
Suggested reviewers
Poem
Pre-merge checks and finishing touches✅ Passed checks (3 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (4)
tests/e2e/Adapter/Scopes/PermissionTests.php (1)
463-468: Tighten exception expectations for clarity and consistencyFunctionally this is fine (it fails if no exception or a non-
AuthorizationExceptionis thrown), but it can be simplified and made consistent with nearby tests that useexpectException.You could either use
expectException:- try { - $database->count($collection->getId()); - $this->fail('Failed to throw exception'); - } catch (\Throwable $th) { - $this->assertInstanceOf(AuthorizationException::class, $th); - } + $this->expectException(AuthorizationException::class); + $database->count($collection->getId());Or, if you prefer the manual pattern, at least narrow the catch to
AuthorizationExceptioninstead of\Throwable.src/Database/Database.php (1)
7965-7970: Auth guard for count/sum now aligned withfind/bulk opsThe new
documentSecurity-aware check incount()andsum()mirrors the pattern used infind(),updateDocuments(), anddeleteDocuments(): if the caller lacks collection-level READ anddocumentSecurityis disabled (and the collection isn’t_metadata), anAuthorizationExceptionis thrown instead of silently returning an empty aggregate. This looks correct and improves consistency across read and aggregate operations.To keep this logic from drifting over time, consider extracting this pattern into a small helper (e.g.
ensureCollectionAuthorized(string $permission): boolreturning$skipAuth), and reuse it infind(),count(), andsum()rather than duplicating the block in multiple methods. This would also let you consistently usegetPermissionsByType()everywhere instead of mixing it withgetRead().Also applies to: 8033-8039
src/Database/Adapter/SQL.php (2)
3197-3204: Top‑level vector queries are now ignored incount(); confirm intended semanticsFiltering
$queriesinto$otherQueriesby excludingQuery::VECTOR_TYPESmeans any top‑level vector query passed tocount()has no effect on the WHERE clause. That’s probably fine (vector queries mainly drive ordering), but it is a silent no‑op.If the intent is to disallow vector usage in
count(), consider either:
- Raising a
DatabaseExceptionwhen a vector query is present, or- At least documenting that vector queries are ignored for
count()to avoid surprises.Given this is behaviour/API‑level, not correctness‑breaking, leaving it as is is acceptable if it matches your expectations.
3279-3284:sum()mirrorscount()vector filtering; consider DRY helper and explicit policy
sum()uses the same pattern of excludingQuery::VECTOR_TYPESinto$otherQueries, so vector queries are likewise ignored when building the aggregation WHERE.Two small follow‑ups you might consider:
- Extract a tiny helper like
filterNonVectorQueries(array $queries): arrayto avoid duplicating this logic betweencount()andsum().- Align and document the policy for vector queries in aggregations (ignored vs. rejected), so callers have clear expectations.
Not blocking, just a clarity/maintainability improvement.
📜 Review details
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (3)
src/Database/Adapter/SQL.php(2 hunks)src/Database/Database.php(2 hunks)tests/e2e/Adapter/Scopes/PermissionTests.php(1 hunks)
🧰 Additional context used
🧠 Learnings (1)
📚 Learning: 2025-10-03T02:04:17.803Z
Learnt from: abnegate
Repo: utopia-php/database PR: 721
File: tests/e2e/Adapter/Scopes/DocumentTests.php:6418-6439
Timestamp: 2025-10-03T02:04:17.803Z
Learning: In tests/e2e/Adapter/Scopes/DocumentTests::testSchemalessDocumentInvalidInteralAttributeValidation (PHP), when the adapter reports getSupportForAttributes() === false (schemaless), the test should not expect exceptions from createDocuments for “invalid” internal attributes; remove try/catch and ensure the test passes without exceptions, keeping at least one assertion.
Applied to files:
tests/e2e/Adapter/Scopes/PermissionTests.php
🧬 Code graph analysis (2)
tests/e2e/Adapter/Scopes/PermissionTests.php (4)
src/Database/Adapter/SQL.php (1)
count(3180-3248)src/Database/Database.php (1)
count(7941-7994)src/Database/Adapter.php (1)
count(849-849)src/Database/Adapter/Mongo.php (1)
count(2095-2182)
src/Database/Adapter/SQL.php (2)
src/Database/Query.php (2)
getMethod(165-168)Query(8-1181)src/Database/Operator.php (1)
getMethod(139-142)
⏰ Context from checks skipped due to timeout of 90000ms. You can increase the timeout in your CodeRabbit configuration to a maximum of 15 minutes (900000ms). (14)
- GitHub Check: Adapter Tests (SharedTables/MySQL)
- GitHub Check: Adapter Tests (SharedTables/SQLite)
- GitHub Check: Adapter Tests (SharedTables/MariaDB)
- GitHub Check: Adapter Tests (SharedTables/MongoDB)
- GitHub Check: Adapter Tests (Schemaless/MongoDB)
- GitHub Check: Adapter Tests (SharedTables/Postgres)
- GitHub Check: Adapter Tests (Mirror)
- GitHub Check: Adapter Tests (Pool)
- GitHub Check: Adapter Tests (MySQL)
- GitHub Check: Adapter Tests (SQLite)
- GitHub Check: Adapter Tests (MariaDB)
- GitHub Check: Adapter Tests (MongoDB)
- GitHub Check: Adapter Tests (Postgres)
- GitHub Check: Unit Test
Summary by CodeRabbit
Bug Fixes
Removed Features
✏️ Tip: You can customize this high-level summary in your review settings.