Skip to content

Commit e760d1b

Browse files
authored
docs(samples): add table snapshot sample (#1274)
* docs(samples): add table snapshot sample * docs(samples): fix region tag
1 parent 728b07c commit e760d1b

File tree

3 files changed

+90
-1
lines changed

3 files changed

+90
-1
lines changed

samples/snippets/conftest.py

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,6 @@
1818
import pytest
1919
import test_utils.prefixer
2020

21-
2221
prefixer = test_utils.prefixer.Prefixer("python-bigquery", "samples/snippets")
2322

2423

@@ -52,6 +51,20 @@ def dataset_id(bigquery_client: bigquery.Client, project_id: str) -> Iterator[st
5251
bigquery_client.delete_dataset(dataset, delete_contents=True, not_found_ok=True)
5352

5453

54+
@pytest.fixture
55+
def table_id(
56+
bigquery_client: bigquery.Client, project_id: str, dataset_id: str
57+
) -> Iterator[str]:
58+
table_id = prefixer.create_prefix()
59+
full_table_id = f"{project_id}.{dataset_id}.{table_id}"
60+
table = bigquery.Table(
61+
full_table_id, schema=[bigquery.SchemaField("string_col", "STRING")]
62+
)
63+
bigquery_client.create_table(table)
64+
yield full_table_id
65+
bigquery_client.delete_table(table, not_found_ok=True)
66+
67+
5568
@pytest.fixture(scope="session")
5669
def entity_id(bigquery_client: bigquery.Client, dataset_id: str) -> str:
5770
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
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+
16+
def create_table_snapshot(source_table_id: str, snapshot_table_id: str) -> None:
17+
original_source_table_id = source_table_id
18+
original_snapshot_table_id = snapshot_table_id
19+
# [START bigquery_create_table_snapshot]
20+
from google.cloud import bigquery
21+
22+
# TODO(developer): Set table_id to the ID of the table to create.
23+
source_table_id = "your-project.your_dataset.your_table_name"
24+
snapshot_table_id = "your-project.your_dataset.snapshot_table_name"
25+
# [END bigquery_create_table_snapshot]
26+
source_table_id = original_source_table_id
27+
snapshot_table_id = original_snapshot_table_id
28+
# [START bigquery_create_table_snapshot]
29+
30+
# Construct a BigQuery client object.
31+
client = bigquery.Client()
32+
copy_config = bigquery.CopyJobConfig()
33+
copy_config.operation_type = bigquery.OperationType.SNAPSHOT
34+
35+
copy_job = client.copy_table(
36+
sources=source_table_id,
37+
destination=snapshot_table_id,
38+
job_config=copy_config,
39+
)
40+
copy_job.result()
41+
42+
print("Created table snapshot {}".format(snapshot_table_id))
43+
# [END bigquery_create_table_snapshot]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
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+
import create_table_snapshot
18+
19+
if typing.TYPE_CHECKING:
20+
import pytest
21+
22+
23+
def test_create_table_snapshot(
24+
capsys: "pytest.CaptureFixture[str]",
25+
table_id: str,
26+
random_table_id: str,
27+
) -> None:
28+
29+
create_table_snapshot.create_table_snapshot(table_id, random_table_id)
30+
31+
out, _ = capsys.readouterr()
32+
33+
assert "Created table snapshot {}".format(random_table_id) in out

0 commit comments

Comments
 (0)