diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index 31d57198e4..7a208dfb14 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -596,6 +596,9 @@ class Migrations extends Action return; } + $destination->success(); + $source->success(); + $destinationType = $migration->getAttribute('destination'); if ($destinationType === DestinationCSV::getName() || $destinationType === DestinationJSON::getName()) { $this->handleDataExportComplete($project, $migration, $publisherForMails, $queueForRealtime, $platform, $authorization); @@ -701,8 +704,6 @@ class Migrations extends Action ...$this->resolveResourceIds($migration), ); } - $destination?->success(); - $source?->success(); } } finally { $source?->cleanup(); diff --git a/tests/unit/Platform/Workers/MigrationsTest.php b/tests/unit/Platform/Workers/MigrationsTest.php new file mode 100644 index 0000000000..c42144add3 --- /dev/null +++ b/tests/unit/Platform/Workers/MigrationsTest.php @@ -0,0 +1,248 @@ +createSourceMock(); + $destination = $this->createDestinationMock(); + + $destination + ->expects($this->once()) + ->method('success') + ->willReturnCallback(static function () use (&$events): void { + $events[] = 'destination:success'; + }); + $source + ->expects($this->once()) + ->method('success') + ->willReturnCallback(static function () use (&$events): void { + $events[] = 'source:success'; + }); + $source->expects($this->never())->method('error'); + $destination->expects($this->never())->method('error'); + + $migration = $this->createMigration(); + $processor = $this->createProcessor($source, $destination, $events); + + $this->process($processor, $migration); + + $this->assertSame('completed', $migration->getAttribute('status')); + $this->assertSame('finished', $migration->getAttribute('stage')); + $this->assertSame([ + 'persist:processing:processing', + 'persist:processing:migrating', + 'destination:success', + 'source:success', + 'persist:completed:finished', + ], $events); + } + + public function testThrowingSourceSuccessHookPersistsFailedMigrationWithoutRerunningHooks(): void + { + $events = []; + $source = $this->createSourceMock(); + $destination = $this->createDestinationMock(); + + $destination + ->expects($this->once()) + ->method('success') + ->willReturnCallback(static function () use (&$events): void { + $events[] = 'destination:success'; + }); + $source + ->expects($this->once()) + ->method('success') + ->willReturnCallback(static function () use (&$events): void { + $events[] = 'source:success'; + throw new \RuntimeException('Finalization failed'); + }); + $source + ->expects($this->once()) + ->method('error') + ->willReturnCallback(static function () use (&$events): void { + $events[] = 'source:error'; + }); + $destination + ->expects($this->once()) + ->method('error') + ->willReturnCallback(static function () use (&$events): void { + $events[] = 'destination:error'; + }); + + $migration = $this->createMigration(); + $processor = $this->createProcessor($source, $destination, $events); + + $this->process($processor, $migration); + + $this->assertSame('failed', $migration->getAttribute('status')); + $this->assertSame('finished', $migration->getAttribute('stage')); + $this->assertSame([ + 'persist:processing:processing', + 'persist:processing:migrating', + 'destination:success', + 'source:success', + 'persist:failed:finished', + 'source:error', + 'destination:error', + ], $events); + $this->assertNotContains('persist:completed:finished', $events); + } + + private function createSourceMock(): Source&MockObject + { + $target = $this->createMock(Source::class); + $target->method('getErrors')->willReturn([]); + $target->expects($this->once())->method('shutdown'); + $target->expects($this->once())->method('cleanUp'); + + return $target; + } + + private function createDestinationMock(): Destination&MockObject + { + $target = $this->createMock(Destination::class); + $target->method('getErrors')->willReturn([]); + $target->expects($this->once())->method('shutdown'); + $target->expects($this->once())->method('cleanUp'); + + return $target; + } + + private function createMigration(): Document + { + return new Document([ + '$id' => 'migration', + '$sequence' => 1, + 'credentials' => [], + 'destination' => 'TestDestination', + 'options' => [], + 'resourceId' => '', + 'resourceType' => '', + 'resources' => [], + 'source' => 'TestSource', + 'stage' => 'pending', + 'status' => 'pending', + ]); + } + + /** + * @param array $events + */ + private function createProcessor(Source $source, Destination $destination, array &$events): \Closure + { + $record = static function (string $event) use (&$events): void { + $events[] = $event; + }; + $worker = new class ($source, $destination, $record) extends Migrations { + public function __construct( + private readonly Source $migrationSource, + private readonly Destination $migrationDestination, + private readonly \Closure $record, + ) { + } + + public function process( + Document $migration, + Document $project, + Realtime $queueForRealtime, + MailPublisher $publisherForMails, + Context $usage, + UsagePublisher $publisherForUsage, + Authorization $authorization, + ): void { + $this->project = $project; + $this->logError = static function (): void { + }; + + $this->processMigration( + $migration, + $queueForRealtime, + $publisherForMails, + $usage, + $publisherForUsage, + [], + $authorization, + ); + } + + #[\Override] + protected function generateAPIKey(Document $project): string + { + return 'key'; + } + + #[\Override] + protected function processSource(Document $migration): Source + { + return $this->migrationSource; + } + + #[\Override] + protected function processDestination(Document $migration): Destination + { + return $this->migrationDestination; + } + + #[\Override] + protected function updateMigrationDocument( + Document $migration, + Document $project, + Realtime $queueForRealtime, + ): Document { + ($this->record)('persist:' + . $migration->getAttribute('status') + . ':' + . $migration->getAttribute('stage')); + + return $migration; + } + }; + + return $worker->process(...); + } + + private function process(\Closure $processor, Document $migration): void + { + $publisher = $this->createStub(Publisher::class); + $queue = new Queue('test'); + $host = \getenv('_APP_MIGRATION_HOST'); + \putenv('_APP_MIGRATION_HOST=localhost'); + + try { + $processor( + $migration, + new Document([ + '$id' => 'project', + '$sequence' => 1, + ]), + new Realtime(), + new MailPublisher($publisher, $queue), + new Context(), + new UsagePublisher($publisher, $queue), + new Authorization(), + ); + } finally { + \putenv($host === false ? '_APP_MIGRATION_HOST' : '_APP_MIGRATION_HOST=' . $host); + } + } +}