-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathPoolTest.php
More file actions
200 lines (170 loc) · 6.74 KB
/
PoolTest.php
File metadata and controls
200 lines (170 loc) · 6.74 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<?php
namespace Tests\E2E\Adapter;
use Redis;
use ReflectionClass;
use Utopia\Cache\Adapter\Redis as RedisAdapter;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter;
use Utopia\Database\Adapter\MySQL;
use Utopia\Database\Adapter\Pool;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Exception;
use Utopia\Database\Exception\Duplicate;
use Utopia\Database\Exception\Limit;
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
use Utopia\Database\PDO;
use Utopia\Pools\Adapter\Stack;
use Utopia\Pools\Pool as UtopiaPool;
class PoolTest extends Base
{
public static ?Database $database = null;
/**
* @var UtopiaPool<MySQL>
*/
protected static UtopiaPool $pool;
protected static string $namespace;
/**
* @return Database
* @throws Exception
* @throws Duplicate
* @throws Limit
*/
public function getDatabase(): Database
{
if (!is_null(self::$database)) {
return self::$database;
}
$redis = new Redis();
$redis->connect('redis', 6379);
$redis->flushAll();
$cache = new Cache(new RedisAdapter($redis));
$pool = new UtopiaPool(new Stack(), 'mysql', 10, function () {
$dbHost = 'mysql';
$dbPort = '3307';
$dbUser = 'root';
$dbPass = 'password';
return new MySQL(new PDO(
dsn: "mysql:host={$dbHost};port={$dbPort};charset=utf8mb4",
username: $dbUser,
password: $dbPass,
config: MySQL::getPDOAttributes(),
));
});
$database = new Database(new Pool($pool), $cache);
$database
->setAuthorization(self::$authorization)
->setDatabase('utopiaTests')
->setNamespace(static::$namespace = 'myapp_' . uniqid());
if ($database->exists()) {
$database->delete();
}
$database->create();
self::$pool = $pool;
return self::$database = $database;
}
protected function deleteColumn(string $collection, string $column): bool
{
$sqlTable = "`" . $this->getDatabase()->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`";
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";
self::$pool->use(function (Adapter $adapter) use ($sql) {
// Hack to get adapter PDO reference
$class = new ReflectionClass($adapter);
$property = $class->getProperty('pdo');
$property->setAccessible(true);
$pdo = $property->getValue($adapter);
$pdo->exec($sql);
});
return true;
}
protected function deleteIndex(string $collection, string $index): bool
{
$sqlTable = "`" . $this->getDatabase()->getDatabase() . "`.`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`";
$sql = "DROP INDEX `{$index}` ON {$sqlTable}";
self::$pool->use(function (Adapter $adapter) use ($sql) {
// Hack to get adapter PDO reference
$class = new ReflectionClass($adapter);
$property = $class->getProperty('pdo');
$property->setAccessible(true);
$pdo = $property->getValue($adapter);
$pdo->exec($sql);
});
return true;
}
/**
* Execute raw SQL via the pool using reflection to access the adapter's PDO.
*
* @param string $sql
* @param array<string, mixed> $binds
*/
private function execRawSQL(string $sql, array $binds = []): void
{
self::$pool->use(function (Adapter $adapter) use ($sql, $binds) {
$class = new ReflectionClass($adapter);
$property = $class->getProperty('pdo');
$property->setAccessible(true);
$pdo = $property->getValue($adapter);
$stmt = $pdo->prepare($sql);
foreach ($binds as $key => $value) {
$stmt->bindValue($key, $value);
}
$stmt->execute();
});
}
/**
* Test that orphaned permission records from a previous failed delete
* don't block document recreation. The createDocument method should
* clean up orphaned perms and retry.
*/
public function testOrphanedPermissionsRecovery(): void
{
$database = $this->getDatabase();
$collection = 'orphanedPermsRecovery';
$database->createCollection($collection);
$database->createAttribute($collection, 'title', Database::VAR_STRING, 128, true);
// Step 1: Create a document with permissions
$doc = $database->createDocument($collection, new Document([
'$id' => 'orphan_test',
'$permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
Permission::delete(Role::any()),
],
'title' => 'Original',
]));
$this->assertEquals('orphan_test', $doc->getId());
// Step 2: Delete the document normally (cleans up both doc and perms)
$database->deleteDocument($collection, 'orphan_test');
$deleted = $database->getDocument($collection, 'orphan_test');
$this->assertTrue($deleted->isEmpty());
// Step 3: Manually re-insert orphaned permission rows (simulating a partial delete failure)
$namespace = $this->getDatabase()->getNamespace();
$dbName = $this->getDatabase()->getDatabase();
$permsTable = "`{$dbName}`.`{$namespace}_{$collection}_perms`";
$this->execRawSQL(
"INSERT INTO {$permsTable} (_type, _permission, _document) VALUES (:type, :perm, :doc)",
[':type' => 'read', ':perm' => 'any', ':doc' => 'orphan_test']
);
$this->execRawSQL(
"INSERT INTO {$permsTable} (_type, _permission, _document) VALUES (:type, :perm, :doc)",
[':type' => 'update', ':perm' => 'any', ':doc' => 'orphan_test']
);
// Step 4: Recreate a document with the same ID - should succeed by cleaning up orphaned perms
$newDoc = $database->createDocument($collection, new Document([
'$id' => 'orphan_test',
'$permissions' => [
Permission::read(Role::any()),
Permission::update(Role::any()),
],
'title' => 'Recreated',
]));
$this->assertEquals('orphan_test', $newDoc->getId());
$this->assertEquals('Recreated', $newDoc->getAttribute('title'));
// Verify the document can be fetched
$found = $database->getDocument($collection, 'orphan_test');
$this->assertFalse($found->isEmpty());
$this->assertEquals('Recreated', $found->getAttribute('title'));
$database->deleteCollection($collection);
}
}