Skip to content

Commit 4eb5c62

Browse files
authored
feat(Bigquery): Undelete table sample. (GoogleCloudPlatform#1774)
1 parent 87dd57b commit 4eb5c62

File tree

2 files changed

+103
-8
lines changed

2 files changed

+103
-8
lines changed
Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
<?php
2+
/**
3+
* Copyright 2023 Google LLC.
4+
*
5+
* Licensed under the Apache License, Version 2.0 (the "License");
6+
* you may not use this file except in compliance with the License.
7+
* You may obtain a copy of the License at
8+
*
9+
* http://www.apache.org/licenses/LICENSE-2.0
10+
*
11+
* Unless required by applicable law or agreed to in writing, software
12+
* distributed under the License is distributed on an "AS IS" BASIS,
13+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
* See the License for the specific language governing permissions and
15+
* limitations under the License.
16+
*/
17+
18+
/**
19+
* For instructions on how to run the full sample:
20+
*
21+
* @see https://github.com/GoogleCloudPlatform/php-docs-samples/tree/main/bigquery/api/README.md
22+
*/
23+
24+
namespace Google\Cloud\Samples\BigQuery;
25+
26+
# [START bigquery_undelete_table]
27+
use Google\Cloud\BigQuery\BigQueryClient;
28+
29+
/**
30+
* Restore a deleted table from its snapshot.
31+
*
32+
* @param string $projectId The project Id of your Google Cloud Project.
33+
* @param string $datasetId The BigQuery dataset ID.
34+
* @param string $tableId Table ID of the table to delete.
35+
* @param string $restoredTableId Table Id for the restored table.
36+
*/
37+
function undelete_table(
38+
string $projectId,
39+
string $datasetId,
40+
string $tableId,
41+
string $restoredTableId
42+
): void {
43+
$bigQuery = new BigQueryClient(['projectId' => $projectId]);
44+
$dataset = $bigQuery->dataset($datasetId);
45+
46+
// Choose an appropriate snapshot point as epoch milliseconds.
47+
// For this example, we choose the current time as we're about to delete the
48+
// table immediately afterwards
49+
$snapshotEpoch = date_create()->format('Uv');
50+
51+
// Delete the table.
52+
$dataset->table($tableId)->delete();
53+
54+
// Construct the restore-from table ID using a snapshot decorator.
55+
$snapshotId = "{$tableId}@{$snapshotEpoch}";
56+
57+
// Restore the deleted table
58+
$restoredTable = $dataset->table($restoredTableId);
59+
$copyConfig = $dataset->table($snapshotId)->copy($restoredTable);
60+
$job = $bigQuery->runJob($copyConfig);
61+
62+
// check if the job is complete
63+
$job->reload();
64+
if (!$job->isComplete()) {
65+
throw new \Exception('Job has not yet completed', 500);
66+
}
67+
// check if the job has errors
68+
if (isset($job->info()['status']['errorResult'])) {
69+
$error = $job->info()['status']['errorResult']['message'];
70+
printf('Error running job: %s' . PHP_EOL, $error);
71+
} else {
72+
print('Snapshot restored successfully' . PHP_EOL);
73+
}
74+
}
75+
# [END bigquery_undelete_table]
76+
require_once __DIR__ . '/../../../testing/sample_helpers.php';
77+
\Google\Cloud\Samples\execute_sample(__FILE__, __NAMESPACE__, $argv);

bigquery/api/test/bigqueryTest.php

Lines changed: 26 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -99,8 +99,8 @@ public function testCreateAndDeleteTable()
9999
{
100100
$tempTableId = sprintf('test_table_%s', time());
101101
$fields = json_encode([
102-
['name' => 'name', 'type' => 'string', 'mode' => 'nullable'],
103-
['name' => 'title', 'type' => 'string', 'mode' => 'nullable']
102+
['name' => 'name', 'type' => 'string', 'mode' => 'nullable'],
103+
['name' => 'title', 'type' => 'string', 'mode' => 'nullable']
104104
]);
105105
$output = $this->runFunctionSnippet('create_table', [
106106
self::$datasetId,
@@ -352,8 +352,8 @@ public function testAddColumnLoadAppend()
352352
{
353353
$tableId = $this->createTempTable();
354354
$output = $this->runFunctionSnippet('add_column_load_append', [
355-
self::$datasetId,
356-
$tableId
355+
self::$datasetId,
356+
$tableId
357357
]);
358358

359359
$this->assertStringContainsString('name', $output);
@@ -365,14 +365,32 @@ public function testAddColumnQueryAppend()
365365
{
366366
$tableId = $this->createTempTable();
367367
$output = $this->runFunctionSnippet('add_column_query_append', [
368-
self::$datasetId,
369-
$tableId
368+
self::$datasetId,
369+
$tableId
370370
]);
371371
$this->assertStringContainsString('name', $output);
372372
$this->assertStringContainsString('title', $output);
373373
$this->assertStringContainsString('description', $output);
374374
}
375375

376+
public function testUndeleteTable()
377+
{
378+
// Create a base table
379+
$sourceTableId = $this->createTempTable();
380+
381+
// run the sample
382+
$restoredTableId = uniqid('restored_');
383+
$output = $this->runFunctionSnippet('undelete_table', [
384+
self::$datasetId,
385+
$sourceTableId,
386+
$restoredTableId,
387+
]);
388+
389+
$restoredTable = self::$dataset->table($restoredTableId);
390+
$this->assertStringContainsString('Snapshot restored successfully', $output);
391+
$this->verifyTable($restoredTable, 'Brent Shaffer', 3);
392+
}
393+
376394
private function runFunctionSnippet($sampleName, $params = [])
377395
{
378396
array_unshift($params, self::$projectId);
@@ -386,8 +404,8 @@ private function createTempEmptyTable()
386404
{
387405
$tempTableId = sprintf('test_table_%s_%s', time(), rand());
388406
$fields = json_encode([
389-
['name' => 'name', 'type' => 'string', 'mode' => 'nullable'],
390-
['name' => 'title', 'type' => 'string', 'mode' => 'nullable']
407+
['name' => 'name', 'type' => 'string', 'mode' => 'nullable'],
408+
['name' => 'title', 'type' => 'string', 'mode' => 'nullable']
391409
]);
392410
$this->runFunctionSnippet('create_table', [
393411
self::$datasetId,

0 commit comments

Comments
 (0)