From a609759951bf696e2808ddccfcba5a82a8de7fc9 Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Sat, 2 Sep 2023 16:37:40 -0700 Subject: [PATCH 1/7] Make runtime optional when updating a function This is important for backwards compatibility since it wasn't possible to update a function runtime in previous versions and it was never included in the request. --- app/controllers/api/functions.php | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index d3d1cb2509..bbdd12c85f 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -660,7 +660,7 @@ App::put('/v1/functions/:functionId') ->label('sdk.response.model', Response::MODEL_FUNCTION) ->param('functionId', '', new UID(), 'Function ID.') ->param('name', '', new Text(128), 'Function name. Max length: 128 chars.') - ->param('runtime', '', new WhiteList(array_keys(Config::getParam('runtimes')), true), 'Execution runtime.') + ->param('runtime', '', new WhiteList(array_keys(Config::getParam('runtimes')), true), 'Execution runtime.', true) ->param('execute', [], new Roles(APP_LIMIT_ARRAY_PARAMS_SIZE), 'An array of role strings with execution permissions. By default no user is granted with any execute permissions. [learn more about roles](https://appwrite.io/docs/permissions#permission-roles). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' roles are allowed, each 64 characters long.', true) ->param('events', [], new ArrayList(new ValidatorEvent(), APP_LIMIT_ARRAY_PARAMS_SIZE), 'Events list. Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' events are allowed.', true) ->param('schedule', '', new Cron(), 'Schedule CRON syntax.', true) @@ -704,6 +704,10 @@ App::put('/v1/functions/:functionId') throw new Exception(Exception::FUNCTION_NOT_FOUND); } + if (empty($runtime)) { + $runtime = $function->getAttribute('runtime'); + } + $enabled ??= $function->getAttribute('enabled', true); $repositoryId = $function->getAttribute('repositoryId', ''); From 03c2a163062fcbd3d2511bf332399456061a0620 Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Sat, 2 Sep 2023 17:29:38 -0700 Subject: [PATCH 2/7] Fix update migrations for functions and deployments - Create additional attributes for the deployments collection. - Set the commands attribute for the functions and deployments based on runtime. --- src/Appwrite/Migration/Version/V19.php | 43 ++++++++++++++++++++++++++ 1 file changed, 43 insertions(+) diff --git a/src/Appwrite/Migration/Version/V19.php b/src/Appwrite/Migration/Version/V19.php index ac6eff98e2..ddfae4dd59 100644 --- a/src/Appwrite/Migration/Version/V19.php +++ b/src/Appwrite/Migration/Version/V19.php @@ -227,7 +227,25 @@ class V19 extends Migration $attributesToCreate = [ 'resourceInternalId', 'buildInternalId', + 'commands', 'type', + 'installationId', + 'installationInternalId', + 'providerRepositoryId', + 'repositoryId', + 'repositoryInternalId', + 'providerRepositoryName', + 'providerRepositoryOwner', + 'providerRepositoryUrl', + 'providerCommitHash', + 'providerCommitAuthorUrl', + 'providerCommitAuthor', + 'providerCommitMessage', + 'providerCommitUrl', + 'providerBranch', + 'providerBranchUrl', + 'providerRootDirectory', + 'providerCommentId', ]; foreach ($attributesToCreate as $attribute) { try { @@ -558,6 +576,25 @@ class V19 extends Migration } } + private function getFunctionCommands(Document $function): string + { + $runtime = $function->getAttribute('runtime'); + $language = explode('-', $runtime)[0]; + $commands = match($language) { + 'dart' => 'dart pub get', + 'deno' => 'deno cache ' . $function->getAttribute('entrypoint'), + 'dotnet' => 'dotnet restore', + 'node' => 'npm install', + 'php' => 'composer install', + 'python' => 'pip install -r requirements.txt', + 'ruby' => 'bundle install', + 'swift' => 'swift package resolve', + default => '', + }; + + return $commands; + } + /** * Fix run on each document * @@ -597,6 +634,9 @@ class V19 extends Migration $document->setAttribute('buildInternalId', $build->getInternalId()); } + $commands = $this->getFunctionCommands($function); + $document->setAttribute('commands', $commands); + $document->setAttribute('type', 'manual'); break; case 'executions': @@ -620,6 +660,9 @@ class V19 extends Migration $document->setAttribute('entrypoint', $deployment->getAttribute('entrypoint')); } + $commands = $this->getFunctionCommands($document); + $document->setAttribute('commands', $commands); + $schedule = $this->consoleDB->createDocument('schedules', new Document([ 'region' => App::getEnv('_APP_REGION', 'default'), // Todo replace with projects region 'resourceType' => 'function', From f317d33fc70cb6ddf188c6892396b3ff6e698d73 Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Sat, 2 Sep 2023 18:06:50 -0700 Subject: [PATCH 3/7] Skip migrating domains without project because it would throw an error --- src/Appwrite/Migration/Version/V19.php | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/Appwrite/Migration/Version/V19.php b/src/Appwrite/Migration/Version/V19.php index ddfae4dd59..e9f50357d1 100644 --- a/src/Appwrite/Migration/Version/V19.php +++ b/src/Appwrite/Migration/Version/V19.php @@ -56,6 +56,14 @@ class V19 extends Migration $status = 'verified'; } + $projectId = $domain->getAttribute('projectId'); + $projectInternalId = $domain->getAttribute('projectInternalId'); + + if (empty($projectId) || empty($projectInternalId)) { + Console::warning("Error migrating domain {$domain->getAttribute('domain')}: Missing projectId or projectInternalId"); + continue; + } + $ruleDocument = new Document([ 'projectId' => $domain->getAttribute('projectId'), 'projectInternalId' => $domain->getAttribute('projectInternalId'), From 9aeb15c3378f9c89dec9ecc16dcb63703168192a Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Sat, 2 Sep 2023 18:35:26 -0700 Subject: [PATCH 4/7] Fix variables migration Set new resourceType attribute to function since all variables were function variables. --- src/Appwrite/Migration/Version/V19.php | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/Appwrite/Migration/Version/V19.php b/src/Appwrite/Migration/Version/V19.php index e9f50357d1..ba40a50976 100644 --- a/src/Appwrite/Migration/Version/V19.php +++ b/src/Appwrite/Migration/Version/V19.php @@ -695,6 +695,9 @@ class V19 extends Migration $document->setAttribute('smtp', []); $document->setAttribute('templates', []); + break; + case 'variables': + $document->setAttribute('resourceType', 'function'); break; default: break; From 87b0851092ad64d97bd06bcbad3075974d7230ec Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Sat, 2 Sep 2023 19:13:15 -0700 Subject: [PATCH 5/7] Fix executions migrations --- src/Appwrite/Migration/Version/V19.php | 43 +++++++++++++++++++++----- 1 file changed, 35 insertions(+), 8 deletions(-) diff --git a/src/Appwrite/Migration/Version/V19.php b/src/Appwrite/Migration/Version/V19.php index ba40a50976..d5011909ce 100644 --- a/src/Appwrite/Migration/Version/V19.php +++ b/src/Appwrite/Migration/Version/V19.php @@ -295,16 +295,31 @@ class V19 extends Migration break; case 'executions': - try { - $this->createAttributeFromCollection($this->projectDB, $id, 'functionInternalId'); - } catch (\Throwable $th) { - Console::warning("'functionInternalId' from {$id}: {$th->getMessage()}"); + $attributesToCreate = [ + 'functionInternalId', + 'deploymentInternalId', + 'requestMethod', + 'requestPath', + 'requestHeaders', + 'responseHeaders', + ]; + foreach ($attributesToCreate as $attribute) { + try { + $this->createAttributeFromCollection($this->projectDB, $id, $attribute); + } catch (\Throwable $th) { + Console::warning("$attribute from {$id}: {$th->getMessage()}"); + } } - try { - $this->createAttributeFromCollection($this->projectDB, $id, 'deploymentInternalId'); - } catch (\Throwable $th) { - Console::warning("'deploymentInternalId' from {$id}: {$th->getMessage()}"); + $attributesToDelete = [ + 'response' + ]; + foreach ($attributesToDelete as $attribute) { + try { + $this->projectDB->deleteAttribute($id, $attribute); + } catch (\Throwable $th) { + Console::warning("'$attribute' from {$id}: {$th->getMessage()}"); + } } try { @@ -325,6 +340,18 @@ class V19 extends Migration Console::warning("'responseStatusCode' from {$id}: {$th->getMessage()}"); } + try { + $this->projectDB->deleteIndex($id, '_key_statusCode'); + } catch (\Throwable $th) { + Console::warning("'_key_statusCode' from {$id}: {$th->getMessage()}"); + } + + try { + $this->createIndexFromCollection($this->projectDB, $id, '_key_responseStatusCode'); + } catch (\Throwable $th) { + Console::warning("'_key_responseStatusCode' from {$id}: {$th->getMessage()}"); + } + $this->projectDB->deleteCachedCollection($id); break; From 605ee2f7c58de61110cb7f1c689e8157245ad2d1 Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Sat, 2 Sep 2023 22:44:10 -0700 Subject: [PATCH 6/7] Update create deployment default commands Default to function commands only if commands weren't supplied. --- app/controllers/api/functions.php | 4 +--- src/Appwrite/Migration/Version/V19.php | 2 +- 2 files changed, 2 insertions(+), 4 deletions(-) diff --git a/app/controllers/api/functions.php b/app/controllers/api/functions.php index bbdd12c85f..5c8499cdf3 100644 --- a/app/controllers/api/functions.php +++ b/app/controllers/api/functions.php @@ -1062,11 +1062,9 @@ App::post('/v1/functions/:functionId/deployments') } if ($commands === null) { - $commands = $function->getAttribute('entrypoint', ''); + $commands = $function->getAttribute('commands', ''); } - $commands = $function->getAttribute('commands', ''); - if (empty($entrypoint)) { throw new Exception(Exception::FUNCTION_ENTRYPOINT_MISSING); } diff --git a/src/Appwrite/Migration/Version/V19.php b/src/Appwrite/Migration/Version/V19.php index d5011909ce..e7cef65738 100644 --- a/src/Appwrite/Migration/Version/V19.php +++ b/src/Appwrite/Migration/Version/V19.php @@ -615,7 +615,7 @@ class V19 extends Migration { $runtime = $function->getAttribute('runtime'); $language = explode('-', $runtime)[0]; - $commands = match($language) { + $commands = match ($language) { 'dart' => 'dart pub get', 'deno' => 'deno cache ' . $function->getAttribute('entrypoint'), 'dotnet' => 'dotnet restore', From c7929572f3459a6edba9b4796b85374f45df2a58 Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Sat, 2 Sep 2023 22:45:37 -0700 Subject: [PATCH 7/7] Fix builds migration --- src/Appwrite/Migration/Version/V19.php | 23 +++++++++++++++-------- 1 file changed, 15 insertions(+), 8 deletions(-) diff --git a/src/Appwrite/Migration/Version/V19.php b/src/Appwrite/Migration/Version/V19.php index e7cef65738..49cfd687bb 100644 --- a/src/Appwrite/Migration/Version/V19.php +++ b/src/Appwrite/Migration/Version/V19.php @@ -190,16 +190,23 @@ class V19 extends Migration break; case 'builds': - try { - $this->createAttributeFromCollection($this->projectDB, $id, 'deploymentInternalId'); - } catch (\Throwable $th) { - Console::warning("'deploymentInternalId' from {$id}: {$th->getMessage()}"); + $attributesToCreate = [ + 'size', + 'deploymentInternalId', + 'logs', + ]; + foreach ($attributesToCreate as $attribute) { + try { + $this->createAttributeFromCollection($this->projectDB, $id, $attribute); + } catch (\Throwable $th) { + Console::warning("$attribute from {$id}: {$th->getMessage()}"); + } } try { - $this->createAttributeFromCollection($this->projectDB, $id, 'logs'); + $this->projectDB->renameAttribute($id, 'outputPath', 'path'); } catch (\Throwable $th) { - Console::warning("'logs' from {$id}: {$th->getMessage()}"); + Console::warning("'path' from {$id}: {$th->getMessage()}"); } $this->projectDB->deleteCachedCollection($id); @@ -209,13 +216,13 @@ class V19 extends Migration try { $this->projectDB->renameAttribute($id, 'log', 'logs'); } catch (\Throwable $th) { - Console::warning("'errors' from {$id}: {$th->getMessage()}"); + Console::warning("'logs' from {$id}: {$th->getMessage()}"); } try { $this->projectDB->updateAttribute($id, 'logs', size: 1000000); } catch (\Throwable $th) { - Console::warning("'errors' from {$id}: {$th->getMessage()}"); + Console::warning("'logs' from {$id}: {$th->getMessage()}"); } $this->projectDB->deleteCachedCollection($id);