Add Custom Domains migration

This commit is contained in:
Prem Palanisamy committed 2026-05-21 11:22:19 +01:00
1 parent 529a3f933e
commit 7af2ef5b76
7 files changed
+357

No files matched your search

+114
View File
@@ -11,12 +11,15 @@ use Appwrite\Enums\Framework;
use Appwrite\Enums\PasswordHash;
use Appwrite\Enums\ProjectProtocolId;
use Appwrite\Enums\ProjectServiceId;
use Appwrite\Enums\ProxyResourceType;
use Appwrite\Enums\Runtime;
use Appwrite\Enums\SmtpEncryption;
use Appwrite\Enums\StatusCode;
use Appwrite\InputFile;
use Appwrite\Services\Functions;
use Appwrite\Services\Messaging;
use Appwrite\Services\Project;
use Appwrite\Services\Proxy;
use Appwrite\Services\Sites;
use Appwrite\Services\Storage;
use Appwrite\Services\Teams;
@@ -52,6 +55,7 @@ use Utopia\Migration\Resources\Database\Database;
use Utopia\Migration\Resources\Database\Index;
use Utopia\Migration\Resources\Database\Row;
use Utopia\Migration\Resources\Database\Table;
use Utopia\Migration\Resources\Domains\Rule;
use Utopia\Migration\Resources\Functions\Deployment;
use Utopia\Migration\Resources\Functions\EnvVar;
use Utopia\Migration\Resources\Functions\Func;
@@ -107,6 +111,7 @@ class Appwrite extends Destination
private Functions $functions;
private Messaging $messaging;
private Project $project;
private Proxy $proxy;
private Sites $sites;
private Storage $storage;
private Teams $teams;
@@ -192,6 +197,7 @@ class Appwrite extends Destination
$this->functions = new Functions($this->client);
$this->messaging = new Messaging($this->client);
$this->project = new Project($this->client);
$this->proxy = new Proxy($this->client);
$this->sites = new Sites($this->client);
$this->storage = new Storage($this->client);
$this->teams = new Teams($this->client);
@@ -303,6 +309,9 @@ class Appwrite extends Destination
// Backups
Resource::TYPE_BACKUP_POLICY,
// Domains
Resource::TYPE_RULE,
];
}
@@ -462,6 +471,7 @@ class Appwrite extends Destination
Transfer::GROUP_INTEGRATIONS => $this->importIntegrationsResource($resource),
Transfer::GROUP_BACKUPS => $this->importBackupResource($resource),
Transfer::GROUP_SETTINGS => $this->importSettingsResource($resource),
Transfer::GROUP_DOMAINS => $this->importDomainsResource($resource),
default => throw new \Exception('Invalid resource group', Exception::CODE_VALIDATION),
};
} catch (\Throwable $e) {
@@ -3157,6 +3167,25 @@ class Appwrite extends Destination
return $resource;
}
public function importDomainsResource(Resource $resource): Resource
{
switch ($resource->getName()) {
case Resource::TYPE_RULE:
/** @var Rule $resource */
$success = $this->createRule($resource);
if (!$success) {
return $resource;
}
break;
}
if ($resource->getStatus() !== Resource::STATUS_SKIPPED) {
$resource->setStatus(Resource::STATUS_SUCCESS);
}
return $resource;
}
protected function createProjectVariable(ProjectVariable $resource): bool
{
$existing = $this->dbForProject->findOne('variables', [
@@ -3295,6 +3324,91 @@ class Appwrite extends Destination
return true;
}
/**
* Auto-generated rules (default `.appwrite.network` domains for functions/sites)
* are recreated automatically on the destination when the parent Function/Site
* is migrated, so only manual rules need to be imported.
*
* Function/site IDs are preserved across migration, so the source
* `deploymentResourceId` is passed through directly.
*/
protected function createRule(Rule $resource): bool
{
if ($resource->getTrigger() !== 'manual') {
$resource->setStatus(Resource::STATUS_SKIPPED, 'Auto-generated rule, recreated by parent resource migration');
return false;
}
$type = $resource->getType();
$deploymentResourceType = $resource->getDeploymentResourceType();
$branch = $resource->getDeploymentVcsProviderBranch();
try {
switch ($type) {
case 'api':
$this->proxy->createAPIRule($resource->getDomain());
break;
case 'redirect':
$statusCode = match ($resource->getRedirectStatusCode()) {
301 => StatusCode::MOVEDPERMANENTLY301(),
302 => StatusCode::FOUND302(),
307 => StatusCode::TEMPORARYREDIRECT307(),
308 => StatusCode::PERMANENTREDIRECT308(),
default => StatusCode::MOVEDPERMANENTLY301(),
};
$resourceType = $deploymentResourceType === 'site'
? ProxyResourceType::SITE()
: ProxyResourceType::FUNCTION();
$this->proxy->createRedirectRule(
$resource->getDomain(),
$resource->getRedirectUrl(),
$statusCode,
$resource->getDeploymentResourceId(),
$resourceType,
);
break;
case 'deployment':
if ($deploymentResourceType === 'function') {
$this->proxy->createFunctionRule(
$resource->getDomain(),
$resource->getDeploymentResourceId(),
$branch !== '' ? $branch : null,
);
} elseif ($deploymentResourceType === 'site') {
$this->proxy->createSiteRule(
$resource->getDomain(),
$resource->getDeploymentResourceId(),
$branch !== '' ? $branch : null,
);
} else {
$resource->setStatus(Resource::STATUS_SKIPPED, 'Unsupported deployment resource type "' . $deploymentResourceType . '"');
return false;
}
break;
default:
$resource->setStatus(Resource::STATUS_SKIPPED, 'Unsupported rule type "' . $type . '"');
return false;
}
} catch (AppwriteException $e) {
// 409 means the domain is owned by another project/organization — the
// user has to release it there before re-running. Surface as a warning,
// not an error, so the rest of the migration continues.
if ($e->getCode() === 409) {
$resource->setStatus(Resource::STATUS_WARNING, 'Domain "' . $resource->getDomain() . '" is owned by another project. Remove it there and re-run the migration.');
return false;
}
throw $e;
}
return true;
}
protected function createWebhook(Webhook $resource): bool
{
$existing = $this->dbForPlatform->findOne('webhooks', [
+4
View File
@@ -87,6 +87,9 @@ abstract class Resource implements \JsonSerializable
public const TYPE_SERVICES = 'services';
public const TYPE_SMTP = 'smtp';
// Domains
public const TYPE_RULE = 'rule';
// Messaging
public const TYPE_SUBSCRIBER = 'subscriber';
public const TYPE_MESSAGE = 'message';
@@ -135,6 +138,7 @@ abstract class Resource implements \JsonSerializable
self::TYPE_LABELS,
self::TYPE_SERVICES,
self::TYPE_SMTP,
self::TYPE_RULE,
self::TYPE_PROVIDER,
self::TYPE_TOPIC,
self::TYPE_SUBSCRIBER,
+117
View File
@@ -0,0 +1,117 @@
<?php
namespace Utopia\Migration\Resources\Domains;
use Utopia\Migration\Resource;
use Utopia\Migration\Transfer;
class Rule extends Resource
{
public function __construct(
string $id,
private readonly string $domain,
private readonly string $type,
private readonly string $trigger,
private readonly string $redirectUrl = '',
private readonly int $redirectStatusCode = 0,
private readonly string $deploymentResourceType = '',
private readonly string $deploymentResourceId = '',
private readonly string $deploymentVcsProviderBranch = '',
string $createdAt = '',
string $updatedAt = '',
) {
$this->id = $id;
$this->createdAt = $createdAt;
$this->updatedAt = $updatedAt;
}
/**
* @param array<string, mixed> $array
*/
public static function fromArray(array $array): self
{
return new self(
$array['id'],
$array['domain'],
$array['type'],
$array['trigger'] ?? 'manual',
(string) ($array['redirectUrl'] ?? ''),
(int) ($array['redirectStatusCode'] ?? 0),
(string) ($array['deploymentResourceType'] ?? ''),
(string) ($array['deploymentResourceId'] ?? ''),
(string) ($array['deploymentVcsProviderBranch'] ?? ''),
createdAt: $array['createdAt'] ?? '',
updatedAt: $array['updatedAt'] ?? '',
);
}
/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'domain' => $this->domain,
'type' => $this->type,
'trigger' => $this->trigger,
'redirectUrl' => $this->redirectUrl,
'redirectStatusCode' => $this->redirectStatusCode,
'deploymentResourceType' => $this->deploymentResourceType,
'deploymentResourceId' => $this->deploymentResourceId,
'deploymentVcsProviderBranch' => $this->deploymentVcsProviderBranch,
'createdAt' => $this->createdAt,
'updatedAt' => $this->updatedAt,
];
}
public static function getName(): string
{
return Resource::TYPE_RULE;
}
public function getGroup(): string
{
return Transfer::GROUP_DOMAINS;
}
public function getDomain(): string
{
return $this->domain;
}
public function getType(): string
{
return $this->type;
}
public function getTrigger(): string
{
return $this->trigger;
}
public function getRedirectUrl(): string
{
return $this->redirectUrl;
}
public function getRedirectStatusCode(): int
{
return $this->redirectStatusCode;
}
public function getDeploymentResourceType(): string
{
return $this->deploymentResourceType;
}
public function getDeploymentResourceId(): string
{
return $this->deploymentResourceId;
}
public function getDeploymentVcsProviderBranch(): string
{
return $this->deploymentVcsProviderBranch;
}
}
+17
View File
@@ -61,6 +61,11 @@ abstract class Source extends Target
return static::$defaultBatchSize;
}
public function getDomainsBatchSize(): int
{
return static::$defaultBatchSize;
}
/**
* @param array<Resource> $resources
* @return void
@@ -127,6 +132,7 @@ abstract class Source extends Target
Transfer::GROUP_INTEGRATIONS => Transfer::GROUP_INTEGRATIONS_RESOURCES,
Transfer::GROUP_BACKUPS => Transfer::GROUP_BACKUPS_RESOURCES,
Transfer::GROUP_SETTINGS => Transfer::GROUP_SETTINGS_RESOURCES,
Transfer::GROUP_DOMAINS => Transfer::GROUP_DOMAINS_RESOURCES,
];
foreach ($mapping as $group => $resources) {
@@ -170,6 +176,9 @@ abstract class Source extends Target
case Transfer::GROUP_SETTINGS:
$this->exportGroupSettings($this->getSettingsBatchSize(), $resources);
break;
case Transfer::GROUP_DOMAINS:
$this->exportGroupDomains($this->getDomainsBatchSize(), $resources);
break;
}
}
}
@@ -245,4 +254,12 @@ abstract class Source extends Target
* @param array<string> $resources Resources to export
*/
abstract protected function exportGroupSettings(int $batchSize, array $resources): void;
/**
* Export Domains Group
*
* @param int $batchSize
* @param array<string> $resources Resources to export
*/
abstract protected function exportGroupDomains(int $batchSize, array $resources): void;
}
+84
View File
@@ -12,6 +12,7 @@ use Appwrite\Query;
use Appwrite\Services\Functions;
use Appwrite\Services\Messaging;
use Appwrite\Services\Project;
use Appwrite\Services\Proxy;
use Appwrite\Services\Sites;
use Appwrite\Services\Storage;
use Appwrite\Services\TablesDB;
@@ -59,6 +60,7 @@ use Utopia\Migration\Resources\Database\Index;
use Utopia\Migration\Resources\Database\Row;
use Utopia\Migration\Resources\Database\Table;
use Utopia\Migration\Resources\Database\VectorsDB;
use Utopia\Migration\Resources\Domains\Rule;
use Utopia\Migration\Resources\Functions\Deployment;
use Utopia\Migration\Resources\Functions\EnvVar;
use Utopia\Migration\Resources\Functions\Func;
@@ -112,6 +114,8 @@ class Appwrite extends Source
private Webhooks $webhooks;
private Proxy $proxy;
/**
* @var callable(UtopiaDocument $database|null): UtopiaDatabase
*/
@@ -142,6 +146,7 @@ class Appwrite extends Source
$this->sites = new Sites($this->client);
$this->project = new Project($this->client);
$this->webhooks = new Webhooks($this->client);
$this->proxy = new Proxy($this->client);
$this->headers['x-appwrite-project'] = $this->projectId;
$this->headers['x-appwrite-key'] = $this->key;
@@ -238,6 +243,9 @@ class Appwrite extends Source
Resource::TYPE_LABELS,
Resource::TYPE_SERVICES,
Resource::TYPE_SMTP,
// Domains
Resource::TYPE_RULE,
];
}
@@ -279,6 +287,7 @@ class Appwrite extends Source
$this->reportIntegrations($resources, $report, $resourceIds);
$this->reportBackups($resources, $report, $resourceIds);
$this->reportSettings($resources, $report, $resourceIds);
$this->reportDomains($resources, $report, $resourceIds);
$report['version'] = $this->call(
'GET',
@@ -1569,6 +1578,17 @@ class Appwrite extends Source
}
}
private function reportDomains(array $resources, array &$report, array $resourceIds = []): void
{
if (\in_array(Resource::TYPE_RULE, $resources)) {
try {
$report[Resource::TYPE_RULE] = $this->proxy->listRules([Query::limit(1)])->total;
} catch (\Throwable) {
$report[Resource::TYPE_RULE] = 0;
}
}
}
private function reportSettings(array $resources, array &$report, array $resourceIds = []): void
{
if (\in_array(Resource::TYPE_PROJECT_VARIABLE, $resources)) {
@@ -1801,6 +1821,70 @@ class Appwrite extends Source
$this->callback([$smtp]);
}
protected function exportGroupDomains(int $batchSize, array $resources): void
{
if (\in_array(Resource::TYPE_RULE, $resources)) {
try {
$this->exportRules($batchSize);
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_RULE,
Transfer::GROUP_DOMAINS,
message: $e->getMessage(),
code: $e->getCode(),
previous: $e
));
}
}
}
/**
* @throws AppwriteException
*/
private function exportRules(int $batchSize): void
{
$lastId = null;
while (true) {
$queries = [Query::limit($batchSize)];
if ($lastId !== null) {
$queries[] = Query::cursorAfter($lastId);
}
$response = $this->proxy->listRules($queries);
if ($response->total === 0) {
break;
}
$rules = [];
foreach ($response->rules as $rule) {
$rules[] = new Rule(
$rule->id,
$rule->domain,
$rule->type,
$rule->trigger,
$rule->redirectUrl,
$rule->redirectStatusCode,
$rule->deploymentResourceType ? (string) $rule->deploymentResourceType : '',
$rule->deploymentResourceId,
$rule->deploymentVcsProviderBranch,
createdAt: $rule->createdAt,
updatedAt: $rule->updatedAt,
);
$lastId = $rule->id;
}
$this->callback($rules);
if (count($response->rules) < $batchSize) {
break;
}
}
}
/**
* @throws AppwriteException
*/
+10
View File
@@ -30,6 +30,8 @@ class Transfer
public const GROUP_SETTINGS = 'settings';
public const GROUP_DOMAINS = 'domains';
public const GROUP_AUTH_RESOURCES = [
Resource::TYPE_USER,
Resource::TYPE_TEAM,
@@ -109,6 +111,10 @@ class Transfer
Resource::TYPE_BACKUP_POLICY,
];
public const GROUP_DOMAINS_RESOURCES = [
Resource::TYPE_RULE,
];
public const GROUP_MESSAGING_RESOURCES = [
Resource::TYPE_PROVIDER,
Resource::TYPE_TOPIC,
@@ -153,6 +159,9 @@ class Transfer
Resource::TYPE_SERVICES,
Resource::TYPE_SMTP,
// Domains
Resource::TYPE_RULE,
// legacy
Resource::TYPE_DOCUMENT,
Resource::TYPE_ATTRIBUTE,
@@ -438,6 +447,7 @@ class Transfer
self::GROUP_DATABASES_VECTOR_DB => array_merge($resources, self::GROUP_VECTORSDB_RESOURCES),
self::GROUP_MESSAGING => array_merge($resources, self::GROUP_MESSAGING_RESOURCES),
self::GROUP_BACKUPS => array_merge($resources, self::GROUP_BACKUPS_RESOURCES),
self::GROUP_DOMAINS => array_merge($resources, self::GROUP_DOMAINS_RESOURCES),
default => throw new \Exception('No service group found'),
};
}
@@ -240,4 +240,15 @@ class MockSource extends Source
$this->handleResourceTransfer(Transfer::GROUP_SETTINGS, $resource);
}
}
protected function exportGroupDomains(int $batchSize, array $resources): void
{
foreach (Transfer::GROUP_DOMAINS_RESOURCES as $resource) {
if (!\in_array($resource, $resources)) {
continue;
}
$this->handleResourceTransfer(Transfer::GROUP_DOMAINS, $resource);
}
}
}