Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

PST-1796: Add dry-run mode #12

Merged
merged 3 commits into from
Jul 11, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
parameters:
checkMissingIterableValueType: false
ignoreErrors:
- '#Cannot call method scalarNode\(\) on Symfony\\Component\\Config\\Definition\\Builder\\NodeParentInterface\|null\.#'
- '#Cannot call method (scalar|boolean)Node\(\) on Symfony\\Component\\Config\\Definition\\Builder\\NodeParentInterface\|null\.#'
2 changes: 2 additions & 0 deletions src/Component.php
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ protected function run(): void
$sourceSapiClient,
$targetSapiClient,
$this->getLogger(),
$this->getConfig()->isDryRun(),
);
break;
case 'database':
Expand Down Expand Up @@ -83,6 +84,7 @@ protected function run(): void
$sourceDatabase,
$replicaDatabase,
$targetDatabase,
$this->getConfig()->isDryRun(),
);
break;
default:
Expand Down
5 changes: 5 additions & 0 deletions src/Config.php
Original file line number Diff line number Diff line change
Expand Up @@ -154,4 +154,9 @@ private function getDbConfigNode(): array
}
return $this->getImageParameters()['db'];
}

public function isDryRun(): bool
{
return (bool) $this->getValue(['parameters', 'dryRun']);
}
}
1 change: 1 addition & 0 deletions src/Configuration/ConfigDefinition.php
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ protected function getParametersDefinition(): ArrayNodeDefinition
$parametersNode
->children()
->enumNode('mode')->values(['sapi', 'database'])->defaultValue('sapi')->end()
->booleanNode('dryRun')->defaultFalse()->end()
->scalarNode('sourceKbcUrl')->isRequired()->cannotBeEmpty()->end()
->scalarNode('#sourceKbcToken')->isRequired()->cannotBeEmpty()->end()
->arrayNode('tables')->prototype('scalar')->end()->end()
Expand Down
14 changes: 12 additions & 2 deletions src/Strategy/DatabaseMigrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ public function __construct(
private readonly string $sourceDatabase,
private readonly string $replicaDatabase,
private readonly string $targetDatabase,
private readonly bool $dryRun = false,
) {
}

Expand Down Expand Up @@ -95,12 +96,21 @@ private function migrateSchema(array $tablesWhiteList, string $schemaName): void
$this->migrateTable($schemaName, $table['name']);
}

$this->logger->info(sprintf('Refreshing table information in bucket %s', $schemaName));
$this->targetSapiClient->refreshTableInformationInBucket($schemaName);
if ($this->dryRun === false) {
$this->logger->info(sprintf('Refreshing table information in bucket %s', $schemaName));
$this->targetSapiClient->refreshTableInformationInBucket($schemaName);
} else {
$this->logger->info(sprintf('[dry-run] Refreshing table information in bucket %s', $schemaName));
}
}

private function migrateTable(string $schemaName, string $tableName): void
{
if ($this->dryRun) {
$this->logger->info(sprintf('[dry-run] Migrating table %s.%s', $schemaName, $tableName));
return;
}

$this->logger->info(sprintf('Migrating table %s.%s', $schemaName, $tableName));
$tableRole = $this->getSourceRole(
$this->targetConnection,
Expand Down
45 changes: 30 additions & 15 deletions src/Strategy/SapiMigrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ public function __construct(
private readonly Client $sourceClient,
private readonly Client $targetClient,
private readonly LoggerInterface $logger,
private readonly bool $dryRun = false,
) {
}

Expand Down Expand Up @@ -70,27 +71,41 @@ private function migrateTable(array $sourceTableInfo): void
$this->logger->info(sprintf('Downloading table %s', $sourceTableInfo['id']));
$slices = $this->sourceClient->downloadSlicedFile($sourceFileId, $tmp->getTmpFolder());

$this->logger->info(sprintf('Uploading table %s', $sourceTableInfo['id']));
$destinationFileId = $this->targetClient->uploadSlicedFile($slices, $optionUploadedFile);
if ($this->dryRun === false) {
$this->logger->info(sprintf('Uploading table %s', $sourceTableInfo['id']));
$destinationFileId = $this->targetClient->uploadSlicedFile($slices, $optionUploadedFile);
} else {
$this->logger->info(sprintf('[dry-run] Uploading table %s', $sourceTableInfo['id']));
$destinationFileId = null;
}
} else {
$fileName = $tmp->getTmpFolder() . '/' . $sourceFileInfo['name'];

$this->logger->info(sprintf('Downloading table %s', $sourceTableInfo['id']));
$this->sourceClient->downloadFile($sourceFileId, $fileName);

$this->logger->info(sprintf('Uploading table %s', $sourceTableInfo['id']));
$destinationFileId = $this->targetClient->uploadFile($fileName, $optionUploadedFile);
if ($this->dryRun === false) {
$this->logger->info(sprintf('Uploading table %s', $sourceTableInfo['id']));
$destinationFileId = $this->targetClient->uploadFile($fileName, $optionUploadedFile);
} else {
$this->logger->info(sprintf('[dry-run] Uploading table %s', $sourceTableInfo['id']));
$destinationFileId = null;
}
}

// Upload data to table
$this->targetClient->writeTableAsyncDirect(
$sourceTableInfo['id'],
[
'name' => $sourceTableInfo['name'],
'dataFileId' => $destinationFileId,
'columns' => $sourceTableInfo['columns'],
],
);
if ($this->dryRun === false) {
// Upload data to table
$this->targetClient->writeTableAsyncDirect(
$sourceTableInfo['id'],
[
'name' => $sourceTableInfo['name'],
'dataFileId' => $destinationFileId,
'columns' => $sourceTableInfo['columns'],
],
);
} else {
$this->logger->info(sprintf('[dry-run] Import data to table "%s"', $sourceTableInfo['name']));
romantmb marked this conversation as resolved.
Show resolved Hide resolved
}
}

private function getAllTables(): array
Expand All @@ -106,9 +121,9 @@ private function getAllTables(): array
fn($v) => $v['rowsCount'] === 0 || is_null($v['rowsCount']),
);

$listTables = array_merge(
array_map(fn($v) => $v['id'], $filteredBucketTables),
array_unshift(
$listTables,
...array_map(fn($v) => $v['id'], $filteredBucketTables),
);
janvanicek marked this conversation as resolved.
Show resolved Hide resolved
}
return $listTables;
Expand Down
1 change: 1 addition & 0 deletions tests/functional/dry-run/expected-code
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
0
4 changes: 4 additions & 0 deletions tests/functional/dry-run/expected-stdout
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
Exporting table in.c-test.simple
Downloading table in.c-test.simple
[dry-run] Uploading table in.c-test.simple
[dry-run] Import data to table "simple"
Empty file.
Empty file.
7 changes: 7 additions & 0 deletions tests/functional/dry-run/source/data/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
{
"parameters": {
"sourceKbcUrl": "%env(string:SOURCE_CLIENT_URL)%",
"#sourceKbcToken": "%env(string:SOURCE_CLIENT_TOKEN)%",
"dryRun": true
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"id","name","glasses","age"
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
"id","name","glasses","age"