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

feat(BigQuery): Snapshots / Clones #5804

Merged
merged 6 commits into from
Jan 25, 2023
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
12 changes: 11 additions & 1 deletion BigQuery/src/Table.php
Original file line number Diff line number Diff line change
Expand Up @@ -334,7 +334,17 @@ public function startJob(JobConfigurationInterface $config, array $options = [])
* {@see BigQueryClient::runJob()} or
* {@see BigQueryClient::startJob()}. A
* configuration can be built using fluent setters or by providing a full
* set of options at once.
* set of options at once. This method can be used to create copy, snapshot
* and clone of the sourceTable as well as can also be used to restore
* snapshots by passing the following options:
* $options = [
* 'configuration' => [
* 'copy' => [
* 'operationType' => ('COPY' | 'SNAPSHOT' | 'CLONE' | 'RESTORE')
* ]
* ]
* ]
*
*
* Example:
* ```
Expand Down
73 changes: 73 additions & 0 deletions BigQuery/tests/System/ManageTablesTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,79 @@ public function testIam()
$this->assertEquals([$perm], $iam->testPermissions([$perm]));
}

public function testCreateAndRestoreSnapshot()
{
$destinationTable = self::$dataset->table(uniqid('test_dest_table_'));
$copyConfig = self::$table->copy(
$destinationTable,
['configuration' => ['copy' => ['operationType' => 'SNAPSHOT']]],
);
$this->runJob($copyConfig);
$snapshotDefinition = $destinationTable->reload()['snapshotDefinition'];

// Assert for proper base table reference
$this->assertEquals(
$snapshotDefinition['baseTableReference'],
self::$table->info()['tableReference']
);

// Assert for proper RFC3339 extended format timestamp
$isRfc3339_extended = \DateTime::createFromFormat(
\DateTime::RFC3339_EXTENDED,
$snapshotDefinition['snapshotTime']
) === false ? false : true;
$this->assertTrue($isRfc3339_extended);

// Test snapshot restore operation
$restoredTable = self::$dataset->table(uniqid('restored_table_'));
$copyConfig = $destinationTable->copy(
$restoredTable,
['configuration' => ['copy' => ['operationType' => 'RESTORE']]],
);
$this->runJob($copyConfig);
}

public function testCreateClone()
vishwarajanand marked this conversation as resolved.
Show resolved Hide resolved
{
$destinationTable = self::$dataset->table(uniqid('test_dest_table_'));
$copyConfig = self::$table->copy(
$destinationTable,
['configuration' => ['copy' => ['operationType' => 'CLONE']]],
);
$this->runJob($copyConfig);
$cloneDefinition = $destinationTable->reload()['cloneDefinition'];

// Assert for proper base table reference
$this->assertEquals(
$cloneDefinition['baseTableReference'],
self::$table->info()['tableReference']
);

// Assert for proper RFC3339 extended format timestamp
$isRfc3339_extended = \DateTime::createFromFormat(
\DateTime::RFC3339_EXTENDED,
$cloneDefinition['cloneTime']
) === false ? false : true;
$this->assertTrue($isRfc3339_extended);

return $destinationTable;
}
vishwarajanand marked this conversation as resolved.
Show resolved Hide resolved

/**
* @depends testCreateClone
*/
public function testUpdateClone($table)
{
$row = ['Name' => 'Yash', 'Age' => 22];
self::$table->insertRow($row);
$insertResponse = $table->insertRow($row);

$expectedRowCount = 1;
$actualRowCount = count(iterator_to_array($table->rows()));
$this->assertTrue($insertResponse->isSuccessful());
$this->assertEquals($expectedRowCount, $actualRowCount);
}

private function runJob($jobConfig, $client = null)
{
if (!isset($client)) {
Expand Down
45 changes: 45 additions & 0 deletions BigQuery/tests/Unit/TableTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -830,6 +830,51 @@ public function testIam()
{
$this->assertInstanceOf(Iam::class, $this->getTable($this->connection)->iam());
}

public function testCloneMetadata()
{
$table = $this->getTable($this->connection);
$expected = [
'cloneDefinition' => [
'baseTableReference' => [
'projectId' => 'test_project',
'datasetId' => 'test_dataset',
'tableId' => 'test_table'
],
'cloneTime' => '2023-01-11T03:42:11.054Z'
]
];
$this->connection->getTable($table->identity())->willReturn($expected);
$result = $table->reload();

$this->assertEquals(
$expected['cloneDefinition'],
$result['cloneDefinition']
);
}

public function testSnapshotMetadata()
{
$table = $this->getTable($this->connection);
$expected = [
'snapshotDefinition' => [
'baseTableReference' => [
'projectId' => 'test_project',
'datasetId' => 'test_dataset',
'tableId' => 'test_table'
],
'snapshotTime' => '2023-01-11T03:42:11.054Z'
]
];
$this->connection->getTable($table->identity())->willReturn($expected);

$result = $table->reload();

$this->assertEquals(
$expected['snapshotDefinition'],
$result['snapshotDefinition']
);
}
}

//@codingStandardsIgnoreStart
Expand Down