From 1b15f024d058692694b5fb24eecd5d99c1253aa5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ond=C5=99ej=20Jodas?= Date: Sun, 25 Aug 2024 22:59:05 +0200 Subject: [PATCH] migrate table/columns in sapi mode --- src/Strategy/SapiMigrate.php | 41 +++++++++++++++++++++++++++++++----- 1 file changed, 36 insertions(+), 5 deletions(-) diff --git a/src/Strategy/SapiMigrate.php b/src/Strategy/SapiMigrate.php index f010637..81fec69 100644 --- a/src/Strategy/SapiMigrate.php +++ b/src/Strategy/SapiMigrate.php @@ -6,6 +6,7 @@ use Keboola\AppProjectMigrateLargeTables\Config; use Keboola\AppProjectMigrateLargeTables\MigrateInterface; +use Keboola\AppProjectMigrateLargeTables\StorageModifier; use Keboola\StorageApi\Client; use Keboola\StorageApi\ClientException; use Keboola\StorageApi\Options\FileUploadOptions; @@ -14,11 +15,17 @@ class SapiMigrate implements MigrateInterface { + private StorageModifier $storageModifier; + + /** @var string[] $bucketsExist */ + private array $bucketsExist = []; + public function __construct( private readonly Client $sourceClient, private readonly Client $targetClient, private readonly LoggerInterface $logger, ) { + $this->storageModifier = new StorageModifier($this->targetClient); } public function migrate(Config $config): void @@ -44,6 +51,20 @@ public function migrate(Config $config): void continue; } + if (!in_array($tableInfo['bucket']['id'], $this->bucketsExist) && + !$this->targetClient->bucketExists($tableInfo['bucket']['id'])) { + $this->logger->info(sprintf('Creating bucket %s', $tableInfo['bucket']['id'])); + $this->bucketsExist[] = $tableInfo['bucket']['id']; + + $this->storageModifier->createBucket($tableInfo['bucket']['id']); + } + + if (!$this->targetClient->tableExists($tableId)) { + $this->logger->info(sprintf('Creating table %s', $tableInfo['id'])); + + $this->storageModifier->createTable($tableInfo); + } + $this->migrateTable($tableInfo); } } @@ -95,15 +116,25 @@ private function migrateTable(array $sourceTableInfo): void private function getAllTables(): array { - $buckets = $this->targetClient->listBuckets(); + $buckets = $this->sourceClient->listBuckets(); $listTables = []; foreach ($buckets as $bucket) { - $bucketTables = $this->targetClient->listTables($bucket['id']); + $sourceBucketTables = $this->sourceClient->listTables($bucket['id']); + if (!$this->targetClient->bucketExists($bucket['id'])) { + $targetBucketTables = []; + } else { + $targetBucketTables = $this->targetClient->listTables($bucket['id']); + } - // migrate only empty tables $filteredBucketTables = array_filter( - $bucketTables, - fn($v) => $v['rowsCount'] === 0 || is_null($v['rowsCount']), + $sourceBucketTables, + function ($sourceTable) use ($targetBucketTables) { + $v = current(array_filter( + $targetBucketTables, + fn($v) => $v['id'] === $sourceTable['id'], + )); + return empty($v) || $v['rowsCount'] === 0 || is_null($v['rowsCount']); + }, ); $listTables = array_merge(