diff --git a/BigQuery/src/Table.php b/BigQuery/src/Table.php index 187a70b89cb4..c2f5820b23c1 100644 --- a/BigQuery/src/Table.php +++ b/BigQuery/src/Table.php @@ -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: * ``` diff --git a/BigQuery/tests/System/ManageTablesTest.php b/BigQuery/tests/System/ManageTablesTest.php index 9718cac7f0a6..278f7877ddde 100644 --- a/BigQuery/tests/System/ManageTablesTest.php +++ b/BigQuery/tests/System/ManageTablesTest.php @@ -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() + { + $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; + } + + /** + * @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)) { diff --git a/BigQuery/tests/Unit/TableTest.php b/BigQuery/tests/Unit/TableTest.php index 59726ea7aaa7..3a0d09d08d19 100644 --- a/BigQuery/tests/Unit/TableTest.php +++ b/BigQuery/tests/Unit/TableTest.php @@ -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