From 8ce46c791fddd83b4d965a266503c2ae27f14325 Mon Sep 17 00:00:00 2001 From: Steven Nguyen Date: Wed, 21 May 2025 15:16:25 -0700 Subject: [PATCH] fix(migration): set owner and region while migrating rules These attributes were added to the rules collection in 1.6.2, but the migration script was not updated to set them. Because they're empty, the 1.7 migration fails while updating rules documents because region is required. --- src/Appwrite/Migration/Version/V22.php | 34 ++++++++++++++++++++------ 1 file changed, 26 insertions(+), 8 deletions(-) diff --git a/src/Appwrite/Migration/Version/V22.php b/src/Appwrite/Migration/Version/V22.php index 50d5bdbb85..a10017c99f 100644 --- a/src/Appwrite/Migration/Version/V22.php +++ b/src/Appwrite/Migration/Version/V22.php @@ -12,6 +12,7 @@ use Utopia\Database\Exception\Conflict; use Utopia\Database\Exception\Structure; use Utopia\Database\Exception\Timeout; use Utopia\Database\Query; +use Utopia\System\System; class V22 extends Migration { @@ -324,7 +325,9 @@ class V22 extends Migration 4. Fill "trigger" with "manual" 5. Fill "deploymentResourceType". If "resourceType" is "function", set "deploymentResourceType" to "function" 6. Fill "search" with "{$id} {domain}" - 7. Fill "deploymentId" and "deploymentInternalId". If "deploymentResourceType" is "function", get project DB, and find function with ID "resourceId". Then fill rule's "deploymentId" with function's "deployment", and "deploymentId" as backup + 7. Set "region" to project region + 8. Fill "owner" with "Appwrite" if "domain" ends with "functions" or "sites" + 9. Fill "deploymentId" and "deploymentInternalId". If "deploymentResourceType" is "function", get project DB, and find function with ID "resourceId". Then fill rule's "deploymentId" with function's "deployment", and "deploymentId" as backup */ $deploymentResourceType = null; @@ -346,14 +349,29 @@ class V22 extends Migration ->setAttribute('deploymentResourceType', $document->getAttribute('deploymentResourceType', $deploymentResourceType)) ->setAttribute('search', \implode(' ', [$document->getId(), $document->getAttribute('domain', '')])); + $project = $this->dbForProject->getDocument('projects', $document->getAttribute('projectId')); + + if ($project->isEmpty()) { + Console::warning("Project \"{$document->getAttribute('projectId')}\" not found for rule \"{$document->getId()}\""); + $document->setAttribute('region', System::getEnv('_APP_DEFAULT_REGION')); + break; + } + + $document->setAttribute('region', $project->getAttribute('region', System::getEnv('_APP_DEFAULT_REGION'))); + + $domain = $document->getAttribute('domain', ''); + $functionsDomain = System::getEnv('_APP_DOMAIN_FUNCTIONS', ''); + $sitesDomain = System::getEnv('_APP_DOMAIN_SITES', ''); + $owner = $document->getAttribute('owner', ''); + if ( + empty($owner) && + (!empty($functionsDomain) && \str_ends_with($domain, $functionsDomain)) || + (!empty($sitesDomain) && \str_ends_with($domain, $sitesDomain)) + ) { + $document->setAttribute('owner', 'Appwrite'); + } + if ($deploymentResourceType === 'function') { - $project = $this->dbForProject->getDocument('projects', $document->getAttribute('projectId')); - - if ($project->isEmpty()) { - Console::warning("Project \"{$document->getAttribute('projectId')}\" not found for rule \"{$document->getId()}\""); - break; - } - $dbForOwnerProject = ($this->getProjectDB)($project); $function = $dbForOwnerProject->getDocument('functions', $resourceId);