|
61 | 61 | use Utopia\Migration\Resources\Messaging\Provider; |
62 | 62 | use Utopia\Migration\Resources\Messaging\Subscriber; |
63 | 63 | use Utopia\Migration\Resources\Messaging\Topic; |
| 64 | +use Utopia\Migration\Resources\Settings\ProjectVariable; |
64 | 65 | use Utopia\Migration\Resources\Sites\Deployment as SiteDeployment; |
65 | 66 | use Utopia\Migration\Resources\Sites\EnvVar as SiteEnvVar; |
66 | 67 | use Utopia\Migration\Resources\Sites\Site; |
@@ -214,6 +215,7 @@ public static function getSupportedResources(): array |
214 | 215 | Resource::TYPE_BACKUP_POLICY, |
215 | 216 |
|
216 | 217 | // Settings |
| 218 | + Resource::TYPE_PROJECT_VARIABLE, |
217 | 219 | ]; |
218 | 220 | } |
219 | 221 |
|
@@ -254,6 +256,7 @@ public function report(array $resources = [], array $resourceIds = []): array |
254 | 256 | $this->reportSites($resources, $report, $resourceIds); |
255 | 257 | $this->reportIntegrations($resources, $report, $resourceIds); |
256 | 258 | $this->reportBackups($resources, $report, $resourceIds); |
| 259 | + $this->reportSettings($resources, $report, $resourceIds); |
257 | 260 |
|
258 | 261 | $report['version'] = $this->call( |
259 | 262 | 'GET', |
@@ -1449,6 +1452,90 @@ protected function reportBackups(array $resources, array &$report, array $resour |
1449 | 1452 | } |
1450 | 1453 | } |
1451 | 1454 |
|
| 1455 | + private function reportSettings(array $resources, array &$report, array $resourceIds = []): void |
| 1456 | + { |
| 1457 | + if (\in_array(Resource::TYPE_PROJECT_VARIABLE, $resources)) { |
| 1458 | + $variableQueries = $this->buildQueries( |
| 1459 | + resourceType: Resource::TYPE_PROJECT_VARIABLE, |
| 1460 | + resourceIds: $resourceIds, |
| 1461 | + limit: 1 |
| 1462 | + ); |
| 1463 | + try { |
| 1464 | + $report[Resource::TYPE_PROJECT_VARIABLE] = $this->project->listVariables($variableQueries)->total; |
| 1465 | + } catch (\Throwable) { |
| 1466 | + $report[Resource::TYPE_PROJECT_VARIABLE] = 0; |
| 1467 | + } |
| 1468 | + } |
| 1469 | + } |
| 1470 | + |
| 1471 | + /** |
| 1472 | + * @param int $batchSize |
| 1473 | + * @param array<string> $resources |
| 1474 | + */ |
| 1475 | + protected function exportGroupSettings(int $batchSize, array $resources): void |
| 1476 | + { |
| 1477 | + if (\in_array(Resource::TYPE_PROJECT_VARIABLE, $resources)) { |
| 1478 | + try { |
| 1479 | + $this->exportProjectVariables($batchSize); |
| 1480 | + } catch (\Throwable $e) { |
| 1481 | + $this->addError(new Exception( |
| 1482 | + Resource::TYPE_PROJECT_VARIABLE, |
| 1483 | + Transfer::GROUP_SETTINGS, |
| 1484 | + message: $e->getMessage(), |
| 1485 | + code: $e->getCode(), |
| 1486 | + previous: $e |
| 1487 | + )); |
| 1488 | + } |
| 1489 | + } |
| 1490 | + } |
| 1491 | + |
| 1492 | + /** |
| 1493 | + * @throws AppwriteException |
| 1494 | + */ |
| 1495 | + private function exportProjectVariables(int $batchSize): void |
| 1496 | + { |
| 1497 | + $lastId = null; |
| 1498 | + |
| 1499 | + while (true) { |
| 1500 | + $queries = [Query::limit($batchSize)]; |
| 1501 | + |
| 1502 | + if ($this->rootResourceId !== '' && $this->rootResourceType === Resource::TYPE_PROJECT_VARIABLE) { |
| 1503 | + $queries[] = Query::equal('$id', $this->rootResourceId); |
| 1504 | + $queries[] = Query::limit(1); |
| 1505 | + } |
| 1506 | + |
| 1507 | + if ($lastId !== null) { |
| 1508 | + $queries[] = Query::cursorAfter($lastId); |
| 1509 | + } |
| 1510 | + |
| 1511 | + $response = $this->project->listVariables($queries); |
| 1512 | + if ($response->total === 0) { |
| 1513 | + break; |
| 1514 | + } |
| 1515 | + |
| 1516 | + $variables = []; |
| 1517 | + |
| 1518 | + foreach ($response->variables as $variable) { |
| 1519 | + $variables[] = new ProjectVariable( |
| 1520 | + $variable->id, |
| 1521 | + $variable->key, |
| 1522 | + $variable->value, |
| 1523 | + $variable->secret, |
| 1524 | + createdAt: $variable->createdAt, |
| 1525 | + updatedAt: $variable->updatedAt, |
| 1526 | + ); |
| 1527 | + |
| 1528 | + $lastId = $variable->id; |
| 1529 | + } |
| 1530 | + |
| 1531 | + $this->callback($variables); |
| 1532 | + |
| 1533 | + if (\count($response->variables) < $batchSize) { |
| 1534 | + break; |
| 1535 | + } |
| 1536 | + } |
| 1537 | + } |
| 1538 | + |
1452 | 1539 | /** |
1453 | 1540 | * @throws AppwriteException |
1454 | 1541 | */ |
|
0 commit comments