1
0
Fork 0
mirror of synced 2024-05-20 04:32:37 +12:00

Set default database for collections

This commit is contained in:
kodumbeats 2021-12-22 20:51:49 -05:00
parent 166ebf7225
commit bc2703ea08
7 changed files with 41 additions and 27 deletions

View file

@ -78,14 +78,14 @@ $http->on('start', function (Server $http) use ($payloadSize, $register) {
$dbForConsole = $app->getResource('dbForConsole'); /** @var Utopia\Database\Database $dbForConsole */
if(!$dbForConsole->exists()) {
// if(!$dbForConsole->exists('appwrite')) {
Console::success('[Setup] - Server database init started...');
$collections = Config::getParam('collections', []); /** @var array $collections */
$redis->flushAll();
$dbForConsole->create();
$dbForConsole->create('appwrite');
$audit = new Audit($dbForConsole);
$audit->setup();
@ -125,7 +125,7 @@ $http->on('start', function (Server $http) use ($payloadSize, $register) {
}
Console::success('[Setup] - Server database init completed...');
}
// }
});
Console::success('Server started successfully (max payload is '.number_format($payloadSize).' bytes)');

View file

@ -785,7 +785,8 @@ App::setResource('dbForInternal', function($db, $cache, $project) {
$cache = new Cache(new RedisCache($cache));
$database = new Database(new MariaDB($db), $cache);
$database->setNamespace('project_'.$project->getId().'_internal');
$database->setDefaultDatabase('appwrite');
$database->setNamespace('_project_'.$project->getId());
return $database;
}, ['db', 'cache', 'project']);
@ -794,7 +795,8 @@ App::setResource('dbForExternal', function($db, $cache, $project) {
$cache = new Cache(new RedisCache($cache));
$database = new Database(new MariaDB($db), $cache);
$database->setNamespace('project_'.$project->getId().'_external');
$database->setDefaultDatabase('appwrite');
$database->setNamespace('project_'.$project->getId());
return $database;
}, ['db', 'cache', 'project']);
@ -803,7 +805,8 @@ App::setResource('dbForConsole', function($db, $cache) {
$cache = new Cache(new RedisCache($cache));
$database = new Database(new MariaDB($db), $cache);
$database->setNamespace('project_console_internal');
$database->setDefaultDatabase('appwrite');
$database->setNamespace('_console');
return $database;
}, ['db', 'cache']);

View file

@ -58,6 +58,7 @@ function getDatabase(Registry &$register, string $namespace)
$cache = new Cache(new RedisCache($redis));
$database = new Database(new MariaDB($db), $cache);
$database->setDefaultDatabase('appwrite');
$database->setNamespace($namespace);
return [
@ -77,7 +78,7 @@ $server->onStart(function () use ($stats, $register, $containerId, &$statsDocume
*/
go(function () use ($register, $containerId, &$statsDocument) {
try {
[$database, $returnDatabase] = getDatabase($register, 'project_console_internal');
[$database, $returnDatabase] = getDatabase($register, '_console');
$document = new Document([
'$id' => $database->getId(),
'$collection' => 'realtime',
@ -133,7 +134,7 @@ $server->onStart(function () use ($stats, $register, $containerId, &$statsDocume
}
try {
[$database, $returnDatabase] = getDatabase($register, 'project_console_internal');
[$database, $returnDatabase] = getDatabase($register, '_console');
$statsDocument
->setAttribute('timestamp', time())
@ -163,7 +164,7 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats,
*/
if ($realtime->hasSubscriber('console', 'role:member', 'project')) {
[$database, $returnDatabase] = getDatabase($register, 'project_console_internal');
[$database, $returnDatabase] = getDatabase($register, '_console');
$payload = [];
@ -267,7 +268,7 @@ $server->onWorkerStart(function (int $workerId) use ($server, $register, $stats,
return;
}
[$database, $returnDatabase] = getDatabase($register, 'project_' . $projectId . '_internal');
[$database, $returnDatabase] = getDatabase($register, 'project_' . $projectId);
$user = $database->getDocument('users', $userId);

View file

@ -45,7 +45,7 @@
"utopia-php/cache": "0.4.*",
"utopia-php/cli": "0.11.*",
"utopia-php/config": "0.2.*",
"utopia-php/database": "0.12.*",
"utopia-php/database": "dev-feat-database-and-namespace as 0.13.0",
"utopia-php/locale": "0.4.*",
"utopia-php/orchestration": "0.2.*",
"utopia-php/registry": "0.5.*",

27
composer.lock generated
View file

@ -4,7 +4,7 @@
"Read more about it at https://getcomposer.org/doc/01-basic-usage.md#installing-dependencies",
"This file is @generated automatically"
],
"content-hash": "c755b0ae991777da3e44b0442690fa46",
"content-hash": "f546dc6dcc6af7ccdb62435aed4fc0ca",
"packages": [
{
"name": "adhocore/jwt",
@ -2138,16 +2138,16 @@
},
{
"name": "utopia-php/database",
"version": "0.12.1",
"version": "dev-feat-database-and-namespace",
"source": {
"type": "git",
"url": "https://github.com/utopia-php/database.git",
"reference": "af512b7a00cc7c6e30fa03efbc5fd7e77a93e2df"
"reference": "dce7c47b51cd5b4ccbdf87e192be13b22c8a25e1"
},
"dist": {
"type": "zip",
"url": "https://api.github.com/repos/utopia-php/database/zipball/af512b7a00cc7c6e30fa03efbc5fd7e77a93e2df",
"reference": "af512b7a00cc7c6e30fa03efbc5fd7e77a93e2df",
"url": "https://api.github.com/repos/utopia-php/database/zipball/dce7c47b51cd5b4ccbdf87e192be13b22c8a25e1",
"reference": "dce7c47b51cd5b4ccbdf87e192be13b22c8a25e1",
"shasum": ""
},
"require": {
@ -2195,9 +2195,9 @@
],
"support": {
"issues": "https://github.com/utopia-php/database/issues",
"source": "https://github.com/utopia-php/database/tree/0.12.1"
"source": "https://github.com/utopia-php/database/tree/feat-database-and-namespace"
},
"time": "2021-12-13T14:57:32+00:00"
"time": "2021-12-22T20:55:21+00:00"
},
{
"name": "utopia-php/domains",
@ -6496,9 +6496,18 @@
"time": "2015-12-17T08:42:14+00:00"
}
],
"aliases": [],
"aliases": [
{
"package": "utopia-php/database",
"version": "dev-feat-database-and-namespace",
"alias": "0.13.0",
"alias_normalized": "0.13.0.0"
}
],
"minimum-stability": "stable",
"stability-flags": [],
"stability-flags": {
"utopia-php/database": 20
},
"prefer-stable": false,
"prefer-lowest": false,
"platform": {

View file

@ -71,7 +71,7 @@ services:
- ./psalm.xml:/usr/src/code/psalm.xml
- ./tests:/usr/src/code/tests
- ./app:/usr/src/code/app
# - ./vendor:/usr/src/code/vendor
- ./vendor:/usr/src/code/vendor
- ./docs:/usr/src/code/docs
- ./src:/usr/src/code/src
# - ./debug:/tmp
@ -441,7 +441,7 @@ services:
- _APP_REDIS_PASS
mariadb:
image: appwrite/mariadb:1.2.0 # fix issues when upgrading using: mysql_upgrade -u root -p
image: mariadb:10.7 # fix issues when upgrading using: mysql_upgrade -u root -p
container_name: appwrite-mariadb
networks:
- appwrite

View file

@ -86,16 +86,16 @@ abstract class Worker
if (!$projectId) {
throw new \Exception('ProjectID not provided - cannot get database');
}
$namespace = "project_{$projectId}_internal";
$namespace = "project_{$projectId}";
break;
case self::DATABASE_EXTERNAL:
if (!$projectId) {
throw new \Exception('ProjectID not provided - cannot get database');
}
$namespace = "project_{$projectId}_external";
$namespace = "project_{$projectId}";
break;
case self::DATABASE_CONSOLE:
$namespace = "project_console_internal";
$namespace = "_console";
$sleep = 5; // ConsoleDB needs extra sleep time to ensure tables are created
break;
default:
@ -110,9 +110,10 @@ abstract class Worker
$attempts++;
$cache = new Cache(new RedisCache($register->get('cache')));
$database = new Database(new MariaDB($register->get('db')), $cache);
$database->setDefaultDatabase('appwrite');
$database->setNamespace($namespace); // Main DB
if (!$database->exists()) {
throw new \Exception("Table does not exist: {$database->getNamespace()}");
if (!empty($projectId) && !$database->getDocument('projects', $projectId)->isEmpty()) {
throw new \Exception("Project does not exist: {$projectId}");
}
break; // leave loop if successful
} catch(\Exception $e) {