-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
chore(bigquery): fix undelete table system test to use milliseconds i…
…n snapshot decorator (#9649) This fixes a flakey system test, where sometimes we sent an invalid timestamp as a snapshot to copy from.
- Loading branch information
Showing
5 changed files
with
121 additions
and
64 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,26 @@ | ||
# Copyright 2019 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from .. import undelete_table | ||
|
||
|
||
def test_undelete_table(capsys, client, table_with_schema_id, random_table_id): | ||
undelete_table.undelete_table(client, table_with_schema_id, random_table_id) | ||
out, _ = capsys.readouterr() | ||
assert ( | ||
"Copied data from deleted table {} to {}".format( | ||
table_with_schema_id, random_table_id | ||
) | ||
in out | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,67 @@ | ||
# Copyright 2019 Google LLC | ||
# | ||
# Licensed under the Apache License, Version 2.0 (the "License"); | ||
# you may not use this file except in compliance with the License. | ||
# You may obtain a copy of the License at | ||
# | ||
# https://www.apache.org/licenses/LICENSE-2.0 | ||
# | ||
# Unless required by applicable law or agreed to in writing, software | ||
# distributed under the License is distributed on an "AS IS" BASIS, | ||
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
# See the License for the specific language governing permissions and | ||
# limitations under the License. | ||
|
||
from google.api_core import datetime_helpers | ||
|
||
|
||
def undelete_table(client, table_id, recovered_table_id): | ||
# [START bigquery_undelete_table] | ||
import time | ||
|
||
# TODO(developer): Import the client library. | ||
# from google.cloud import bigquery | ||
|
||
# TODO(developer): Construct a BigQuery client object. | ||
# client = bigquery.Client() | ||
|
||
# TODO(developer): Choose a table to recover. | ||
# table_id = "your-project.your_dataset.your_table" | ||
|
||
# TODO(developer): Choose a new table ID for the recovered table data. | ||
# recovery_table_id = "your-project.your_dataset.your_table_recovered" | ||
|
||
# TODO(developer): 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. | ||
snapshot_epoch = int(time.time() * 1000) | ||
|
||
# [START_EXCLUDE] | ||
# Due to very short lifecycle of the table, ensure we're not picking a time | ||
# prior to the table creation due to time drift between backend and client. | ||
table = client.get_table(table_id) | ||
created_epoch = datetime_helpers.to_milliseconds(table.created) | ||
if created_epoch > snapshot_epoch: | ||
snapshot_epoch = created_epoch | ||
# [END_EXCLUDE] | ||
|
||
# "Accidentally" delete the table. | ||
client.delete_table(table_id) # API request | ||
|
||
# Construct the restore-from table ID using a snapshot decorator. | ||
snapshot_table_id = "{}@{}".format(table_id, snapshot_epoch) | ||
|
||
# Construct and run a copy job. | ||
job = client.copy_table( | ||
snapshot_table_id, | ||
recovered_table_id, | ||
# Location must match that of the source and destination tables. | ||
location="US", | ||
) # API request | ||
|
||
job.result() # Wait for job to complete. | ||
|
||
print( | ||
"Copied data from deleted table {} to {}".format(table_id, recovered_table_id) | ||
) | ||
# [END bigquery_undelete_table] |