mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge pull request #184 from utopia-php/add-project-variable-migration
Add project variable migration support
This commit is contained in:
11 files changed
+288
-2
No files matched your search
@@ -56,6 +56,7 @@ use Utopia\Migration\Resources\Messaging\Message;
|
||||
use Utopia\Migration\Resources\Messaging\Provider;
|
||||
use Utopia\Migration\Resources\Messaging\Subscriber;
|
||||
use Utopia\Migration\Resources\Messaging\Topic;
|
||||
use Utopia\Migration\Resources\Settings\ProjectVariable;
|
||||
use Utopia\Migration\Resources\Sites\Deployment as SiteDeployment;
|
||||
use Utopia\Migration\Resources\Sites\EnvVar as SiteEnvVar;
|
||||
use Utopia\Migration\Resources\Sites\Site;
|
||||
@@ -278,6 +279,9 @@ class Appwrite extends Destination
|
||||
Resource::TYPE_PLATFORM,
|
||||
Resource::TYPE_API_KEY,
|
||||
|
||||
// Settings
|
||||
Resource::TYPE_PROJECT_VARIABLE,
|
||||
|
||||
// Backups
|
||||
Resource::TYPE_BACKUP_POLICY,
|
||||
];
|
||||
@@ -438,6 +442,7 @@ class Appwrite extends Destination
|
||||
Transfer::GROUP_SITES => $this->importSiteResource($resource),
|
||||
Transfer::GROUP_INTEGRATIONS => $this->importIntegrationsResource($resource),
|
||||
Transfer::GROUP_BACKUPS => $this->importBackupResource($resource),
|
||||
Transfer::GROUP_SETTINGS => $this->importSettingsResource($resource),
|
||||
default => throw new \Exception('Invalid resource group', Exception::CODE_VALIDATION),
|
||||
};
|
||||
} catch (\Throwable $e) {
|
||||
@@ -3089,6 +3094,61 @@ class Appwrite extends Destination
|
||||
return $resource;
|
||||
}
|
||||
|
||||
public function importSettingsResource(Resource $resource): Resource
|
||||
{
|
||||
switch ($resource->getName()) {
|
||||
case Resource::TYPE_PROJECT_VARIABLE:
|
||||
/** @var ProjectVariable $resource */
|
||||
$this->createProjectVariable($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', [
|
||||
Query::equal('resourceType', ['project']),
|
||||
Query::equal('key', [$resource->getKey()]),
|
||||
]);
|
||||
|
||||
if ($existing !== false && !$existing->isEmpty()) {
|
||||
$resource->setStatus(Resource::STATUS_SKIPPED, 'Project variable already exists');
|
||||
return false;
|
||||
}
|
||||
|
||||
$createdAt = $this->normalizeDateTime($resource->getCreatedAt());
|
||||
$updatedAt = $this->normalizeDateTime($resource->getUpdatedAt(), $createdAt);
|
||||
$variableId = ID::unique();
|
||||
$key = $resource->getKey();
|
||||
|
||||
try {
|
||||
$this->dbForProject->createDocument('variables', new UtopiaDocument([
|
||||
'$id' => $variableId,
|
||||
'$permissions' => $resource->getPermissions(),
|
||||
'resourceInternalId' => '',
|
||||
'resourceId' => '',
|
||||
'resourceType' => 'project',
|
||||
'key' => $key,
|
||||
'value' => $resource->getValue(),
|
||||
'secret' => $resource->isSecret(),
|
||||
'search' => \implode(' ', [$variableId, $key, 'project']),
|
||||
'$createdAt' => $createdAt,
|
||||
'$updatedAt' => $updatedAt,
|
||||
]));
|
||||
} catch (DuplicateException) {
|
||||
$resource->setStatus(Resource::STATUS_SKIPPED, 'Project variable already exists');
|
||||
return false;
|
||||
}
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Throwable
|
||||
*/
|
||||
|
||||
@@ -74,8 +74,12 @@ abstract class Resource implements \JsonSerializable
|
||||
// Integrations
|
||||
public const TYPE_PLATFORM = 'platform';
|
||||
public const TYPE_API_KEY = 'api-key';
|
||||
public const TYPE_SUBSCRIBER = 'subscriber';
|
||||
|
||||
// Settings
|
||||
public const TYPE_PROJECT_VARIABLE = 'project-variable';
|
||||
|
||||
// Messaging
|
||||
public const TYPE_SUBSCRIBER = 'subscriber';
|
||||
public const TYPE_MESSAGE = 'message';
|
||||
|
||||
// Backups
|
||||
@@ -114,6 +118,7 @@ abstract class Resource implements \JsonSerializable
|
||||
self::TYPE_MEMBERSHIP,
|
||||
self::TYPE_PLATFORM,
|
||||
self::TYPE_API_KEY,
|
||||
self::TYPE_PROJECT_VARIABLE,
|
||||
self::TYPE_PROVIDER,
|
||||
self::TYPE_TOPIC,
|
||||
self::TYPE_SUBSCRIBER,
|
||||
|
||||
@@ -0,0 +1,78 @@
|
||||
<?php
|
||||
|
||||
namespace Utopia\Migration\Resources\Settings;
|
||||
|
||||
use Utopia\Migration\Resource;
|
||||
use Utopia\Migration\Transfer;
|
||||
|
||||
class ProjectVariable extends Resource
|
||||
{
|
||||
public function __construct(
|
||||
string $id,
|
||||
private readonly string $key,
|
||||
private readonly string $value = '',
|
||||
private readonly bool $secret = false,
|
||||
string $createdAt = '',
|
||||
string $updatedAt = '',
|
||||
) {
|
||||
$this->id = $id;
|
||||
$this->createdAt = $createdAt;
|
||||
$this->updatedAt = $updatedAt;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $array
|
||||
* @return self
|
||||
*/
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
return new self(
|
||||
$array['id'],
|
||||
$array['key'],
|
||||
$array['value'] ?? '',
|
||||
(bool) ($array['secret'] ?? false),
|
||||
createdAt: $array['createdAt'] ?? '',
|
||||
updatedAt: $array['updatedAt'] ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'key' => $this->key,
|
||||
'value' => $this->value,
|
||||
'secret' => $this->secret,
|
||||
'createdAt' => $this->createdAt,
|
||||
'updatedAt' => $this->updatedAt,
|
||||
];
|
||||
}
|
||||
|
||||
public static function getName(): string
|
||||
{
|
||||
return Resource::TYPE_PROJECT_VARIABLE;
|
||||
}
|
||||
|
||||
public function getGroup(): string
|
||||
{
|
||||
return Transfer::GROUP_SETTINGS;
|
||||
}
|
||||
|
||||
public function getKey(): string
|
||||
{
|
||||
return $this->key;
|
||||
}
|
||||
|
||||
public function getValue(): string
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
public function isSecret(): bool
|
||||
{
|
||||
return $this->secret;
|
||||
}
|
||||
}
|
||||
@@ -56,6 +56,11 @@ abstract class Source extends Target
|
||||
return static::$defaultBatchSize;
|
||||
}
|
||||
|
||||
public function getSettingsBatchSize(): int
|
||||
{
|
||||
return static::$defaultBatchSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<Resource> $resources
|
||||
* @return void
|
||||
@@ -121,6 +126,7 @@ abstract class Source extends Target
|
||||
Transfer::GROUP_SITES => Transfer::GROUP_SITES_RESOURCES,
|
||||
Transfer::GROUP_INTEGRATIONS => Transfer::GROUP_INTEGRATIONS_RESOURCES,
|
||||
Transfer::GROUP_BACKUPS => Transfer::GROUP_BACKUPS_RESOURCES,
|
||||
Transfer::GROUP_SETTINGS => Transfer::GROUP_SETTINGS_RESOURCES,
|
||||
];
|
||||
|
||||
foreach ($mapping as $group => $resources) {
|
||||
@@ -161,6 +167,9 @@ abstract class Source extends Target
|
||||
case Transfer::GROUP_BACKUPS:
|
||||
$this->exportGroupBackups($this->getBackupsBatchSize(), $resources);
|
||||
break;
|
||||
case Transfer::GROUP_SETTINGS:
|
||||
$this->exportGroupSettings($this->getSettingsBatchSize(), $resources);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -228,4 +237,12 @@ abstract class Source extends Target
|
||||
* @param array<string> $resources Resources to export
|
||||
*/
|
||||
abstract protected function exportGroupBackups(int $batchSize, array $resources): void;
|
||||
|
||||
/**
|
||||
* Export Settings Group
|
||||
*
|
||||
* @param int $batchSize
|
||||
* @param array<string> $resources Resources to export
|
||||
*/
|
||||
abstract protected function exportGroupSettings(int $batchSize, array $resources): void;
|
||||
}
|
||||
@@ -61,6 +61,7 @@ use Utopia\Migration\Resources\Messaging\Message;
|
||||
use Utopia\Migration\Resources\Messaging\Provider;
|
||||
use Utopia\Migration\Resources\Messaging\Subscriber;
|
||||
use Utopia\Migration\Resources\Messaging\Topic;
|
||||
use Utopia\Migration\Resources\Settings\ProjectVariable;
|
||||
use Utopia\Migration\Resources\Sites\Deployment as SiteDeployment;
|
||||
use Utopia\Migration\Resources\Sites\EnvVar as SiteEnvVar;
|
||||
use Utopia\Migration\Resources\Sites\Site;
|
||||
@@ -214,6 +215,7 @@ class Appwrite extends Source
|
||||
Resource::TYPE_BACKUP_POLICY,
|
||||
|
||||
// Settings
|
||||
Resource::TYPE_PROJECT_VARIABLE,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -254,6 +256,7 @@ class Appwrite extends Source
|
||||
$this->reportSites($resources, $report, $resourceIds);
|
||||
$this->reportIntegrations($resources, $report, $resourceIds);
|
||||
$this->reportBackups($resources, $report, $resourceIds);
|
||||
$this->reportSettings($resources, $report, $resourceIds);
|
||||
|
||||
$report['version'] = $this->call(
|
||||
'GET',
|
||||
@@ -1449,6 +1452,90 @@ class Appwrite extends Source
|
||||
}
|
||||
}
|
||||
|
||||
private function reportSettings(array $resources, array &$report, array $resourceIds = []): void
|
||||
{
|
||||
if (\in_array(Resource::TYPE_PROJECT_VARIABLE, $resources)) {
|
||||
$variableQueries = $this->buildQueries(
|
||||
resourceType: Resource::TYPE_PROJECT_VARIABLE,
|
||||
resourceIds: $resourceIds,
|
||||
limit: 1
|
||||
);
|
||||
try {
|
||||
$report[Resource::TYPE_PROJECT_VARIABLE] = $this->project->listVariables($variableQueries)->total;
|
||||
} catch (\Throwable) {
|
||||
$report[Resource::TYPE_PROJECT_VARIABLE] = 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @param int $batchSize
|
||||
* @param array<string> $resources
|
||||
*/
|
||||
protected function exportGroupSettings(int $batchSize, array $resources): void
|
||||
{
|
||||
if (\in_array(Resource::TYPE_PROJECT_VARIABLE, $resources)) {
|
||||
try {
|
||||
$this->exportProjectVariables($batchSize);
|
||||
} catch (\Throwable $e) {
|
||||
$this->addError(new Exception(
|
||||
Resource::TYPE_PROJECT_VARIABLE,
|
||||
Transfer::GROUP_SETTINGS,
|
||||
message: $e->getMessage(),
|
||||
code: $e->getCode(),
|
||||
previous: $e
|
||||
));
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws AppwriteException
|
||||
*/
|
||||
private function exportProjectVariables(int $batchSize): void
|
||||
{
|
||||
$lastId = null;
|
||||
|
||||
while (true) {
|
||||
$queries = [Query::limit($batchSize)];
|
||||
|
||||
if ($this->rootResourceId !== '' && $this->rootResourceType === Resource::TYPE_PROJECT_VARIABLE) {
|
||||
$queries[] = Query::equal('$id', $this->rootResourceId);
|
||||
$queries[] = Query::limit(1);
|
||||
}
|
||||
|
||||
if ($lastId !== null) {
|
||||
$queries[] = Query::cursorAfter($lastId);
|
||||
}
|
||||
|
||||
$response = $this->project->listVariables($queries);
|
||||
if ($response->total === 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
$variables = [];
|
||||
|
||||
foreach ($response->variables as $variable) {
|
||||
$variables[] = new ProjectVariable(
|
||||
$variable->id,
|
||||
$variable->key,
|
||||
$variable->value,
|
||||
$variable->secret,
|
||||
createdAt: $variable->createdAt,
|
||||
updatedAt: $variable->updatedAt,
|
||||
);
|
||||
|
||||
$lastId = $variable->id;
|
||||
}
|
||||
|
||||
$this->callback($variables);
|
||||
|
||||
if (\count($response->variables) < $batchSize) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws AppwriteException
|
||||
*/
|
||||
|
||||
@@ -440,6 +440,11 @@ class CSV extends Source
|
||||
throw new \Exception('Not Implemented');
|
||||
}
|
||||
|
||||
protected function exportGroupSettings(int $batchSize, array $resources): void
|
||||
{
|
||||
throw new \Exception('Not Implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable(resource $stream, string $delimiter): void $callback
|
||||
* @return void
|
||||
|
||||
@@ -827,4 +827,9 @@ class Firebase extends Source
|
||||
{
|
||||
throw new \Exception('Not implemented');
|
||||
}
|
||||
|
||||
protected function exportGroupSettings(int $batchSize, array $resources): void
|
||||
{
|
||||
throw new \Exception('Not implemented');
|
||||
}
|
||||
}
|
||||
@@ -214,6 +214,11 @@ class JSON extends Source
|
||||
throw new \Exception('Not Implemented');
|
||||
}
|
||||
|
||||
protected function exportGroupSettings(int $batchSize, array $resources): void
|
||||
{
|
||||
throw new \Exception('Not Implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
|
||||
@@ -962,4 +962,9 @@ class NHost extends Source
|
||||
{
|
||||
throw new \Exception('Not Implemented');
|
||||
}
|
||||
|
||||
protected function exportGroupSettings(int $batchSize, array $resources): void
|
||||
{
|
||||
throw new \Exception('Not Implemented');
|
||||
}
|
||||
}
|
||||
@@ -28,6 +28,8 @@ class Transfer
|
||||
|
||||
public const GROUP_BACKUPS = 'backups';
|
||||
|
||||
public const GROUP_SETTINGS = 'settings';
|
||||
|
||||
public const GROUP_AUTH_RESOURCES = [
|
||||
Resource::TYPE_USER,
|
||||
Resource::TYPE_TEAM,
|
||||
@@ -92,7 +94,9 @@ class Transfer
|
||||
Resource::TYPE_ATTRIBUTE
|
||||
];
|
||||
|
||||
public const GROUP_SETTINGS_RESOURCES = [];
|
||||
public const GROUP_SETTINGS_RESOURCES = [
|
||||
Resource::TYPE_PROJECT_VARIABLE,
|
||||
];
|
||||
|
||||
public const GROUP_BACKUPS_RESOURCES = [
|
||||
Resource::TYPE_BACKUP_POLICY,
|
||||
@@ -132,6 +136,9 @@ class Transfer
|
||||
Resource::TYPE_PLATFORM,
|
||||
Resource::TYPE_API_KEY,
|
||||
|
||||
// Settings
|
||||
Resource::TYPE_PROJECT_VARIABLE,
|
||||
|
||||
// legacy
|
||||
Resource::TYPE_DOCUMENT,
|
||||
Resource::TYPE_ATTRIBUTE,
|
||||
@@ -417,6 +424,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_SETTINGS => array_merge($resources, self::GROUP_SETTINGS_RESOURCES),
|
||||
default => throw new \Exception('No service group found'),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -229,4 +229,15 @@ class MockSource extends Source
|
||||
$this->handleResourceTransfer(Transfer::GROUP_BACKUPS, $resource);
|
||||
}
|
||||
}
|
||||
|
||||
protected function exportGroupSettings(int $batchSize, array $resources): void
|
||||
{
|
||||
foreach (Transfer::GROUP_SETTINGS_RESOURCES as $resource) {
|
||||
if (!\in_array($resource, $resources)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->handleResourceTransfer(Transfer::GROUP_SETTINGS, $resource);
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user