diff --git a/src/Strategy/SapiMigrate.php b/src/Strategy/SapiMigrate.php index f010637..f5b9a33 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,10 +116,10 @@ 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']); + $bucketTables = $this->sourceClient->listTables($bucket['id']); // migrate only empty tables $filteredBucketTables = array_filter(