Skip to content

Commit 1e3f40b

Browse files
authored
Merge pull request #724 from utopia-php/feat-relationship-updates
Feat relationship updates
2 parents eaffdc1 + 7e39c67 commit 1e3f40b

File tree

14 files changed

+3297
-623
lines changed

14 files changed

+3297
-623
lines changed

bin/cli.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
require_once '/usr/src/code/vendor/autoload.php';
44

55
use Utopia\CLI\CLI;
6+
use Utopia\CLI\Console;
67

78
ini_set('memory_limit', '-1');
89

@@ -11,5 +12,13 @@
1112
include 'tasks/load.php';
1213
include 'tasks/index.php';
1314
include 'tasks/query.php';
15+
include 'tasks/relationships.php';
16+
17+
$cli
18+
->error()
19+
->inject('error')
20+
->action(function ($error) {
21+
Console::error($error->getMessage());
22+
});
1423

1524
$cli->run();

bin/relationships

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
php /usr/src/code/bin/cli.php relationships "$@"

bin/tasks/index.php

Lines changed: 59 additions & 61 deletions
Original file line numberDiff line numberDiff line change
@@ -10,105 +10,103 @@
1010
use Utopia\CLI\CLI;
1111
use Utopia\CLI\Console;
1212
use Utopia\Database\Adapter\MariaDB;
13-
use Utopia\Database\Adapter\Mongo;
1413
use Utopia\Database\Adapter\MySQL;
14+
use Utopia\Database\Adapter\Postgres;
1515
use Utopia\Database\Database;
1616
use Utopia\Database\PDO;
17-
use Utopia\Mongo\Client;
17+
use Utopia\Validator\Boolean;
1818
use Utopia\Validator\Text;
1919

2020
/**
2121
* @Example
2222
* docker compose exec tests bin/index --adapter=mysql --name=testing
2323
*/
24-
2524
$cli
2625
->task('index')
2726
->desc('Index mock data for testing queries')
2827
->param('adapter', '', new Text(0), 'Database adapter')
2928
->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) {
3131
$namespace = '_ns';
3232
$cache = new Cache(new NoCache());
3333

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+
}
6668

67-
$database = new Database(new MySQL($pdo), $cache);
68-
break;
69+
$cfg = $dbAdapters[$adapter];
6970

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+
);
7477

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);
7782

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'");
7984
$start = microtime(true);
8085
$database->createIndex('articles', 'createdGenre', Database::INDEX_KEY, ['created', 'genre'], [], [Database::ORDER_DESC, Database::ORDER_DESC]);
8186
$time = microtime(true) - $start;
82-
Console::success("{$time} seconds");
87+
Console::success("Index 'createdGenre' created in {$time} seconds");
8388

84-
Console::info("equal('genre', ['fashion', 'finance', 'sports'])");
89+
Console::info("Creating key index 'genre' on 'articles' for genres: fashion, finance, sports");
8590
$start = microtime(true);
8691
$database->createIndex('articles', 'genre', Database::INDEX_KEY, ['genre'], [], [Database::ORDER_ASC]);
8792
$time = microtime(true) - $start;
88-
Console::success("{$time} seconds");
93+
Console::success("Index 'genre' created in {$time} seconds");
8994

90-
Console::info("greaterThan('views', 100000)");
95+
Console::info("Creating key index 'views' on 'articles' for views > 100000");
9196
$start = microtime(true);
9297
$database->createIndex('articles', 'views', Database::INDEX_KEY, ['views'], [], [Database::ORDER_DESC]);
9398
$time = microtime(true) - $start;
94-
Console::success("{$time} seconds");
99+
Console::success("Index 'views' created in {$time} seconds");
95100

96-
Console::info("search('text', 'Alice')");
101+
Console::info("Creating fulltext index 'fulltextsearch' on 'articles' for search term 'Alice'");
97102
$start = microtime(true);
98103
$database->createIndex('articles', 'fulltextsearch', Database::INDEX_FULLTEXT, ['text']);
99104
$time = microtime(true) - $start;
100-
Console::success("{$time} seconds");
105+
Console::success("Index 'fulltextsearch' created in {$time} seconds");
101106

102-
Console::info("contains('tags', ['tag1'])");
107+
Console::info("Creating key index 'tags' on 'articles' for tags containing 'tag1'");
103108
$start = microtime(true);
104109
$database->createIndex('articles', 'tags', Database::INDEX_KEY, ['tags']);
105110
$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");
114112
});

0 commit comments

Comments
 (0)