-
Notifications
You must be signed in to change notification settings - Fork 309
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
docs: Revise update_table_expiration sample (#1457)
* docs: Revise update_table_expiration sample * 🦉 Updates from OwlBot post-processor See https://github.com/googleapis/repo-automation-bots/blob/main/packages/owl-bot/README.md --------- Co-authored-by: Owl Bot <gcf-owl-bot[bot]@users.noreply.github.com> Co-authored-by: Tim Swast <swast@google.com> Co-authored-by: aribray <45905583+aribray@users.noreply.github.com> Co-authored-by: Anthonios Partheniou <partheniou@google.com>
- Loading branch information
1 parent
5deba50
commit 03194e0
Showing
3 changed files
with
91 additions
and
0 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
# Copyright 2022 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. | ||
|
||
import datetime | ||
|
||
|
||
def update_table_expiration(table_id, expiration): | ||
orig_table_id = table_id | ||
orig_expiration = expiration | ||
|
||
# [START bigquery_update_table_expiration] | ||
from google.cloud import bigquery | ||
|
||
client = bigquery.Client() | ||
|
||
# TODO(dev): Change table_id to the full name of the table you want to update. | ||
table_id = "your-project.your_dataset.your_table_name" | ||
|
||
# TODO(dev): Set table to expire for desired days days from now. | ||
expiration = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta( | ||
days=5 | ||
) | ||
# [END bigquery_update_table_expiration] | ||
|
||
table_id = orig_table_id | ||
expiration = orig_expiration | ||
|
||
# [START bigquery_update_table_expiration] | ||
table = client.get_table(table_id) # Make an API request. | ||
table.expires = expiration | ||
table = client.update_table(table, ["expires"]) # API request | ||
|
||
print(f"Updated {table_id}, expires {table.expires}.") | ||
# [END bigquery_update_table_expiration] |
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,44 @@ | ||
# Copyright 2022 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. | ||
|
||
import datetime | ||
import typing | ||
|
||
import update_table_expiration | ||
|
||
if typing.TYPE_CHECKING: | ||
import pathlib | ||
|
||
import pytest | ||
|
||
|
||
def test_update_table_expiration( | ||
capsys: "pytest.CaptureFixture[str]", | ||
table_id: str, | ||
tmp_path: "pathlib.Path", | ||
) -> None: | ||
|
||
# This was not needed for function, only for test | ||
expiration = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta( | ||
days=5 | ||
) | ||
|
||
update_table_expiration.update_table_expiration(table_id, expiration) | ||
|
||
out, _ = capsys.readouterr() | ||
assert "Updated" in out | ||
assert table_id in out | ||
assert str(expiration.day) in out | ||
assert str(expiration.month) in out | ||
assert str(expiration.year) in out |