Merge pull request #187 from utopia-php/add-smtp-migration

Add SMTP migration
This commit is contained in:
Jake Barnby authored and GitHub committed 2026-06-03 18:07:11 +12:00
commit b07a09f651
7 files changed
+215

No files matched your search

+36
View File
@@ -64,6 +64,7 @@ use Utopia\Migration\Resources\Settings\Labels;
use Utopia\Migration\Resources\Settings\ProjectVariable;
use Utopia\Migration\Resources\Settings\Protocols;
use Utopia\Migration\Resources\Settings\Services as ServicesResource;
use Utopia\Migration\Resources\Settings\SMTP;
use Utopia\Migration\Resources\Settings\Webhook;
use Utopia\Migration\Resources\Sites\Deployment as SiteDeployment;
use Utopia\Migration\Resources\Sites\EnvVar as SiteEnvVar;
@@ -291,6 +292,7 @@ class Appwrite extends Destination
Resource::TYPE_PLATFORM,
Resource::TYPE_API_KEY,
Resource::TYPE_WEBHOOK,
Resource::TYPE_SMTP,
// Project
Resource::TYPE_PROJECT_VARIABLE,
@@ -3113,6 +3115,10 @@ class Appwrite extends Destination
/** @var Webhook $resource */
$this->createWebhook($resource);
break;
case Resource::TYPE_SMTP:
/** @var SMTP $resource */
$this->createSMTP($resource);
break;
}
if ($resource->getStatus() !== Resource::STATUS_SKIPPED) {
@@ -3244,6 +3250,36 @@ class Appwrite extends Destination
return true;
}
/**
* Password is intentionally not copied — source API never exposes it.
* Read-then-merge preserves the destination's existing password.
*/
protected function createSMTP(SMTP $resource): bool
{
$project = $this->dbForPlatform->getDocument('projects', $this->projectId);
$smtp = $project->getAttribute('smtp', []);
$smtp['enabled'] = $resource->getEnabled();
$smtp['senderName'] = $resource->getSenderName();
$smtp['senderEmail'] = $resource->getSenderEmail();
$smtp['replyToName'] = $resource->getReplyToName();
$smtp['replyToEmail'] = $resource->getReplyToEmail();
$smtp['host'] = $resource->getHost();
$smtp['port'] = $resource->getPort();
$smtp['username'] = $resource->getUsername();
$smtp['secure'] = $resource->getSecure();
$this->dbForPlatform->getAuthorization()->skip(fn () => $this->dbForPlatform->updateDocument(
'projects',
$this->projectId,
new UtopiaDocument(['smtp' => $smtp]),
));
$this->dbForPlatform->purgeCachedDocument('projects', $this->projectId);
return true;
}
protected function createWebhook(Webhook $resource): bool
{
$existing = $this->dbForPlatform->findOne('webhooks', [
+2
View File
@@ -79,6 +79,7 @@ abstract class Resource implements \JsonSerializable
public const TYPE_PLATFORM = 'platform';
public const TYPE_API_KEY = 'api-key';
public const TYPE_WEBHOOK = 'webhook';
public const TYPE_SMTP = 'smtp';
// Project (per-project singleton/settings resources)
public const TYPE_PROJECT_VARIABLE = 'project-variable';
@@ -129,6 +130,7 @@ abstract class Resource implements \JsonSerializable
self::TYPE_PLATFORM,
self::TYPE_API_KEY,
self::TYPE_WEBHOOK,
self::TYPE_SMTP,
self::TYPE_PROJECT_VARIABLE,
self::TYPE_PROJECT_PROTOCOLS,
self::TYPE_PROJECT_LABELS,
+129
View File
@@ -0,0 +1,129 @@
<?php
namespace Utopia\Migration\Resources\Settings;
use Utopia\Migration\Resource;
use Utopia\Migration\Transfer;
/**
* Singleton resource representing the project's custom SMTP configuration.
* Password is not migrated — the source API never exposes it.
*/
class SMTP extends Resource
{
public function __construct(
string $id,
private readonly bool $enabled = false,
private readonly string $senderName = '',
private readonly string $senderEmail = '',
private readonly string $replyToName = '',
private readonly string $replyToEmail = '',
private readonly string $host = '',
private readonly int $port = 0,
private readonly string $username = '',
private readonly string $secure = '',
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'],
(bool) ($array['enabled'] ?? false),
(string) ($array['senderName'] ?? ''),
(string) ($array['senderEmail'] ?? ''),
(string) ($array['replyToName'] ?? ''),
(string) ($array['replyToEmail'] ?? ''),
(string) ($array['host'] ?? ''),
(int) ($array['port'] ?? 0),
(string) ($array['username'] ?? ''),
(string) ($array['secure'] ?? ''),
createdAt: $array['createdAt'] ?? '',
updatedAt: $array['updatedAt'] ?? '',
);
}
/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'enabled' => $this->enabled,
'senderName' => $this->senderName,
'senderEmail' => $this->senderEmail,
'replyToName' => $this->replyToName,
'replyToEmail' => $this->replyToEmail,
'host' => $this->host,
'port' => $this->port,
'username' => $this->username,
'secure' => $this->secure,
'createdAt' => $this->createdAt,
'updatedAt' => $this->updatedAt,
];
}
public static function getName(): string
{
return Resource::TYPE_SMTP;
}
public function getGroup(): string
{
return Transfer::GROUP_INTEGRATIONS;
}
public function getEnabled(): bool
{
return $this->enabled;
}
public function getSenderName(): string
{
return $this->senderName;
}
public function getSenderEmail(): string
{
return $this->senderEmail;
}
public function getReplyToName(): string
{
return $this->replyToName;
}
public function getReplyToEmail(): string
{
return $this->replyToEmail;
}
public function getHost(): string
{
return $this->host;
}
public function getPort(): int
{
return $this->port;
}
public function getUsername(): string
{
return $this->username;
}
public function getSecure(): string
{
return $this->secure;
}
}
+44
View File
@@ -71,6 +71,7 @@ use Utopia\Migration\Resources\Settings\Labels;
use Utopia\Migration\Resources\Settings\ProjectVariable;
use Utopia\Migration\Resources\Settings\Protocols;
use Utopia\Migration\Resources\Settings\Services as ServicesResource;
use Utopia\Migration\Resources\Settings\SMTP;
use Utopia\Migration\Resources\Settings\Webhook;
use Utopia\Migration\Resources\Sites\Deployment as SiteDeployment;
use Utopia\Migration\Resources\Sites\EnvVar as SiteEnvVar;
@@ -226,6 +227,7 @@ class Appwrite extends Source
Resource::TYPE_PLATFORM,
Resource::TYPE_API_KEY,
Resource::TYPE_WEBHOOK,
Resource::TYPE_SMTP,
// Backups
Resource::TYPE_BACKUP_POLICY,
@@ -1658,6 +1660,7 @@ class Appwrite extends Source
previous: $e
));
}
}
private function exportServices(): void
@@ -1714,6 +1717,28 @@ class Appwrite extends Source
$this->callback([$protocols]);
}
private function exportSMTP(): void
{
$project = $this->project->get();
$smtp = new SMTP(
$this->projectId,
$project->smtpEnabled,
$project->smtpSenderName,
$project->smtpSenderEmail,
$project->smtpReplyToName,
$project->smtpReplyToEmail,
$project->smtpHost,
$project->smtpPort,
$project->smtpUsername,
$project->smtpSecure,
createdAt: $project->createdAt,
updatedAt: $project->updatedAt,
);
$this->callback([$smtp]);
}
/**
* @throws AppwriteException
*/
@@ -2630,6 +2655,11 @@ class Appwrite extends Source
$report[Resource::TYPE_WEBHOOK] = 0;
}
}
if (\in_array(Resource::TYPE_SMTP, $resources)) {
// Singleton — one SMTP config per project.
$report[Resource::TYPE_SMTP] = 1;
}
}
/**
@@ -2703,6 +2733,20 @@ class Appwrite extends Source
));
}
}
try {
if (\in_array(Resource::TYPE_SMTP, $resources)) {
$this->exportSMTP();
}
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_SMTP,
Transfer::GROUP_INTEGRATIONS,
message: $e->getMessage(),
code: (int) $e->getCode() ?: Exception::CODE_INTERNAL,
previous: $e
));
}
}
/**
+2
View File
@@ -68,6 +68,7 @@ class Transfer
Resource::TYPE_PLATFORM,
Resource::TYPE_API_KEY,
Resource::TYPE_WEBHOOK,
Resource::TYPE_SMTP,
];
public const GROUP_DOCUMENTSDB_RESOURCES = [
Resource::TYPE_DATABASE_DOCUMENTSDB,
@@ -144,6 +145,7 @@ class Transfer
Resource::TYPE_PLATFORM,
Resource::TYPE_API_KEY,
Resource::TYPE_WEBHOOK,
Resource::TYPE_SMTP,
// Project
Resource::TYPE_PROJECT_VARIABLE,
@@ -53,6 +53,7 @@ class MockDestination extends Destination
Resource::TYPE_MEMBERSHIP,
Resource::TYPE_PLATFORM,
Resource::TYPE_API_KEY,
Resource::TYPE_SMTP,
Resource::TYPE_PROJECT_VARIABLE,
Resource::TYPE_WEBHOOK,
Resource::TYPE_AUTH_METHODS,
@@ -82,6 +82,7 @@ class MockSource extends Source
Resource::TYPE_MEMBERSHIP,
Resource::TYPE_PLATFORM,
Resource::TYPE_API_KEY,
Resource::TYPE_SMTP,
Resource::TYPE_PROJECT_VARIABLE,
Resource::TYPE_WEBHOOK,
Resource::TYPE_AUTH_METHODS,