|
10 | 10 | use Utopia\CLI\CLI; |
11 | 11 | use Utopia\CLI\Console; |
12 | 12 | use Utopia\Database\Adapter\MariaDB; |
13 | | -use Utopia\Database\Adapter\Mongo; |
14 | 13 | use Utopia\Database\Adapter\MySQL; |
| 14 | +use Utopia\Database\Adapter\Postgres; |
15 | 15 | use Utopia\Database\Database; |
16 | 16 | use Utopia\Database\PDO; |
17 | | -use Utopia\Mongo\Client; |
| 17 | +use Utopia\Validator\Boolean; |
18 | 18 | use Utopia\Validator\Text; |
19 | 19 |
|
20 | 20 | /** |
21 | 21 | * @Example |
22 | 22 | * docker compose exec tests bin/index --adapter=mysql --name=testing |
23 | 23 | */ |
24 | | - |
25 | 24 | $cli |
26 | 25 | ->task('index') |
27 | 26 | ->desc('Index mock data for testing queries') |
28 | 27 | ->param('adapter', '', new Text(0), 'Database adapter') |
29 | 28 | ->param('name', '', new Text(0), 'Name of created database.') |
30 | | - ->action(function ($adapter, $name) { |
| 29 | + ->param('sharedTables', false, new Boolean(true), 'Whether to use shared tables', true) |
| 30 | + ->action(function (string $adapter, string $name, bool $sharedTables) { |
31 | 31 | $namespace = '_ns'; |
32 | 32 | $cache = new Cache(new NoCache()); |
33 | 33 |
|
34 | | - switch ($adapter) { |
35 | | - case 'mongodb': |
36 | | - $client = new Client( |
37 | | - $name, |
38 | | - 'mongo', |
39 | | - 27017, |
40 | | - 'root', |
41 | | - 'example', |
42 | | - false |
43 | | - ); |
44 | | - |
45 | | - $database = new Database(new Mongo($client), $cache); |
46 | | - break; |
47 | | - |
48 | | - case 'mariadb': |
49 | | - $dbHost = 'mariadb'; |
50 | | - $dbPort = '3306'; |
51 | | - $dbUser = 'root'; |
52 | | - $dbPass = 'password'; |
53 | | - |
54 | | - $pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, MariaDB::getPDOAttributes()); |
55 | | - |
56 | | - $database = new Database(new MariaDB($pdo), $cache); |
57 | | - break; |
58 | | - |
59 | | - case 'mysql': |
60 | | - $dbHost = 'mysql'; |
61 | | - $dbPort = '3307'; |
62 | | - $dbUser = 'root'; |
63 | | - $dbPass = 'password'; |
64 | | - |
65 | | - $pdo = new PDO("mysql:host={$dbHost};port={$dbPort};charset=utf8mb4", $dbUser, $dbPass, MySQL::getPDOAttributes()); |
| 34 | + $dbAdapters = [ |
| 35 | + 'mariadb' => [ |
| 36 | + 'host' => 'mariadb', |
| 37 | + 'port' => 3306, |
| 38 | + 'user' => 'root', |
| 39 | + 'pass' => 'password', |
| 40 | + 'dsn' => static fn (string $host, int $port) => "mysql:host={$host};port={$port};charset=utf8mb4", |
| 41 | + 'adapter' => MariaDB::class, |
| 42 | + 'pdoAttr' => MariaDB::getPDOAttributes(), |
| 43 | + ], |
| 44 | + 'mysql' => [ |
| 45 | + 'host' => 'mysql', |
| 46 | + 'port' => 3307, |
| 47 | + 'user' => 'root', |
| 48 | + 'pass' => 'password', |
| 49 | + 'dsn' => static fn (string $host, int $port) => "mysql:host={$host};port={$port};charset=utf8mb4", |
| 50 | + 'adapter' => MySQL::class, |
| 51 | + 'pdoAttr' => MySQL::getPDOAttributes(), |
| 52 | + ], |
| 53 | + 'postgres' => [ |
| 54 | + 'host' => 'postgres', |
| 55 | + 'port' => 5432, |
| 56 | + 'user' => 'postgres', |
| 57 | + 'pass' => 'password', |
| 58 | + 'dsn' => static fn (string $host, int $port) => "pgsql:host={$host};port={$port}", |
| 59 | + 'adapter' => Postgres::class, |
| 60 | + 'pdoAttr' => Postgres::getPDOAttributes(), |
| 61 | + ], |
| 62 | + ]; |
| 63 | + |
| 64 | + if (!isset($dbAdapters[$adapter])) { |
| 65 | + Console::error("Adapter '{$adapter}' not supported"); |
| 66 | + return; |
| 67 | + } |
66 | 68 |
|
67 | | - $database = new Database(new MySQL($pdo), $cache); |
68 | | - break; |
| 69 | + $cfg = $dbAdapters[$adapter]; |
69 | 70 |
|
70 | | - default: |
71 | | - Console::error('Adapter not supported'); |
72 | | - return; |
73 | | - } |
| 71 | + $pdo = new PDO( |
| 72 | + ($cfg['dsn'])($cfg['host'], $cfg['port']), |
| 73 | + $cfg['user'], |
| 74 | + $cfg['pass'], |
| 75 | + $cfg['pdoAttr'] |
| 76 | + ); |
74 | 77 |
|
75 | | - $database->setDatabase($name); |
76 | | - $database->setNamespace($namespace); |
| 78 | + $database = (new Database(new ($cfg['adapter'])($pdo), $cache)) |
| 79 | + ->setDatabase($name) |
| 80 | + ->setNamespace($namespace) |
| 81 | + ->setSharedTables($sharedTables); |
77 | 82 |
|
78 | | - Console::info("greaterThan('created', ['2010-01-01 05:00:00']), equal('genre', ['travel'])"); |
| 83 | + Console::info("Creating key index 'createdGenre' on 'articles' for created > '2010-01-01 05:00:00' and genre = 'travel'"); |
79 | 84 | $start = microtime(true); |
80 | 85 | $database->createIndex('articles', 'createdGenre', Database::INDEX_KEY, ['created', 'genre'], [], [Database::ORDER_DESC, Database::ORDER_DESC]); |
81 | 86 | $time = microtime(true) - $start; |
82 | | - Console::success("{$time} seconds"); |
| 87 | + Console::success("Index 'createdGenre' created in {$time} seconds"); |
83 | 88 |
|
84 | | - Console::info("equal('genre', ['fashion', 'finance', 'sports'])"); |
| 89 | + Console::info("Creating key index 'genre' on 'articles' for genres: fashion, finance, sports"); |
85 | 90 | $start = microtime(true); |
86 | 91 | $database->createIndex('articles', 'genre', Database::INDEX_KEY, ['genre'], [], [Database::ORDER_ASC]); |
87 | 92 | $time = microtime(true) - $start; |
88 | | - Console::success("{$time} seconds"); |
| 93 | + Console::success("Index 'genre' created in {$time} seconds"); |
89 | 94 |
|
90 | | - Console::info("greaterThan('views', 100000)"); |
| 95 | + Console::info("Creating key index 'views' on 'articles' for views > 100000"); |
91 | 96 | $start = microtime(true); |
92 | 97 | $database->createIndex('articles', 'views', Database::INDEX_KEY, ['views'], [], [Database::ORDER_DESC]); |
93 | 98 | $time = microtime(true) - $start; |
94 | | - Console::success("{$time} seconds"); |
| 99 | + Console::success("Index 'views' created in {$time} seconds"); |
95 | 100 |
|
96 | | - Console::info("search('text', 'Alice')"); |
| 101 | + Console::info("Creating fulltext index 'fulltextsearch' on 'articles' for search term 'Alice'"); |
97 | 102 | $start = microtime(true); |
98 | 103 | $database->createIndex('articles', 'fulltextsearch', Database::INDEX_FULLTEXT, ['text']); |
99 | 104 | $time = microtime(true) - $start; |
100 | | - Console::success("{$time} seconds"); |
| 105 | + Console::success("Index 'fulltextsearch' created in {$time} seconds"); |
101 | 106 |
|
102 | | - Console::info("contains('tags', ['tag1'])"); |
| 107 | + Console::info("Creating key index 'tags' on 'articles' for tags containing 'tag1'"); |
103 | 108 | $start = microtime(true); |
104 | 109 | $database->createIndex('articles', 'tags', Database::INDEX_KEY, ['tags']); |
105 | 110 | $time = microtime(true) - $start; |
106 | | - Console::success("{$time} seconds"); |
107 | | - }); |
108 | | - |
109 | | -$cli |
110 | | - ->error() |
111 | | - ->inject('error') |
112 | | - ->action(function (Exception $error) { |
113 | | - Console::error($error->getMessage()); |
| 111 | + Console::success("Index 'tags' created in {$time} seconds"); |
114 | 112 | }); |
0 commit comments