appwrite/app/controllers/api/migrations.php

670 lines
27 KiB
PHP
Raw Normal View History

2023-08-04 16:21:41 +00:00
<?php
use Appwrite\Event\Event;
use Appwrite\Event\Migration;
use Appwrite\Extend\Exception;
2025-01-17 04:31:39 +00:00
use Appwrite\SDK\AuthType;
use Appwrite\SDK\ContentType;
use Appwrite\SDK\Method;
use Appwrite\SDK\Response as SDKResponse;
2023-08-04 16:21:41 +00:00
use Appwrite\Utopia\Database\Validator\Queries\Migrations;
use Appwrite\Utopia\Response;
2024-10-08 07:54:40 +00:00
use Utopia\App;
2023-08-04 16:21:41 +00:00
use Utopia\Database\Database;
use Utopia\Database\Document;
2024-02-12 16:02:04 +00:00
use Utopia\Database\Exception\Query as QueryException;
2023-08-04 16:21:41 +00:00
use Utopia\Database\Helpers\ID;
use Utopia\Database\Query;
use Utopia\Database\Validator\Query\Cursor;
2023-08-04 16:21:41 +00:00
use Utopia\Database\Validator\UID;
2023-08-09 17:08:10 +00:00
use Utopia\Migration\Sources\Appwrite;
use Utopia\Migration\Sources\Firebase;
use Utopia\Migration\Sources\NHost;
use Utopia\Migration\Sources\Supabase;
2024-10-08 07:54:40 +00:00
use Utopia\Validator\ArrayList;
use Utopia\Validator\Integer;
use Utopia\Validator\Text;
use Utopia\Validator\URL;
use Utopia\Validator\WhiteList;
2023-08-04 16:21:41 +00:00
include_once __DIR__ . '/../shared/api.php';
2024-10-08 07:54:40 +00:00
App::post('/v1/migrations/appwrite')
2023-08-04 16:21:41 +00:00
->groups(['api', 'migrations'])
->desc('Migrate Appwrite data')
2023-08-04 16:21:41 +00:00
->label('scope', 'migrations.write')
2023-08-16 15:01:56 +00:00
->label('event', 'migrations.[migrationId].create')
2023-08-04 16:21:41 +00:00
->label('audits.event', 'migration.create')
2025-01-17 04:31:39 +00:00
->label('sdk', new Method(
namespace: 'migrations',
name: 'createAppwriteMigration',
description: '/docs/references/migrations/migration-appwrite.md',
auth: [AuthType::ADMIN],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_ACCEPTED,
model: Response::MODEL_MIGRATION,
)
]
))
2023-08-04 16:21:41 +00:00
->param('resources', [], new ArrayList(new WhiteList(Appwrite::getSupportedResources())), 'List of resources to migrate')
->param('endpoint', '', new URL(), "Source's Appwrite Endpoint")
->param('projectId', '', new UID(), "Source's Project ID")
->param('apiKey', '', new Text(512), "Source's API Key")
->inject('response')
->inject('dbForProject')
->inject('project')
->inject('user')
2023-10-01 17:39:26 +00:00
->inject('queueForEvents')
->inject('queueForMigrations')
->action(function (array $resources, string $endpoint, string $projectId, string $apiKey, Response $response, Database $dbForProject, Document $project, Document $user, Event $queueForEvents, Migration $queueForMigrations) {
2023-08-04 16:21:41 +00:00
$migration = $dbForProject->createDocument('migrations', new Document([
'$id' => ID::unique(),
'status' => 'pending',
'stage' => 'init',
'source' => Appwrite::getName(),
2024-05-27 15:56:28 +00:00
'destination' => Appwrite::getName(),
2023-08-04 16:21:41 +00:00
'credentials' => [
'endpoint' => $endpoint,
'projectId' => $projectId,
'apiKey' => $apiKey,
],
'resources' => $resources,
'statusCounters' => '{}',
'resourceData' => '{}',
'errors' => [],
]));
2023-10-01 17:39:26 +00:00
$queueForEvents->setParam('migrationId', $migration->getId());
2023-08-04 16:21:41 +00:00
// Trigger Transfer
2023-10-01 17:39:26 +00:00
$queueForMigrations
2023-08-04 16:21:41 +00:00
->setMigration($migration)
->setProject($project)
->setUser($user)
->trigger();
$response
->setStatusCode(Response::STATUS_CODE_ACCEPTED)
->dynamic($migration, Response::MODEL_MIGRATION);
});
2025-01-17 04:31:39 +00:00
2024-10-08 07:54:40 +00:00
App::post('/v1/migrations/firebase')
2023-08-09 22:46:23 +00:00
->groups(['api', 'migrations'])
2024-12-30 09:00:52 +00:00
->desc('Migrate Firebase data')
2023-08-09 22:46:23 +00:00
->label('scope', 'migrations.write')
2023-08-16 15:01:56 +00:00
->label('event', 'migrations.[migrationId].create')
2023-08-09 22:46:23 +00:00
->label('audits.event', 'migration.create')
2025-01-17 04:31:39 +00:00
->label('sdk', new Method(
namespace: 'migrations',
name: 'createFirebaseMigration',
description: '/docs/references/migrations/migration-firebase.md',
auth: [AuthType::ADMIN],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_ACCEPTED,
model: Response::MODEL_MIGRATION,
)
]
))
2023-08-09 22:46:23 +00:00
->param('resources', [], new ArrayList(new WhiteList(Firebase::getSupportedResources())), 'List of resources to migrate')
->param('serviceAccount', '', new Text(65536), 'JSON of the Firebase service account credentials')
->inject('response')
->inject('dbForProject')
->inject('project')
->inject('user')
2023-10-01 17:39:26 +00:00
->inject('queueForEvents')
->inject('queueForMigrations')
->action(function (array $resources, string $serviceAccount, Response $response, Database $dbForProject, Document $project, Document $user, Event $queueForEvents, Migration $queueForMigrations) {
$serviceAccountData = json_decode($serviceAccount, true);
if (empty($serviceAccountData)) {
throw new Exception(Exception::MIGRATION_PROVIDER_ERROR, 'Invalid Service Account JSON');
}
if (!isset($serviceAccountData['project_id']) || !isset($serviceAccountData['client_email']) || !isset($serviceAccountData['private_key'])) {
throw new Exception(Exception::MIGRATION_PROVIDER_ERROR, 'Invalid Service Account JSON');
}
2023-10-13 15:23:20 +00:00
2023-08-09 22:46:23 +00:00
$migration = $dbForProject->createDocument('migrations', new Document([
'$id' => ID::unique(),
'status' => 'pending',
'stage' => 'init',
'source' => Firebase::getName(),
2024-05-27 15:56:28 +00:00
'destination' => Appwrite::getName(),
2023-08-09 22:46:23 +00:00
'credentials' => [
'serviceAccount' => $serviceAccount,
],
'resources' => $resources,
'statusCounters' => '{}',
'resourceData' => '{}',
'errors' => [],
]));
2023-10-01 17:39:26 +00:00
$queueForEvents->setParam('migrationId', $migration->getId());
2023-08-09 22:46:23 +00:00
// Trigger Transfer
2023-10-01 17:39:26 +00:00
$queueForMigrations
->setMigration($migration)
->setProject($project)
->setUser($user)
->trigger();
$response
->setStatusCode(Response::STATUS_CODE_ACCEPTED)
->dynamic($migration, Response::MODEL_MIGRATION);
2023-08-04 16:21:41 +00:00
});
2024-10-08 07:54:40 +00:00
App::post('/v1/migrations/supabase')
2023-08-04 16:21:41 +00:00
->groups(['api', 'migrations'])
->desc('Migrate Supabase data')
2023-08-04 16:21:41 +00:00
->label('scope', 'migrations.write')
2023-08-16 15:01:56 +00:00
->label('event', 'migrations.[migrationId].create')
2023-08-04 16:21:41 +00:00
->label('audits.event', 'migration.create')
2025-01-17 04:31:39 +00:00
->label('sdk', new Method(
namespace: 'migrations',
name: 'createSupabaseMigration',
description: '/docs/references/migrations/migration-supabase.md',
auth: [AuthType::ADMIN],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_ACCEPTED,
model: Response::MODEL_MIGRATION,
)
]
))
2023-08-04 16:21:41 +00:00
->param('resources', [], new ArrayList(new WhiteList(Supabase::getSupportedResources(), true)), 'List of resources to migrate')
->param('endpoint', '', new URL(), 'Source\'s Supabase Endpoint')
->param('apiKey', '', new Text(512), 'Source\'s API Key')
->param('databaseHost', '', new Text(512), 'Source\'s Database Host')
->param('username', '', new Text(512), 'Source\'s Database Username')
->param('password', '', new Text(512), 'Source\'s Database Password')
->param('port', 5432, new Integer(true), 'Source\'s Database Port', true)
->inject('response')
->inject('dbForProject')
->inject('project')
->inject('user')
2023-10-01 17:39:26 +00:00
->inject('queueForEvents')
->inject('queueForMigrations')
->action(function (array $resources, string $endpoint, string $apiKey, string $databaseHost, string $username, string $password, int $port, Response $response, Database $dbForProject, Document $project, Document $user, Event $queueForEvents, Migration $queueForMigrations) {
2023-08-04 16:21:41 +00:00
$migration = $dbForProject->createDocument('migrations', new Document([
'$id' => ID::unique(),
'status' => 'pending',
'stage' => 'init',
'source' => Supabase::getName(),
2024-05-27 15:56:28 +00:00
'destination' => Appwrite::getName(),
2023-08-04 16:21:41 +00:00
'credentials' => [
'endpoint' => $endpoint,
'apiKey' => $apiKey,
'databaseHost' => $databaseHost,
'username' => $username,
'password' => $password,
'port' => $port,
],
'resources' => $resources,
'statusCounters' => '{}',
'resourceData' => '{}',
'errors' => [],
]));
2023-10-01 17:39:26 +00:00
$queueForEvents->setParam('migrationId', $migration->getId());
2023-08-04 16:21:41 +00:00
// Trigger Transfer
2023-10-01 17:39:26 +00:00
$queueForMigrations
2023-08-04 16:21:41 +00:00
->setMigration($migration)
->setProject($project)
->setUser($user)
->trigger();
$response
->setStatusCode(Response::STATUS_CODE_ACCEPTED)
->dynamic($migration, Response::MODEL_MIGRATION);
});
2024-10-08 07:54:40 +00:00
App::post('/v1/migrations/nhost')
2023-08-04 16:21:41 +00:00
->groups(['api', 'migrations'])
->desc('Migrate NHost data')
2023-08-04 16:21:41 +00:00
->label('scope', 'migrations.write')
2023-08-16 15:01:56 +00:00
->label('event', 'migrations.[migrationId].create')
2023-08-04 16:21:41 +00:00
->label('audits.event', 'migration.create')
2025-01-17 04:31:39 +00:00
->label('sdk', new Method(
namespace: 'migrations',
name: 'createNHostMigration',
description: '/docs/references/migrations/migration-nhost.md',
auth: [AuthType::ADMIN],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_ACCEPTED,
model: Response::MODEL_MIGRATION,
)
]
))
2023-08-04 16:21:41 +00:00
->param('resources', [], new ArrayList(new WhiteList(NHost::getSupportedResources())), 'List of resources to migrate')
2023-08-16 15:01:56 +00:00
->param('subdomain', '', new Text(512), 'Source\'s Subdomain')
2023-08-04 16:21:41 +00:00
->param('region', '', new Text(512), 'Source\'s Region')
->param('adminSecret', '', new Text(512), 'Source\'s Admin Secret')
->param('database', '', new Text(512), 'Source\'s Database Name')
->param('username', '', new Text(512), 'Source\'s Database Username')
->param('password', '', new Text(512), 'Source\'s Database Password')
->param('port', 5432, new Integer(true), 'Source\'s Database Port', true)
->inject('response')
->inject('dbForProject')
->inject('project')
->inject('user')
2023-10-01 17:39:26 +00:00
->inject('queueForEvents')
->inject('queueForMigrations')
->action(function (array $resources, string $subdomain, string $region, string $adminSecret, string $database, string $username, string $password, int $port, Response $response, Database $dbForProject, Document $project, Document $user, Event $queueForEvents, Migration $queueForMigrations) {
2023-08-04 16:21:41 +00:00
$migration = $dbForProject->createDocument('migrations', new Document([
'$id' => ID::unique(),
'status' => 'pending',
'stage' => 'init',
'source' => NHost::getName(),
2024-05-27 15:56:28 +00:00
'destination' => Appwrite::getName(),
2023-08-04 16:21:41 +00:00
'credentials' => [
'subdomain' => $subdomain,
'region' => $region,
'adminSecret' => $adminSecret,
'database' => $database,
'username' => $username,
'password' => $password,
'port' => $port,
],
'resources' => $resources,
'statusCounters' => '{}',
'resourceData' => '{}',
'errors' => [],
]));
2023-10-01 17:39:26 +00:00
$queueForEvents->setParam('migrationId', $migration->getId());
2023-08-04 16:21:41 +00:00
// Trigger Transfer
2023-10-01 17:39:26 +00:00
$queueForMigrations
2023-08-04 16:21:41 +00:00
->setMigration($migration)
->setProject($project)
->setUser($user)
->trigger();
$response
->setStatusCode(Response::STATUS_CODE_ACCEPTED)
->dynamic($migration, Response::MODEL_MIGRATION);
});
2024-10-08 07:54:40 +00:00
App::get('/v1/migrations')
2023-08-04 16:21:41 +00:00
->groups(['api', 'migrations'])
->desc('List migrations')
2023-08-04 16:21:41 +00:00
->label('scope', 'migrations.read')
2025-01-17 04:31:39 +00:00
->label('sdk', new Method(
namespace: 'migrations',
name: 'list',
description: '/docs/references/migrations/list-migrations.md',
auth: [AuthType::ADMIN],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_MIGRATION_LIST,
)
]
))
2023-08-04 16:21:41 +00:00
->param('queries', [], new Migrations(), 'Array of query strings generated using the Query class provided by the SDK. [Learn more about queries](https://appwrite.io/docs/databases#querying-documents). Maximum of ' . APP_LIMIT_ARRAY_PARAMS_SIZE . ' queries are allowed, each ' . APP_LIMIT_ARRAY_ELEMENT_SIZE . ' characters long. You may filter on the following attributes: ' . implode(', ', Migrations::ALLOWED_ATTRIBUTES), true)
->param('search', '', new Text(256), 'Search term to filter your list results. Max length: 256 chars.', true)
->inject('response')
->inject('dbForProject')
->action(function (array $queries, string $search, Response $response, Database $dbForProject) {
2024-02-12 16:02:04 +00:00
try {
$queries = Query::parseQueries($queries);
} catch (QueryException $e) {
throw new Exception(Exception::GENERAL_QUERY_INVALID, $e->getMessage());
}
2023-08-04 16:21:41 +00:00
if (!empty($search)) {
$queries[] = Query::search('search', $search);
}
2024-02-12 09:55:45 +00:00
/**
2024-02-12 10:03:31 +00:00
* Get cursor document if there was a cursor query, we use array_filter and reset for reference $cursor to $queries
2024-02-12 09:55:45 +00:00
*/
2023-08-22 03:25:55 +00:00
$cursor = \array_filter($queries, function ($query) {
2023-12-06 14:10:40 +00:00
return \in_array($query->getMethod(), [Query::TYPE_CURSOR_AFTER, Query::TYPE_CURSOR_BEFORE]);
2023-08-22 03:25:55 +00:00
});
2023-08-04 16:21:41 +00:00
$cursor = reset($cursor);
if ($cursor) {
/** @var Query $cursor */
$validator = new Cursor();
if (!$validator->isValid($cursor)) {
throw new Exception(Exception::GENERAL_QUERY_INVALID, $validator->getDescription());
}
2023-08-04 16:21:41 +00:00
$migrationId = $cursor->getValue();
$cursorDocument = $dbForProject->getDocument('migrations', $migrationId);
if ($cursorDocument->isEmpty()) {
throw new Exception(Exception::GENERAL_CURSOR_NOT_FOUND, "Migration '{$migrationId}' for the 'cursor' value not found.");
}
$cursor->setValue($cursorDocument);
}
$filterQueries = Query::groupByType($queries)['filters'];
$response->dynamic(new Document([
'migrations' => $dbForProject->find('migrations', $queries),
'total' => $dbForProject->count('migrations', $filterQueries, APP_LIMIT_COUNT),
]), Response::MODEL_MIGRATION_LIST);
});
2024-10-08 07:54:40 +00:00
App::get('/v1/migrations/:migrationId')
2023-08-04 16:21:41 +00:00
->groups(['api', 'migrations'])
->desc('Get migration')
2023-08-04 16:21:41 +00:00
->label('scope', 'migrations.read')
2025-01-17 04:31:39 +00:00
->label('sdk', new Method(
namespace: 'migrations',
name: 'get',
description: '/docs/references/migrations/get-migration.md',
auth: [AuthType::ADMIN],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_MIGRATION,
)
]
))
2023-08-04 16:21:41 +00:00
->param('migrationId', '', new UID(), 'Migration unique ID.')
->inject('response')
->inject('dbForProject')
->action(function (string $migrationId, Response $response, Database $dbForProject) {
$migration = $dbForProject->getDocument('migrations', $migrationId);
if ($migration->isEmpty()) {
throw new Exception(Exception::MIGRATION_NOT_FOUND);
}
$response->dynamic($migration, Response::MODEL_MIGRATION);
});
2024-10-08 07:54:40 +00:00
App::get('/v1/migrations/appwrite/report')
2023-08-04 16:21:41 +00:00
->groups(['api', 'migrations'])
->desc('Generate a report on Appwrite data')
2023-08-04 16:21:41 +00:00
->label('scope', 'migrations.write')
2025-01-17 04:31:39 +00:00
->label('sdk', new Method(
namespace: 'migrations',
name: 'getAppwriteReport',
description: '/docs/references/migrations/migration-appwrite-report.md',
auth: [AuthType::ADMIN],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_MIGRATION_REPORT,
)
]
))
2023-08-04 16:21:41 +00:00
->param('resources', [], new ArrayList(new WhiteList(Appwrite::getSupportedResources())), 'List of resources to migrate')
->param('endpoint', '', new URL(), "Source's Appwrite Endpoint")
->param('projectID', '', new Text(512), "Source's Project ID")
->param('key', '', new Text(512), "Source's API Key")
->inject('response')
->inject('dbForProject')
->inject('project')
->inject('user')
->action(function (array $resources, string $endpoint, string $projectID, string $key, Response $response) {
$appwrite = new Appwrite($projectID, $endpoint, $key);
2023-08-04 16:21:41 +00:00
try {
$report = $appwrite->report($resources);
} catch (\Throwable $e) {
switch ($e->getCode()) {
case 401:
throw new Exception(Exception::GENERAL_UNAUTHORIZED_SCOPE, 'Source Error: ' . $e->getMessage());
case 429:
throw new Exception(Exception::GENERAL_RATE_LIMIT_EXCEEDED, 'Source Error: Rate Limit Exceeded, Is your Cloud Provider blocking Appwrite\'s IP?');
case 500:
throw new Exception(Exception::MIGRATION_PROVIDER_ERROR, 'Source Error: ' . $e->getMessage());
}
throw new Exception(Exception::MIGRATION_PROVIDER_ERROR, 'Source Error: ' . $e->getMessage());
2023-08-04 16:21:41 +00:00
}
$response
->setStatusCode(Response::STATUS_CODE_OK)
->dynamic(new Document($report), Response::MODEL_MIGRATION_REPORT);
2023-08-04 16:21:41 +00:00
});
2024-10-08 07:54:40 +00:00
App::get('/v1/migrations/firebase/report')
2023-08-04 16:21:41 +00:00
->groups(['api', 'migrations'])
->desc('Generate a report on Firebase data')
2023-08-04 16:21:41 +00:00
->label('scope', 'migrations.write')
2025-01-17 04:31:39 +00:00
->label('sdk', new Method(
namespace: 'migrations',
name: 'getFirebaseReport',
description: '/docs/references/migrations/migration-firebase-report.md',
auth: [AuthType::ADMIN],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_MIGRATION_REPORT,
)
]
))
2023-08-04 16:21:41 +00:00
->param('resources', [], new ArrayList(new WhiteList(Firebase::getSupportedResources())), 'List of resources to migrate')
->param('serviceAccount', '', new Text(65536), 'JSON of the Firebase service account credentials')
->inject('response')
->action(function (array $resources, string $serviceAccount, Response $response) {
$serviceAccount = json_decode($serviceAccount, true);
if (empty($serviceAccount)) {
throw new Exception(Exception::MIGRATION_PROVIDER_ERROR, 'Invalid Service Account JSON');
}
if (!isset($serviceAccount['project_id']) || !isset($serviceAccount['client_email']) || !isset($serviceAccount['private_key'])) {
throw new Exception(Exception::MIGRATION_PROVIDER_ERROR, 'Invalid Service Account JSON');
}
$firebase = new Firebase($serviceAccount);
2023-08-04 16:21:41 +00:00
try {
$report = $firebase->report($resources);
} catch (\Throwable $e) {
switch ($e->getCode()) {
case 401:
throw new Exception(Exception::GENERAL_UNAUTHORIZED_SCOPE, 'Source Error: ' . $e->getMessage());
case 429:
throw new Exception(Exception::GENERAL_RATE_LIMIT_EXCEEDED, 'Source Error: Rate Limit Exceeded, Is your Cloud Provider blocking Appwrite\'s IP?');
case 500:
throw new Exception(Exception::MIGRATION_PROVIDER_ERROR, 'Source Error: ' . $e->getMessage());
}
2023-08-04 16:21:41 +00:00
throw new Exception(Exception::MIGRATION_PROVIDER_ERROR, 'Source Error: ' . $e->getMessage());
2023-08-04 16:21:41 +00:00
}
$response
->setStatusCode(Response::STATUS_CODE_OK)
->dynamic(new Document($report), Response::MODEL_MIGRATION_REPORT);
2023-08-04 16:21:41 +00:00
});
2024-10-08 07:54:40 +00:00
App::get('/v1/migrations/supabase/report')
2023-08-04 16:21:41 +00:00
->groups(['api', 'migrations'])
->desc('Generate a report on Supabase Data')
->label('scope', 'migrations.write')
2025-01-17 04:31:39 +00:00
->label('sdk', new Method(
namespace: 'migrations',
name: 'getSupabaseReport',
description: '/docs/references/migrations/migration-supabase-report.md',
auth: [AuthType::ADMIN],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_MIGRATION_REPORT,
)
]
))
2023-08-04 16:21:41 +00:00
->param('resources', [], new ArrayList(new WhiteList(Supabase::getSupportedResources(), true)), 'List of resources to migrate')
->param('endpoint', '', new URL(), 'Source\'s Supabase Endpoint.')
->param('apiKey', '', new Text(512), 'Source\'s API Key.')
->param('databaseHost', '', new Text(512), 'Source\'s Database Host.')
->param('username', '', new Text(512), 'Source\'s Database Username.')
->param('password', '', new Text(512), 'Source\'s Database Password.')
->param('port', 5432, new Integer(true), 'Source\'s Database Port.', true)
2023-08-04 16:21:41 +00:00
->inject('response')
->inject('dbForProject')
->action(function (array $resources, string $endpoint, string $apiKey, string $databaseHost, string $username, string $password, int $port, Response $response) {
$supabase = new Supabase($endpoint, $apiKey, $databaseHost, 'postgres', $username, $password, $port);
2023-08-04 16:21:41 +00:00
try {
$report = $supabase->report($resources);
} catch (\Throwable $e) {
switch ($e->getCode()) {
case 401:
throw new Exception(Exception::GENERAL_UNAUTHORIZED_SCOPE, 'Source Error: ' . $e->getMessage());
case 429:
throw new Exception(Exception::GENERAL_RATE_LIMIT_EXCEEDED, 'Source Error: Rate Limit Exceeded, Is your Cloud Provider blocking Appwrite\'s IP?');
case 500:
throw new Exception(Exception::MIGRATION_PROVIDER_ERROR, 'Source Error: ' . $e->getMessage());
}
2023-08-04 16:21:41 +00:00
throw new Exception(Exception::MIGRATION_PROVIDER_ERROR, 'Source Error: ' . $e->getMessage());
2023-08-04 16:21:41 +00:00
}
$response
->setStatusCode(Response::STATUS_CODE_OK)
->dynamic(new Document($report), Response::MODEL_MIGRATION_REPORT);
2023-08-04 16:21:41 +00:00
});
2024-10-08 07:54:40 +00:00
App::get('/v1/migrations/nhost/report')
2023-08-04 16:21:41 +00:00
->groups(['api', 'migrations'])
->desc('Generate a report on NHost Data')
->label('scope', 'migrations.write')
2025-01-17 04:31:39 +00:00
->label('sdk', new Method(
namespace: 'migrations',
name: 'getNHostReport',
description: '/docs/references/migrations/migration-nhost-report.md',
auth: [AuthType::ADMIN],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_OK,
model: Response::MODEL_MIGRATION_REPORT,
)
]
))
->param('resources', [], new ArrayList(new WhiteList(NHost::getSupportedResources())), 'List of resources to migrate.')
->param('subdomain', '', new Text(512), 'Source\'s Subdomain.')
->param('region', '', new Text(512), 'Source\'s Region.')
->param('adminSecret', '', new Text(512), 'Source\'s Admin Secret.')
->param('database', '', new Text(512), 'Source\'s Database Name.')
->param('username', '', new Text(512), 'Source\'s Database Username.')
->param('password', '', new Text(512), 'Source\'s Database Password.')
->param('port', 5432, new Integer(true), 'Source\'s Database Port.', true)
2023-08-04 16:21:41 +00:00
->inject('response')
->action(function (array $resources, string $subdomain, string $region, string $adminSecret, string $database, string $username, string $password, int $port, Response $response) {
$nhost = new NHost($subdomain, $region, $adminSecret, $database, $username, $password, $port);
2023-08-04 16:21:41 +00:00
try {
$report = $nhost->report($resources);
} catch (\Throwable $e) {
switch ($e->getCode()) {
case 401:
throw new Exception(Exception::GENERAL_UNAUTHORIZED_SCOPE, 'Source Error: ' . $e->getMessage());
case 429:
throw new Exception(Exception::GENERAL_RATE_LIMIT_EXCEEDED, 'Source Error: Rate Limit Exceeded, Is your Cloud Provider blocking Appwrite\'s IP?');
case 500:
throw new Exception(Exception::MIGRATION_PROVIDER_ERROR, 'Source Error: ' . $e->getMessage());
}
2023-08-04 16:21:41 +00:00
throw new Exception(Exception::MIGRATION_PROVIDER_ERROR, 'Source Error: ' . $e->getMessage());
2023-08-04 16:21:41 +00:00
}
$response
->setStatusCode(Response::STATUS_CODE_OK)
->dynamic(new Document($report), Response::MODEL_MIGRATION_REPORT);
2023-08-04 16:21:41 +00:00
});
2024-10-08 07:54:40 +00:00
App::patch('/v1/migrations/:migrationId')
2023-08-04 16:21:41 +00:00
->groups(['api', 'migrations'])
->desc('Retry migration')
2023-08-04 16:21:41 +00:00
->label('scope', 'migrations.write')
->label('event', 'migrations.[migrationId].retry')
->label('audits.event', 'migration.retry')
->label('audits.resource', 'migrations/{request.migrationId}')
2025-01-17 04:31:39 +00:00
->label('sdk', new Method(
namespace: 'migrations',
name: 'retry',
description: '/docs/references/migrations/retry-migration.md',
auth: [AuthType::ADMIN],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_ACCEPTED,
model: Response::MODEL_MIGRATION,
)
]
))
2023-08-04 16:21:41 +00:00
->param('migrationId', '', new UID(), 'Migration unique ID.')
->inject('response')
->inject('dbForProject')
->inject('project')
->inject('user')
2023-10-01 17:39:26 +00:00
->inject('queueForMigrations')
->action(function (string $migrationId, Response $response, Database $dbForProject, Document $project, Document $user, Migration $queueForMigrations) {
2023-08-04 16:21:41 +00:00
$migration = $dbForProject->getDocument('migrations', $migrationId);
if ($migration->isEmpty()) {
throw new Exception(Exception::MIGRATION_NOT_FOUND);
}
if ($migration->getAttribute('status') !== 'failed') {
throw new Exception(Exception::MIGRATION_IN_PROGRESS, 'Migration not failed yet');
}
$migration
->setAttribute('status', 'pending')
->setAttribute('dateUpdated', \time());
// Trigger Migration
2023-10-01 17:39:26 +00:00
$queueForMigrations
2023-08-04 16:21:41 +00:00
->setMigration($migration)
->setProject($project)
->setUser($user)
->trigger();
$response->noContent();
});
2024-10-08 07:54:40 +00:00
App::delete('/v1/migrations/:migrationId')
2023-08-04 16:21:41 +00:00
->groups(['api', 'migrations'])
->desc('Delete migration')
2023-08-04 16:21:41 +00:00
->label('scope', 'migrations.write')
->label('event', 'migrations.[migrationId].delete')
->label('audits.event', 'migrationId.delete')
->label('audits.resource', 'migrations/{request.migrationId}')
2025-01-17 04:31:39 +00:00
->label('sdk', new Method(
namespace: 'migrations',
name: 'delete',
description: '/docs/references/migrations/delete-migration.md',
auth: [AuthType::ADMIN],
responses: [
new SDKResponse(
code: Response::STATUS_CODE_NOCONTENT,
model: Response::MODEL_NONE,
)
],
contentType: ContentType::NONE
))
2023-08-04 16:21:41 +00:00
->param('migrationId', '', new UID(), 'Migration ID.')
->inject('response')
->inject('dbForProject')
2023-10-01 17:39:26 +00:00
->inject('queueForEvents')
->action(function (string $migrationId, Response $response, Database $dbForProject, Event $queueForEvents) {
2023-08-04 16:21:41 +00:00
$migration = $dbForProject->getDocument('migrations', $migrationId);
if ($migration->isEmpty()) {
throw new Exception(Exception::MIGRATION_NOT_FOUND);
}
if (!$dbForProject->deleteDocument('migrations', $migration->getId())) {
throw new Exception(Exception::GENERAL_SERVER_ERROR, 'Failed to remove migration from DB');
2023-08-04 16:21:41 +00:00
}
2023-10-01 17:39:26 +00:00
$queueForEvents->setParam('migrationId', $migration->getId());
2023-08-04 16:21:41 +00:00
$response->noContent();
2025-01-17 04:39:16 +00:00
});