Merge pull request #12313 from appwrite/add-project-variable-migration

Add project variable migration support
This commit is contained in:
Jake Barnby authored and GitHub committed 2026-06-04 15:24:13 +12:00
commit 2498573b7e
2 files changed
+131

No files matched your search

@@ -65,6 +65,12 @@ class MigrationReport extends Model
'default' => 0,
'example' => 5,
])
->addRule(Resource::TYPE_PROJECT_VARIABLE, [
'type' => self::TYPE_INTEGER,
'description' => 'Number of project variables to be migrated.',
'default' => 0,
'example' => 5,
])
->addRule(Resource::TYPE_SITE, [
'type' => self::TYPE_INTEGER,
'description' => 'Number of sites to be migrated.',
@@ -11,6 +11,7 @@ use Tests\E2E\Scopes\ProjectCustom;
use Tests\E2E\Services\Functions\FunctionsBase;
use Utopia\Console;
use Utopia\Database\Database;
use Utopia\Database\Document;
use Utopia\Database\Helpers\ID;
use Utopia\Database\Helpers\Permission;
use Utopia\Database\Helpers\Role;
@@ -2627,6 +2628,130 @@ trait MigrationsBase
$this->client->call(Client::METHOD_DELETE, '/project/keys/' . $apiKey['$id'], $sourceHeaders);
}
public function testAppwriteMigrationProjectVariable(): void
{
$sourceHeaders = [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getProject()['$id'],
'x-appwrite-key' => $this->getProject()['apiKey'],
];
$destinationHeaders = [
'content-type' => 'application/json',
'x-appwrite-project' => $this->getDestinationProject()['$id'],
'x-appwrite-key' => $this->getDestinationProject()['apiKey'],
];
$listDestinationVariables = function () use ($destinationHeaders): array {
$variables = [];
$cursorId = null;
do {
$queries = [
Query::limit(100)->toString(),
];
if ($cursorId !== null) {
$queries[] = Query::cursorAfter(new Document(['$id' => $cursorId]))->toString();
}
$response = $this->client->call(Client::METHOD_GET, '/project/variables', $destinationHeaders, [
'queries' => $queries,
'total' => false,
]);
$this->assertEquals(200, $response['headers']['status-code']);
$batch = $response['body']['variables'];
array_push($variables, ...$batch);
$cursorId = !empty($batch) ? $batch[array_key_last($batch)]['$id'] : null;
} while (\count($batch) === 100);
return $variables;
};
$existingDestinationVariableIds = \array_flip(\array_column($listDestinationVariables(), '$id'));
// Source-side variable IDs and keys are uniquified so re-runs and parallel suites
// can't trip the source-side findOne('variables', [key=...]) skip path.
$plainKey = 'TEST_PLAIN_' . \strtoupper(ID::unique());
$secretKey = 'TEST_SECRET_' . \strtoupper(ID::unique());
// Non-secret variable: value should round-trip exactly.
$plainResp = $this->client->call(Client::METHOD_POST, '/project/variables', $sourceHeaders, [
'variableId' => ID::unique(),
'key' => $plainKey,
'value' => 'plain-value',
'secret' => false,
]);
$this->assertEquals(201, $plainResp['headers']['status-code']);
$plainVariable = $plainResp['body'];
// Secret variable: SDK strips `value` on subsequent reads, so the migration
// source sees empty and the destination writes empty. Test asserts that.
$secretResp = $this->client->call(Client::METHOD_POST, '/project/variables', $sourceHeaders, [
'variableId' => ID::unique(),
'key' => $secretKey,
'value' => 'real-secret-value',
'secret' => true,
]);
$this->assertEquals(201, $secretResp['headers']['status-code']);
$secretVariable = $secretResp['body'];
$result = $this->performMigrationSync([
'resources' => [
Resource::TYPE_PROJECT_VARIABLE,
],
'endpoint' => $this->webEndpoint,
'projectId' => $this->getProject()['$id'],
'apiKey' => $this->getProject()['apiKey'],
]);
$this->assertEquals('completed', $result['status']);
$this->assertEquals([Resource::TYPE_PROJECT_VARIABLE], $result['resources']);
$this->assertArrayHasKey(Resource::TYPE_PROJECT_VARIABLE, $result['statusCounters']);
$this->assertEquals(0, $result['statusCounters'][Resource::TYPE_PROJECT_VARIABLE]['error']);
$this->assertEquals(0, $result['statusCounters'][Resource::TYPE_PROJECT_VARIABLE]['pending']);
$this->assertGreaterThanOrEqual(2, $result['statusCounters'][Resource::TYPE_PROJECT_VARIABLE]['success']);
$this->assertEquals(0, $result['statusCounters'][Resource::TYPE_PROJECT_VARIABLE]['processing']);
$this->assertEquals(0, $result['statusCounters'][Resource::TYPE_PROJECT_VARIABLE]['warning']);
$destinationVariables = $listDestinationVariables();
$foundPlain = null;
$foundSecret = null;
foreach ($destinationVariables as $v) {
if ($v['key'] === $plainKey) {
$foundPlain = $v;
} elseif ($v['key'] === $secretKey) {
$foundSecret = $v;
}
}
$this->assertNotNull($foundPlain, 'Plain variable not found on destination');
$this->assertEquals($plainKey, $foundPlain['key']);
$this->assertEquals('plain-value', $foundPlain['value']);
$this->assertFalse($foundPlain['secret']);
$this->assertNotNull($foundSecret, 'Secret variable not found on destination');
$this->assertEquals($secretKey, $foundSecret['key']);
// Secret variables: source SDK never returned the real value, so the destination
// also stores empty. The original 'real-secret-value' must not have leaked.
$this->assertEmpty($foundSecret['value']);
$this->assertTrue($foundSecret['secret']);
// Cleanup every destination variable this migration added, including any
// unrelated source variables copied by the resource-level migration.
foreach ($destinationVariables as $variable) {
if (!isset($existingDestinationVariableIds[$variable['$id']])) {
$this->client->call(Client::METHOD_DELETE, '/project/variables/' . $variable['$id'], $destinationHeaders);
}
}
// Cleanup on source
$this->client->call(Client::METHOD_DELETE, '/project/variables/' . $plainVariable['$id'], $sourceHeaders);
$this->client->call(Client::METHOD_DELETE, '/project/variables/' . $secretVariable['$id'], $sourceHeaders);
}
/**
* Import documents from a CSV file.
*/