diff --git a/composer.json b/composer.json index b158dbd78d..565d22d80b 100644 --- a/composer.json +++ b/composer.json @@ -64,7 +64,7 @@ "utopia-php/locale": "0.8.*", "utopia-php/logger": "0.6.*", "utopia-php/messaging": "0.20.*", - "utopia-php/migration": "1.*", + "utopia-php/migration": "1.3.*", "utopia-php/orchestration": "0.9.*", "utopia-php/platform": "0.7.*", "utopia-php/pools": "0.8.*", diff --git a/composer.lock b/composer.lock index 9409786ee0..a53b751f1d 100644 --- a/composer.lock +++ b/composer.lock @@ -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": "6943e0da7b0675c8941b1dfffc3c41fc", + "content-hash": "a3bbbe3de978c5cb73dc91c872cbc1c4", "packages": [ { "name": "adhocore/jwt", diff --git a/src/Appwrite/Platform/Workers/Deletes.php b/src/Appwrite/Platform/Workers/Deletes.php index 3e49ca3799..9687f4f4bb 100644 --- a/src/Appwrite/Platform/Workers/Deletes.php +++ b/src/Appwrite/Platform/Workers/Deletes.php @@ -185,7 +185,7 @@ class Deletes extends Action case DELETE_TYPE_MAINTENANCE: $this->deleteExpiredTargets($project, $getProjectDB); $this->deleteExecutionLogs($project, $getProjectDB, $executionRetention); - $this->deleteAuditLogs($project, $getProjectDB, $auditRetention); + $this->deleteAuditLogs($project, $getAudit, $auditRetention); $this->deleteUsageStats($project, $getProjectDB, $getLogsDB, $hourlyUsageRetentionDatetime); $this->deleteExpiredSessions($project, $getProjectDB); $this->deleteExpiredTransactions($project, $getProjectDB); diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index 7c4efc6234..17f9c6c1b0 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -112,10 +112,18 @@ class Migrations extends Action $events = $payload['events'] ?? []; $migration = new Document($payload['migration'] ?? []); + if ($migration->isEmpty()) { + throw new \Exception('Migration not found'); + } + if ($project->getId() === 'console') { return; } + if ($project->isEmpty()) { + throw new \Exception('Project not found'); + } + $this->dbForProject = $dbForProject; $this->dbForPlatform = $dbForPlatform; $this->project = $project; @@ -321,7 +329,8 @@ class Migrations extends Action array $platform, Authorization $authorization, ): void { - $project = $this->dbForPlatform->getDocument('projects', $this->project->getId()); + $project = $this->project; + $tempAPIKey = $this->generateAPIKey($project); $transfer = $source = $destination = null; @@ -395,62 +404,59 @@ class Migrations extends Action Console::error('Line: ' . $th->getLine()); Console::error($th->getTraceAsString()); - if (! $migration->isEmpty()) { - $migration->setAttribute('status', 'failed'); - $migration->setAttribute('stage', 'finished'); + $migration->setAttribute('status', 'failed'); + $migration->setAttribute('stage', 'finished'); - call_user_func($this->logError, $th, 'appwrite-worker', 'appwrite-queue-'.self::getName(), [ - 'migrationId' => $migration->getId(), - 'source' => $migration->getAttribute('source') ?? '', - 'destination' => $migration->getAttribute('destination') ?? '', - ]); + call_user_func($this->logError, $th, 'appwrite-worker', 'appwrite-queue-'.self::getName(), [ + 'migrationId' => $migration->getId(), + 'source' => $migration->getAttribute('source') ?? '', + 'destination' => $migration->getAttribute('destination') ?? '', + ]); - return; - } - - if ($transfer) { - $sourceErrors = $source->getErrors(); - $destinationErrors = $destination->getErrors(); - $migration->setAttribute('errors', $this->sanitizeErrors($sourceErrors, $destinationErrors)); - } } finally { - $this->updateMigrationDocument($migration, $project, $queueForRealtime); + try { + $this->updateMigrationDocument($migration, $project, $queueForRealtime); - if ($migration->getAttribute('status', '') === 'failed') { - Console::error('Migration('.$migration->getSequence().':'.$migration->getId().') failed, Project('.$this->project->getSequence().':'.$this->project->getId().')'); + if ($migration->getAttribute('status', '') === 'failed') { + Console::error('Migration('.$migration->getSequence().':'.$migration->getId().') failed, Project('.$this->project->getSequence().':'.$this->project->getId().')'); - $sourceErrors = $source?->getErrors() ?? []; - $destinationErrors = $destination?->getErrors() ?? []; + $sourceErrors = $source?->getErrors() ?? []; + $destinationErrors = $destination?->getErrors() ?? []; - foreach ([...$sourceErrors, ...$destinationErrors] as $error) { - /** @var MigrationException $error */ - if ($error->getCode() === 0 || $error->getCode() >= 500) { - ($this->logError)($error, 'appwrite-worker', 'appwrite-queue-' . self::getName(), [ - 'migrationId' => $migration->getId(), - 'source' => $migration->getAttribute('source') ?? '', - 'destination' => $migration->getAttribute('destination') ?? '', - 'resourceName' => $error->getResourceName(), - 'resourceGroup' => $error->getResourceGroup(), - ]); + foreach ([...$sourceErrors, ...$destinationErrors] as $error) { + /** @var MigrationException $error */ + if ($error->getCode() === 0 || $error->getCode() >= 500) { + ($this->logError)($error, 'appwrite-worker', 'appwrite-queue-' . self::getName(), [ + 'migrationId' => $migration->getId(), + 'source' => $migration->getAttribute('source') ?? '', + 'destination' => $migration->getAttribute('destination') ?? '', + 'resourceName' => $error->getResourceName(), + 'resourceGroup' => $error->getResourceGroup(), + ]); + } + } + + $source?->error(); + $destination?->error(); + } + + if ($migration->getAttribute('status', '') === 'completed') { + $destination?->success(); + $source?->success(); + + // todo: Move to CSV hook + if ($migration->getAttribute('destination') === DestinationCSV::getName()) { + $this->handleCSVExportComplete($project, $migration, $queueForMails, $queueForRealtime, $platform); } } + } finally { + $source?->cleanUp(); + $destination?->cleanUp(); - $source?->error(); - $destination?->error(); + $transfer = null; + $source = null; + $destination = null; } - - if ($migration->getAttribute('status', '') === 'completed') { - $destination?->success(); - $source?->success(); - - if ($migration->getAttribute('destination') === DestinationCSV::getName()) { - $this->handleCSVExportComplete($project, $migration, $queueForMails, $queueForRealtime, $platform, $authorization); - } - } - - $transfer = null; - $source = null; - $destination = null; } }