Skip to content

Commit

Permalink
migrate table/columns in sapi mode
Browse files Browse the repository at this point in the history
  • Loading branch information
ondrajodas committed Aug 25, 2024
1 parent 94c87b3 commit 30416cc
Showing 1 changed file with 23 additions and 2 deletions.
25 changes: 23 additions & 2 deletions src/Strategy/SapiMigrate.php
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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
Expand All @@ -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);
}
}
Expand Down Expand Up @@ -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(
Expand Down

0 comments on commit 30416cc

Please sign in to comment.