Skip to content

Commit 03194e0

Browse files
thejaredchapmangcf-owl-bot[bot]tswastaribrayparthea
authored
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 <[email protected]> Co-authored-by: aribray <[email protected]> Co-authored-by: Anthonios Partheniou <[email protected]>
1 parent 5deba50 commit 03194e0

File tree

3 files changed

+91
-0
lines changed

3 files changed

+91
-0
lines changed

docs/snippets.py

+2
Original file line numberDiff line numberDiff line change
@@ -220,6 +220,8 @@ def test_update_table_expiration(client, to_delete):
220220
table = bigquery.Table(dataset.table(table_id), schema=SCHEMA)
221221
table = client.create_table(table)
222222

223+
# TODO(thejaredchapman): After code sample has been updated from cloud.google.com delete this.
224+
223225
# [START bigquery_update_table_expiration]
224226
import datetime
225227

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import datetime
16+
17+
18+
def update_table_expiration(table_id, expiration):
19+
orig_table_id = table_id
20+
orig_expiration = expiration
21+
22+
# [START bigquery_update_table_expiration]
23+
from google.cloud import bigquery
24+
25+
client = bigquery.Client()
26+
27+
# TODO(dev): Change table_id to the full name of the table you want to update.
28+
table_id = "your-project.your_dataset.your_table_name"
29+
30+
# TODO(dev): Set table to expire for desired days days from now.
31+
expiration = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(
32+
days=5
33+
)
34+
# [END bigquery_update_table_expiration]
35+
36+
table_id = orig_table_id
37+
expiration = orig_expiration
38+
39+
# [START bigquery_update_table_expiration]
40+
table = client.get_table(table_id) # Make an API request.
41+
table.expires = expiration
42+
table = client.update_table(table, ["expires"]) # API request
43+
44+
print(f"Updated {table_id}, expires {table.expires}.")
45+
# [END bigquery_update_table_expiration]
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
# Copyright 2022 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
import datetime
16+
import typing
17+
18+
import update_table_expiration
19+
20+
if typing.TYPE_CHECKING:
21+
import pathlib
22+
23+
import pytest
24+
25+
26+
def test_update_table_expiration(
27+
capsys: "pytest.CaptureFixture[str]",
28+
table_id: str,
29+
tmp_path: "pathlib.Path",
30+
) -> None:
31+
32+
# This was not needed for function, only for test
33+
expiration = datetime.datetime.now(datetime.timezone.utc) + datetime.timedelta(
34+
days=5
35+
)
36+
37+
update_table_expiration.update_table_expiration(table_id, expiration)
38+
39+
out, _ = capsys.readouterr()
40+
assert "Updated" in out
41+
assert table_id in out
42+
assert str(expiration.day) in out
43+
assert str(expiration.month) in out
44+
assert str(expiration.year) in out

0 commit comments

Comments
 (0)