Add Labels migration

Singleton settings resource carrying the project's RBAC label array.
Source reads via raw GET /v1/project; destination overwrites via
Project::updateLabels() (wholesale replace).
This commit is contained in:
Prem Palanisamy committed 2026-05-18 15:18:57 +01:00
1 parent d74e8cbd19
commit b7b64ac3ed
5 files changed
+135

No files matched your search

+17
View File
@@ -60,6 +60,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\Labels;
use Utopia\Migration\Resources\Settings\ProjectVariable;
use Utopia\Migration\Resources\Settings\Protocols;
use Utopia\Migration\Resources\Settings\Webhook;
@@ -292,6 +293,7 @@ class Appwrite extends Destination
Resource::TYPE_PROJECT_VARIABLE,
Resource::TYPE_WEBHOOK,
Resource::TYPE_PROTOCOLS,
Resource::TYPE_LABELS,
// Backups
Resource::TYPE_BACKUP_POLICY,
@@ -3124,6 +3126,10 @@ class Appwrite extends Destination
/** @var Protocols $resource */
$this->createProtocols($resource);
break;
case Resource::TYPE_LABELS:
/** @var Labels $resource */
$this->createLabels($resource);
break;
}
if ($resource->getStatus() !== Resource::STATUS_SKIPPED) {
@@ -3192,6 +3198,17 @@ class Appwrite extends Destination
return true;
}
/**
* Overwrite destination labels with the source array. Project::updateLabels
* is a wholesale replace, so the source's array is authoritative.
*/
protected function createLabels(Labels $resource): bool
{
$this->project->updateLabels($resource->getLabels());
return true;
}
protected function createWebhook(Webhook $resource): bool
{
$existing = $this->dbForPlatform->findOne('webhooks', [
+2
View File
@@ -81,6 +81,7 @@ abstract class Resource implements \JsonSerializable
public const TYPE_PROJECT_VARIABLE = 'project-variable';
public const TYPE_WEBHOOK = 'webhook';
public const TYPE_PROTOCOLS = 'protocols';
public const TYPE_LABELS = 'labels';
// Messaging
public const TYPE_SUBSCRIBER = 'subscriber';
@@ -126,6 +127,7 @@ abstract class Resource implements \JsonSerializable
self::TYPE_PROJECT_VARIABLE,
self::TYPE_WEBHOOK,
self::TYPE_PROTOCOLS,
self::TYPE_LABELS,
self::TYPE_PROVIDER,
self::TYPE_TOPIC,
self::TYPE_SUBSCRIBER,
@@ -0,0 +1,72 @@
<?php
namespace Utopia\Migration\Resources\Settings;
use Utopia\Migration\Resource;
use Utopia\Migration\Transfer;
/**
* Singleton resource carrying the project's RBAC labels (arbitrary string
* array). One per project; destination overwrites the array via
* Project::updateLabels().
*/
class Labels extends Resource
{
/**
* @param array<string> $labels
*/
public function __construct(
string $id,
private readonly array $labels = [],
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) ($array['labels'] ?? []),
createdAt: $array['createdAt'] ?? '',
updatedAt: $array['updatedAt'] ?? '',
);
}
/**
* @return array<string, mixed>
*/
public function jsonSerialize(): array
{
return [
'id' => $this->id,
'labels' => $this->labels,
'createdAt' => $this->createdAt,
'updatedAt' => $this->updatedAt,
];
}
public static function getName(): string
{
return Resource::TYPE_LABELS;
}
public function getGroup(): string
{
return Transfer::GROUP_SETTINGS;
}
/**
* @return array<string>
*/
public function getLabels(): array
{
return $this->labels;
}
}
+42
View File
@@ -63,6 +63,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\Labels;
use Utopia\Migration\Resources\Settings\ProjectVariable;
use Utopia\Migration\Resources\Settings\Protocols;
use Utopia\Migration\Resources\Settings\Webhook;
@@ -226,6 +227,7 @@ class Appwrite extends Source
Resource::TYPE_PROJECT_VARIABLE,
Resource::TYPE_WEBHOOK,
Resource::TYPE_PROTOCOLS,
Resource::TYPE_LABELS,
];
}
@@ -1541,6 +1543,11 @@ class Appwrite extends Source
// Singleton — there is exactly one protocols config per project.
$report[Resource::TYPE_PROTOCOLS] = 1;
}
if (\in_array(Resource::TYPE_LABELS, $resources)) {
// Singleton — one labels array per project.
$report[Resource::TYPE_LABELS] = 1;
}
}
/**
@@ -1590,6 +1597,41 @@ class Appwrite extends Source
previous: $e
));
}
try {
if (\in_array(Resource::TYPE_LABELS, $resources)) {
$this->exportLabels();
}
} catch (\Throwable $e) {
$this->addError(new Exception(
Resource::TYPE_LABELS,
Transfer::GROUP_SETTINGS,
message: $e->getMessage(),
code: $e->getCode(),
previous: $e
));
}
}
/**
* Read project-level labels from /v1/project. No SDK Project.get() exposed.
*/
private function exportLabels(): void
{
$response = $this->call('GET', '/v1/project');
if (!\is_array($response)) {
return;
}
$labels = new Labels(
$this->projectId,
(array) ($response['labels'] ?? []),
createdAt: $response['$createdAt'] ?? '',
updatedAt: $response['$updatedAt'] ?? '',
);
$this->callback([$labels]);
}
/**
+2
View File
@@ -99,6 +99,7 @@ class Transfer
Resource::TYPE_PROJECT_VARIABLE,
Resource::TYPE_WEBHOOK,
Resource::TYPE_PROTOCOLS,
Resource::TYPE_LABELS,
];
public const GROUP_BACKUPS_RESOURCES = [
@@ -144,6 +145,7 @@ class Transfer
Resource::TYPE_PROJECT_VARIABLE,
Resource::TYPE_WEBHOOK,
Resource::TYPE_PROTOCOLS,
Resource::TYPE_LABELS,
// legacy
Resource::TYPE_DOCUMENT,