-
Notifications
You must be signed in to change notification settings - Fork 55
Expand file tree
/
Copy pathSQLiteTest.php
More file actions
82 lines (63 loc) · 2.04 KB
/
SQLiteTest.php
File metadata and controls
82 lines (63 loc) · 2.04 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
<?php
namespace Tests\E2E\Adapter;
use Redis;
use Utopia\Cache\Adapter\Redis as RedisAdapter;
use Utopia\Cache\Cache;
use Utopia\Database\Adapter\SQLite;
use Utopia\Database\Database;
use Utopia\Database\PDO;
class SQLiteTest extends Base
{
public static ?Database $database = null;
protected static ?PDO $pdo = null;
protected static string $namespace;
/**
* @return Database
*/
public function getDatabase(): Database
{
if (!is_null(self::$database)) {
return self::$database;
}
$db = __DIR__."/database.sql";
if (file_exists($db)) {
unlink($db);
}
$dsn = $db;
//$dsn = 'memory'; // Overwrite for fast tests
$pdo = new PDO("sqlite:" . $dsn, null, null, SQLite::getPDOAttributes());
$redis = new Redis();
$redis->connect('redis', 6379);
$redis->flushAll();
$cache = new Cache(new RedisAdapter($redis));
$database = new Database(new SQLite($pdo), $cache);
$database
->setAuthorization(self::$authorization)
->setDatabase('utopiaTests')
->setNamespace(static::$namespace = 'myapp_' . uniqid());
if ($database->exists()) {
$database->delete();
}
$database->create();
self::$pdo = $pdo;
return self::$database = $database;
}
protected function getPDO(): mixed
{
return self::$pdo;
}
protected function deleteColumn(string $collection, string $column): bool
{
$sqlTable = "`" . $this->getDatabase()->getNamespace() . "_" . $collection . "`";
$sql = "ALTER TABLE {$sqlTable} DROP COLUMN `{$column}`";
self::$pdo->exec($sql);
return true;
}
protected function deleteIndex(string $collection, string $index): bool
{
$index = "`".$this->getDatabase()->getNamespace()."_".$this->getDatabase()->getTenant()."_{$collection}_{$index}`";
$sql = "DROP INDEX {$index}";
self::$pdo->exec($sql);
return true;
}
}