From 7d0a2e1d7d203f18a2e229f9bd413f9b1fc8a786 Mon Sep 17 00:00:00 2001 From: yash30201 <54198301+yash30201@users.noreply.github.com> Date: Sun, 22 Jan 2023 04:26:19 +0000 Subject: [PATCH 1/4] feat(BigQuery): Snapshot / Clones * Wrote system tests for Snapshots and Clones --- BigQuery/tests/System/ManageTablesTest.php | 75 ++++++++++++++++++++++ 1 file changed, 75 insertions(+) diff --git a/BigQuery/tests/System/ManageTablesTest.php b/BigQuery/tests/System/ManageTablesTest.php index cf4f7bf847e3..890a214d9f0c 100644 --- a/BigQuery/tests/System/ManageTablesTest.php +++ b/BigQuery/tests/System/ManageTablesTest.php @@ -111,6 +111,8 @@ public function testCreatesTableWithRangePartitioning() $this->assertTrue(self::$dataset->table($id)->exists()); $this->assertEquals($id, $table->id()); $this->assertEquals($table->info()['rangePartitioning'], $options['rangePartitioning']); + + return $table; } public function testExtractsTable() @@ -255,6 +257,79 @@ public function testIam() $this->assertEquals([$perm], $iam->testPermissions([$perm])); } + public function testSnapshot() + { + $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' => 'Dave', 'Age' => 101]; + 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)) { From d3117950a281641ec520a16fa3f5a99c57587b05 Mon Sep 17 00:00:00 2001 From: yash30201 <54198301+yash30201@users.noreply.github.com> Date: Sun, 22 Jan 2023 05:25:28 +0000 Subject: [PATCH 2/4] feat(BigQuery): Snapshot / Clone * Added Unit tests for snapshot and clone. * Fixed System tests --- BigQuery/tests/System/ManageTablesTest.php | 4 +- BigQuery/tests/Unit/TableTest.php | 45 ++++++++++++++++++++++ 2 files changed, 46 insertions(+), 3 deletions(-) diff --git a/BigQuery/tests/System/ManageTablesTest.php b/BigQuery/tests/System/ManageTablesTest.php index 890a214d9f0c..c56e7a2b4f76 100644 --- a/BigQuery/tests/System/ManageTablesTest.php +++ b/BigQuery/tests/System/ManageTablesTest.php @@ -111,8 +111,6 @@ public function testCreatesTableWithRangePartitioning() $this->assertTrue(self::$dataset->table($id)->exists()); $this->assertEquals($id, $table->id()); $this->assertEquals($table->info()['rangePartitioning'], $options['rangePartitioning']); - - return $table; } public function testExtractsTable() @@ -320,7 +318,7 @@ public function testCreateClone() */ public function testUpdateClone($table) { - $row = ['Name' => 'Dave', 'Age' => 101]; + $row = ['Name' => 'Yash', 'Age' => 22]; self::$table->insertRow($row); $insertResponse = $table->insertRow($row); 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 From 2e93e230fe376e9f3c9523f60febadb180128ff7 Mon Sep 17 00:00:00 2001 From: yash30201 <54198301+yash30201@users.noreply.github.com> Date: Sun, 22 Jan 2023 07:35:03 +0000 Subject: [PATCH 3/4] Added doc blocks --- BigQuery/src/Table.php | 12 +++++++++++- 1 file changed, 11 insertions(+), 1 deletion(-) 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: * ``` From 59f1f9111277a7fa3db1f6bec80a7b163a08c8e5 Mon Sep 17 00:00:00 2001 From: yash30201 <54198301+yash30201@users.noreply.github.com> Date: Wed, 25 Jan 2023 05:25:42 +0000 Subject: [PATCH 4/4] Renamed testSnapshot() method --- BigQuery/tests/System/ManageTablesTest.php | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/BigQuery/tests/System/ManageTablesTest.php b/BigQuery/tests/System/ManageTablesTest.php index ab2797444243..278f7877ddde 100644 --- a/BigQuery/tests/System/ManageTablesTest.php +++ b/BigQuery/tests/System/ManageTablesTest.php @@ -255,7 +255,7 @@ public function testIam() $this->assertEquals([$perm], $iam->testPermissions([$perm])); } - public function testSnapshot() + public function testCreateAndRestoreSnapshot() { $destinationTable = self::$dataset->table(uniqid('test_dest_table_')); $copyConfig = self::$table->copy(