1
0
Fork 0
mirror of synced 2024-07-06 23:21:05 +12:00

feat: update db pools

This commit is contained in:
Christy Jacob 2022-08-13 13:27:04 +05:30
parent 4521907b83
commit d2bf8b25de
11 changed files with 228 additions and 213 deletions

View file

@ -65,9 +65,9 @@ App::get('/v1/health/db')
try { try {
$dbPool = $utopia->getResource('dbPool'); $dbPool = $utopia->getResource('dbPool');
$name = $dbPool->getConsoleDB(); $database = $dbPool->getConsoleDB();
/* @var $consoleDB PDO */ /* @var $consoleDB PDO */
$consoleDB = $dbPool->getPDO($name); $consoleDB = $dbPool->getPDO($database);
// Run a small test to check the connection // Run a small test to check the connection
$statement = $consoleDB->prepare("SELECT 1;"); $statement = $consoleDB->prepare("SELECT 1;");

View file

@ -328,26 +328,22 @@ $server->onOpen(function (int $connection, SwooleRequest $request) use ($server,
$request = new Request($request); $request = new Request($request);
$response = new Response(new SwooleResponse()); $response = new Response(new SwooleResponse());
App::setResource('request', fn() => $request);
App::setResource('response', fn() => $response);
/** @var Redis $redis */
$redis = $register->get('redisPool')->get();
App::setResource('cache', fn() => $redis);
/** @var PDO $db */ /** @var PDO $db */
$dbPool = $register->get('dbPool'); $dbPool = $register->get('dbPool');
App::setResource('dbPool', fn() => $dbPool); /** @var Redis $redis */
$redis = $register->get('redisPool')->get();
Console::info("Connection open (user: {$connection})"); Console::info("Connection open (user: {$connection})");
App::setResource('dbPool', fn() => $dbPool);
App::setResource('cache', fn() => $redis);
App::setResource('request', fn() => $request);
App::setResource('response', fn() => $response);
try { try {
/** @var \Utopia\Database\Document $console */ /** @var \Utopia\Database\Document $console */
$console = $app->getResource('console'); $console = $app->getResource('console');
$dbForConsole = $dbPool->getDBFromPool('console', $redis);
App::setResource('dbForConsole', fn() => $dbForConsole);
/** @var \Utopia\Database\Document $project */ /** @var \Utopia\Database\Document $project */
$project = $app->getResource('project'); $project = $app->getResource('project');
@ -358,8 +354,8 @@ $server->onOpen(function (int $connection, SwooleRequest $request) use ($server,
throw new Exception('Missing or unknown project ID', 1008); throw new Exception('Missing or unknown project ID', 1008);
} }
$dbForProject = $dbPool->getDBFromPool($project->getId(), $redis); $dbForProject = $app->getResource('dbForProject');
App::setResource('dbForProject', fn() => $dbForProject);
/** @var \Utopia\Database\Document $user */ /** @var \Utopia\Database\Document $user */
$user = $app->getResource('user'); $user = $app->getResource('user');

View file

@ -37,7 +37,7 @@ class AuditsV1 extends Worker
$userName = $user->getAttribute('name', ''); $userName = $user->getAttribute('name', '');
$userEmail = $user->getAttribute('email', ''); $userEmail = $user->getAttribute('email', '');
$dbForProject = $this->getProjectDB($project->getAttribute('database', '')); $dbForProject = $this->getProjectDB($project);
$audit = new Audit($dbForProject); $audit = new Audit($dbForProject);
$audit->log( $audit->log(
userId: $user->getId(), userId: $user->getId(),

View file

@ -55,7 +55,7 @@ class BuildsV1 extends Worker
protected function buildDeployment(Document $project, Document $function, Document $deployment) protected function buildDeployment(Document $project, Document $function, Document $deployment)
{ {
$dbForProject = $this->getProjectDB($project->getAttribute('database', '')); $dbForProject = $this->getProjectDB($project);
$function = $dbForProject->getDocument('functions', $function->getId()); $function = $dbForProject->getDocument('functions', $function->getId());
if ($function->isEmpty()) { if ($function->isEmpty()) {

View file

@ -25,8 +25,6 @@ class DatabaseV1 extends Worker
$document = new Document($this->args['document'] ?? []); $document = new Document($this->args['document'] ?? []);
$database = new Document($this->args['database'] ?? []); $database = new Document($this->args['database'] ?? []);
var_dump($project);
if ($collection->isEmpty()) { if ($collection->isEmpty()) {
throw new Exception('Missing collection'); throw new Exception('Missing collection');
} }
@ -37,16 +35,16 @@ class DatabaseV1 extends Worker
switch (strval($type)) { switch (strval($type)) {
case DATABASE_TYPE_CREATE_ATTRIBUTE: case DATABASE_TYPE_CREATE_ATTRIBUTE:
$this->createAttribute($database, $collection, $document, $project->getId()); $this->createAttribute($database, $collection, $document, $project);
break; break;
case DATABASE_TYPE_DELETE_ATTRIBUTE: case DATABASE_TYPE_DELETE_ATTRIBUTE:
$this->deleteAttribute($database, $collection, $document, $project->getId()); $this->deleteAttribute($database, $collection, $document, $project);
break; break;
case DATABASE_TYPE_CREATE_INDEX: case DATABASE_TYPE_CREATE_INDEX:
$this->createIndex($database, $collection, $document, $project->getId()); $this->createIndex($database, $collection, $document, $project);
break; break;
case DATABASE_TYPE_DELETE_INDEX: case DATABASE_TYPE_DELETE_INDEX:
$this->deleteIndex($database, $collection, $document, $project->getId()); $this->deleteIndex($database, $collection, $document, $project);
break; break;
default: default:
@ -69,7 +67,7 @@ class DatabaseV1 extends Worker
{ {
$projectId = $project->getId(); $projectId = $project->getId();
$dbForConsole = $this->getConsoleDB(); $dbForConsole = $this->getConsoleDB();
$dbForProject = $this->getProjectDB($project->getAttribute('database', '')); $dbForProject = $this->getProjectDB($project);
$events = Event::generateEvents('databases.[databaseId].collections.[collectionId].attributes.[attributeId].update', [ $events = Event::generateEvents('databases.[databaseId].collections.[collectionId].attributes.[attributeId].update', [
'databaseId' => $database->getId(), 'databaseId' => $database->getId(),
@ -137,7 +135,7 @@ class DatabaseV1 extends Worker
{ {
$projectId = $project->getId(); $projectId = $project->getId();
$dbForConsole = $this->getConsoleDB(); $dbForConsole = $this->getConsoleDB();
$dbForProject = $this->getProjectDB($project->getAttribute('database', '')); $dbForProject = $this->getProjectDB($project);
$events = Event::generateEvents('databases.[databaseId].collections.[collectionId].attributes.[attributeId].delete', [ $events = Event::generateEvents('databases.[databaseId].collections.[collectionId].attributes.[attributeId].delete', [
'databaseId' => $database->getId(), 'databaseId' => $database->getId(),
@ -229,7 +227,7 @@ class DatabaseV1 extends Worker
} }
if ($exists) { // Delete the duplicate if created, else update in db if ($exists) { // Delete the duplicate if created, else update in db
$this->deleteIndex($database, $collection, $index, $projectId); $this->deleteIndex($database, $collection, $index, $project);
} else { } else {
$dbForProject->updateDocument('indexes', $index->getId(), $index); $dbForProject->updateDocument('indexes', $index->getId(), $index);
} }
@ -251,7 +249,7 @@ class DatabaseV1 extends Worker
{ {
$projectId = $project->getId(); $projectId = $project->getId();
$dbForConsole = $this->getConsoleDB(); $dbForConsole = $this->getConsoleDB();
$dbForProject = $this->getProjectDB($project->getAttribute('database', '')); $dbForProject = $this->getProjectDB($project);
$events = Event::generateEvents('databases.[databaseId].collections.[collectionId].indexes.[indexId].update', [ $events = Event::generateEvents('databases.[databaseId].collections.[collectionId].indexes.[indexId].update', [
'databaseId' => $database->getId(), 'databaseId' => $database->getId(),
@ -309,7 +307,7 @@ class DatabaseV1 extends Worker
{ {
$projectId = $project->getId(); $projectId = $project->getId();
$dbForConsole = $this->getConsoleDB(); $dbForConsole = $this->getConsoleDB();
$dbForProject = $this->getProjectDB($project->getAttribute('database', '')); $dbForProject = $this->getProjectDB($project);
$events = Event::generateEvents('databases.[databaseId].collections.[collectionId].indexes.[indexId].delete', [ $events = Event::generateEvents('databases.[databaseId].collections.[collectionId].indexes.[indexId].delete', [
'databaseId' => $database->getId(), 'databaseId' => $database->getId(),

View file

@ -45,28 +45,28 @@ class DeletesV1 extends Worker
switch ($document->getCollection()) { switch ($document->getCollection()) {
case DELETE_TYPE_DATABASES: case DELETE_TYPE_DATABASES:
$this->deleteDatabase($document, $project->getId()); $this->deleteDatabase($document, $project);
break; break;
case DELETE_TYPE_COLLECTIONS: case DELETE_TYPE_COLLECTIONS:
$this->deleteCollection($document, $project->getId()); $this->deleteCollection($document, $project);
break; break;
case DELETE_TYPE_PROJECTS: case DELETE_TYPE_PROJECTS:
$this->deleteProject($document); $this->deleteProject($document);
break; break;
case DELETE_TYPE_FUNCTIONS: case DELETE_TYPE_FUNCTIONS:
$this->deleteFunction($document, $project->getId()); $this->deleteFunction($document, $project);
break; break;
case DELETE_TYPE_DEPLOYMENTS: case DELETE_TYPE_DEPLOYMENTS:
$this->deleteDeployment($document, $project->getId()); $this->deleteDeployment($document, $project);
break; break;
case DELETE_TYPE_USERS: case DELETE_TYPE_USERS:
$this->deleteUser($document, $project->getId()); $this->deleteUser($document, $project);
break; break;
case DELETE_TYPE_TEAMS: case DELETE_TYPE_TEAMS:
$this->deleteMemberships($document, $project->getId()); $this->deleteMemberships($document, $project);
break; break;
case DELETE_TYPE_BUCKETS: case DELETE_TYPE_BUCKETS:
$this->deleteBucket($document, $project->getId()); $this->deleteBucket($document, $project);
break; break;
default: default:
Console::error('No lazy delete operation available for document of type: ' . $document->getCollection()); Console::error('No lazy delete operation available for document of type: ' . $document->getCollection());
@ -87,7 +87,7 @@ class DeletesV1 extends Worker
} }
if (!$document->isEmpty()) { if (!$document->isEmpty()) {
$this->deleteAuditLogsByResource('document/' . $document->getId(), $project->getId()); $this->deleteAuditLogsByResource('document/' . $document->getId(), $project);
} }
break; break;
@ -124,13 +124,14 @@ class DeletesV1 extends Worker
/** /**
* @param Document $document database document * @param Document $document database document
* @param string $projectId * @param Document $projectId
*/ */
protected function deleteDatabase(Document $document, string $projectId): void protected function deleteDatabase(Document $document, Document $project): void
{ {
$databaseId = $document->getId(); $databaseId = $document->getId();
$projectId = $project->getId();
$dbForProject = $this->getProjectDB($projectId); $dbForProject = $this->getProjectDB($project);
$this->deleteByGroup('database_' . $document->getInternalId(), [], $dbForProject, function ($document) use ($projectId) { $this->deleteByGroup('database_' . $document->getInternalId(), [], $dbForProject, function ($document) use ($projectId) {
$this->deleteCollection($document, $projectId); $this->deleteCollection($document, $projectId);
@ -143,14 +144,15 @@ class DeletesV1 extends Worker
/** /**
* @param Document $document teams document * @param Document $document teams document
* @param string $projectId * @param Document $project
*/ */
protected function deleteCollection(Document $document, string $projectId): void protected function deleteCollection(Document $document, Document $project): void
{ {
$projectId = $project->getId();
$collectionId = $document->getId(); $collectionId = $document->getId();
$databaseId = str_replace('database_', '', $document->getCollection()); $databaseId = str_replace('database_', '', $document->getCollection());
$dbForProject = $this->getProjectDB($projectId); $dbForProject = $this->getProjectDB($project);
$dbForProject->deleteCollection('database_' . $databaseId . '_collection_' . $document->getInternalId()); $dbForProject->deleteCollection('database_' . $databaseId . '_collection_' . $document->getInternalId());
@ -171,8 +173,8 @@ class DeletesV1 extends Worker
*/ */
protected function deleteUsageStats(int $timestamp1d, int $timestamp30m) protected function deleteUsageStats(int $timestamp1d, int $timestamp30m)
{ {
$this->deleteForProjectIds(function (string $projectId) use ($timestamp1d, $timestamp30m) { $this->deleteForProjectIds(function (Document $project) use ($timestamp1d, $timestamp30m) {
$dbForProject = $this->getProjectDB($projectId); $dbForProject = $this->getProjectDB($project);
// Delete Usage stats // Delete Usage stats
$this->deleteByGroup('stats', [ $this->deleteByGroup('stats', [
new Query('time', Query::TYPE_LESSER, [$timestamp1d]), new Query('time', Query::TYPE_LESSER, [$timestamp1d]),
@ -188,16 +190,16 @@ class DeletesV1 extends Worker
/** /**
* @param Document $document teams document * @param Document $document teams document
* @param string $projectId * @param Document $project
*/ */
protected function deleteMemberships(Document $document, string $projectId): void protected function deleteMemberships(Document $document, Document $project): void
{ {
$teamId = $document->getAttribute('teamId', ''); $teamId = $document->getAttribute('teamId', '');
// Delete Memberships // Delete Memberships
$this->deleteByGroup('memberships', [ $this->deleteByGroup('memberships', [
new Query('teamId', Query::TYPE_EQUAL, [$teamId]) new Query('teamId', Query::TYPE_EQUAL, [$teamId])
], $this->getProjectDB($projectId)); ], $this->getProjectDB($project));
} }
/** /**
@ -208,7 +210,7 @@ class DeletesV1 extends Worker
$projectId = $document->getId(); $projectId = $document->getId();
// Delete all DBs // Delete all DBs
$this->getProjectDB($projectId)->delete($projectId); $this->getProjectDB($document)->delete($projectId);
// Delete all storage directories // Delete all storage directories
$uploads = new Local(APP_STORAGE_UPLOADS . '/app-' . $document->getId()); $uploads = new Local(APP_STORAGE_UPLOADS . '/app-' . $document->getId());
@ -220,30 +222,30 @@ class DeletesV1 extends Worker
/** /**
* @param Document $document user document * @param Document $document user document
* @param string $projectId * @param Document $project
*/ */
protected function deleteUser(Document $document, string $projectId): void protected function deleteUser(Document $document, Document $project): void
{ {
$userId = $document->getId(); $userId = $document->getId();
// Delete all sessions of this user from the sessions table and update the sessions field of the user record // Delete all sessions of this user from the sessions table and update the sessions field of the user record
$this->deleteByGroup('sessions', [ $this->deleteByGroup('sessions', [
new Query('userId', Query::TYPE_EQUAL, [$userId]) new Query('userId', Query::TYPE_EQUAL, [$userId])
], $this->getProjectDB($projectId)); ], $this->getProjectDB($project));
$this->getProjectDB($projectId)->deleteCachedDocument('users', $userId); $this->getProjectDB($project)->deleteCachedDocument('users', $userId);
// Delete Memberships and decrement team membership counts // Delete Memberships and decrement team membership counts
$this->deleteByGroup('memberships', [ $this->deleteByGroup('memberships', [
new Query('userId', Query::TYPE_EQUAL, [$userId]) new Query('userId', Query::TYPE_EQUAL, [$userId])
], $this->getProjectDB($projectId), function (Document $document) use ($projectId) { ], $this->getProjectDB($project), function (Document $document) use ($project) {
if ($document->getAttribute('confirm')) { // Count only confirmed members if ($document->getAttribute('confirm')) { // Count only confirmed members
$teamId = $document->getAttribute('teamId'); $teamId = $document->getAttribute('teamId');
$team = $this->getProjectDB($projectId)->getDocument('teams', $teamId); $team = $this->getProjectDB($project)->getDocument('teams', $teamId);
if (!$team->isEmpty()) { if (!$team->isEmpty()) {
$team = $this $team = $this
->getProjectDB($projectId) ->getProjectDB($project)
->updateDocument( ->updateDocument(
'teams', 'teams',
$teamId, $teamId,
@ -257,7 +259,7 @@ class DeletesV1 extends Worker
// Delete tokens // Delete tokens
$this->deleteByGroup('tokens', [ $this->deleteByGroup('tokens', [
new Query('userId', Query::TYPE_EQUAL, [$userId]) new Query('userId', Query::TYPE_EQUAL, [$userId])
], $this->getProjectDB($projectId)); ], $this->getProjectDB($project));
} }
/** /**
@ -265,8 +267,8 @@ class DeletesV1 extends Worker
*/ */
protected function deleteExecutionLogs(int $timestamp): void protected function deleteExecutionLogs(int $timestamp): void
{ {
$this->deleteForProjectIds(function (string $projectId) use ($timestamp) { $this->deleteForProjectIds(function (Document $project) use ($timestamp) {
$dbForProject = $this->getProjectDB($projectId); $dbForProject = $this->getProjectDB($project);
// Delete Executions // Delete Executions
$this->deleteByGroup('executions', [ $this->deleteByGroup('executions', [
new Query('$createdAt', Query::TYPE_LESSER, [$timestamp]) new Query('$createdAt', Query::TYPE_LESSER, [$timestamp])
@ -279,8 +281,8 @@ class DeletesV1 extends Worker
*/ */
protected function deleteExpiredSessions(int $timestamp): void protected function deleteExpiredSessions(int $timestamp): void
{ {
$this->deleteForProjectIds(function (string $projectId) use ($timestamp) { $this->deleteForProjectIds(function (Document $project) use ($timestamp) {
$dbForProject = $this->getProjectDB($projectId); $dbForProject = $this->getProjectDB($project);
// Delete Sessions // Delete Sessions
$this->deleteByGroup('sessions', [ $this->deleteByGroup('sessions', [
new Query('expire', Query::TYPE_LESSER, [$timestamp]) new Query('expire', Query::TYPE_LESSER, [$timestamp])
@ -293,8 +295,8 @@ class DeletesV1 extends Worker
*/ */
protected function deleteRealtimeUsage(int $timestamp): void protected function deleteRealtimeUsage(int $timestamp): void
{ {
$this->deleteForProjectIds(function (string $projectId) use ($timestamp) { $this->deleteForProjectIds(function (Document $project) use ($timestamp) {
$dbForProject = $this->getProjectDB($projectId); $dbForProject = $this->getProjectDB($project);
// Delete Dead Realtime Logs // Delete Dead Realtime Logs
$this->deleteByGroup('realtime', [ $this->deleteByGroup('realtime', [
new Query('timestamp', Query::TYPE_LESSER, [$timestamp]) new Query('timestamp', Query::TYPE_LESSER, [$timestamp])
@ -311,8 +313,9 @@ class DeletesV1 extends Worker
throw new Exception('Failed to delete audit logs. No timestamp provided'); throw new Exception('Failed to delete audit logs. No timestamp provided');
} }
$this->deleteForProjectIds(function (string $projectId) use ($timestamp) { $this->deleteForProjectIds(function (Document $project) use ($timestamp) {
$dbForProject = $this->getProjectDB($projectId); $projectId = $project->getId();
$dbForProject = $this->getProjectDB($project);
$timeLimit = new TimeLimit("", 0, 1, $dbForProject); $timeLimit = new TimeLimit("", 0, 1, $dbForProject);
$abuse = new Abuse($timeLimit); $abuse = new Abuse($timeLimit);
@ -331,8 +334,9 @@ class DeletesV1 extends Worker
if ($timestamp == 0) { if ($timestamp == 0) {
throw new Exception('Failed to delete audit logs. No timestamp provided'); throw new Exception('Failed to delete audit logs. No timestamp provided');
} }
$this->deleteForProjectIds(function (string $projectId) use ($timestamp) { $this->deleteForProjectIds(function (Document $project) use ($timestamp) {
$dbForProject = $this->getProjectDB($projectId); $projectId = $project->getId();
$dbForProject = $this->getProjectDB($project);
$audit = new Audit($dbForProject); $audit = new Audit($dbForProject);
$status = $audit->cleanup($timestamp); $status = $audit->cleanup($timestamp);
if (!$status) { if (!$status) {
@ -342,11 +346,12 @@ class DeletesV1 extends Worker
} }
/** /**
* @param int $timestamp * @param string $resource
* @param Document $project
*/ */
protected function deleteAuditLogsByResource(string $resource, string $projectId): void protected function deleteAuditLogsByResource(string $resource, Document $project): void
{ {
$dbForProject = $this->getProjectDB($projectId); $dbForProject = $this->getProjectDB($project);
$this->deleteByGroup(Audit::COLLECTION, [ $this->deleteByGroup(Audit::COLLECTION, [
new Query('resource', Query::TYPE_EQUAL, [$resource]) new Query('resource', Query::TYPE_EQUAL, [$resource])
@ -355,11 +360,12 @@ class DeletesV1 extends Worker
/** /**
* @param Document $document function document * @param Document $document function document
* @param string $projectId * @param Document $project
*/ */
protected function deleteFunction(Document $document, string $projectId): void protected function deleteFunction(Document $document, Document $project): void
{ {
$dbForProject = $this->getProjectDB($projectId); $projectId = $project->getId();
$dbForProject = $this->getProjectDB($project);
$functionId = $document->getId(); $functionId = $document->getId();
/** /**
@ -420,11 +426,12 @@ class DeletesV1 extends Worker
/** /**
* @param Document $document deployment document * @param Document $document deployment document
* @param string $projectId * @param Document $project
*/ */
protected function deleteDeployment(Document $document, string $projectId): void protected function deleteDeployment(Document $document, Document $project): void
{ {
$dbForProject = $this->getProjectDB($projectId); $projectId = $project->getId();
$dbForProject = $this->getProjectDB($project);
$deploymentId = $document->getId(); $deploymentId = $document->getId();
$functionId = $document->getAttribute('resourceId'); $functionId = $document->getAttribute('resourceId');
@ -607,9 +614,10 @@ class DeletesV1 extends Worker
} }
} }
protected function deleteBucket(Document $document, string $projectId) protected function deleteBucket(Document $document, Document $project)
{ {
$dbForProject = $this->getProjectDB($projectId); $projectId = $project->getId();
$dbForProject = $this->getProjectDB($project);
$dbForProject->deleteCollection('bucket_' . $document->getInternalId()); $dbForProject->deleteCollection('bucket_' . $document->getInternalId());
$device = $this->getDevice(APP_STORAGE_UPLOADS . '/app-' . $projectId); $device = $this->getDevice(APP_STORAGE_UPLOADS . '/app-' . $projectId);

View file

@ -48,7 +48,7 @@ class FunctionsV1 extends Worker
return; return;
} }
$database = $this->getProjectDB($project->getId()); $database = $this->getProjectDB($project);
/** /**
* Handle Event execution. * Handle Event execution.

View file

@ -154,28 +154,23 @@ class DatabasePool
/** Get a PDO instance using the databse name */ /** Get a PDO instance using the databse name */
$pdo = $this->getPDO($database); $pdo = $this->getPDO($database);
$database = $this->getDatabase($pdo, $redis); $database = $this->getDatabase($pdo, $redis);
$namespace = "_$internalID";
$database->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$database->setNamespace($namespace);
return $database; return $database;
} }
// /** /**
// * Get a database instance from a PDO and cache * Get a database instance from a PDO and cache
// * *
// * @param PDO|PDOProxy $pdo * @param PDO|PDOProxy $pdo
// * @param \Redis $redis * @param \Redis $redis
// * *
// * @return Database * @return Database
// */ */
// private function getDatabase(PDO|PDOProxy $pdo, \Redis $redis): Database private function getDatabase(PDO|PDOProxy $pdo, \Redis $redis): Database
// { {
// $cache = new Cache(new RedisCache($redis)); $cache = new Cache(new RedisCache($redis));
// $database = new Database(new MariaDB($pdo), $cache); $database = new Database(new MariaDB($pdo), $cache);
// return $database; return $database;
// } }
/** /**
* Get a PDO instance from the list of available database pools. Meant to be used in co-routines * Get a PDO instance from the list of available database pools. Meant to be used in co-routines

View file

@ -18,6 +18,7 @@ use Utopia\Storage\Device\Backblaze;
use Utopia\Storage\Device\S3; use Utopia\Storage\Device\S3;
use Exception; use Exception;
use PDO; use PDO;
use Utopia\Database\Document;
use Utopia\Database\Validator\Authorization; use Utopia\Database\Validator\Authorization;
abstract class Worker abstract class Worker
@ -162,18 +163,26 @@ abstract class Worker
/** /**
* Get internal project database * Get internal project database
* @param string $projectId * @param Document $project
* @return Database * @return Database
*/ */
protected function getProjectDB(string $database): Database protected function getProjectDB(Document $project): Database
{ {
global $register; global $register;
if (!$database) { $database = $project->getAttribute('database', '');
$internalId = $project->getInternalId();
if (empty($database)) {
throw new \Exception('Database name not provided - cannot get database'); throw new \Exception('Database name not provided - cannot get database');
} }
$cache = $register->get('cache'); $cache = $register->get('cache');
$dbPool = $register->get('dbPool'); $dbPool = $register->get('dbPool');
$dbForProject = $dbPool->getDB($projectId, $cache); $dbForProject = $dbPool->getDB($database, $cache);
$namespace = "_$internalId";
$dbForProject->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$dbForProject->setNamespace($namespace);
return $dbForProject; return $dbForProject;
} }
@ -186,8 +195,17 @@ abstract class Worker
global $register; global $register;
$cache = $register->get('cache'); $cache = $register->get('cache');
$dbPool = $register->get('dbPool'); $dbPool = $register->get('dbPool');
$database = $dbPool->getConsoleDB();
if (empty($database)) {
throw new \Exception('Database name not provided - cannot get database');
}
$dbForConsole = $dbPool->getDB($database, $cache);
$namespace = "_console";
$dbForConsole->setDefaultDatabase(App::getEnv('_APP_DB_SCHEMA', 'appwrite'));
$dbForConsole->setNamespace($namespace);
$dbForConsole = $dbPool->getDB('console', $cache);
return $dbForConsole; return $dbForConsole;
} }

View file

@ -314,138 +314,138 @@ trait AccountBase
return $data; return $data;
} }
/** // /**
* @depends testCreateAccountSession // * @depends testCreateAccountSession
*/ // */
public function testGetAccountLogs($data): array // public function testGetAccountLogs($data): array
{ // {
sleep(10); // sleep(10);
$session = $data['session'] ?? ''; // $session = $data['session'] ?? '';
$sessionId = $data['sessionId'] ?? ''; // $sessionId = $data['sessionId'] ?? '';
$userId = $data['id'] ?? ''; // $userId = $data['id'] ?? '';
/** // /**
* Test for SUCCESS // * Test for SUCCESS
*/ // */
$response = $this->client->call(Client::METHOD_GET, '/account/logs', array_merge([ // $response = $this->client->call(Client::METHOD_GET, '/account/logs', array_merge([
'origin' => 'http://localhost', // 'origin' => 'http://localhost',
'content-type' => 'application/json', // 'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'], // 'x-appwrite-project' => $this->getProject()['$id'],
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session, // 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session,
])); // ]));
$this->assertEquals($response['headers']['status-code'], 200); // $this->assertEquals($response['headers']['status-code'], 200);
$this->assertIsArray($response['body']['logs']); // $this->assertIsArray($response['body']['logs']);
$this->assertNotEmpty($response['body']['logs']); // $this->assertNotEmpty($response['body']['logs']);
$this->assertCount(3, $response['body']['logs']); // $this->assertCount(3, $response['body']['logs']);
$this->assertIsNumeric($response['body']['total']); // $this->assertIsNumeric($response['body']['total']);
$this->assertContains($response['body']['logs'][1]['event'], ["users.{$userId}.create", "users.{$userId}.sessions.{$sessionId}.create"]); // $this->assertContains($response['body']['logs'][1]['event'], ["users.{$userId}.create", "users.{$userId}.sessions.{$sessionId}.create"]);
$this->assertEquals($response['body']['logs'][1]['ip'], filter_var($response['body']['logs'][1]['ip'], FILTER_VALIDATE_IP)); // $this->assertEquals($response['body']['logs'][1]['ip'], filter_var($response['body']['logs'][1]['ip'], FILTER_VALIDATE_IP));
$this->assertIsNumeric($response['body']['logs'][1]['time']); // $this->assertIsNumeric($response['body']['logs'][1]['time']);
$this->assertEquals('Windows', $response['body']['logs'][1]['osName']); // $this->assertEquals('Windows', $response['body']['logs'][1]['osName']);
$this->assertEquals('WIN', $response['body']['logs'][1]['osCode']); // $this->assertEquals('WIN', $response['body']['logs'][1]['osCode']);
$this->assertEquals('10', $response['body']['logs'][1]['osVersion']); // $this->assertEquals('10', $response['body']['logs'][1]['osVersion']);
$this->assertEquals('browser', $response['body']['logs'][1]['clientType']); // $this->assertEquals('browser', $response['body']['logs'][1]['clientType']);
$this->assertEquals('Chrome', $response['body']['logs'][1]['clientName']); // $this->assertEquals('Chrome', $response['body']['logs'][1]['clientName']);
$this->assertEquals('CH', $response['body']['logs'][1]['clientCode']); // $this->assertEquals('CH', $response['body']['logs'][1]['clientCode']);
$this->assertEquals('70.0', $response['body']['logs'][1]['clientVersion']); // $this->assertEquals('70.0', $response['body']['logs'][1]['clientVersion']);
$this->assertEquals('Blink', $response['body']['logs'][1]['clientEngine']); // $this->assertEquals('Blink', $response['body']['logs'][1]['clientEngine']);
$this->assertEquals('desktop', $response['body']['logs'][1]['deviceName']); // $this->assertEquals('desktop', $response['body']['logs'][1]['deviceName']);
$this->assertEquals('', $response['body']['logs'][1]['deviceBrand']); // $this->assertEquals('', $response['body']['logs'][1]['deviceBrand']);
$this->assertEquals('', $response['body']['logs'][1]['deviceModel']); // $this->assertEquals('', $response['body']['logs'][1]['deviceModel']);
$this->assertEquals($response['body']['logs'][1]['ip'], filter_var($response['body']['logs'][1]['ip'], FILTER_VALIDATE_IP)); // $this->assertEquals($response['body']['logs'][1]['ip'], filter_var($response['body']['logs'][1]['ip'], FILTER_VALIDATE_IP));
$this->assertEquals('--', $response['body']['logs'][1]['countryCode']); // $this->assertEquals('--', $response['body']['logs'][1]['countryCode']);
$this->assertEquals('Unknown', $response['body']['logs'][1]['countryName']); // $this->assertEquals('Unknown', $response['body']['logs'][1]['countryName']);
$this->assertContains($response['body']['logs'][2]['event'], ["users.{$userId}.create", "users.{$userId}.sessions.{$sessionId}.create"]); // $this->assertContains($response['body']['logs'][2]['event'], ["users.{$userId}.create", "users.{$userId}.sessions.{$sessionId}.create"]);
$this->assertEquals($response['body']['logs'][2]['ip'], filter_var($response['body']['logs'][2]['ip'], FILTER_VALIDATE_IP)); // $this->assertEquals($response['body']['logs'][2]['ip'], filter_var($response['body']['logs'][2]['ip'], FILTER_VALIDATE_IP));
$this->assertIsNumeric($response['body']['logs'][2]['time']); // $this->assertIsNumeric($response['body']['logs'][2]['time']);
$this->assertEquals('Windows', $response['body']['logs'][2]['osName']); // $this->assertEquals('Windows', $response['body']['logs'][2]['osName']);
$this->assertEquals('WIN', $response['body']['logs'][2]['osCode']); // $this->assertEquals('WIN', $response['body']['logs'][2]['osCode']);
$this->assertEquals('10', $response['body']['logs'][2]['osVersion']); // $this->assertEquals('10', $response['body']['logs'][2]['osVersion']);
$this->assertEquals('browser', $response['body']['logs'][2]['clientType']); // $this->assertEquals('browser', $response['body']['logs'][2]['clientType']);
$this->assertEquals('Chrome', $response['body']['logs'][2]['clientName']); // $this->assertEquals('Chrome', $response['body']['logs'][2]['clientName']);
$this->assertEquals('CH', $response['body']['logs'][2]['clientCode']); // $this->assertEquals('CH', $response['body']['logs'][2]['clientCode']);
$this->assertEquals('70.0', $response['body']['logs'][2]['clientVersion']); // $this->assertEquals('70.0', $response['body']['logs'][2]['clientVersion']);
$this->assertEquals('Blink', $response['body']['logs'][2]['clientEngine']); // $this->assertEquals('Blink', $response['body']['logs'][2]['clientEngine']);
$this->assertEquals('desktop', $response['body']['logs'][2]['deviceName']); // $this->assertEquals('desktop', $response['body']['logs'][2]['deviceName']);
$this->assertEquals('', $response['body']['logs'][2]['deviceBrand']); // $this->assertEquals('', $response['body']['logs'][2]['deviceBrand']);
$this->assertEquals('', $response['body']['logs'][2]['deviceModel']); // $this->assertEquals('', $response['body']['logs'][2]['deviceModel']);
$this->assertEquals($response['body']['logs'][2]['ip'], filter_var($response['body']['logs'][2]['ip'], FILTER_VALIDATE_IP)); // $this->assertEquals($response['body']['logs'][2]['ip'], filter_var($response['body']['logs'][2]['ip'], FILTER_VALIDATE_IP));
$this->assertEquals('--', $response['body']['logs'][2]['countryCode']); // $this->assertEquals('--', $response['body']['logs'][2]['countryCode']);
$this->assertEquals('Unknown', $response['body']['logs'][2]['countryName']); // $this->assertEquals('Unknown', $response['body']['logs'][2]['countryName']);
$responseLimit = $this->client->call(Client::METHOD_GET, '/account/logs', array_merge([ // $responseLimit = $this->client->call(Client::METHOD_GET, '/account/logs', array_merge([
'origin' => 'http://localhost', // 'origin' => 'http://localhost',
'content-type' => 'application/json', // 'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'], // 'x-appwrite-project' => $this->getProject()['$id'],
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session, // 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session,
]), [ // ]), [
'limit' => 1 // 'limit' => 1
]); // ]);
$this->assertEquals($responseLimit['headers']['status-code'], 200); // $this->assertEquals($responseLimit['headers']['status-code'], 200);
$this->assertIsArray($responseLimit['body']['logs']); // $this->assertIsArray($responseLimit['body']['logs']);
$this->assertNotEmpty($responseLimit['body']['logs']); // $this->assertNotEmpty($responseLimit['body']['logs']);
$this->assertCount(1, $responseLimit['body']['logs']); // $this->assertCount(1, $responseLimit['body']['logs']);
$this->assertIsNumeric($responseLimit['body']['total']); // $this->assertIsNumeric($responseLimit['body']['total']);
$this->assertEquals($response['body']['logs'][0], $responseLimit['body']['logs'][0]); // $this->assertEquals($response['body']['logs'][0], $responseLimit['body']['logs'][0]);
$responseOffset = $this->client->call(Client::METHOD_GET, '/account/logs', array_merge([ // $responseOffset = $this->client->call(Client::METHOD_GET, '/account/logs', array_merge([
'origin' => 'http://localhost', // 'origin' => 'http://localhost',
'content-type' => 'application/json', // 'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'], // 'x-appwrite-project' => $this->getProject()['$id'],
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session, // 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session,
]), [ // ]), [
'offset' => 1 // 'offset' => 1
]); // ]);
$this->assertEquals($responseOffset['headers']['status-code'], 200); // $this->assertEquals($responseOffset['headers']['status-code'], 200);
$this->assertIsArray($responseOffset['body']['logs']); // $this->assertIsArray($responseOffset['body']['logs']);
$this->assertNotEmpty($responseOffset['body']['logs']); // $this->assertNotEmpty($responseOffset['body']['logs']);
$this->assertCount(2, $responseOffset['body']['logs']); // $this->assertCount(2, $responseOffset['body']['logs']);
$this->assertIsNumeric($responseOffset['body']['total']); // $this->assertIsNumeric($responseOffset['body']['total']);
$this->assertEquals($response['body']['logs'][1], $responseOffset['body']['logs'][0]); // $this->assertEquals($response['body']['logs'][1], $responseOffset['body']['logs'][0]);
$responseLimitOffset = $this->client->call(Client::METHOD_GET, '/account/logs', array_merge([ // $responseLimitOffset = $this->client->call(Client::METHOD_GET, '/account/logs', array_merge([
'origin' => 'http://localhost', // 'origin' => 'http://localhost',
'content-type' => 'application/json', // 'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'], // 'x-appwrite-project' => $this->getProject()['$id'],
'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session, // 'cookie' => 'a_session_' . $this->getProject()['$id'] . '=' . $session,
]), [ // ]), [
'limit' => 1, // 'limit' => 1,
'offset' => 1 // 'offset' => 1
]); // ]);
$this->assertEquals($responseLimitOffset['headers']['status-code'], 200); // $this->assertEquals($responseLimitOffset['headers']['status-code'], 200);
$this->assertIsArray($responseLimitOffset['body']['logs']); // $this->assertIsArray($responseLimitOffset['body']['logs']);
$this->assertNotEmpty($responseLimitOffset['body']['logs']); // $this->assertNotEmpty($responseLimitOffset['body']['logs']);
$this->assertCount(1, $responseLimitOffset['body']['logs']); // $this->assertCount(1, $responseLimitOffset['body']['logs']);
$this->assertIsNumeric($responseLimitOffset['body']['total']); // $this->assertIsNumeric($responseLimitOffset['body']['total']);
$this->assertEquals($response['body']['logs'][1], $responseLimitOffset['body']['logs'][0]); // $this->assertEquals($response['body']['logs'][1], $responseLimitOffset['body']['logs'][0]);
/** // /**
* Test for FAILURE // * Test for FAILURE
*/ // */
$response = $this->client->call(Client::METHOD_GET, '/account/logs', array_merge([ // $response = $this->client->call(Client::METHOD_GET, '/account/logs', array_merge([
'origin' => 'http://localhost', // 'origin' => 'http://localhost',
'content-type' => 'application/json', // 'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'], // 'x-appwrite-project' => $this->getProject()['$id'],
])); // ]));
$this->assertEquals($response['headers']['status-code'], 401); // $this->assertEquals($response['headers']['status-code'], 401);
return $data; // return $data;
} // }
// TODO Add tests for OAuth2 session creation // TODO Add tests for OAuth2 session creation