Skip to content

Commit b8c9276

Browse files
docs: revised relax column mode sample (#1467)
* docs: Revised relax_column sample * add todo for snippets.py cleanup --------- Co-authored-by: Tim Swast <[email protected]>
1 parent 40ba859 commit b8c9276

File tree

3 files changed

+100
-0
lines changed

3 files changed

+100
-0
lines changed

docs/snippets.py

+2
Original file line numberDiff line numberDiff line change
@@ -265,6 +265,8 @@ def test_relax_column(client, to_delete):
265265
dataset = client.create_dataset(dataset)
266266
to_delete.append(dataset)
267267

268+
# TODO(tswast): remove code sample once references to it on
269+
# cloud.google.com are updated to samples/snippets/relax_column.py
268270
# [START bigquery_relax_column]
269271
# from google.cloud import bigquery
270272
# client = bigquery.Client()

samples/snippets/relax_column.py

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
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+
from google.cloud import bigquery
16+
17+
18+
def relax_column(table_id: str) -> bigquery.Table:
19+
orig_table_id = table_id
20+
21+
# [START bigquery_relax_column]
22+
from google.cloud import bigquery
23+
24+
client = bigquery.Client()
25+
26+
# TODO(dev): Change table_id to full name of the table you want to create.
27+
table_id = "your-project.your_dataset.your_table"
28+
29+
# [END bigquery_relax_column]
30+
table_id = orig_table_id
31+
32+
# [START bigquery_relax_column]
33+
table = client.get_table(table_id)
34+
new_schema = []
35+
for field in table.schema:
36+
if field.mode != "REQUIRED":
37+
new_schema.append(field)
38+
else:
39+
# SchemaField properties cannot be edited after initialization.
40+
# To make changes, construct new SchemaField objects.
41+
new_field = field.to_api_repr()
42+
new_field["mode"] = "NULLABLE"
43+
relaxed_field = bigquery.SchemaField.from_api_repr(new_field)
44+
new_schema.append(relaxed_field)
45+
46+
table.schema = new_schema
47+
table = client.update_table(table, ["schema"])
48+
49+
print(f"Updated {table_id} schema: {table.schema}.")
50+
51+
# [END bigquery_relax_column]
52+
return table

samples/snippets/relax_column_test.py

+46
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
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 typing
16+
17+
from google.cloud import bigquery
18+
19+
import relax_column
20+
21+
if typing.TYPE_CHECKING:
22+
import pytest
23+
24+
25+
def test_relax_column(
26+
capsys: "pytest.CaptureFixture[str]",
27+
bigquery_client: bigquery.Client,
28+
random_table_id: str,
29+
) -> None:
30+
table = bigquery.Table(
31+
random_table_id,
32+
schema=[
33+
bigquery.SchemaField("string_col", "STRING", mode="NULLABLE"),
34+
bigquery.SchemaField("string_col2", "STRING", mode="REQUIRED"),
35+
],
36+
)
37+
38+
bigquery_client.create_table(table)
39+
table = relax_column.relax_column(random_table_id)
40+
41+
out, _ = capsys.readouterr()
42+
43+
assert all(field.mode == "NULLABLE" for field in table.schema)
44+
assert "REQUIRED" not in out
45+
assert "NULLABLE" in out
46+
assert random_table_id in out

0 commit comments

Comments
 (0)