Skip to content

Commit 5679019

Browse files
authored
Merge pull request #880 from utopia-php/skip-silent-hooks
Add SQL trigger in getDocument
2 parents 477bae8 + 0d95a66 commit 5679019

3 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/Database/Adapter/SQL.php

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -394,6 +394,8 @@ public function getDocument(Document $collection, string $id, array $queries = [
394394
$sql .= " {$forUpdate}";
395395
}
396396

397+
$sql = $this->trigger(Database::EVENT_DOCUMENT_READ, $sql);
398+
397399
$stmt = $this->getPDO()->prepare($sql);
398400

399401
$stmt->bindValue(':_uid', $id);

src/Database/Database.php

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -732,10 +732,10 @@ public function on(string $event, string $name, ?callable $callback): static
732732
*
733733
* @param string $event
734734
* @param string $name
735-
* @param callable $callback
735+
* @param ?callable $callback
736736
* @return $this
737737
*/
738-
public function before(string $event, string $name, callable $callback): static
738+
public function before(string $event, string $name, ?callable $callback): static
739739
{
740740
$this->adapter->before($event, $name, $callback);
741741

tests/e2e/Adapter/Scopes/CollectionTests.php

Lines changed: 15 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
namespace Tests\E2E\Adapter\Scopes;
44

55
use Exception;
6+
use Utopia\Database\Adapter\SQL;
67
use Utopia\Database\Database;
78
use Utopia\Database\Document;
89
use Utopia\Database\Exception as DatabaseException;
@@ -1670,13 +1671,25 @@ public function testTransformations(): void
16701671
'name' => 'value1',
16711672
]));
16721673

1673-
$database->before(Database::EVENT_DOCUMENT_READ, 'test', function (string $query) {
1674-
return "SELECT 1";
1674+
$database->setMetadata('scope', 'api.users');
1675+
1676+
$capturedSql = '';
1677+
$database->before(Database::EVENT_DOCUMENT_READ, 'test', function (string $sql) use (&$capturedSql) {
1678+
$sql .= ' AND 1=0';
1679+
$capturedSql = $sql;
1680+
return $sql;
16751681
});
16761682

16771683
$result = $database->getDocument('docs', 'doc1');
16781684

16791685
$this->assertTrue($result->isEmpty());
1686+
1687+
if ($database->getAdapter() instanceof SQL) {
1688+
$this->assertStringContainsString('/* scope: api.users */', $capturedSql);
1689+
}
1690+
1691+
$database->before(Database::EVENT_DOCUMENT_READ, 'test', null);
1692+
$database->resetMetadata();
16801693
}
16811694

16821695
public function testSetGlobalCollection(): void

0 commit comments

Comments
 (0)