diff --git a/src/Appwrite/Platform/Workers/Migrations.php b/src/Appwrite/Platform/Workers/Migrations.php index 6b0a33dfd7..a6f65fc742 100644 --- a/src/Appwrite/Platform/Workers/Migrations.php +++ b/src/Appwrite/Platform/Workers/Migrations.php @@ -425,6 +425,8 @@ class Migrations extends Action 'targets.write', 'webhooks.read', 'webhooks.write', + 'rules.read', + 'rules.write', 'project.read', 'project.write', 'keys.read', diff --git a/src/Appwrite/Utopia/Response/Model/MigrationReport.php b/src/Appwrite/Utopia/Response/Model/MigrationReport.php index de4c7111c3..b02b26f395 100644 --- a/src/Appwrite/Utopia/Response/Model/MigrationReport.php +++ b/src/Appwrite/Utopia/Response/Model/MigrationReport.php @@ -113,6 +113,12 @@ class MigrationReport extends Model 'default' => 0, 'example' => 1, ]) + ->addRule(Resource::TYPE_RULE, [ + 'type' => self::TYPE_INTEGER, + 'description' => 'Number of custom-domain proxy rules to be migrated. Auto-generated `.appwrite.network` rules are skipped — they are recreated by parent Function/Site migration.', + 'default' => 0, + 'example' => 5, + ]) ->addRule(Resource::TYPE_SITE, [ 'type' => self::TYPE_INTEGER, 'description' => 'Number of sites to be migrated.', diff --git a/tests/e2e/Services/Migrations/MigrationsBase.php b/tests/e2e/Services/Migrations/MigrationsBase.php index 6b44c857a4..3f575e515a 100644 --- a/tests/e2e/Services/Migrations/MigrationsBase.php +++ b/tests/e2e/Services/Migrations/MigrationsBase.php @@ -3198,6 +3198,72 @@ trait MigrationsBase $this->client->call(Client::METHOD_PATCH, '/project/smtp', $destinationKeyHeaders, ['enabled' => false]); } + public function testAppwriteMigrationCustomDomains(): void + { + $sourceHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getProject()['$id'], + 'x-appwrite-key' => $this->getProject()['apiKey'], + ]; + + $destinationHeaders = [ + 'content-type' => 'application/json', + 'x-appwrite-project' => $this->getDestinationProject()['$id'], + 'x-appwrite-key' => $this->getDestinationProject()['apiKey'], + ]; + + // Domains are globally unique across projects; orphans from prior failed runs + // poison both source list+report (returns extra rules) and destination create + // (409 conflict). Sweep both before creating the new rule. + foreach ([$sourceHeaders, $destinationHeaders] as $headers) { + $existing = $this->client->call(Client::METHOD_GET, '/proxy/rules', $headers); + if ($existing['headers']['status-code'] === 200) { + foreach ($existing['body']['rules'] ?? [] as $r) { + if (\str_ends_with($r['domain'] ?? '', '-migration-api.myapp.com')) { + $this->client->call(Client::METHOD_DELETE, '/proxy/rules/' . $r['$id'], $headers); + } + } + } + } + + // Unique domain so re-runs and parallel suites can't collide on the + // global domain uniqueness check. + $domain = \uniqid() . '-migration-api.myapp.com'; + + $createResp = $this->client->call(Client::METHOD_POST, '/proxy/rules/api', $sourceHeaders, [ + 'domain' => $domain, + ]); + $this->assertEquals(201, $createResp['headers']['status-code']); + $sourceRule = $createResp['body']; + + $result = $this->performMigrationSync([ + 'resources' => [ + Resource::TYPE_RULE, + ], + 'endpoint' => $this->webEndpoint, + 'projectId' => $this->getProject()['$id'], + 'apiKey' => $this->getProject()['apiKey'], + ]); + + $this->assertEquals('completed', $result['status']); + $this->assertEquals([Resource::TYPE_RULE], $result['resources']); + $this->assertArrayHasKey(Resource::TYPE_RULE, $result['statusCounters']); + $this->assertEquals(0, $result['statusCounters'][Resource::TYPE_RULE]['error']); + $this->assertEquals(0, $result['statusCounters'][Resource::TYPE_RULE]['pending']); + $this->assertEquals(0, $result['statusCounters'][Resource::TYPE_RULE]['processing']); + // Domain uniqueness is enforced globally across projects. In this single-server + // E2E setup the source project still owns the domain when the migration runs, + // so the destination create hits the cross-project 409 — the destination must + // surface a WARNING, not a hard error, so the rest of the migration continues. + // (In a real self-hosted-to-cloud migration the source domain is on a separate + // server, so this conflict does not occur and we'd see `success` instead.) + $this->assertEquals(0, $result['statusCounters'][Resource::TYPE_RULE]['success']); + $this->assertGreaterThanOrEqual(1, $result['statusCounters'][Resource::TYPE_RULE]['warning']); + + // Cleanup on source + $this->client->call(Client::METHOD_DELETE, '/proxy/rules/' . $sourceRule['$id'], $sourceHeaders); + } + /** * Import documents from a CSV file. */