-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathMirrorTest.php
More file actions
350 lines (289 loc) · 11.2 KB
/
MirrorTest.php
File metadata and controls
350 lines (289 loc) · 11.2 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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
<?php
namespace Tests\E2E\Adapter;
use Redis;
use Utopia\Cache\Adapter\Redis as RedisAdapter;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter\MariaDB;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Exception;
use Utopia\Database\Exception\Authorization;
use Utopia\Database\Exception\Conflict;
use Utopia\Database\Exception\Duplicate;
use Utopia\Database\Exception\Limit;
use Utopia\Database\Exception\Structure;
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
use Utopia\Database\Mirror;
use Utopia\Database\PDO;
class MirrorTest extends Base
{
protected static ?Mirror $database = null;
protected static ?PDO $destinationPdo = null;
protected static ?PDO $sourcePdo = null;
protected static Database $source;
protected static Database $destination;
protected static string $namespace;
/**
* @throws \RedisException
* @throws Exception
*/
protected function getDatabase(bool $fresh = false): Mirror
{
if (!is_null(self::$database) && !$fresh) {
return self::$database;
}
$dbHost = 'mariadb';
$dbPort = '3306';
$dbUser = 'root';
$dbPass = 'password';
$pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, MariaDB::getPDOAttributes());
$redis = new Redis();
$redis->connect('redis');
$redis->flushAll();
$cache = new Cache(new RedisAdapter($redis));
self::$sourcePdo = $pdo;
self::$source = new Database(new MariaDB($pdo), $cache);
$mirrorHost = 'mariadb-mirror';
$mirrorPort = '3306';
$mirrorUser = 'root';
$mirrorPass = 'password';
$mirrorPdo = new PDO("mysql:host={$mirrorHost};port={$mirrorPort};charset=utf8mb4", $mirrorUser, $mirrorPass, MariaDB::getPDOAttributes());
$mirrorRedis = new Redis();
$mirrorRedis->connect('redis-mirror');
$mirrorRedis->flushAll();
$mirrorCache = new Cache(new RedisAdapter($mirrorRedis));
self::$destinationPdo = $mirrorPdo;
self::$destination = new Database(new MariaDB($mirrorPdo), $mirrorCache);
$database = new Mirror(self::$source, self::$destination);
$schemas = [
'utopiaTests',
'schema1',
'schema2',
'sharedTables',
'sharedTablesTenantPerDocument'
];
/**
* Handle cases where the source and destination databases are not in sync because of previous tests
*/
foreach ($schemas as $schema) {
if ($database->getSource()->exists($schema)) {
$database->getSource()->setAuthorization(self::$authorization);
$database->getSource()->setDatabase($schema)->delete();
}
if ($database->getDestination()->exists($schema)) {
$database->getDestination()->setAuthorization(self::$authorization);
$database->getDestination()->setDatabase($schema)->delete();
}
}
$database
->setDatabase('utopiaTests')
->setAuthorization(self::$authorization)
->setNamespace(static::$namespace = 'myapp_' . uniqid());
$database->create();
return self::$database = $database;
}
/**
* @throws Exception
* @throws \RedisException
*/
public function testGetMirrorSource(): void
{
$database = $this->getDatabase();
$source = $database->getSource();
$this->assertInstanceOf(Database::class, $source);
$this->assertEquals(self::$source, $source);
}
/**
* @throws Exception
* @throws \RedisException
*/
public function testGetMirrorDestination(): void
{
$database = $this->getDatabase();
$destination = $database->getDestination();
$this->assertInstanceOf(Database::class, $destination);
$this->assertEquals(self::$destination, $destination);
}
/**
* @throws Limit
* @throws Duplicate
* @throws Exception
* @throws \RedisException
*/
public function testCreateMirroredCollection(): void
{
$database = $this->getDatabase();
$database->createCollection('testCreateMirroredCollection');
// Assert collection exists in both databases
$this->assertFalse($database->getSource()->getCollection('testCreateMirroredCollection')->isEmpty());
$this->assertFalse($database->getDestination()->getCollection('testCreateMirroredCollection')->isEmpty());
}
/**
* @throws Limit
* @throws Duplicate
* @throws \RedisException
* @throws Conflict
* @throws Exception
*/
public function testUpdateMirroredCollection(): void
{
$database = $this->getDatabase();
$database->createCollection('testUpdateMirroredCollection', permissions: [
Permission::read(Role::any()),
]);
$collection = $database->getCollection('testUpdateMirroredCollection');
$database->updateCollection(
'testUpdateMirroredCollection',
[
Permission::read(Role::users()),
],
$collection->getAttribute('documentSecurity')
);
// Asset both databases have updated the collection
$this->assertEquals(
[Permission::read(Role::users())],
$database->getSource()->getCollection('testUpdateMirroredCollection')->getPermissions()
);
$this->assertEquals(
[Permission::read(Role::users())],
$database->getDestination()->getCollection('testUpdateMirroredCollection')->getPermissions()
);
}
public function testDeleteMirroredCollection(): void
{
$database = $this->getDatabase();
$database->createCollection('testDeleteMirroredCollection');
$database->deleteCollection('testDeleteMirroredCollection');
// Assert collection is deleted in both databases
$this->assertTrue($database->getSource()->getCollection('testDeleteMirroredCollection')->isEmpty());
$this->assertTrue($database->getDestination()->getCollection('testDeleteMirroredCollection')->isEmpty());
}
/**
* @throws Authorization
* @throws Duplicate
* @throws \RedisException
* @throws Limit
* @throws Structure
* @throws Exception
*/
public function testCreateMirroredDocument(): void
{
$database = $this->getDatabase();
$database->createCollection('testCreateMirroredDocument', attributes: [
new Document([
'$id' => 'name',
'type' => Database::VAR_STRING,
'required' => true,
'size' => Database::LENGTH_KEY,
]),
], permissions: [
Permission::create(Role::any()),
Permission::read(Role::any()),
], documentSecurity: false);
$document = $database->createDocument('testCreateMirroredDocument', new Document([
'name' => 'Jake',
'$permissions' => []
]));
// Assert document is created in both databases
$this->assertEquals(
$document,
$database->getSource()->getDocument('testCreateMirroredDocument', $document->getId())
);
$this->assertEquals(
$document,
$database->getDestination()->getDocument('testCreateMirroredDocument', $document->getId())
);
}
/**
* @throws Authorization
* @throws Duplicate
* @throws \RedisException
* @throws Conflict
* @throws Limit
* @throws Structure
* @throws Exception
*/
public function testUpdateMirroredDocument(): void
{
$database = $this->getDatabase();
$database->createCollection('testUpdateMirroredDocument', attributes: [
new Document([
'$id' => 'name',
'type' => Database::VAR_STRING,
'required' => true,
'size' => Database::LENGTH_KEY,
]),
], permissions: [
Permission::create(Role::any()),
Permission::read(Role::any()),
Permission::update(Role::any()),
], documentSecurity: false);
$document = $database->createDocument('testUpdateMirroredDocument', new Document([
'name' => 'Jake',
'$permissions' => []
]));
$document = $database->updateDocument(
'testUpdateMirroredDocument',
$document->getId(),
$document->setAttribute('name', 'John')
);
// Assert document is updated in both databases
$this->assertEquals(
$document,
$database->getSource()->getDocument('testUpdateMirroredDocument', $document->getId())
);
$this->assertEquals(
$document,
$database->getDestination()->getDocument('testUpdateMirroredDocument', $document->getId())
);
}
public function testDeleteMirroredDocument(): void
{
$database = $this->getDatabase();
$database->createCollection('testDeleteMirroredDocument', attributes: [
new Document([
'$id' => 'name',
'type' => Database::VAR_STRING,
'required' => true,
'size' => Database::LENGTH_KEY,
]),
], permissions: [
Permission::create(Role::any()),
Permission::read(Role::any()),
Permission::delete(Role::any()),
], documentSecurity: false);
$document = $database->createDocument('testDeleteMirroredDocument', new Document([
'name' => 'Jake',
'$permissions' => []
]));
$database->deleteDocument('testDeleteMirroredDocument', $document->getId());
// Assert document is deleted in both databases
$this->assertTrue($database->getSource()->getDocument('testDeleteMirroredDocument', $document->getId())->isEmpty());
$this->assertTrue($database->getDestination()->getDocument('testDeleteMirroredDocument', $document->getId())->isEmpty());
}
protected function getPDO(): mixed
{
return self::$sourcePdo;
}
protected function deleteColumn(string $collection, string $column): bool
{
$sqlTable = "`" . self::$source->getDatabase() . "`.`" . self::$source->getNamespace() . "_" . $collection . "`";
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";
self::$sourcePdo->exec($sql);
$sqlTable = "`" . self::$destination->getDatabase() . "`.`" . self::$destination->getNamespace() . "_" . $collection . "`";
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";
self::$destinationPdo->exec($sql);
return true;
}
protected function deleteIndex(string $collection, string $index): bool
{
$sqlTable = "`" . self::$source->getDatabase() . "`.`" . self::$source->getNamespace() . "_" . $collection . "`";
$sql = "DROP INDEX `{$index}` ON {$sqlTable}";
self::$sourcePdo->exec($sql);
$sqlTable = "`" . self::$destination->getDatabase() . "`.`" . self::$destination->getNamespace() . "_" . $collection . "`";
$sql = "DROP INDEX `{$index}` ON {$sqlTable}";
self::$destinationPdo->exec($sql);
return true;
}
}