From abcb2c96c8b3001f8af301565b2d620e963d736a Mon Sep 17 00:00:00 2001 From: yash30201 <54198301+yash30201@users.noreply.github.com> Date: Tue, 7 Feb 2023 15:50:20 +0000 Subject: [PATCH 1/6] added snapshot restore sample and test --- bigquery/api/src/undelete_table.php | 71 +++++++++++++++++++++++++++++ bigquery/api/test/bigqueryTest.php | 51 +++++++++++++++++---- 2 files changed, 112 insertions(+), 10 deletions(-) create mode 100644 bigquery/api/src/undelete_table.php diff --git a/bigquery/api/src/undelete_table.php b/bigquery/api/src/undelete_table.php new file mode 100644 index 0000000000..5363fc2f6f --- /dev/null +++ b/bigquery/api/src/undelete_table.php @@ -0,0 +1,71 @@ + $projectId, + ]); + $dataset = $bigQuery->dataset($datasetId); + $snapshot = $dataset->table($snapshotId); + $restoredTable = $dataset->table($restoredTableId); + $copyConfig = $snapshot->copy($restoredTable, [ + 'configuration' => ['copy' => ['operationType' => 'RESTORE']] + ]); + $job = $bigQuery->runJob($copyConfig); + + // check if the job is complete + $job->reload(); + if (!$job->isComplete()) { + throw new \Exception('Job has not yet completed', 500); + } + // check if the job has errors + if (isset($job->info()['status']['errorResult'])) { + $error = $job->info()['status']['errorResult']['message']; + printf('Error running job: %s' . PHP_EOL, $error); + } else { + print('Snapshot restored successfully' . PHP_EOL); + } +} +# [END bigquery_undelete_table] +require_once __DIR__ . '/../../../testing/sample_helpers.php'; +\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv); diff --git a/bigquery/api/test/bigqueryTest.php b/bigquery/api/test/bigqueryTest.php index d96258bc43..a8b5466f4e 100644 --- a/bigquery/api/test/bigqueryTest.php +++ b/bigquery/api/test/bigqueryTest.php @@ -1,4 +1,5 @@ self::$projectId, ]); self::$datasetId = sprintf('temp_dataset_%s', time()); - self::$dataset = $client->createDataset(self::$datasetId); + self::$dataset = self::$client->createDataset(self::$datasetId); } public function testBigQueryClient() @@ -99,8 +101,8 @@ public function testCreateAndDeleteTable() { $tempTableId = sprintf('test_table_%s', time()); $fields = json_encode([ - ['name' => 'name', 'type' => 'string', 'mode' => 'nullable'], - ['name' => 'title', 'type' => 'string', 'mode' => 'nullable'] + ['name' => 'name', 'type' => 'string', 'mode' => 'nullable'], + ['name' => 'title', 'type' => 'string', 'mode' => 'nullable'] ]); $output = $this->runFunctionSnippet('create_table', [ self::$datasetId, @@ -352,8 +354,8 @@ public function testAddColumnLoadAppend() { $tableId = $this->createTempTable(); $output = $this->runFunctionSnippet('add_column_load_append', [ - self::$datasetId, - $tableId + self::$datasetId, + $tableId ]); $this->assertStringContainsString('name', $output); @@ -365,14 +367,43 @@ public function testAddColumnQueryAppend() { $tableId = $this->createTempTable(); $output = $this->runFunctionSnippet('add_column_query_append', [ - self::$datasetId, - $tableId + self::$datasetId, + $tableId ]); $this->assertStringContainsString('name', $output); $this->assertStringContainsString('title', $output); $this->assertStringContainsString('description', $output); } + public function testUndeleteTable() + { + // Create a base table + $sourceTableId = $this->createTempTable(); + $snapshot = self::$dataset->table(uniqid('snapshot_')); + + // Create base table's snapshot + $copyConfig = self::$dataset->table($sourceTableId)->copy( + $snapshot, + ['configuration' => ['copy' => ['operationType' => 'SNAPSHOT']]], + ); + self::$client->runJob($copyConfig); + + // Delete base table + self::$dataset->table($sourceTableId)->delete(); + + // run the sample + $restoredTableId = uniqid('restored_'); + $output = $this->runFunctionSnippet('undelete_table', [ + self::$datasetId, + $snapshot->id(), + $restoredTableId, + ]); + + $restoredTable = self::$dataset->table($restoredTableId); + $this->assertStringContainsString('Snapshot restored successfully', $output); + $this->verifyTable($restoredTable, 'Brent Shaffer', 3); + } + private function runFunctionSnippet($sampleName, $params = []) { array_unshift($params, self::$projectId); @@ -386,8 +417,8 @@ private function createTempEmptyTable() { $tempTableId = sprintf('test_table_%s_%s', time(), rand()); $fields = json_encode([ - ['name' => 'name', 'type' => 'string', 'mode' => 'nullable'], - ['name' => 'title', 'type' => 'string', 'mode' => 'nullable'] + ['name' => 'name', 'type' => 'string', 'mode' => 'nullable'], + ['name' => 'title', 'type' => 'string', 'mode' => 'nullable'] ]); $this->runFunctionSnippet('create_table', [ self::$datasetId, From 84630956b7a6f5c17a73325e954c6c186a19a898 Mon Sep 17 00:00:00 2001 From: yash30201 <54198301+yash30201@users.noreply.github.com> Date: Tue, 14 Feb 2023 06:51:48 +0000 Subject: [PATCH 2/6] Modified the sample and test according to the requirements. --- bigquery/api/src/undelete_table.php | 31 ++++++++++++++++++----------- bigquery/api/test/bigqueryTest.php | 13 +----------- 2 files changed, 20 insertions(+), 24 deletions(-) diff --git a/bigquery/api/src/undelete_table.php b/bigquery/api/src/undelete_table.php index 5363fc2f6f..d58efa4226 100644 --- a/bigquery/api/src/undelete_table.php +++ b/bigquery/api/src/undelete_table.php @@ -28,29 +28,36 @@ use Google\Cloud\BigQuery\BigQueryClient; /** - * Restored a table from its snapshot for with deleted base table. + * Restore a deleted table from its snapshot. * * @param string $projectId The project Id of your Google Cloud Project. * @param string $datasetId The BigQuery dataset ID. - * @param string $snapshotId Snapshot ID of a snapshot with deleted base table - * to be restored. - * @param string $restoredTableId TableId in which snapshot would be restored. + * @param string $tableId Table ID of the table to delete. + * @param string $restoredTableId Table Id for the restored table. */ function undelete_table( string $projectId, string $datasetId, - string $snapshotId, + string $tableId, string $restoredTableId ): void { - $bigQuery = new BigQueryClient([ - 'projectId' => $projectId, - ]); + $bigQuery = new BigQueryClient(['projectId' => $projectId]); $dataset = $bigQuery->dataset($datasetId); - $snapshot = $dataset->table($snapshotId); + + // Choose an appropriate snapshot point as epoch milliseconds. + // For this example, we choose the current time as we're about to delete the + // table immediately afterwards + $snapshotEpoch = date_create()->format('Uv'); + + // Delete the table. + $dataset->table($tableId)->delete(); + + // Construct the restore-from table ID using a snapshot decorator. + $snapshotId = "{$tableId}@{$snapshotEpoch}"; + + // Restore the deleted table $restoredTable = $dataset->table($restoredTableId); - $copyConfig = $snapshot->copy($restoredTable, [ - 'configuration' => ['copy' => ['operationType' => 'RESTORE']] - ]); + $copyConfig = $dataset->table($snapshotId)->copy($restoredTable); $job = $bigQuery->runJob($copyConfig); // check if the job is complete diff --git a/bigquery/api/test/bigqueryTest.php b/bigquery/api/test/bigqueryTest.php index a8b5466f4e..52ee5ec148 100644 --- a/bigquery/api/test/bigqueryTest.php +++ b/bigquery/api/test/bigqueryTest.php @@ -379,23 +379,12 @@ public function testUndeleteTable() { // Create a base table $sourceTableId = $this->createTempTable(); - $snapshot = self::$dataset->table(uniqid('snapshot_')); - - // Create base table's snapshot - $copyConfig = self::$dataset->table($sourceTableId)->copy( - $snapshot, - ['configuration' => ['copy' => ['operationType' => 'SNAPSHOT']]], - ); - self::$client->runJob($copyConfig); - - // Delete base table - self::$dataset->table($sourceTableId)->delete(); // run the sample $restoredTableId = uniqid('restored_'); $output = $this->runFunctionSnippet('undelete_table', [ self::$datasetId, - $snapshot->id(), + $sourceTableId, $restoredTableId, ]); From 4e66b98882855596cef23b8d404ea457e50b425d Mon Sep 17 00:00:00 2001 From: yash30201 <54198301+yash30201@users.noreply.github.com> Date: Wed, 15 Feb 2023 10:31:05 +0000 Subject: [PATCH 3/6] Iterated on PR review comments. --- bigquery/api/src/undelete_table.php | 5 +++-- bigquery/api/test/bigqueryTest.php | 6 ++---- 2 files changed, 5 insertions(+), 6 deletions(-) diff --git a/bigquery/api/src/undelete_table.php b/bigquery/api/src/undelete_table.php index d58efa4226..edcde0d4a5 100644 --- a/bigquery/api/src/undelete_table.php +++ b/bigquery/api/src/undelete_table.php @@ -1,5 +1,4 @@ table($restoredTableId); - $copyConfig = $dataset->table($snapshotId)->copy($restoredTable); + $copyConfig = $dataset->table($snapshotId)->copy($restoredTable, [ + 'configuration' => ['copy' => ['operationType' => 'RESTORE']] + ]); $job = $bigQuery->runJob($copyConfig); // check if the job is complete diff --git a/bigquery/api/test/bigqueryTest.php b/bigquery/api/test/bigqueryTest.php index 52ee5ec148..58e8e6109f 100644 --- a/bigquery/api/test/bigqueryTest.php +++ b/bigquery/api/test/bigqueryTest.php @@ -33,19 +33,17 @@ class FunctionsTest extends TestCase TestTrait::runFunctionSnippet as traitRunFunctionSnippet; } use EventuallyConsistentTestTrait; - - private static $client; private static $datasetId; private static $dataset; public static function setUpBeforeClass(): void { self::$projectId = self::requireEnv('GOOGLE_PROJECT_ID'); - self::$client = new BigQueryClient([ + $client = new BigQueryClient([ 'projectId' => self::$projectId, ]); self::$datasetId = sprintf('temp_dataset_%s', time()); - self::$dataset = self::$client->createDataset(self::$datasetId); + self::$dataset = $client->createDataset(self::$datasetId); } public function testBigQueryClient() From 85037b63e24d157e65d111f36d6e40fd7458700e Mon Sep 17 00:00:00 2001 From: yash30201 <54198301+yash30201@users.noreply.github.com> Date: Wed, 15 Feb 2023 11:09:41 +0000 Subject: [PATCH 4/6] Removed lingering code. --- bigquery/api/src/undelete_table.php | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/bigquery/api/src/undelete_table.php b/bigquery/api/src/undelete_table.php index edcde0d4a5..1fd1f18e8d 100644 --- a/bigquery/api/src/undelete_table.php +++ b/bigquery/api/src/undelete_table.php @@ -56,9 +56,7 @@ function undelete_table( // Restore the deleted table $restoredTable = $dataset->table($restoredTableId); - $copyConfig = $dataset->table($snapshotId)->copy($restoredTable, [ - 'configuration' => ['copy' => ['operationType' => 'RESTORE']] - ]); + $copyConfig = $dataset->table($snapshotId)->copy($restoredTable); $job = $bigQuery->runJob($copyConfig); // check if the job is complete From c63c2e5acc75d620212c8f121dac02d8f0cfffba Mon Sep 17 00:00:00 2001 From: yash30201 <54198301+yash30201@users.noreply.github.com> Date: Wed, 15 Feb 2023 11:11:33 +0000 Subject: [PATCH 5/6] Removed spaces. --- bigquery/api/test/bigqueryTest.php | 1 + 1 file changed, 1 insertion(+) diff --git a/bigquery/api/test/bigqueryTest.php b/bigquery/api/test/bigqueryTest.php index 58e8e6109f..768e6aa410 100644 --- a/bigquery/api/test/bigqueryTest.php +++ b/bigquery/api/test/bigqueryTest.php @@ -33,6 +33,7 @@ class FunctionsTest extends TestCase TestTrait::runFunctionSnippet as traitRunFunctionSnippet; } use EventuallyConsistentTestTrait; + private static $datasetId; private static $dataset; From d4ff22bf00be2d41cfd982ee22d8c39b761d9a52 Mon Sep 17 00:00:00 2001 From: yash30201 <54198301+yash30201@users.noreply.github.com> Date: Thu, 16 Feb 2023 08:36:04 +0000 Subject: [PATCH 6/6] Removed extra line --- bigquery/api/test/bigqueryTest.php | 1 - 1 file changed, 1 deletion(-) diff --git a/bigquery/api/test/bigqueryTest.php b/bigquery/api/test/bigqueryTest.php index 768e6aa410..2b128b7dca 100644 --- a/bigquery/api/test/bigqueryTest.php +++ b/bigquery/api/test/bigqueryTest.php @@ -1,5 +1,4 @@