mirror of
https://github.com/appwrite/appwrite.git
synced 2026-11-18 20:36:03 +00:00
Merge remote-tracking branch 'origin/main' into feat-platform-db-access
# Conflicts: # src/Migration/Destinations/Appwrite.php # src/Migration/Source.php # src/Migration/Sources/Appwrite.php # src/Migration/Sources/CSV.php # src/Migration/Sources/Firebase.php # src/Migration/Sources/NHost.php # tests/Migration/Unit/Adapters/MockSource.php
This commit is contained in:
commit
ad72a46e27
30 files changed
+2270
-657
No files matched your search
+4
-6
@@ -22,12 +22,13 @@
|
||||
"check": "./vendor/bin/phpstan analyse --level 3 src tests --memory-limit 2G"
|
||||
},
|
||||
"require": {
|
||||
"php": ">=8.1",
|
||||
"php": ">=8.2",
|
||||
"ext-curl": "*",
|
||||
"ext-openssl": "*",
|
||||
"appwrite/appwrite": "19.*",
|
||||
|
||||
"appwrite/appwrite": "23.*",
|
||||
"utopia-php/database": "5.*",
|
||||
"utopia-php/storage": "1.0.*",
|
||||
"utopia-php/storage": "2.*",
|
||||
"utopia-php/dsn": "0.2.*",
|
||||
"halaxa/json-machine": "^1.2"
|
||||
},
|
||||
@@ -38,9 +39,6 @@
|
||||
"laravel/pint": "1.*",
|
||||
"phpstan/phpstan": "1.*"
|
||||
},
|
||||
"platform": {
|
||||
"php": "8.1"
|
||||
},
|
||||
"config": {
|
||||
"allow-plugins": {
|
||||
"php-http/discovery": true,
|
||||
|
||||
Generated
+201
-238
File diff suppressed because it is too large.
Load diff
+1015
-147
File diff suppressed because it is too large.
Load diff
@@ -316,5 +316,4 @@ class JSON extends Destination
|
||||
return $this->convertValueToJSON($item);
|
||||
}, $value);
|
||||
}
|
||||
|
||||
}
|
||||
@@ -0,0 +1,70 @@
|
||||
<?php
|
||||
|
||||
namespace Utopia\Migration\Destinations;
|
||||
|
||||
enum SchemaAction
|
||||
{
|
||||
case Create;
|
||||
case Skip;
|
||||
case Overwrite;
|
||||
}
|
||||
|
||||
enum OnDuplicate: string
|
||||
{
|
||||
case Fail = 'fail';
|
||||
case Skip = 'skip';
|
||||
case Overwrite = 'overwrite';
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
public static function values(): array
|
||||
{
|
||||
return \array_values(\array_map(fn (self $case) => $case->value, self::cases()));
|
||||
}
|
||||
|
||||
/**
|
||||
* Coarse routing for re-migration. The caller follows up with a spec-match
|
||||
* check that overrides Overwrite to Skip when source and dest
|
||||
* already have identical spec — see DestinationAppwrite for the full flow.
|
||||
*/
|
||||
public function resolveSchemaAction(
|
||||
bool $exists,
|
||||
?string $sourceUpdatedAt = null,
|
||||
?string $destUpdatedAt = null,
|
||||
): SchemaAction {
|
||||
if (!$exists) {
|
||||
return SchemaAction::Create;
|
||||
}
|
||||
return match ($this) {
|
||||
self::Fail => SchemaAction::Create,
|
||||
self::Skip => SchemaAction::Skip,
|
||||
self::Overwrite => $this->sourceIsNewer($sourceUpdatedAt, $destUpdatedAt)
|
||||
? SchemaAction::Overwrite
|
||||
: SchemaAction::Skip,
|
||||
};
|
||||
}
|
||||
|
||||
private function sourceIsNewer(?string $source, ?string $dest): bool
|
||||
{
|
||||
$src = $this->parseTimestamp($source);
|
||||
$dst = $this->parseTimestamp($dest);
|
||||
return $src !== null && $dst !== null && $src > $dst;
|
||||
}
|
||||
|
||||
/**
|
||||
* strtotime accepts '0000-00-00' leniently (returns a large negative epoch,
|
||||
* not false), so non-positive epochs are rejected too.
|
||||
*/
|
||||
private function parseTimestamp(?string $value): ?int
|
||||
{
|
||||
if ($value === null || $value === '') {
|
||||
return null;
|
||||
}
|
||||
$epoch = \strtotime($value);
|
||||
if ($epoch === false || $epoch <= 0) {
|
||||
return null;
|
||||
}
|
||||
return $epoch;
|
||||
}
|
||||
}
|
||||
@@ -77,6 +77,9 @@ abstract class Resource implements \JsonSerializable
|
||||
|
||||
public const TYPE_MESSAGE = 'message';
|
||||
|
||||
// Backups
|
||||
public const TYPE_BACKUP_POLICY = 'backup-policy';
|
||||
|
||||
// legacy terminologies
|
||||
public const TYPE_DOCUMENT = 'document';
|
||||
public const TYPE_ATTRIBUTE = 'attribute';
|
||||
@@ -113,6 +116,7 @@ abstract class Resource implements \JsonSerializable
|
||||
self::TYPE_TOPIC,
|
||||
self::TYPE_SUBSCRIBER,
|
||||
self::TYPE_MESSAGE,
|
||||
self::TYPE_BACKUP_POLICY,
|
||||
|
||||
// legacy
|
||||
self::TYPE_DOCUMENT,
|
||||
|
||||
@@ -0,0 +1,115 @@
|
||||
<?php
|
||||
|
||||
namespace Utopia\Migration\Resources\Backups;
|
||||
|
||||
use Utopia\Migration\Resource;
|
||||
use Utopia\Migration\Transfer;
|
||||
|
||||
class Policy extends Resource
|
||||
{
|
||||
/**
|
||||
* @param string $id
|
||||
* @param string $name
|
||||
* @param array<string> $services
|
||||
* @param int $retention
|
||||
* @param string $schedule
|
||||
* @param bool $enabled
|
||||
* @param string $resourceId
|
||||
* @param string $resourceType
|
||||
*/
|
||||
public function __construct(
|
||||
string $id = '',
|
||||
private readonly string $name = '',
|
||||
private readonly array $services = [],
|
||||
private readonly int $retention = 0,
|
||||
private readonly string $schedule = '',
|
||||
private readonly bool $enabled = true,
|
||||
private readonly string $resourceId = '',
|
||||
private readonly string $resourceType = '',
|
||||
) {
|
||||
$this->id = $id;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<string, mixed> $array
|
||||
* @return self
|
||||
*/
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
return new self(
|
||||
$array['id'],
|
||||
$array['name'] ?? '',
|
||||
$array['services'] ?? [],
|
||||
$array['retention'] ?? 0,
|
||||
$array['schedule'] ?? '',
|
||||
$array['enabled'] ?? true,
|
||||
$array['resourceId'] ?? '',
|
||||
$array['resourceType'] ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, mixed>
|
||||
*/
|
||||
public function jsonSerialize(): array
|
||||
{
|
||||
return [
|
||||
'id' => $this->id,
|
||||
'name' => $this->name,
|
||||
'services' => $this->services,
|
||||
'retention' => $this->retention,
|
||||
'schedule' => $this->schedule,
|
||||
'enabled' => $this->enabled,
|
||||
'resourceId' => $this->resourceId,
|
||||
'resourceType' => $this->resourceType,
|
||||
];
|
||||
}
|
||||
|
||||
public static function getName(): string
|
||||
{
|
||||
return Resource::TYPE_BACKUP_POLICY;
|
||||
}
|
||||
|
||||
public function getGroup(): string
|
||||
{
|
||||
return Transfer::GROUP_BACKUPS;
|
||||
}
|
||||
|
||||
public function getPolicyName(): string
|
||||
{
|
||||
return $this->name;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string>
|
||||
*/
|
||||
public function getServices(): array
|
||||
{
|
||||
return $this->services;
|
||||
}
|
||||
|
||||
public function getRetention(): int
|
||||
{
|
||||
return $this->retention;
|
||||
}
|
||||
|
||||
public function getSchedule(): string
|
||||
{
|
||||
return $this->schedule;
|
||||
}
|
||||
|
||||
public function getEnabled(): bool
|
||||
{
|
||||
return $this->enabled;
|
||||
}
|
||||
|
||||
public function getResourceId(): string
|
||||
{
|
||||
return $this->resourceId;
|
||||
}
|
||||
|
||||
public function getResourceType(): string
|
||||
{
|
||||
return $this->resourceType;
|
||||
}
|
||||
}
|
||||
@@ -14,6 +14,7 @@ abstract class Column extends Resource
|
||||
public const TYPE_LONGTEXT = 'longtext';
|
||||
|
||||
public const TYPE_INTEGER = 'integer';
|
||||
public const TYPE_BIG_INT = 'bigint';
|
||||
public const TYPE_FLOAT = 'double';
|
||||
public const TYPE_BOOLEAN = 'boolean';
|
||||
public const TYPE_DATETIME = 'datetime';
|
||||
|
||||
@@ -0,0 +1,108 @@
|
||||
<?php
|
||||
|
||||
namespace Utopia\Migration\Resources\Database\Columns;
|
||||
|
||||
use Utopia\Migration\Resources\Database\Column;
|
||||
use Utopia\Migration\Resources\Database\Table;
|
||||
|
||||
class BigInt extends Column
|
||||
{
|
||||
public function __construct(
|
||||
string $key,
|
||||
Table $table,
|
||||
bool $required = false,
|
||||
?int $default = null,
|
||||
bool $array = false,
|
||||
?int $min = null,
|
||||
?int $max = null,
|
||||
bool $signed = true,
|
||||
string $createdAt = '',
|
||||
string $updatedAt = ''
|
||||
) {
|
||||
$min ??= PHP_INT_MIN;
|
||||
$max ??= PHP_INT_MAX;
|
||||
$size = 8;
|
||||
|
||||
parent::__construct(
|
||||
$key,
|
||||
$table,
|
||||
size: $size,
|
||||
required: $required,
|
||||
default: $default,
|
||||
array: $array,
|
||||
signed: $signed,
|
||||
formatOptions: [
|
||||
'min' => $min,
|
||||
'max' => $max,
|
||||
],
|
||||
createdAt: $createdAt,
|
||||
updatedAt: $updatedAt
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array{
|
||||
* key: string,
|
||||
* collection?: array{
|
||||
* database: array{
|
||||
* id: string,
|
||||
* name: string,
|
||||
* },
|
||||
* name: string,
|
||||
* id: string,
|
||||
* documentSecurity: bool,
|
||||
* permissions: ?array<string>
|
||||
* },
|
||||
* table?: array{
|
||||
* database: array{
|
||||
* id: string,
|
||||
* name: string,
|
||||
* },
|
||||
* name: string,
|
||||
* id: string,
|
||||
* rowSecurity: bool,
|
||||
* permissions: ?array<string>
|
||||
* },
|
||||
* required: bool,
|
||||
* array: bool,
|
||||
* default: ?int,
|
||||
* formatOptions: array{
|
||||
* min: ?int,
|
||||
* max: ?int
|
||||
* },
|
||||
* createdAt: string,
|
||||
* updatedAt: string,
|
||||
* } $array
|
||||
* @return self
|
||||
*/
|
||||
public static function fromArray(array $array): self
|
||||
{
|
||||
return new self(
|
||||
$array['key'],
|
||||
Table::fromArray($array['table'] ?? $array['collection']),
|
||||
required: $array['required'],
|
||||
default: $array['default'],
|
||||
array: $array['array'],
|
||||
min: $array['formatOptions']['min'] ?? null,
|
||||
max: $array['formatOptions']['max'] ?? null,
|
||||
signed: $array['signed'] ?? true,
|
||||
createdAt: $array['createdAt'] ?? '',
|
||||
updatedAt: $array['updatedAt'] ?? '',
|
||||
);
|
||||
}
|
||||
|
||||
public function getType(): string
|
||||
{
|
||||
return Column::TYPE_BIG_INT;
|
||||
}
|
||||
|
||||
public function getMin(): ?int
|
||||
{
|
||||
return (int)$this->formatOptions['min'];
|
||||
}
|
||||
|
||||
public function getMax(): ?int
|
||||
{
|
||||
return (int)$this->formatOptions['max'];
|
||||
}
|
||||
}
|
||||
@@ -26,7 +26,7 @@ class Database extends Resource
|
||||
protected bool $enabled = true,
|
||||
protected string $originalId = '',
|
||||
protected string $type = '',
|
||||
protected string $database = ''
|
||||
protected ?string $database = ''
|
||||
) {
|
||||
$this->id = $id;
|
||||
}
|
||||
@@ -52,7 +52,7 @@ class Database extends Resource
|
||||
enabled: $array['enabled'] ?? true,
|
||||
originalId: $array['originalId'] ?? '',
|
||||
type: $array['type'] ?? 'legacy',
|
||||
database: $array['database']
|
||||
database: $array['database'] ?? null
|
||||
);
|
||||
}
|
||||
|
||||
@@ -97,7 +97,7 @@ class Database extends Resource
|
||||
return $this->type;
|
||||
}
|
||||
|
||||
public function getDatabase(): string
|
||||
public function getDatabase(): ?string
|
||||
{
|
||||
return $this->database;
|
||||
}
|
||||
|
||||
@@ -53,7 +53,7 @@ class Table extends Resource
|
||||
Database::fromArray($array['database']),
|
||||
name: $array['name'],
|
||||
id: $array['id'],
|
||||
rowSecurity: $array['rowSecurity'] ?? $array['documentSecurity'],
|
||||
rowSecurity: (bool) ($array['rowSecurity'] ?? $array['documentSecurity'] ?? false),
|
||||
permissions: $array['permissions'] ?? [],
|
||||
createdAt: $array['createdAt'] ?? '',
|
||||
updatedAt: $array['updatedAt'] ?? '',
|
||||
|
||||
@@ -18,6 +18,10 @@ class Func extends Resource
|
||||
* @param int $timeout
|
||||
* @param string $activeDeployment
|
||||
* @param string $entrypoint
|
||||
* @param string $commands
|
||||
* @param bool $logging
|
||||
* @param array<string> $scopes
|
||||
* @param string $specification
|
||||
*/
|
||||
public function __construct(
|
||||
string $id,
|
||||
@@ -29,7 +33,11 @@ class Func extends Resource
|
||||
private readonly string $schedule = '',
|
||||
private readonly int $timeout = 0,
|
||||
private readonly string $activeDeployment = '',
|
||||
private readonly string $entrypoint = ''
|
||||
private readonly string $entrypoint = '',
|
||||
private readonly string $commands = '',
|
||||
private readonly bool $logging = true,
|
||||
private readonly array $scopes = [],
|
||||
private readonly string $specification = ''
|
||||
) {
|
||||
$this->id = $id;
|
||||
}
|
||||
@@ -50,7 +58,11 @@ class Func extends Resource
|
||||
$array['schedule'] ?? '',
|
||||
$array['timeout'] ?? 0,
|
||||
$array['activeDeployment'] ?? '',
|
||||
$array['entrypoint'] ?? ''
|
||||
$array['entrypoint'] ?? '',
|
||||
$array['commands'] ?? '',
|
||||
$array['logging'] ?? true,
|
||||
$array['scopes'] ?? [],
|
||||
$array['specification'] ?? ''
|
||||
);
|
||||
}
|
||||
|
||||
@@ -70,6 +82,10 @@ class Func extends Resource
|
||||
'timeout' => $this->timeout,
|
||||
'activeDeployment' => $this->activeDeployment,
|
||||
'entrypoint' => $this->entrypoint,
|
||||
'commands' => $this->commands,
|
||||
'logging' => $this->logging,
|
||||
'scopes' => $this->scopes,
|
||||
'specification' => $this->specification,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -133,4 +149,27 @@ class Func extends Resource
|
||||
{
|
||||
return $this->entrypoint;
|
||||
}
|
||||
|
||||
public function getCommands(): string
|
||||
{
|
||||
return $this->commands;
|
||||
}
|
||||
|
||||
public function getLogging(): bool
|
||||
{
|
||||
return $this->logging;
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string>
|
||||
*/
|
||||
public function getScopes(): array
|
||||
{
|
||||
return $this->scopes;
|
||||
}
|
||||
|
||||
public function getSpecification(): string
|
||||
{
|
||||
return $this->specification;
|
||||
}
|
||||
}
|
||||
@@ -67,5 +67,4 @@ class EnvVar extends Resource
|
||||
{
|
||||
return $this->value;
|
||||
}
|
||||
|
||||
}
|
||||
@@ -19,6 +19,7 @@ class Bucket extends Resource
|
||||
* @param bool $encryption
|
||||
* @param bool $antiVirus
|
||||
* @param bool $updateLimits
|
||||
* @param bool $transformations
|
||||
*/
|
||||
public function __construct(
|
||||
string $id = '',
|
||||
@@ -32,6 +33,7 @@ class Bucket extends Resource
|
||||
private readonly bool $encryption = false,
|
||||
private readonly bool $antiVirus = false,
|
||||
private readonly bool $updateLimits = false,
|
||||
private readonly bool $transformations = false,
|
||||
) {
|
||||
$this->id = $id;
|
||||
$this->permissions = $permissions;
|
||||
@@ -54,7 +56,8 @@ class Bucket extends Resource
|
||||
$array['compression'] ?? 'none',
|
||||
$array['encryption'] ?? false,
|
||||
$array['antiVirus'] ?? false,
|
||||
$array['updateLimits'] ?? false
|
||||
$array['updateLimits'] ?? false,
|
||||
$array['transformations'] ?? false
|
||||
);
|
||||
}
|
||||
|
||||
@@ -74,6 +77,7 @@ class Bucket extends Resource
|
||||
'encryption' => $this->encryption,
|
||||
'antiVirus' => $this->antiVirus,
|
||||
'updateLimits' => $this->updateLimits,
|
||||
'transformations' => $this->transformations,
|
||||
];
|
||||
}
|
||||
|
||||
@@ -131,4 +135,9 @@ class Bucket extends Resource
|
||||
{
|
||||
return $this->antiVirus;
|
||||
}
|
||||
|
||||
public function getTransformations(): bool
|
||||
{
|
||||
return $this->transformations;
|
||||
}
|
||||
}
|
||||
@@ -51,6 +51,11 @@ abstract class Source extends Target
|
||||
return static::$defaultBatchSize;
|
||||
}
|
||||
|
||||
public function getBackupsBatchSize(): int
|
||||
{
|
||||
return static::$defaultBatchSize;
|
||||
}
|
||||
|
||||
/**
|
||||
* @param array<Resource> $resources
|
||||
* @return void
|
||||
@@ -115,6 +120,7 @@ abstract class Source extends Target
|
||||
Transfer::GROUP_MESSAGING => Transfer::GROUP_MESSAGING_RESOURCES,
|
||||
Transfer::GROUP_SITES => Transfer::GROUP_SITES_RESOURCES,
|
||||
Transfer::GROUP_INTEGRATIONS => Transfer::GROUP_INTEGRATIONS_RESOURCES,
|
||||
Transfer::GROUP_BACKUPS => Transfer::GROUP_BACKUPS_RESOURCES,
|
||||
];
|
||||
|
||||
foreach ($mapping as $group => $resources) {
|
||||
@@ -152,6 +158,9 @@ abstract class Source extends Target
|
||||
case Transfer::GROUP_INTEGRATIONS:
|
||||
$this->exportGroupIntegrations($this->getIntegrationsBatchSize(), $resources);
|
||||
break;
|
||||
case Transfer::GROUP_BACKUPS:
|
||||
$this->exportGroupBackups($this->getBackupsBatchSize(), $resources);
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -211,4 +220,12 @@ abstract class Source extends Target
|
||||
* @param array<string> $resources Resources to export
|
||||
*/
|
||||
abstract protected function exportGroupIntegrations(int $batchSize, array $resources): void;
|
||||
|
||||
/**
|
||||
* Export Backups Group
|
||||
*
|
||||
* @param int $batchSize
|
||||
* @param array<string> $resources Resources to export
|
||||
*/
|
||||
abstract protected function exportGroupBackups(int $batchSize, array $resources): void;
|
||||
}
|
||||
+234
-197
@@ -5,11 +5,11 @@ namespace Utopia\Migration\Sources;
|
||||
use Appwrite\AppwriteException;
|
||||
use Appwrite\Client;
|
||||
use Appwrite\Query;
|
||||
use Appwrite\Services\Databases;
|
||||
use Appwrite\Services\Functions;
|
||||
use Appwrite\Services\Messaging;
|
||||
use Appwrite\Services\Sites;
|
||||
use Appwrite\Services\Storage;
|
||||
use Appwrite\Services\TablesDB;
|
||||
use Appwrite\Services\Teams;
|
||||
use Appwrite\Services\Users;
|
||||
use Utopia\Database\Database as UtopiaDatabase;
|
||||
@@ -24,6 +24,7 @@ use Utopia\Migration\Resources\Auth\User;
|
||||
use Utopia\Migration\Resources\Database\Attribute;
|
||||
use Utopia\Migration\Resources\Database\Collection;
|
||||
use Utopia\Migration\Resources\Database\Column;
|
||||
use Utopia\Migration\Resources\Database\Columns\BigInt;
|
||||
use Utopia\Migration\Resources\Database\Columns\Boolean;
|
||||
use Utopia\Migration\Resources\Database\Columns\DateTime;
|
||||
use Utopia\Migration\Resources\Database\Columns\Decimal;
|
||||
@@ -132,7 +133,7 @@ class Appwrite extends Source
|
||||
|
||||
switch ($this->source) {
|
||||
case static::SOURCE_API:
|
||||
$this->reader = new APIReader(new Databases($this->client));
|
||||
$this->reader = new APIReader(new TablesDB($this->client));
|
||||
break;
|
||||
|
||||
case static::SOURCE_DATABASE:
|
||||
@@ -149,7 +150,6 @@ class Appwrite extends Source
|
||||
default:
|
||||
throw new \Exception('Unknown source', Exception::CODE_VALIDATION);
|
||||
}
|
||||
|
||||
}
|
||||
|
||||
public function setConsoleKey(string $key): void
|
||||
@@ -239,6 +239,11 @@ class Appwrite extends Source
|
||||
|
||||
// Integrations
|
||||
Resource::TYPE_PLATFORM,
|
||||
|
||||
// Backups
|
||||
Resource::TYPE_BACKUP_POLICY,
|
||||
|
||||
// Settings
|
||||
];
|
||||
}
|
||||
|
||||
@@ -278,6 +283,7 @@ class Appwrite extends Source
|
||||
$this->reportMessaging($resources, $report, $resourceIds);
|
||||
$this->reportSites($resources, $report, $resourceIds);
|
||||
$this->reportIntegrations($resources, $report, $resourceIds);
|
||||
$this->reportBackups($resources, $report, $resourceIds);
|
||||
|
||||
$report['version'] = $this->call(
|
||||
'GET',
|
||||
@@ -323,7 +329,7 @@ class Appwrite extends Source
|
||||
limit: 1
|
||||
);
|
||||
$userList = $this->users->list($userQueries);
|
||||
$report[Resource::TYPE_USER] = $userList['total'];
|
||||
$report[Resource::TYPE_USER] = $userList->total;
|
||||
}
|
||||
|
||||
if ($needTeams) {
|
||||
@@ -339,11 +345,11 @@ class Appwrite extends Source
|
||||
);
|
||||
$teamList = $this->teams->list($params);
|
||||
|
||||
$totalTeams = $teamList['total'];
|
||||
$currentTeams = $teamList['teams'];
|
||||
$totalTeams = $teamList->total;
|
||||
$currentTeams = $teamList->teams;
|
||||
|
||||
$allTeams = array_merge($allTeams, $currentTeams);
|
||||
$lastTeam = $currentTeams[count($currentTeams) - 1]['$id'] ?? null;
|
||||
$lastTeam = empty($currentTeams) ? null : end($currentTeams)->id;
|
||||
|
||||
if (count($currentTeams) < self::DEFAULT_PAGE_LIMIT) {
|
||||
break;
|
||||
@@ -357,7 +363,7 @@ class Appwrite extends Source
|
||||
limit: 1
|
||||
);
|
||||
$teamList = $this->teams->list($params);
|
||||
$teams = ['total' => $teamList['total'], 'teams' => []];
|
||||
$teams = ['total' => $teamList->total, 'teams' => []];
|
||||
}
|
||||
}
|
||||
|
||||
@@ -369,9 +375,9 @@ class Appwrite extends Source
|
||||
$report[Resource::TYPE_MEMBERSHIP] = 0;
|
||||
foreach ($teams['teams'] as $team) {
|
||||
$report[Resource::TYPE_MEMBERSHIP] += $this->teams->listMemberships(
|
||||
$team['$id'],
|
||||
$team->id,
|
||||
[Query::limit(1)]
|
||||
)['total'];
|
||||
)->total;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -393,14 +399,13 @@ class Appwrite extends Source
|
||||
*/
|
||||
private function reportStorage(array $resources, array &$report, array $resourceIds = []): void
|
||||
{
|
||||
|
||||
if (\in_array(Resource::TYPE_BUCKET, $resources)) {
|
||||
$bucketQueries = $this->buildQueries(
|
||||
resourceType: Resource::TYPE_BUCKET,
|
||||
resourceIds: $resourceIds,
|
||||
limit: 1
|
||||
);
|
||||
$report[Resource::TYPE_BUCKET] = $this->storage->listBuckets($bucketQueries)['total'];
|
||||
$report[Resource::TYPE_BUCKET] = $this->storage->listBuckets($bucketQueries)->total;
|
||||
}
|
||||
|
||||
if (\in_array(Resource::TYPE_FILE, $resources)) {
|
||||
@@ -415,10 +420,10 @@ class Appwrite extends Source
|
||||
resourceIds: $resourceIds,
|
||||
cursor: $lastBucket,
|
||||
);
|
||||
$currentBuckets = $this->storage->listBuckets($queries)['buckets'];
|
||||
$currentBuckets = $this->storage->listBuckets($queries)->buckets;
|
||||
|
||||
$buckets = array_merge($buckets, $currentBuckets);
|
||||
$lastBucket = $buckets[count($buckets) - 1]['$id'] ?? null;
|
||||
$lastBucket = $buckets[count($buckets) - 1]->id ?? null;
|
||||
|
||||
if (count($currentBuckets) < self::DEFAULT_PAGE_LIMIT) {
|
||||
break;
|
||||
@@ -427,12 +432,12 @@ class Appwrite extends Source
|
||||
|
||||
foreach ($buckets as $bucket) {
|
||||
$filesResponse = $this->storage->listFiles(
|
||||
$bucket['$id'],
|
||||
$bucket->id,
|
||||
[Query::limit(1)]
|
||||
);
|
||||
|
||||
$report['size'] += $bucket['totalSize'] ?? 0;
|
||||
$report[Resource::TYPE_FILE] += $filesResponse['total'];
|
||||
$report['size'] += $bucket->totalSize ?? 0;
|
||||
$report[Resource::TYPE_FILE] += $filesResponse->total;
|
||||
}
|
||||
|
||||
$report['size'] = $report['size'] / 1000 / 1000; // MB
|
||||
@@ -455,7 +460,7 @@ class Appwrite extends Source
|
||||
resourceIds: $resourceIds,
|
||||
limit: 1
|
||||
);
|
||||
$report[Resource::TYPE_FUNCTION] = $this->functions->list($functionQueries)['total'];
|
||||
$report[Resource::TYPE_FUNCTION] = $this->functions->list($functionQueries)->total;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -469,11 +474,11 @@ class Appwrite extends Source
|
||||
);
|
||||
$funcList = $this->functions->list($params);
|
||||
|
||||
$totalFunctions = $funcList['total'];
|
||||
$currentFunctions = $funcList['functions'];
|
||||
$totalFunctions = $funcList->total;
|
||||
$currentFunctions = $funcList->functions;
|
||||
$functions = array_merge($functions, $currentFunctions);
|
||||
|
||||
$lastFunction = $currentFunctions[count($currentFunctions) - 1]['$id'] ?? null;
|
||||
$lastFunction = $currentFunctions[count($currentFunctions) - 1]->id ?? null;
|
||||
if (count($currentFunctions) < self::DEFAULT_PAGE_LIMIT) {
|
||||
break;
|
||||
}
|
||||
@@ -487,7 +492,7 @@ class Appwrite extends Source
|
||||
if (\in_array(Resource::TYPE_DEPLOYMENT, $resources)) {
|
||||
$report[Resource::TYPE_DEPLOYMENT] = 0;
|
||||
foreach ($functions as $function) {
|
||||
if (!empty($function['deploymentId'])) {
|
||||
if (!empty($function->deploymentId)) {
|
||||
$report[Resource::TYPE_DEPLOYMENT] += 1;
|
||||
}
|
||||
}
|
||||
@@ -497,7 +502,7 @@ class Appwrite extends Source
|
||||
$report[Resource::TYPE_ENVIRONMENT_VARIABLE] = 0;
|
||||
foreach ($functions as $function) {
|
||||
// function model contains `vars`, we don't need to fetch the list again.
|
||||
$report[Resource::TYPE_ENVIRONMENT_VARIABLE] += count($function['vars'] ?? []);
|
||||
$report[Resource::TYPE_ENVIRONMENT_VARIABLE] += count($function->vars ?? []);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -518,7 +523,7 @@ class Appwrite extends Source
|
||||
resourceIds: $resourceIds,
|
||||
limit: 1
|
||||
);
|
||||
$report[Resource::TYPE_SITE] = $this->sites->list($siteQueries)['total'];
|
||||
$report[Resource::TYPE_SITE] = $this->sites->list($siteQueries)->total;
|
||||
return;
|
||||
}
|
||||
|
||||
@@ -532,15 +537,15 @@ class Appwrite extends Source
|
||||
);
|
||||
$siteList = $this->sites->list($params);
|
||||
|
||||
$totalSites = $siteList['total'];
|
||||
$currentSites = $siteList['sites'];
|
||||
$totalSites = $siteList->total;
|
||||
$currentSites = $siteList->sites;
|
||||
$sites = array_merge($sites, $currentSites);
|
||||
|
||||
if (count($currentSites) === 0 || count($currentSites) < self::DEFAULT_PAGE_LIMIT) {
|
||||
break;
|
||||
}
|
||||
|
||||
$lastSite = $currentSites[count($currentSites) - 1]['$id'];
|
||||
$lastSite = $currentSites[count($currentSites) - 1]->id;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -551,7 +556,7 @@ class Appwrite extends Source
|
||||
if (\in_array(Resource::TYPE_SITE_DEPLOYMENT, $resources)) {
|
||||
$report[Resource::TYPE_SITE_DEPLOYMENT] = 0;
|
||||
foreach ($sites as $site) {
|
||||
if (!empty($site['deploymentId'])) {
|
||||
if (!empty($site->deploymentId)) {
|
||||
$report[Resource::TYPE_SITE_DEPLOYMENT] += 1;
|
||||
}
|
||||
}
|
||||
@@ -560,8 +565,8 @@ class Appwrite extends Source
|
||||
if (\in_array(Resource::TYPE_SITE_VARIABLE, $resources)) {
|
||||
$report[Resource::TYPE_SITE_VARIABLE] = 0;
|
||||
foreach ($sites as $site) {
|
||||
$variables = $this->sites->listVariables($site['$id']);
|
||||
$report[Resource::TYPE_SITE_VARIABLE] += $variables['total'] ?? 0;
|
||||
$variables = $this->sites->listVariables($site->id);
|
||||
$report[Resource::TYPE_SITE_VARIABLE] += $variables->total ?? 0;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -639,27 +644,27 @@ class Appwrite extends Source
|
||||
}
|
||||
|
||||
$response = $this->users->list($queries);
|
||||
if ($response['total'] == 0) {
|
||||
if ($response->total == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($response['users'] as $user) {
|
||||
foreach ($response->users as $user) {
|
||||
$users[] = new User(
|
||||
$user['$id'],
|
||||
empty($user['email']) ? null : $user['email'],
|
||||
empty($user['name']) ? null : $user['name'],
|
||||
$user['password'] ? new Hash($user['password'], algorithm: $user['hash']) : null,
|
||||
empty($user['phone']) ? null : $user['phone'],
|
||||
$user['labels'] ?? [],
|
||||
$user->id,
|
||||
empty($user->email) ? null : $user->email,
|
||||
empty($user->name) ? null : $user->name,
|
||||
$user->password ? new Hash($user->password, algorithm: $user->hash) : null,
|
||||
empty($user->phone) ? null : $user->phone,
|
||||
$user->labels ?? [],
|
||||
'',
|
||||
$user['emailVerification'] ?? false,
|
||||
$user['phoneVerification'] ?? false,
|
||||
!$user['status'],
|
||||
$user['prefs'] ?? [],
|
||||
$user['targets'] ?? [],
|
||||
$user->emailVerification ?? false,
|
||||
$user->phoneVerification ?? false,
|
||||
!$user->status,
|
||||
$user->prefs->data ?? [],
|
||||
\array_map(fn ($target) => $target->toArray(), $user->targets ?? []),
|
||||
);
|
||||
|
||||
$lastDocument = $user['$id'];
|
||||
$lastDocument = $user->id;
|
||||
}
|
||||
|
||||
$this->callback($users);
|
||||
@@ -693,18 +698,18 @@ class Appwrite extends Source
|
||||
}
|
||||
|
||||
$response = $this->teams->list($queries);
|
||||
if ($response['total'] == 0) {
|
||||
if ($response->total == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($response['teams'] as $team) {
|
||||
foreach ($response->teams as $team) {
|
||||
$teams[] = new Team(
|
||||
$team['$id'],
|
||||
$team['name'],
|
||||
$team['prefs'],
|
||||
$team->id,
|
||||
$team->name,
|
||||
$team->prefs->data,
|
||||
);
|
||||
|
||||
$lastDocument = $team['$id'];
|
||||
$lastDocument = $team->id;
|
||||
}
|
||||
|
||||
$this->callback($teams);
|
||||
@@ -745,25 +750,25 @@ class Appwrite extends Source
|
||||
|
||||
$response = $this->teams->listMemberships($team->getId(), $queries);
|
||||
|
||||
if ($response['total'] == 0) {
|
||||
if ($response->total == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($response['memberships'] as $membership) {
|
||||
$user = $cacheUsers[$membership['userId']] ?? null;
|
||||
foreach ($response->memberships as $membership) {
|
||||
$user = $cacheUsers[$membership->userId] ?? null;
|
||||
if ($user === null) {
|
||||
throw new \Exception('User not found', Exception::CODE_NOT_FOUND);
|
||||
}
|
||||
|
||||
$memberships[] = new Membership(
|
||||
$membership['$id'],
|
||||
$membership->id,
|
||||
$team,
|
||||
$user,
|
||||
$membership['roles'],
|
||||
$membership['confirm']
|
||||
$membership->roles,
|
||||
$membership->confirm
|
||||
);
|
||||
|
||||
$lastDocument = $membership['$id'];
|
||||
$lastDocument = $membership->id;
|
||||
}
|
||||
|
||||
$this->callback($memberships);
|
||||
@@ -908,7 +913,6 @@ class Appwrite extends Source
|
||||
'enabled' => $database['enabled'] ?? true,
|
||||
]);
|
||||
$databases[] = $newDatabase;
|
||||
|
||||
}
|
||||
}
|
||||
|
||||
@@ -967,10 +971,12 @@ class Appwrite extends Source
|
||||
|
||||
$response = $this->reader->listTables($database, $queries);
|
||||
foreach ($response as $table) {
|
||||
$rowSecurity = $table['rowSecurity'] ?? $table['documentSecurity'] ?? false;
|
||||
$newTable = self::getEntity($databaseName, [
|
||||
'id' => $table['$id'],
|
||||
'name' => $table['name'],
|
||||
'documentSecurity' => $table['documentSecurity'],
|
||||
'rowSecurity' => $rowSecurity,
|
||||
'documentSecurity' => $rowSecurity,
|
||||
'permissions' => $table['$permissions'],
|
||||
'createdAt' => $table['$createdAt'],
|
||||
'updatedAt' => $table['$updatedAt'],
|
||||
@@ -1034,7 +1040,7 @@ class Appwrite extends Source
|
||||
}
|
||||
|
||||
/** @var Table $table */
|
||||
$col = match($table->getDatabase()->getType()) {
|
||||
$col = match ($table->getDatabase()->getType()) {
|
||||
Resource::TYPE_DATABASE_VECTORSDB => self::getColumn($table, $column)->getAttribute(),
|
||||
default => self::getColumn($table, $column),
|
||||
};
|
||||
@@ -1192,10 +1198,11 @@ class Appwrite extends Source
|
||||
|
||||
unset($row['$id']);
|
||||
unset($row['$permissions']);
|
||||
unset($row['$collectionId']);
|
||||
unset($row['$databaseId']);
|
||||
unset($row['$sequence']);
|
||||
unset($row['$collection']);
|
||||
unset($row['$tableId']);
|
||||
unset($row['$table']);
|
||||
|
||||
$row = self::getRecord($table->getDatabase()->getType(), [
|
||||
'id' => $id,
|
||||
@@ -1279,18 +1286,20 @@ class Appwrite extends Source
|
||||
|
||||
$convertedBuckets = [];
|
||||
|
||||
foreach ($buckets['buckets'] as $bucket) {
|
||||
foreach ($buckets->buckets as $bucket) {
|
||||
$bucket = new Bucket(
|
||||
$bucket['$id'],
|
||||
$bucket['name'],
|
||||
$bucket['$permissions'],
|
||||
$bucket['fileSecurity'],
|
||||
$bucket['enabled'],
|
||||
$bucket['maximumFileSize'],
|
||||
$bucket['allowedFileExtensions'],
|
||||
$bucket['compression'],
|
||||
$bucket['encryption'],
|
||||
$bucket['antivirus'],
|
||||
$bucket->id,
|
||||
$bucket->name,
|
||||
$bucket->permissions,
|
||||
$bucket->fileSecurity,
|
||||
$bucket->enabled,
|
||||
$bucket->maximumFileSize,
|
||||
$bucket->allowedFileExtensions,
|
||||
$bucket->compression,
|
||||
$bucket->encryption,
|
||||
$bucket->antivirus,
|
||||
false,
|
||||
$bucket->transformations ?? false,
|
||||
);
|
||||
$convertedBuckets[] = $bucket;
|
||||
}
|
||||
@@ -1325,31 +1334,31 @@ class Appwrite extends Source
|
||||
$queries
|
||||
);
|
||||
|
||||
foreach ($response['files'] as $file) {
|
||||
foreach ($response->files as $file) {
|
||||
try {
|
||||
$this->exportFileData(new File(
|
||||
$file['$id'],
|
||||
$file->id,
|
||||
$bucket,
|
||||
$file['name'],
|
||||
$file['signature'],
|
||||
$file['mimeType'],
|
||||
$file['$permissions'],
|
||||
$file['sizeOriginal'],
|
||||
$file->name,
|
||||
$file->signature,
|
||||
$file->mimeType,
|
||||
$file->permissions,
|
||||
$file->sizeOriginal,
|
||||
));
|
||||
} catch (\Throwable $e) {
|
||||
$this->addError(new Exception(
|
||||
resourceName: Resource::TYPE_FILE,
|
||||
resourceGroup: Transfer::GROUP_STORAGE,
|
||||
resourceId: $file['$id'],
|
||||
resourceId: $file->id,
|
||||
message: $e->getMessage(),
|
||||
code: $e->getCode()
|
||||
));
|
||||
}
|
||||
|
||||
$lastDocument = $file['$id'];
|
||||
$lastDocument = $file->id;
|
||||
}
|
||||
|
||||
if (count($response['files']) < $batchSize) {
|
||||
if (count($response->files) < $batchSize) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1458,6 +1467,18 @@ class Appwrite extends Source
|
||||
}
|
||||
}
|
||||
|
||||
protected function exportGroupBackups(int $batchSize, array $resources): void
|
||||
{
|
||||
// No-op: backup policies are Cloud-only
|
||||
}
|
||||
|
||||
protected function reportBackups(array $resources, array &$report, array $resourceIds = []): void
|
||||
{
|
||||
if (\in_array(Resource::TYPE_BACKUP_POLICY, $resources)) {
|
||||
$report[Resource::TYPE_BACKUP_POLICY] = 0;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws AppwriteException
|
||||
*/
|
||||
@@ -1483,36 +1504,40 @@ class Appwrite extends Source
|
||||
$response = $this->functions->list($queries);
|
||||
|
||||
|
||||
if ($response['total'] === 0) {
|
||||
if ($response->total === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$functions = [];
|
||||
$convertedResources = [];
|
||||
|
||||
foreach ($response['functions'] as $function) {
|
||||
foreach ($response->functions as $function) {
|
||||
$convertedFunc = new Func(
|
||||
$function['$id'],
|
||||
$function['name'],
|
||||
$function['runtime'],
|
||||
$function['execute'],
|
||||
$function['enabled'],
|
||||
$function['events'],
|
||||
$function['schedule'],
|
||||
$function['timeout'],
|
||||
$function['deploymentId'] ?? '',
|
||||
$function['entrypoint']
|
||||
$function->id,
|
||||
$function->name,
|
||||
$function->runtime,
|
||||
$function->execute,
|
||||
$function->enabled,
|
||||
$function->events,
|
||||
$function->schedule,
|
||||
$function->timeout,
|
||||
$function->deploymentId ?? '',
|
||||
$function->entrypoint,
|
||||
$function->commands ?? '',
|
||||
$function->logging ?? true,
|
||||
$function->scopes ?? [],
|
||||
$function->runtimeSpecification ?: $function->buildSpecification ?: '',
|
||||
);
|
||||
$functions[] = $convertedFunc;
|
||||
|
||||
$convertedResources[] = $convertedFunc;
|
||||
|
||||
foreach ($function['vars'] as $var) {
|
||||
foreach ($function->vars as $var) {
|
||||
$convertedResources[] = new EnvVar(
|
||||
$var['$id'],
|
||||
$var->id,
|
||||
$convertedFunc,
|
||||
$var['key'],
|
||||
$var['value'],
|
||||
$var->key,
|
||||
$var->value,
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -1567,17 +1592,17 @@ class Appwrite extends Source
|
||||
$queries
|
||||
);
|
||||
|
||||
foreach ($response['deployments'] as $deployment) {
|
||||
foreach ($response->deployments as $deployment) {
|
||||
try {
|
||||
$this->exportDeploymentData($func, $deployment);
|
||||
} catch (\Throwable $e) {
|
||||
$func->setStatus(Resource::STATUS_ERROR, $e->getMessage());
|
||||
}
|
||||
|
||||
$lastDocument = $deployment['$id'];
|
||||
$lastDocument = $deployment->id;
|
||||
}
|
||||
|
||||
if (count($response['deployments']) < $batchSize) {
|
||||
if (count($response->deployments) < $batchSize) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1587,7 +1612,7 @@ class Appwrite extends Source
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function exportDeploymentData(Func $func, array $deployment): void
|
||||
private function exportDeploymentData(Func $func, \Appwrite\Models\Deployment $deployment): void
|
||||
{
|
||||
// Set the chunk size (5MB)
|
||||
$start = 0;
|
||||
@@ -1598,7 +1623,7 @@ class Appwrite extends Source
|
||||
|
||||
$this->call(
|
||||
'HEAD',
|
||||
"/functions/{$func->getId()}/deployments/{$deployment['$id']}/download",
|
||||
"/functions/{$func->getId()}/deployments/{$deployment->id}/download",
|
||||
[],
|
||||
[],
|
||||
$responseHeaders
|
||||
@@ -1608,7 +1633,7 @@ class Appwrite extends Source
|
||||
if (!array_key_exists('content-length', $responseHeaders)) {
|
||||
$file = $this->call(
|
||||
'GET',
|
||||
"/functions/{$func->getId()}/deployments/{$deployment['$id']}/download",
|
||||
"/functions/{$func->getId()}/deployments/{$deployment->id}/download",
|
||||
[],
|
||||
[],
|
||||
$responseHeaders
|
||||
@@ -1621,14 +1646,14 @@ class Appwrite extends Source
|
||||
}
|
||||
|
||||
$deployment = new Deployment(
|
||||
$deployment['$id'],
|
||||
$deployment->id,
|
||||
$func,
|
||||
$size,
|
||||
$deployment['entrypoint'],
|
||||
$deployment->entrypoint,
|
||||
$start,
|
||||
$end,
|
||||
$file,
|
||||
$deployment['activate']
|
||||
$deployment->activate
|
||||
);
|
||||
$deployment->setSequence($deployment->getId());
|
||||
|
||||
@@ -1644,14 +1669,14 @@ class Appwrite extends Source
|
||||
}
|
||||
|
||||
$deployment = new Deployment(
|
||||
$deployment['$id'],
|
||||
$deployment->id,
|
||||
$func,
|
||||
$fileSize,
|
||||
$deployment['entrypoint'],
|
||||
$deployment->entrypoint,
|
||||
$start,
|
||||
$end,
|
||||
'',
|
||||
$deployment['activate']
|
||||
$deployment->activate
|
||||
);
|
||||
|
||||
$deployment->setSequence($deployment->getId());
|
||||
@@ -1696,7 +1721,7 @@ class Appwrite extends Source
|
||||
resourceIds: $resourceIds,
|
||||
limit: 1
|
||||
);
|
||||
$report[Resource::TYPE_PROVIDER] = $this->messaging->listProviders($providerQueries)['total'];
|
||||
$report[Resource::TYPE_PROVIDER] = $this->messaging->listProviders($providerQueries)->total;
|
||||
}
|
||||
|
||||
if (\in_array(Resource::TYPE_TOPIC, $resources)) {
|
||||
@@ -1705,7 +1730,7 @@ class Appwrite extends Source
|
||||
resourceIds: $resourceIds,
|
||||
limit: 1
|
||||
);
|
||||
$report[Resource::TYPE_TOPIC] = $this->messaging->listTopics($topicQueries)['total'];
|
||||
$report[Resource::TYPE_TOPIC] = $this->messaging->listTopics($topicQueries)->total;
|
||||
}
|
||||
|
||||
if (\in_array(Resource::TYPE_SUBSCRIBER, $resources)) {
|
||||
@@ -1719,16 +1744,16 @@ class Appwrite extends Source
|
||||
}
|
||||
|
||||
$topicResponse = $this->messaging->listTopics($topicQueries);
|
||||
if ($topicResponse['total'] == 0 || empty($topicResponse['topics'])) {
|
||||
if ($topicResponse->total == 0 || empty($topicResponse->topics)) {
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($topicResponse['topics'] as $topic) {
|
||||
$subscriberTotal += $this->messaging->listSubscribers($topic['$id'], [Query::limit(1)])['total'];
|
||||
$lastTopic = $topic['$id'];
|
||||
foreach ($topicResponse->topics as $topic) {
|
||||
$subscriberTotal += $this->messaging->listSubscribers($topic->id, [Query::limit(1)])->total;
|
||||
$lastTopic = $topic->id;
|
||||
}
|
||||
|
||||
if (\count($topicResponse['topics']) < self::DEFAULT_PAGE_LIMIT) {
|
||||
if (\count($topicResponse->topics) < self::DEFAULT_PAGE_LIMIT) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -1742,7 +1767,7 @@ class Appwrite extends Source
|
||||
resourceIds: $resourceIds,
|
||||
limit: 1
|
||||
);
|
||||
$report[Resource::TYPE_MESSAGE] = $this->messaging->listMessages($messageQueries)['total'];
|
||||
$report[Resource::TYPE_MESSAGE] = $this->messaging->listMessages($messageQueries)->total;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1828,24 +1853,24 @@ class Appwrite extends Source
|
||||
|
||||
$response = $this->messaging->listProviders($queries);
|
||||
|
||||
if ($response['total'] == 0) {
|
||||
if ($response->total == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($response['providers'] as $provider) {
|
||||
foreach ($response->providers as $provider) {
|
||||
$providers[] = new Provider(
|
||||
$provider['$id'],
|
||||
$provider['name'],
|
||||
$provider['provider'],
|
||||
$provider['type'],
|
||||
$provider['enabled'],
|
||||
$provider['credentials'] ?? [],
|
||||
$provider['options'] ?? [],
|
||||
$provider['$createdAt'] ?? '',
|
||||
$provider['$updatedAt'] ?? '',
|
||||
$provider->id,
|
||||
$provider->name,
|
||||
$provider->provider,
|
||||
$provider->type,
|
||||
$provider->enabled,
|
||||
$provider->credentials ?? [],
|
||||
$provider->options ?? [],
|
||||
$provider->createdAt ?? '',
|
||||
$provider->updatedAt ?? '',
|
||||
);
|
||||
|
||||
$lastDocument = $provider['$id'];
|
||||
$lastDocument = $provider->id;
|
||||
}
|
||||
|
||||
$this->callback($providers);
|
||||
@@ -1879,20 +1904,20 @@ class Appwrite extends Source
|
||||
|
||||
$response = $this->messaging->listTopics($queries);
|
||||
|
||||
if ($response['total'] == 0) {
|
||||
if ($response->total == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($response['topics'] as $topic) {
|
||||
foreach ($response->topics as $topic) {
|
||||
$topics[] = new Topic(
|
||||
$topic['$id'],
|
||||
$topic['name'],
|
||||
$topic['subscribe'] ?? [],
|
||||
$topic['$createdAt'] ?? '',
|
||||
$topic['$updatedAt'] ?? '',
|
||||
$topic->id,
|
||||
$topic->name,
|
||||
$topic->subscribe ?? [],
|
||||
$topic->createdAt ?? '',
|
||||
$topic->updatedAt ?? '',
|
||||
);
|
||||
|
||||
$lastDocument = $topic['$id'];
|
||||
$lastDocument = $topic->id;
|
||||
}
|
||||
|
||||
$this->callback($topics);
|
||||
@@ -1925,23 +1950,23 @@ class Appwrite extends Source
|
||||
|
||||
$response = $this->messaging->listSubscribers($topic->getId(), $queries);
|
||||
|
||||
if ($response['total'] == 0) {
|
||||
if ($response->total == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($response['subscribers'] as $subscriber) {
|
||||
foreach ($response->subscribers as $subscriber) {
|
||||
$subscribers[] = new Subscriber(
|
||||
$subscriber['$id'],
|
||||
$subscriber['topicId'],
|
||||
$subscriber['targetId'],
|
||||
$subscriber['userId'] ?? '',
|
||||
$subscriber['userName'] ?? '',
|
||||
$subscriber['providerType'] ?? '',
|
||||
$subscriber['$createdAt'] ?? '',
|
||||
$subscriber['$updatedAt'] ?? '',
|
||||
$subscriber->id,
|
||||
$subscriber->topicId,
|
||||
$subscriber->targetId,
|
||||
$subscriber->userId ?? '',
|
||||
$subscriber->userName ?? '',
|
||||
$subscriber->providerType ?? '',
|
||||
$subscriber->createdAt ?? '',
|
||||
$subscriber->updatedAt ?? '',
|
||||
);
|
||||
|
||||
$lastDocument = $subscriber['$id'];
|
||||
$lastDocument = $subscriber->id;
|
||||
}
|
||||
|
||||
$this->callback($subscribers);
|
||||
@@ -1976,28 +2001,28 @@ class Appwrite extends Source
|
||||
|
||||
$response = $this->messaging->listMessages($queries);
|
||||
|
||||
if ($response['total'] == 0) {
|
||||
if ($response->total == 0) {
|
||||
break;
|
||||
}
|
||||
|
||||
foreach ($response['messages'] as $message) {
|
||||
foreach ($response->messages as $message) {
|
||||
$messages[] = new Message(
|
||||
$message['$id'],
|
||||
$message['providerType'] ?? '',
|
||||
$message['topics'] ?? [],
|
||||
$message['users'] ?? [],
|
||||
$message['targets'] ?? [],
|
||||
$message['data'] ?? [],
|
||||
$message['status'] ?? '',
|
||||
$message['scheduledAt'] ?? '',
|
||||
$message['deliveredAt'] ?? '',
|
||||
$message['deliveryErrors'] ?? [],
|
||||
$message['deliveredTotal'] ?? 0,
|
||||
$message['$createdAt'] ?? '',
|
||||
$message['$updatedAt'] ?? '',
|
||||
$message->id,
|
||||
$message->providerType ?? '',
|
||||
$message->topics ?? [],
|
||||
$message->users ?? [],
|
||||
$message->targets ?? [],
|
||||
$message->data ?? [],
|
||||
(string) $message->status,
|
||||
$message->scheduledAt ?? '',
|
||||
$message->deliveredAt ?? '',
|
||||
$message->deliveryErrors ?? [],
|
||||
$message->deliveredTotal ?? 0,
|
||||
$message->createdAt ?? '',
|
||||
$message->updatedAt ?? '',
|
||||
);
|
||||
|
||||
$lastDocument = $message['$id'];
|
||||
$lastDocument = $message->id;
|
||||
}
|
||||
|
||||
$this->callback($messages);
|
||||
@@ -2031,40 +2056,40 @@ class Appwrite extends Source
|
||||
|
||||
$response = $this->sites->list($queries);
|
||||
|
||||
if ($response['total'] === 0) {
|
||||
if ($response->total === 0) {
|
||||
return;
|
||||
}
|
||||
|
||||
$sites = [];
|
||||
$convertedResources = [];
|
||||
|
||||
foreach ($response['sites'] as $site) {
|
||||
foreach ($response->sites as $site) {
|
||||
$convertedSite = new Site(
|
||||
$site['$id'],
|
||||
$site['name'],
|
||||
$site['framework'],
|
||||
$site['buildRuntime'],
|
||||
$site['enabled'],
|
||||
$site['logging'],
|
||||
$site['timeout'],
|
||||
$site['installCommand'] ?? '',
|
||||
$site['buildCommand'] ?? '',
|
||||
$site['outputDirectory'] ?? '',
|
||||
$site['adapter'] ?? 'static',
|
||||
$site['fallbackFile'] ?? '',
|
||||
$site['specification'] ?? '',
|
||||
$site['deploymentId'] ?? ''
|
||||
$site->id,
|
||||
$site->name,
|
||||
$site->framework,
|
||||
$site->buildRuntime,
|
||||
$site->enabled,
|
||||
$site->logging,
|
||||
$site->timeout,
|
||||
$site->installCommand ?? '',
|
||||
$site->buildCommand ?? '',
|
||||
$site->outputDirectory ?? '',
|
||||
$site->adapter ?? 'static',
|
||||
$site->fallbackFile ?? '',
|
||||
$site->runtimeSpecification ?: $site->buildSpecification ?: '',
|
||||
$site->deploymentId ?? ''
|
||||
);
|
||||
$sites[] = $convertedSite;
|
||||
$convertedResources[] = $convertedSite;
|
||||
|
||||
$variables = $this->sites->listVariables($site['$id']);
|
||||
foreach ($variables['variables'] ?? [] as $var) {
|
||||
$variables = $this->sites->listVariables($site->id);
|
||||
foreach ($variables->variables ?? [] as $var) {
|
||||
$convertedResources[] = new SiteEnvVar(
|
||||
$var['$id'],
|
||||
$var->id,
|
||||
$convertedSite,
|
||||
$var['key'],
|
||||
$var['value']
|
||||
$var->key,
|
||||
$var->value
|
||||
);
|
||||
}
|
||||
}
|
||||
@@ -2120,17 +2145,17 @@ class Appwrite extends Source
|
||||
$queries
|
||||
);
|
||||
|
||||
foreach ($response['deployments'] as $deployment) {
|
||||
foreach ($response->deployments as $deployment) {
|
||||
try {
|
||||
$this->exportSiteDeploymentData($site, $deployment);
|
||||
} catch (\Throwable $e) {
|
||||
$site->setStatus(Resource::STATUS_ERROR, $e->getMessage());
|
||||
}
|
||||
|
||||
$lastDocument = $deployment['$id'];
|
||||
$lastDocument = $deployment->id;
|
||||
}
|
||||
|
||||
if (count($response['deployments']) < $batchSize) {
|
||||
if (count($response->deployments) < $batchSize) {
|
||||
break;
|
||||
}
|
||||
}
|
||||
@@ -2140,7 +2165,7 @@ class Appwrite extends Source
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
private function exportSiteDeploymentData(Site $site, array $deployment): void
|
||||
private function exportSiteDeploymentData(Site $site, \Appwrite\Models\Deployment $deployment): void
|
||||
{
|
||||
$start = 0;
|
||||
$end = Transfer::STORAGE_MAX_CHUNK_SIZE - 1;
|
||||
@@ -2149,7 +2174,7 @@ class Appwrite extends Source
|
||||
|
||||
$this->call(
|
||||
'HEAD',
|
||||
"/sites/{$site->getId()}/deployments/{$deployment['$id']}/download",
|
||||
"/sites/{$site->getId()}/deployments/{$deployment->id}/download",
|
||||
[],
|
||||
[],
|
||||
$responseHeaders
|
||||
@@ -2158,7 +2183,7 @@ class Appwrite extends Source
|
||||
if (!\array_key_exists('content-length', $responseHeaders)) {
|
||||
$file = $this->call(
|
||||
'GET',
|
||||
"/sites/{$site->getId()}/deployments/{$deployment['$id']}/download",
|
||||
"/sites/{$site->getId()}/deployments/{$deployment->id}/download",
|
||||
[],
|
||||
[],
|
||||
$responseHeaders
|
||||
@@ -2171,13 +2196,13 @@ class Appwrite extends Source
|
||||
}
|
||||
|
||||
$siteDeployment = new SiteDeployment(
|
||||
$deployment['$id'],
|
||||
$deployment->id,
|
||||
$site,
|
||||
$size,
|
||||
$start,
|
||||
$end,
|
||||
$file,
|
||||
$deployment['$id'] === $site->getActiveDeployment()
|
||||
$deployment->id === $site->getActiveDeployment()
|
||||
);
|
||||
$siteDeployment->setSequence($siteDeployment->getId());
|
||||
|
||||
@@ -2193,13 +2218,13 @@ class Appwrite extends Source
|
||||
}
|
||||
|
||||
$siteDeployment = new SiteDeployment(
|
||||
$deployment['$id'],
|
||||
$deployment->id,
|
||||
$site,
|
||||
$fileSize,
|
||||
$start,
|
||||
$end,
|
||||
'',
|
||||
$deployment['$id'] === $site->getActiveDeployment()
|
||||
$deployment->id === $site->getActiveDeployment()
|
||||
);
|
||||
|
||||
$siteDeployment->setSequence($siteDeployment->getId());
|
||||
@@ -2494,6 +2519,19 @@ class Appwrite extends Source
|
||||
updatedAt: $column['$updatedAt'] ?? '',
|
||||
),
|
||||
|
||||
Column::TYPE_BIG_INT => new BigInt(
|
||||
$column['key'],
|
||||
$table,
|
||||
required: $column['required'],
|
||||
default: $column['default'],
|
||||
array: $column['array'],
|
||||
min: $column['min'] ?? null,
|
||||
max: $column['max'] ?? null,
|
||||
signed: $column['signed'] ?? true,
|
||||
createdAt: $column['$createdAt'] ?? '',
|
||||
updatedAt: $column['$updatedAt'] ?? '',
|
||||
),
|
||||
|
||||
Column::TYPE_FLOAT => new Decimal(
|
||||
$column['key'],
|
||||
$table,
|
||||
@@ -2622,7 +2660,6 @@ class Appwrite extends Source
|
||||
|
||||
default => throw new \InvalidArgumentException("Unsupported column type: {$column['type']}"),
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -108,5 +108,4 @@ interface Reader
|
||||
*/
|
||||
public function queryLimit(int $limit): mixed;
|
||||
public function getSupportForAttributes(): bool;
|
||||
|
||||
}
|
||||
@@ -4,21 +4,23 @@ namespace Utopia\Migration\Sources\Appwrite\Reader;
|
||||
|
||||
use Appwrite\AppwriteException;
|
||||
use Appwrite\Query;
|
||||
use Appwrite\Services\Databases;
|
||||
/* use Appwrite\Services\Tables; */
|
||||
use Appwrite\Services\TablesDB;
|
||||
use Utopia\Migration\Resource;
|
||||
use Utopia\Migration\Resources\Database\Database;
|
||||
use Utopia\Migration\Resources\Database\Table;
|
||||
use Utopia\Migration\Sources\Appwrite\Reader;
|
||||
|
||||
/**
|
||||
* Adapts Appwrite SDK typed models to the Reader's plain-array contract so that
|
||||
* downstream callers can use array-offset syntax interchangeably with the
|
||||
* Database reader (which returns utopia-php Document objects).
|
||||
*
|
||||
* @implements Reader<Query>
|
||||
*/
|
||||
class API implements Reader
|
||||
{
|
||||
public function __construct(
|
||||
private readonly Databases $database,
|
||||
/* private readonly Tables $table, */
|
||||
private readonly TablesDB $database,
|
||||
) {
|
||||
}
|
||||
|
||||
@@ -53,10 +55,10 @@ class API implements Reader
|
||||
}
|
||||
|
||||
$databasesResponse = $this->database->list($databaseQueries);
|
||||
$databases = $databasesResponse['databases'];
|
||||
$databases = \array_map(fn ($database) => $database->toArray(), $databasesResponse->databases);
|
||||
|
||||
if (in_array(Resource::TYPE_DATABASE, $resources)) {
|
||||
$report[Resource::TYPE_DATABASE] = $databasesResponse['total'];
|
||||
$report[Resource::TYPE_DATABASE] = $databasesResponse->total;
|
||||
}
|
||||
|
||||
if (count(array_intersect($resources, $relevantResources)) === 1 &&
|
||||
@@ -73,13 +75,13 @@ class API implements Reader
|
||||
$lastTable = null;
|
||||
|
||||
while (true) {
|
||||
/* $currentTables = $this->tables->list(...); */
|
||||
$currentTables = $this->database->listCollections(
|
||||
$tablesResponse = $this->database->listTables(
|
||||
$databaseId,
|
||||
$lastTable
|
||||
? [Query::cursorAfter($lastTable)]
|
||||
: [Query::limit($pageLimit)]
|
||||
)['collections']; /* ['tables'] */
|
||||
);
|
||||
$currentTables = \array_map(fn ($table) => $table->toArray(), $tablesResponse->tables);
|
||||
|
||||
$tables = \array_merge($tables, $currentTables);
|
||||
$lastTable = $tables[count($tables) - 1]['$id'] ?? null;
|
||||
@@ -108,14 +110,13 @@ class API implements Reader
|
||||
}
|
||||
|
||||
if (Resource::isSupported(Resource::TYPE_ROW, $resources)) {
|
||||
/* $rowsResponse = $this->tables->listRows(...) */
|
||||
$rowsResponse = $this->database->listDocuments(
|
||||
$rowsResponse = $this->database->listRows(
|
||||
$databaseId,
|
||||
$tableId,
|
||||
[Query::limit(1)]
|
||||
);
|
||||
|
||||
$report[Resource::TYPE_ROW] += $rowsResponse['total'];
|
||||
$report[Resource::TYPE_ROW] += $rowsResponse->total;
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -125,90 +126,89 @@ class API implements Reader
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<array<string, mixed>>
|
||||
* @throws AppwriteException
|
||||
*/
|
||||
public function listDatabases(array $queries = []): array
|
||||
{
|
||||
return $this->database->list($queries)['databases'];
|
||||
return \array_map(
|
||||
fn ($database) => $database->toArray(),
|
||||
$this->database->list($queries)->databases
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<array<string, mixed>>
|
||||
* @throws AppwriteException
|
||||
*/
|
||||
public function listTables(Database $resource, array $queries = []): array
|
||||
{
|
||||
/* $this->tables->list(...)['tables'] */
|
||||
return $this->database->listCollections(
|
||||
$resource->getId(),
|
||||
$queries
|
||||
)['collections'];
|
||||
return \array_map(
|
||||
fn ($table) => $table->toArray(),
|
||||
$this->database->listTables($resource->getId(), $queries)->tables
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Table $resource
|
||||
* @param array $queries
|
||||
* @return array
|
||||
* @return array<array<string, mixed>>
|
||||
* @throws AppwriteException
|
||||
*/
|
||||
public function listColumns(Table $resource, array $queries = []): array
|
||||
{
|
||||
/* $this->tables->listColumns(...)['columns'] */
|
||||
return $this->database->listAttributes(
|
||||
$resource->getDatabase()->getId(),
|
||||
$resource->getId(),
|
||||
$queries
|
||||
)['attributes'];
|
||||
return \array_map(
|
||||
fn ($column) => $column->toArray(),
|
||||
$this->database->listColumns(
|
||||
$resource->getDatabase()->getId(),
|
||||
$resource->getId(),
|
||||
$queries
|
||||
)->columns
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Table $resource
|
||||
* @param array $queries
|
||||
* @return array
|
||||
* @return array<array<string, mixed>>
|
||||
* @throws AppwriteException
|
||||
*/
|
||||
public function listIndexes(Table $resource, array $queries = []): array
|
||||
{
|
||||
/* $this->tables->listIndexes(...)['indexes'] */
|
||||
return $this->database->listIndexes(
|
||||
$resource->getDatabase()->getId(),
|
||||
$resource->getId(),
|
||||
$queries
|
||||
)['indexes'];
|
||||
return \array_map(
|
||||
fn ($index) => $index->toArray(),
|
||||
$this->database->listIndexes(
|
||||
$resource->getDatabase()->getId(),
|
||||
$resource->getId(),
|
||||
$queries
|
||||
)->indexes
|
||||
);
|
||||
}
|
||||
|
||||
|
||||
/**
|
||||
* @param Table $resource
|
||||
* @param array $queries
|
||||
* @return array
|
||||
* @return array<array<string, mixed>>
|
||||
* @throws AppwriteException
|
||||
*/
|
||||
public function listRows(Table $resource, array $queries = []): array
|
||||
{
|
||||
/* $this->tables->listRows(...)['rows'] */
|
||||
return $this->database->listDocuments(
|
||||
$resource->getDatabase()->getId(),
|
||||
$resource->getId(),
|
||||
$queries
|
||||
)['documents'];
|
||||
return \array_map(
|
||||
fn ($row) => $row->toArray(),
|
||||
$this->database->listRows(
|
||||
$resource->getDatabase()->getId(),
|
||||
$resource->getId(),
|
||||
$queries
|
||||
)->rows
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @param Table $resource
|
||||
* @param string $rowId
|
||||
* @param array $queries
|
||||
* @return array
|
||||
* @return array<string, mixed>
|
||||
* @throws AppwriteException
|
||||
*/
|
||||
public function getRow(Table $resource, string $rowId, array $queries = []): array
|
||||
{
|
||||
/* $this->tables->getRow(...) */
|
||||
return $this->database->getDocument(
|
||||
return $this->database->getRow(
|
||||
$resource->getDatabase()->getId(),
|
||||
$resource->getId(),
|
||||
$rowId,
|
||||
$queries
|
||||
);
|
||||
)->toArray();
|
||||
}
|
||||
|
||||
/**
|
||||
|
||||
@@ -301,7 +301,7 @@ class CSV extends Source
|
||||
|
||||
$parsedData[$key] = array_map(function ($item) use ($type) {
|
||||
return match ($type) {
|
||||
Column::TYPE_INTEGER => is_numeric($item) ? (int)$item : null,
|
||||
Column::TYPE_INTEGER,Column::TYPE_BIG_INT => is_numeric($item) ? (int)$item : null,
|
||||
Column::TYPE_FLOAT => is_numeric($item) ? (float)$item : null,
|
||||
Column::TYPE_BOOLEAN => filter_var($item, FILTER_VALIDATE_BOOLEAN, FILTER_NULL_ON_FAILURE),
|
||||
default => $item,
|
||||
@@ -323,6 +323,7 @@ class CSV extends Source
|
||||
'null' => null, // 'null' string is converted to null
|
||||
'' => match ($type) {
|
||||
Column::TYPE_INTEGER,
|
||||
Column::TYPE_BIG_INT,
|
||||
Column::TYPE_FLOAT,
|
||||
Column::TYPE_BOOLEAN,
|
||||
Column::TYPE_DATETIME,
|
||||
@@ -330,7 +331,7 @@ class CSV extends Source
|
||||
default => '', // but empty string stays empty string for compatibility
|
||||
},
|
||||
default => match ($type) {
|
||||
Column::TYPE_INTEGER => \is_numeric($parsedValue) ? (int)$parsedValue : null,
|
||||
Column::TYPE_INTEGER, Column::TYPE_BIG_INT => \is_numeric($parsedValue) ? (int)$parsedValue : null,
|
||||
Column::TYPE_FLOAT => \is_numeric($parsedValue) ? (float)$parsedValue : null,
|
||||
Column::TYPE_BOOLEAN => \filter_var(
|
||||
$parsedValue,
|
||||
@@ -434,6 +435,11 @@ class CSV extends Source
|
||||
throw new \Exception('Not Implemented');
|
||||
}
|
||||
|
||||
protected function exportGroupBackups(int $batchSize, array $resources): void
|
||||
{
|
||||
throw new \Exception('Not Implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* @param callable(resource $stream, string $delimiter): void $callback
|
||||
* @return void
|
||||
|
||||
@@ -822,4 +822,9 @@ class Firebase extends Source
|
||||
{
|
||||
throw new \Exception('Not implemented');
|
||||
}
|
||||
|
||||
protected function exportGroupBackups(int $batchSize, array $resources): void
|
||||
{
|
||||
throw new \Exception('Not implemented');
|
||||
}
|
||||
}
|
||||
@@ -209,6 +209,11 @@ class JSON extends Source
|
||||
throw new \Exception('Not Implemented');
|
||||
}
|
||||
|
||||
protected function exportGroupBackups(int $batchSize, array $resources): void
|
||||
{
|
||||
throw new \Exception('Not Implemented');
|
||||
}
|
||||
|
||||
/**
|
||||
* @throws \Exception
|
||||
*/
|
||||
|
||||
@@ -8,6 +8,7 @@ use Utopia\Migration\Resource;
|
||||
use Utopia\Migration\Resources\Auth\Hash;
|
||||
use Utopia\Migration\Resources\Auth\User;
|
||||
use Utopia\Migration\Resources\Database\Column;
|
||||
use Utopia\Migration\Resources\Database\Columns\BigInt;
|
||||
use Utopia\Migration\Resources\Database\Columns\Boolean;
|
||||
use Utopia\Migration\Resources\Database\Columns\DateTime;
|
||||
use Utopia\Migration\Resources\Database\Columns\Decimal;
|
||||
@@ -679,7 +680,7 @@ class NHost extends Source
|
||||
$column['column_default'] = null;
|
||||
}
|
||||
|
||||
return new Integer(
|
||||
return new BigInt(
|
||||
$column['column_name'],
|
||||
$table,
|
||||
required: $column['is_nullable'] === 'NO',
|
||||
@@ -956,4 +957,9 @@ class NHost extends Source
|
||||
{
|
||||
throw new \Exception('Not Implemented');
|
||||
}
|
||||
|
||||
protected function exportGroupBackups(int $batchSize, array $resources): void
|
||||
{
|
||||
throw new \Exception('Not Implemented');
|
||||
}
|
||||
}
|
||||
@@ -150,7 +150,6 @@ abstract class Target
|
||||
throw new \Exception(\curl_error($ch), Exception::CODE_INTERNAL);
|
||||
}
|
||||
|
||||
\curl_close($ch);
|
||||
|
||||
if ($responseStatus >= 400) {
|
||||
if (\is_array($responseBody)) {
|
||||
|
||||
@@ -26,6 +26,8 @@ class Transfer
|
||||
|
||||
public const GROUP_MESSAGING = 'messaging';
|
||||
|
||||
public const GROUP_BACKUPS = 'backups';
|
||||
|
||||
public const GROUP_AUTH_RESOURCES = [
|
||||
Resource::TYPE_USER,
|
||||
Resource::TYPE_TEAM,
|
||||
@@ -91,6 +93,10 @@ class Transfer
|
||||
|
||||
public const GROUP_SETTINGS_RESOURCES = [];
|
||||
|
||||
public const GROUP_BACKUPS_RESOURCES = [
|
||||
Resource::TYPE_BACKUP_POLICY,
|
||||
];
|
||||
|
||||
public const GROUP_MESSAGING_RESOURCES = [
|
||||
Resource::TYPE_PROVIDER,
|
||||
Resource::TYPE_TOPIC,
|
||||
@@ -119,6 +125,7 @@ class Transfer
|
||||
Resource::TYPE_TOPIC,
|
||||
Resource::TYPE_SUBSCRIBER,
|
||||
Resource::TYPE_MESSAGE,
|
||||
Resource::TYPE_BACKUP_POLICY,
|
||||
|
||||
// Integrations
|
||||
Resource::TYPE_PLATFORM,
|
||||
@@ -406,6 +413,7 @@ class Transfer
|
||||
self::GROUP_DATABASES_DOCUMENTS_DB => array_merge($resources, self::GROUP_DOCUMENTSDB_RESOURCES),
|
||||
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),
|
||||
default => throw new \Exception('No service group found'),
|
||||
};
|
||||
}
|
||||
|
||||
@@ -124,7 +124,6 @@ abstract class Base extends TestCase
|
||||
throw new \Exception(curl_error($ch));
|
||||
}
|
||||
|
||||
curl_close($ch);
|
||||
|
||||
if ($responseStatus >= 400) {
|
||||
if (is_array($responseBody)) {
|
||||
|
||||
@@ -85,6 +85,7 @@ class MockSource extends Source
|
||||
Resource::TYPE_TOPIC,
|
||||
Resource::TYPE_SUBSCRIBER,
|
||||
Resource::TYPE_MESSAGE,
|
||||
Resource::TYPE_BACKUP_POLICY,
|
||||
|
||||
// legacy
|
||||
Resource::TYPE_DOCUMENT,
|
||||
@@ -216,4 +217,15 @@ class MockSource extends Source
|
||||
$this->handleResourceTransfer(Transfer::GROUP_INTEGRATIONS, $resource);
|
||||
}
|
||||
}
|
||||
|
||||
protected function exportGroupBackups(int $batchSize, array $resources): void
|
||||
{
|
||||
foreach (Transfer::GROUP_BACKUPS_RESOURCES as $resource) {
|
||||
if (!\in_array($resource, $resources)) {
|
||||
continue;
|
||||
}
|
||||
|
||||
$this->handleResourceTransfer(Transfer::GROUP_BACKUPS, $resource);
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace Utopia\Tests\Unit\Destinations;
|
||||
|
||||
use Appwrite\Services\Databases;
|
||||
use Appwrite\Services\TablesDB;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionClass;
|
||||
use ReflectionMethod;
|
||||
use Utopia\Migration\Destinations\Appwrite as AppwriteDestination;
|
||||
|
||||
/**
|
||||
* Lock-in for the SDK boundary that drives ATTRIBUTE_IMMUTABLE_FIELDS.
|
||||
*
|
||||
* Reflects on appwrite/appwrite's TablesDB + Databases services. If the SDK
|
||||
* ships a new updateXColumn / updateXAttribute endpoint that exposes a
|
||||
* previously-non-SDK field, this test fails so DestinationAppwrite's
|
||||
* ATTRIBUTE_IMMUTABLE_FIELDS constant can be synced. Catches drift in CI
|
||||
* rather than at runtime.
|
||||
*/
|
||||
class AppwriteAttributeSdkBoundaryTest extends TestCase
|
||||
{
|
||||
/** Routing/identity params that aren't mutable column state. */
|
||||
private const IGNORED_PARAMS = ['databaseId', 'tableId', 'collectionId', 'key', 'newKey'];
|
||||
|
||||
/**
|
||||
* SDK param name → meta-doc field name. Param names that don't appear on
|
||||
* the meta doc as a top-level key map to null (e.g. min/max/elements live
|
||||
* inside formatOptions; onDelete lives inside the relationship's options).
|
||||
*
|
||||
* @var array<string, string|null>
|
||||
*/
|
||||
private const PARAM_TO_META_FIELD = [
|
||||
'xdefault' => 'default',
|
||||
'min' => null,
|
||||
'max' => null,
|
||||
'elements' => null,
|
||||
'onDelete' => null,
|
||||
];
|
||||
|
||||
public function testSdkExposedFieldsAreNotInNonSdkConstant(): void
|
||||
{
|
||||
$sdkExposed = $this->collectSdkExposedMetaFields();
|
||||
$nonSdk = $this->readNonSdkConstant();
|
||||
|
||||
$overlap = \array_intersect($sdkExposed, $nonSdk);
|
||||
$this->assertSame(
|
||||
[],
|
||||
\array_values($overlap),
|
||||
'ATTRIBUTE_IMMUTABLE_FIELDS marks these fields as non-SDK, but the appwrite/appwrite '
|
||||
. 'SDK exposes them via updateXColumn/updateXAttribute: ' . \implode(', ', $overlap)
|
||||
. '. Either drop these from ATTRIBUTE_IMMUTABLE_FIELDS or update PARAM_TO_META_FIELD '
|
||||
. 'in this test if the SDK param doesn\'t correspond to a top-level meta-doc field.',
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string> meta-doc field names the SDK can mutate
|
||||
*/
|
||||
private function collectSdkExposedMetaFields(): array
|
||||
{
|
||||
$fields = [];
|
||||
foreach ([TablesDB::class, Databases::class] as $service) {
|
||||
$service = new ReflectionClass($service);
|
||||
foreach ($service->getMethods(ReflectionMethod::IS_PUBLIC) as $method) {
|
||||
if (!\preg_match('/^update.+(Column|Attribute)$/', $method->getName())) {
|
||||
continue;
|
||||
}
|
||||
foreach ($method->getParameters() as $param) {
|
||||
$name = $param->getName();
|
||||
if (\in_array($name, self::IGNORED_PARAMS, true)) {
|
||||
continue;
|
||||
}
|
||||
$mapped = \array_key_exists($name, self::PARAM_TO_META_FIELD)
|
||||
? self::PARAM_TO_META_FIELD[$name]
|
||||
: $name;
|
||||
if ($mapped === null) {
|
||||
continue;
|
||||
}
|
||||
$fields[$mapped] = true;
|
||||
}
|
||||
}
|
||||
}
|
||||
return \array_keys($fields);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return list<string>
|
||||
*/
|
||||
private function readNonSdkConstant(): array
|
||||
{
|
||||
/** @var list<string> $value */
|
||||
$value = (new ReflectionClass(AppwriteDestination::class))->getConstant('ATTRIBUTE_IMMUTABLE_FIELDS');
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,96 @@
|
||||
<?php
|
||||
|
||||
namespace Utopia\Tests\Unit\Destinations;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use ReflectionClass;
|
||||
use Utopia\Database\Database as UtopiaDatabase;
|
||||
use Utopia\Database\Document as UtopiaDocument;
|
||||
use Utopia\Migration\Destinations\Appwrite as AppwriteDestination;
|
||||
use Utopia\Migration\Destinations\OnDuplicate;
|
||||
use Utopia\Migration\Resources\Database\Database as DatabaseResource;
|
||||
|
||||
/**
|
||||
* Regression for PR #151: the destination must never write the source's DSN
|
||||
* into `_databases.database`. With no resolver, the value must be blank so
|
||||
* the runtime falls back to the destination project's DSN. With a resolver,
|
||||
* the resolver's value must be written and the source's value ignored.
|
||||
*
|
||||
* Reproduces the comuneo-pre-production incident where post-migration
|
||||
* `_databases.database` rows pointed at the source's host (db11) and
|
||||
* destination reads hit `Table 'appwrite._<tenant>__metadata' doesn't exist`.
|
||||
*/
|
||||
class AppwriteDestinationDsnTest extends TestCase
|
||||
{
|
||||
public function testWithoutResolverReturnsEmptyString(): void
|
||||
{
|
||||
$destination = $this->makeDestination(getDatabaseDSN: null);
|
||||
$resource = $this->makeResource(sourceDsn: 'database_db_fra1_self_hosted_11_0');
|
||||
|
||||
$resolved = $this->invokeResolver($destination, $resource);
|
||||
|
||||
$this->assertSame('', $resolved, 'Without a resolver the destination must not propagate the source DSN.');
|
||||
}
|
||||
|
||||
public function testWithResolverUsesItsReturnValue(): void
|
||||
{
|
||||
$expected = 'appwrite://database_db_fra1_self_hosted_17_0?database=appwrite&namespace=_1';
|
||||
$destination = $this->makeDestination(
|
||||
getDatabaseDSN: fn (DatabaseResource $r): string => $expected,
|
||||
);
|
||||
$resource = $this->makeResource(sourceDsn: 'database_db_fra1_self_hosted_11_0');
|
||||
|
||||
$resolved = $this->invokeResolver($destination, $resource);
|
||||
|
||||
$this->assertSame($expected, $resolved);
|
||||
$this->assertNotSame($resource->getDatabase(), $resolved, 'Source DSN must not leak through the resolver path.');
|
||||
}
|
||||
|
||||
public function testResolverReceivesTheResource(): void
|
||||
{
|
||||
$captured = null;
|
||||
$destination = $this->makeDestination(
|
||||
getDatabaseDSN: function (DatabaseResource $r) use (&$captured): string {
|
||||
$captured = $r;
|
||||
return 'resolved';
|
||||
},
|
||||
);
|
||||
$resource = $this->makeResource(sourceDsn: 'src');
|
||||
|
||||
$this->invokeResolver($destination, $resource);
|
||||
|
||||
$this->assertSame($resource, $captured);
|
||||
}
|
||||
|
||||
private function makeDestination(?callable $getDatabaseDSN): AppwriteDestination
|
||||
{
|
||||
return new AppwriteDestination(
|
||||
project: 'destination-project',
|
||||
endpoint: 'http://example.test/v1',
|
||||
key: 'test-key',
|
||||
dbForProject: $this->createStub(UtopiaDatabase::class),
|
||||
getDatabasesDB: fn (UtopiaDocument $database): UtopiaDatabase => $this->createStub(UtopiaDatabase::class),
|
||||
collectionStructure: ['attributes' => [], 'indexes' => []],
|
||||
onDuplicate: OnDuplicate::Fail,
|
||||
getDatabaseDSN: $getDatabaseDSN,
|
||||
);
|
||||
}
|
||||
|
||||
private function makeResource(string $sourceDsn): DatabaseResource
|
||||
{
|
||||
return new DatabaseResource(
|
||||
id: 'src-database',
|
||||
name: 'src',
|
||||
type: 'legacy',
|
||||
database: $sourceDsn,
|
||||
);
|
||||
}
|
||||
|
||||
private function invokeResolver(AppwriteDestination $destination, DatabaseResource $resource): string
|
||||
{
|
||||
$method = (new ReflectionClass(AppwriteDestination::class))->getMethod('resolveDestinationDsn');
|
||||
/** @var string $value */
|
||||
$value = $method->invoke($destination, $resource);
|
||||
return $value;
|
||||
}
|
||||
}
|
||||
@@ -1,6 +1,6 @@
|
||||
<?php
|
||||
|
||||
namespace Migration\Unit\General;
|
||||
namespace Utopia\Tests\Unit\General;
|
||||
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Utopia\Migration\Cache;
|
||||
|
||||
@@ -0,0 +1,155 @@
|
||||
<?php
|
||||
|
||||
namespace Utopia\Tests\Unit\General;
|
||||
|
||||
use PHPUnit\Framework\Attributes\DataProvider;
|
||||
use PHPUnit\Framework\TestCase;
|
||||
use Utopia\Migration\Destinations\OnDuplicate;
|
||||
use Utopia\Migration\Destinations\SchemaAction;
|
||||
|
||||
/**
|
||||
* OnDuplicate::resolveSchemaAction is the load-bearing decision point for
|
||||
* re-migration tolerance. The destination then runs a spec-match guard that
|
||||
* overrides Overwrite to Skip when source and dest already have
|
||||
* identical spec — see DestinationAppwrite. These tests lock the
|
||||
* mode × existence × updatedAt matrix.
|
||||
*/
|
||||
class OnDuplicateTest extends TestCase
|
||||
{
|
||||
public function testNotExistsAlwaysCreates(): void
|
||||
{
|
||||
foreach (OnDuplicate::cases() as $mode) {
|
||||
$this->assertSame(
|
||||
SchemaAction::Create,
|
||||
$mode->resolveSchemaAction(exists: false),
|
||||
"{$mode->value} on non-existing resource must return Create",
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
public function testFailExistsReturnsCreateSoCallerDDLThrows(): void
|
||||
{
|
||||
// Fail is routed through Create (not Skip) so the caller's normal
|
||||
// create flow runs and the library surfaces DuplicateException as
|
||||
// designed. Returning Skip here would silently hide the error.
|
||||
$this->assertSame(
|
||||
SchemaAction::Create,
|
||||
OnDuplicate::Fail->resolveSchemaAction(exists: true),
|
||||
);
|
||||
}
|
||||
|
||||
public function testSkipAlwaysSkipsExisting(): void
|
||||
{
|
||||
// Skip must ignore updatedAt entirely — it's the "don't touch" contract.
|
||||
$this->assertSame(
|
||||
SchemaAction::Skip,
|
||||
OnDuplicate::Skip->resolveSchemaAction(
|
||||
exists: true,
|
||||
sourceUpdatedAt: '2026-01-01T00:00:00.000+00:00',
|
||||
destUpdatedAt: '2020-01-01T00:00:00.000+00:00',
|
||||
),
|
||||
);
|
||||
$this->assertSame(
|
||||
SchemaAction::Skip,
|
||||
OnDuplicate::Skip->resolveSchemaAction(
|
||||
exists: true,
|
||||
sourceUpdatedAt: '2020-01-01T00:00:00.000+00:00',
|
||||
destUpdatedAt: '2026-01-01T00:00:00.000+00:00',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function testOverwriteSourceNewerUpdatesInPlace(): void
|
||||
{
|
||||
// Source updatedAt strictly newer than dest → Overwrite. The
|
||||
// caller (DestinationAppwrite) follows up with attribute/index spec
|
||||
// checks and falls through to drop+recreate when the SDK can't
|
||||
// express the change.
|
||||
$this->assertSame(
|
||||
SchemaAction::Overwrite,
|
||||
OnDuplicate::Overwrite->resolveSchemaAction(
|
||||
exists: true,
|
||||
sourceUpdatedAt: '2026-04-23T10:00:00.000+00:00',
|
||||
destUpdatedAt: '2026-04-23T09:59:59.000+00:00',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function testOverwriteSourceEqualSkips(): void
|
||||
{
|
||||
$when = '2026-04-23T10:00:00.000+00:00';
|
||||
$this->assertSame(
|
||||
SchemaAction::Skip,
|
||||
OnDuplicate::Overwrite->resolveSchemaAction(
|
||||
exists: true,
|
||||
sourceUpdatedAt: $when,
|
||||
destUpdatedAt: $when,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function testOverwriteSourceOlderSkips(): void
|
||||
{
|
||||
// Dest is ahead — don't roll back. Avoids overwriting newer
|
||||
// destination edits with stale source data.
|
||||
$this->assertSame(
|
||||
SchemaAction::Skip,
|
||||
OnDuplicate::Overwrite->resolveSchemaAction(
|
||||
exists: true,
|
||||
sourceUpdatedAt: '2026-04-23T09:00:00.000+00:00',
|
||||
destUpdatedAt: '2026-04-23T10:00:00.000+00:00',
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function testOverwriteNoTimestampsSkips(): void
|
||||
{
|
||||
// No timestamps provided at all → no information to act on. Conservative:
|
||||
// Skip rather than risk a destructive update on uncertain input.
|
||||
$this->assertSame(
|
||||
SchemaAction::Skip,
|
||||
OnDuplicate::Overwrite->resolveSchemaAction(exists: true),
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* @return array<string, array{0: ?string, 1: ?string}>
|
||||
*/
|
||||
public static function unparseableTimestampPairs(): array
|
||||
{
|
||||
return [
|
||||
'both empty' => ['', ''],
|
||||
'both null' => [null, null],
|
||||
'source empty' => ['', '2026-04-23T10:00:00.000+00:00'],
|
||||
'dest empty' => ['2026-04-23T10:00:00.000+00:00', ''],
|
||||
'source null' => [null, '2026-04-23T10:00:00.000+00:00'],
|
||||
'dest null' => ['2026-04-23T10:00:00.000+00:00', null],
|
||||
'source zero-date' => ['0000-00-00 00:00:00', '2026-04-23T10:00:00.000+00:00'],
|
||||
'dest zero-date' => ['2026-04-23T10:00:00.000+00:00', '0000-00-00 00:00:00'],
|
||||
'source garbage' => ['not-a-date', '2026-04-23T10:00:00.000+00:00'],
|
||||
'dest garbage' => ['2026-04-23T10:00:00.000+00:00', 'not-a-date'],
|
||||
];
|
||||
}
|
||||
|
||||
#[DataProvider('unparseableTimestampPairs')]
|
||||
public function testOverwriteUnparseableUpdatedAtSkips(?string $source, ?string $dest): void
|
||||
{
|
||||
// Conservative: unparseable updatedAt preserves existing destination
|
||||
// rather than risk a destructive update on garbage input.
|
||||
$this->assertSame(
|
||||
SchemaAction::Skip,
|
||||
OnDuplicate::Overwrite->resolveSchemaAction(
|
||||
exists: true,
|
||||
sourceUpdatedAt: $source,
|
||||
destUpdatedAt: $dest,
|
||||
),
|
||||
);
|
||||
}
|
||||
|
||||
public function testValuesListsAllCasesInDeclarationOrder(): void
|
||||
{
|
||||
// The values() helper is consumed by API/SDK param validators; this
|
||||
// protects against an accidental case-rename or reorder.
|
||||
$this->assertSame(['fail', 'skip', 'overwrite'], OnDuplicate::values());
|
||||
}
|
||||
}
|
||||
Reference in new issue
Block a user