Skip to content

Commit

Permalink
Add migrate secrets params
Browse files Browse the repository at this point in the history
  • Loading branch information
janvanicek committed Mar 26, 2024
1 parent 71d7e4f commit d6edfa6
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 7 deletions.
3 changes: 2 additions & 1 deletion src/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ protected function run(): void
$config->getSourceProjectUrl(),
$config->getSourceProjectToken(),
$config->directDataMigration(),
$logger
$logger,
$config->shouldMigrateSecrets(),
);
$migrate->run();

Expand Down
11 changes: 11 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

namespace Keboola\AppProjectMigrate;

use InvalidArgumentException;
use Keboola\Component\Config\BaseConfig;

class Config extends BaseConfig
Expand All @@ -28,4 +29,14 @@ public function directDataMigration(): bool
{
return $this->getValue(['parameters', 'directDataMigration']);
}

public function shouldMigrateSecrets(): bool
{
return $this->getValue(['parameters', 'migrateSecrets']);
}

public function getManageToken(): ?string
{
return $this->getValue(['parameters', '#manageToken']);
}
}
7 changes: 7 additions & 0 deletions src/ConfigDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,13 @@ protected function getParametersDefinition(): ArrayNodeDefinition
->cannotBeEmpty()
->end()
->booleanNode('directDataMigration')->defaultTrue()->end()
->booleanNode('migrateSecrets')->defaultFalse()->end()
->scalarNode('#manageToken')->end()
->end()
->validate()
->ifTrue(fn($values) =>
isset($values['migrateSecrets']) && $values['migrateSecrets'] && !isset($values['#manageToken']))
->thenInvalid('Parameter "#manageToken" is required when "migrateSecrets" is set to true.')
->end()
;
// @formatter:on
Expand Down
8 changes: 7 additions & 1 deletion src/Migrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -26,20 +26,24 @@ class Migrate

private bool $directDataMigration;

private bool $migrateSecrets;

public function __construct(
JobRunner $sourceJobRunner,
JobRunner $destJobRunner,
string $sourceProjectUrl,
string $sourceProjectToken,
bool $directDataMigration,
LoggerInterface $logger
LoggerInterface $logger,
bool $migrateSecrets
) {
$this->sourceJobRunner = $sourceJobRunner;
$this->destJobRunner = $destJobRunner;
$this->sourceProjectUrl = $sourceProjectUrl;
$this->sourceProjectToken = $sourceProjectToken;
$this->directDataMigration = $directDataMigration;
$this->logger = $logger;
$this->migrateSecrets = $migrateSecrets;
}

public function run(): void
Expand Down Expand Up @@ -180,6 +184,7 @@ private function getRestoreConfigData(array $restoreCredentials): array
'#sessionToken' => $restoreCredentials['credentials']['sessionToken'],
],
'useDefaultBackend' => true,
'restoreConfigs' => $this->migrateSecrets === false,
],
];
} elseif (isset($restoreCredentials['credentials']['connectionString'])) {
Expand All @@ -190,6 +195,7 @@ private function getRestoreConfigData(array $restoreCredentials): array
'#connectionString' => $restoreCredentials['credentials']['connectionString'],
],
'useDefaultBackend' => true,
'restoreConfigs' => $this->migrateSecrets === false,
],
];
} else {
Expand Down
20 changes: 15 additions & 5 deletions tests/phpunit/MigrateTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ public function testMigrateSuccess(
[
Config::PROJECT_RESTORE_COMPONENT,
[
'parameters' => array_merge($expectedCredentialsData, ['useDefaultBackend' => true]),
'parameters' => array_merge(
$expectedCredentialsData,
[
'useDefaultBackend' => true,
'restoreConfigs' => true,
]
),
],
],
];
Expand Down Expand Up @@ -111,7 +117,8 @@ public function testMigrateSuccess(
$sourceProjectUrl,
$sourceProjectToken,
$migrateDataOfTablesDirectly,
new NullLogger()
new NullLogger(),
false,
);
$migrate->run();
}
Expand Down Expand Up @@ -146,7 +153,8 @@ public function testShouldFailOnSnapshotError(): void
'xxx',
'yyy',
false,
new NullLogger()
new NullLogger(),
false,
);
$migrate->run();
}
Expand Down Expand Up @@ -184,7 +192,8 @@ public function testShouldFailOnRestoreError(): void
'xxx',
'yyy',
false,
new NullLogger()
new NullLogger(),
false,
);
$migrate->run();
}
Expand Down Expand Up @@ -217,7 +226,8 @@ public function testCatchSyrupClientException(): void
'xxx',
'yyy',
false,
new NullLogger()
new NullLogger(),
false,
);

$this->expectException(UserException::class);
Expand Down

0 comments on commit d6edfa6

Please sign in to comment.