From bbdf70ee902e26613e99eeb4c40ab50b613cfa47 Mon Sep 17 00:00:00 2001 From: Torsten Dittmann Date: Wed, 15 Jun 2022 14:46:52 +0200 Subject: [PATCH 1/3] feat: use new $createdAt and $updatedAt attributes --- app/config/collections.php | 132 +----------------- app/controllers/api/account.php | 3 +- app/controllers/api/database.php | 3 - app/controllers/api/functions.php | 9 +- app/controllers/api/projects.php | 5 +- app/controllers/api/storage.php | 4 - app/controllers/api/teams.php | 1 - app/controllers/general.php | 3 +- app/http.php | 2 - app/init.php | 8 +- app/views/console/database/collection.phtml | 4 +- app/views/console/database/search/files.phtml | 2 +- app/views/console/functions/function.phtml | 12 +- app/views/console/storage/bucket.phtml | 8 +- app/views/console/users/index.phtml | 2 +- app/views/console/users/team.phtml | 2 +- app/workers/deletes.php | 2 +- app/workers/functions.php | 8 +- composer.json | 2 +- composer.lock | 28 ++-- src/Appwrite/Utopia/Response/Model/Bucket.php | 24 ++-- .../Utopia/Response/Model/Collection.php | 12 ++ .../Utopia/Response/Model/Deployment.php | 18 ++- .../Utopia/Response/Model/Document.php | 12 ++ src/Appwrite/Utopia/Response/Model/Domain.php | 12 ++ .../Utopia/Response/Model/Execution.php | 18 ++- src/Appwrite/Utopia/Response/Model/File.php | 18 ++- src/Appwrite/Utopia/Response/Model/Func.php | 24 ++-- src/Appwrite/Utopia/Response/Model/Key.php | 12 ++ .../Utopia/Response/Model/Membership.php | 12 ++ .../Utopia/Response/Model/Platform.php | 12 ++ .../Utopia/Response/Model/Project.php | 12 ++ .../Utopia/Response/Model/Session.php | 6 + src/Appwrite/Utopia/Response/Model/Team.php | 18 ++- src/Appwrite/Utopia/Response/Model/Token.php | 6 + src/Appwrite/Utopia/Response/Model/User.php | 12 ++ .../Utopia/Response/Model/Webhook.php | 12 ++ .../Database/DatabaseCustomServerTest.php | 6 +- .../Functions/FunctionsCustomServerTest.php | 20 +-- tests/e2e/Services/Storage/StorageBase.php | 8 +- .../Storage/StorageCustomClientTest.php | 4 +- .../Storage/StorageCustomServerTest.php | 4 +- tests/e2e/Services/Teams/TeamsBase.php | 14 +- tests/e2e/Services/Teams/TeamsBaseServer.php | 4 +- tests/e2e/Services/Webhooks/WebhooksBase.php | 12 +- 45 files changed, 282 insertions(+), 270 deletions(-) diff --git a/app/config/collections.php b/app/config/collections.php index ac13bae126..5ce8667032 100644 --- a/app/config/collections.php +++ b/app/config/collections.php @@ -30,28 +30,6 @@ $collections = [ 'array' => false, 'filters' => [], ], - [ - '$id' => 'dateCreated', - 'type' => Database::VAR_INTEGER, - 'format' => '', - 'size' => 0, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => 'dateUpdated', - 'type' => Database::VAR_INTEGER, - 'format' => '', - 'size' => 0, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], [ '$id' => 'enabled', 'type' => Database::VAR_BOOLEAN, @@ -659,29 +637,7 @@ $collections = [ 'default' => null, 'array' => false, 'filters' => [], - ], - [ - '$id' => 'dateCreated', - 'type' => Database::VAR_INTEGER, - 'format' => '', - 'size' => 0, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => 'dateUpdated', - 'type' => Database::VAR_INTEGER, - 'format' => '', - 'size' => 0, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], + ] ], 'indexes' => [ [ @@ -1508,17 +1464,6 @@ $collections = [ 'array' => false, 'filters' => [], ], - [ - '$id' => 'dateCreated', - 'type' => Database::VAR_INTEGER, - 'format' => '', - 'size' => 0, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], [ '$id' => 'total', 'type' => Database::VAR_INTEGER, @@ -1706,28 +1651,6 @@ $collections = [ 'array' => false, 'filters' => [], ], - [ - '$id' => 'dateCreated', - 'type' => Database::VAR_INTEGER, - 'format' => '', - 'size' => 0, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => 'dateUpdated', - 'type' => Database::VAR_INTEGER, - 'format' => '', - 'size' => 0, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], [ 'array' => false, '$id' => 'status', @@ -1856,17 +1779,6 @@ $collections = [ '$id' => 'deployments', 'name' => 'Deployments', 'attributes' => [ - [ - '$id' => 'dateCreated', - 'type' => Database::VAR_INTEGER, - 'format' => '', - 'size' => 0, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], [ '$id' => 'resourceId', 'type' => Database::VAR_STRING, @@ -2158,17 +2070,6 @@ $collections = [ '$id' => 'executions', 'name' => 'Executions', 'attributes' => [ - [ - '$id' => 'dateCreated', - 'type' => Database::VAR_INTEGER, - 'format' => '', - 'size' => 0, - 'signed' => true, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], [ '$id' => 'functionId', 'type' => Database::VAR_STRING, @@ -2379,26 +2280,6 @@ $collections = [ '$id' => 'buckets', 'name' => 'Buckets', 'attributes' => [ - [ - '$id' => 'dateCreated', - 'type' => Database::VAR_INTEGER, - 'format' => '', - 'signed' => false, - 'size' => 0, - 'required' => false, - 'array' => false, - 'filters' => [], - ], - [ - '$id' => 'dateUpdated', - 'type' => Database::VAR_INTEGER, - 'size' => 0, - 'format' => '', - 'signed' => false, - 'required' => false, - 'array' => false, - 'filters' => [], - ], [ '$id' => 'enabled', 'type' => Database::VAR_BOOLEAN, @@ -2639,17 +2520,6 @@ $collections = [ '$id' => 'files', '$name' => 'Files', 'attributes' => [ - [ - '$id' => 'dateCreated', - 'type' => Database::VAR_INTEGER, - 'format' => '', - 'size' => 0, - 'signed' => false, - 'required' => false, - 'default' => null, - 'array' => false, - 'filters' => [], - ], [ 'array' => false, '$id' => 'bucketId', diff --git a/app/controllers/api/account.php b/app/controllers/api/account.php index c039c21346..cc6294df29 100644 --- a/app/controllers/api/account.php +++ b/app/controllers/api/account.php @@ -1530,8 +1530,7 @@ App::patch('/v1/account/sessions/:sessionId') $session ->setAttribute('providerAccessToken', $oauth2->getAccessToken('')) ->setAttribute('providerRefreshToken', $oauth2->getRefreshToken('')) - ->setAttribute('providerAccessTokenExpiry', \time() + (int) $oauth2->getAccessTokenExpiry('')) - ; + ->setAttribute('providerAccessTokenExpiry', \time() + (int) $oauth2->getAccessTokenExpiry('')); $dbForProject->updateDocument('sessions', $sessionId, $session); diff --git a/app/controllers/api/database.php b/app/controllers/api/database.php index 358da9a527..5334ad1869 100644 --- a/app/controllers/api/database.php +++ b/app/controllers/api/database.php @@ -166,8 +166,6 @@ App::post('/v1/database/collections') '$read' => $read ?? [], // Collection permissions for collection documents (based on permission model) '$write' => $write ?? [], // Collection permissions for collection documents (based on permission model) 'permission' => $permission, // Permissions model type (document vs collection) - 'dateCreated' => time(), - 'dateUpdated' => time(), 'enabled' => true, 'name' => $name, 'search' => implode(' ', [$collectionId, $name]), @@ -597,7 +595,6 @@ App::put('/v1/database/collections/:collectionId') ->setAttribute('$read', $read) ->setAttribute('name', $name) ->setAttribute('permission', $permission) - ->setAttribute('dateUpdated', time()) ->setAttribute('enabled', $enabled) ->setAttribute('search', implode(' ', [$collectionId, $name]))); } catch (AuthorizationException $exception) { diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index 6b07c97045..7dfbe71420 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -66,8 +66,6 @@ App::post('/v1/functions') $function = $dbForProject->createDocument('functions', new Document([ '$id' => $functionId, 'execute' => $execute, - 'dateCreated' => time(), - 'dateUpdated' => time(), 'status' => 'disabled', 'name' => $name, 'runtime' => $runtime, @@ -315,7 +313,6 @@ App::put('/v1/functions/:functionId') $function = $dbForProject->updateDocument('functions', $function->getId(), new Document(array_merge($function->getArrayCopy(), [ 'execute' => $execute, - 'dateUpdated' => time(), 'name' => $name, 'vars' => $vars, 'events' => $events, @@ -575,7 +572,6 @@ App::post('/v1/functions/:functionId/deployments') '$write' => ['role:all'], 'resourceId' => $function->getId(), 'resourceType' => 'functions', - 'dateCreated' => time(), 'entrypoint' => $entrypoint, 'path' => $path, 'size' => $fileSize, @@ -605,7 +601,6 @@ App::post('/v1/functions/:functionId/deployments') '$write' => ['role:all'], 'resourceId' => $function->getId(), 'resourceType' => 'functions', - 'dateCreated' => time(), 'entrypoint' => $entrypoint, 'path' => $path, 'size' => $fileSize, @@ -854,11 +849,11 @@ App::post('/v1/functions/:functionId/executions') $executionId = $dbForProject->getId(); + /** @var Document $execution */ $execution = Authorization::skip(fn () => $dbForProject->createDocument('executions', new Document([ '$id' => $executionId, '$read' => (!$user->isEmpty()) ? ['user:' . $user->getId()] : [], '$write' => [], - 'dateCreated' => time(), 'functionId' => $function->getId(), 'deploymentId' => $deployment->getId(), 'trigger' => 'http', // http / schedule / event @@ -952,7 +947,7 @@ App::post('/v1/functions/:functionId/executions') $execution->setAttribute('time', $executionResponse['time']); } catch (\Throwable $th) { $endtime = \microtime(true); - $time = $endtime - $execution->getAttribute('dateCreated'); + $time = $endtime - $execution->getCreatedAt(); $execution->setAttribute('time', $time); $execution->setAttribute('status', 'failed'); $execution->setAttribute('statusCode', $th->getCode()); diff --git a/app/controllers/api/projects.php b/app/controllers/api/projects.php index 2a7e7ad65e..9613f821cf 100644 --- a/app/controllers/api/projects.php +++ b/app/controllers/api/projects.php @@ -998,9 +998,7 @@ App::post('/v1/projects/:projectId/platforms') 'name' => $name, 'key' => $key, 'store' => $store, - 'hostname' => $hostname, - 'dateCreated' => \time(), - 'dateUpdated' => \time(), + 'hostname' => $hostname ]); $platform = $dbForConsole->createDocument('platforms', $platform); @@ -1112,7 +1110,6 @@ App::put('/v1/projects/:projectId/platforms/:platformId') $platform ->setAttribute('name', $name) - ->setAttribute('dateUpdated', \time()) ->setAttribute('key', $key) ->setAttribute('store', $store) ->setAttribute('hostname', $hostname) diff --git a/app/controllers/api/storage.php b/app/controllers/api/storage.php index 2f55d97205..e09cd7963f 100644 --- a/app/controllers/api/storage.php +++ b/app/controllers/api/storage.php @@ -107,8 +107,6 @@ App::post('/v1/storage/buckets') $bucket = $dbForProject->createDocument('buckets', new Document([ '$id' => $bucketId, '$collection' => 'buckets', - 'dateCreated' => \time(), - 'dateUpdated' => \time(), 'name' => $name, 'permission' => $permission, 'maximumFileSize' => $maximumFileSize, @@ -546,7 +544,6 @@ App::post('/v1/storage/buckets/:bucketId/files') '$id' => $fileId, '$read' => $read, '$write' => $write, - 'dateCreated' => \time(), 'bucketId' => $bucket->getId(), 'name' => $fileName, 'path' => $path, @@ -613,7 +610,6 @@ App::post('/v1/storage/buckets/:bucketId/files') '$id' => $fileId, '$read' => $read, '$write' => $write, - 'dateCreated' => \time(), 'bucketId' => $bucket->getId(), 'name' => $fileName, 'path' => $path, diff --git a/app/controllers/api/teams.php b/app/controllers/api/teams.php index a983745a95..a32dc87bcd 100644 --- a/app/controllers/api/teams.php +++ b/app/controllers/api/teams.php @@ -63,7 +63,6 @@ App::post('/v1/teams') '$write' => ['team:' . $teamId . '/owner'], 'name' => $name, 'total' => ($isPrivilegedUser || $isAppUser) ? 0 : 1, - 'dateCreated' => \time(), 'search' => implode(' ', [$teamId, $name]), ]))); diff --git a/app/controllers/general.php b/app/controllers/general.php index a67d9cfd7c..bd05fea04f 100644 --- a/app/controllers/general.php +++ b/app/controllers/general.php @@ -298,11 +298,10 @@ App::init(function (App $utopia, Request $request, Response $response, Document $service = $route->getLabel('sdk.namespace', ''); if (!empty($service)) { - $roles = Authorization::getRoles(); if ( array_key_exists($service, $project->getAttribute('services', [])) && !$project->getAttribute('services', [])[$service] - && !(Auth::isPrivilegedUser($roles) || Auth::isAppUser($roles)) + && !(Auth::isPrivilegedUser(Authorization::getRoles()) || Auth::isAppUser(Authorization::getRoles())) ) { throw new AppwriteException('Service is disabled', 503, AppwriteException::GENERAL_SERVICE_DISABLED); } diff --git a/app/http.php b/app/http.php index a7d10b9e0a..d900947dc7 100644 --- a/app/http.php +++ b/app/http.php @@ -155,8 +155,6 @@ $http->on('start', function (Server $http) use ($payloadSize, $register) { $dbForConsole->createDocument('buckets', new Document([ '$id' => 'default', '$collection' => 'buckets', - 'dateCreated' => \time(), - 'dateUpdated' => \time(), 'name' => 'Default', 'permission' => 'file', 'maximumFileSize' => (int) App::getEnv('_APP_STORAGE_LIMIT', 0), // 10MB diff --git a/app/init.php b/app/init.php index 4a456a4d1f..308ec54389 100644 --- a/app/init.php +++ b/app/init.php @@ -461,6 +461,11 @@ $register->set('dbPool', function () { ->withPassword($dbPass) ->withOptions([ PDO::ATTR_ERRMODE => App::isDevelopment() ? PDO::ERRMODE_WARNING : PDO::ERRMODE_SILENT, // If in production mode, warnings are not displayed + PDO::ATTR_TIMEOUT => 3, // Seconds + PDO::ATTR_PERSISTENT => true, + PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, + PDO::ATTR_EMULATE_PREPARES => true, + PDO::ATTR_STRINGIFY_FETCHES => true, ]), 64 ); @@ -553,11 +558,12 @@ $register->set('db', function () { $dbScheme = App::getEnv('_APP_DB_SCHEMA', ''); $pdo = new PDO("mysql:host={$dbHost};port={$dbPort};dbname={$dbScheme};charset=utf8mb4", $dbUser, $dbPass, array( - PDO::MYSQL_ATTR_INIT_COMMAND => 'SET NAMES utf8mb4', PDO::ATTR_TIMEOUT => 3, // Seconds PDO::ATTR_PERSISTENT => true, PDO::ATTR_DEFAULT_FETCH_MODE => PDO::FETCH_ASSOC, PDO::ATTR_ERRMODE => PDO::ERRMODE_EXCEPTION, + PDO::ATTR_EMULATE_PREPARES => true, + PDO::ATTR_STRINGIFY_FETCHES => true, )); return $pdo; diff --git a/app/views/console/database/collection.phtml b/app/views/console/database/collection.phtml index 48662ead17..fa782adb8e 100644 --- a/app/views/console/database/collection.phtml +++ b/app/views/console/database/collection.phtml @@ -568,8 +568,8 @@ $logs = $this->getParam('logs', null);
- +