Skip to content

Commit 3ef43c7

Browse files
committed
fix arguments mode in dataset and table
1 parent 9374e9d commit 3ef43c7

2 files changed

Lines changed: 37 additions & 34 deletions

File tree

python-package/basedosdados/upload/dataset.py

Lines changed: 22 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -36,36 +36,38 @@ def dataset_config(self) -> dict[str, Any]:
3636
"""
3737
return self.backend.get_dataset_config(self.dataset_id)
3838

39-
def _loop_modes(self, mode: str = "all"):
39+
def _loop_modes(self, project_gcp: str = "all"):
4040
"""
4141
Loop modes.
4242
"""
4343

4444
def dataset_tag(m):
4545
return f"_{m}" if m == "staging" else ""
4646

47-
mode_ = ["prod", "staging"] if mode == "all" else [mode]
47+
_project_gcp = (
48+
["prod", "staging"] if project_gcp == "all" else [project_gcp]
49+
)
4850
return (
4951
{
5052
"client": self.client[f"bigquery_{m}"],
5153
"id": f"{self.client[f'bigquery_{m}'].project}.{self.dataset_id}{dataset_tag(m)}",
5254
"mode": m,
5355
}
54-
for m in mode_
56+
for m in _project_gcp
5557
)
5658

5759
def _setup_dataset_object(
5860
self,
5961
dataset_id: str,
6062
location: Optional[str] = None,
61-
mode: str = "staging",
63+
project_gcp: str = "staging",
6264
) -> bigquery.Dataset:
6365
"""
6466
Setup dataset object.
6567
"""
6668

6769
dataset = bigquery.Dataset(dataset_id)
68-
if mode == "staging":
70+
if project_gcp == "staging":
6971
dataset_path = dataset_id.replace("_staging", "")
7072
description = f"staging dataset for `{dataset_path}`"
7173
labels = {"staging": True}
@@ -89,19 +91,19 @@ def _setup_dataset_object(
8991
return dataset
9092

9193
def publicize(
92-
self, mode: str = "all", dataset_is_public: bool = True
94+
self, project_gcp: str = "all", dataset_is_public: bool = True
9395
) -> None:
9496
"""
9597
Changes IAM configuration to turn BigQuery dataset public.
9698
9799
Args:
98-
mode: Which dataset to create [`prod`|`staging`|`all`].
100+
project_gcp: Which dataset to create [`prod`|`staging`].
99101
dataset_is_public: Control if prod dataset is public or not. By
100102
default, staging datasets like `dataset_id_staging` are not
101103
public.
102104
"""
103105

104-
for m in self._loop_modes(mode):
106+
for m in self._loop_modes(project_gcp):
105107
dataset = m["client"].get_dataset(m["id"])
106108
entries = dataset.access_entries
107109
# TODO https://github.com/basedosdados/sdk/pull/1020
@@ -140,9 +142,9 @@ def publicize(
140142
dataset.access_entries = entries
141143
m["client"].update_dataset(dataset, ["access_entries"])
142144
logger.success(
143-
" {object} {object_id}_{mode} was {action}!",
145+
" {object} {object_id}_{project_gcp} was {action}!",
144146
object_id=self.dataset_id,
145-
mode=m["mode"],
147+
project_gcp=m["mode"],
146148
object="Dataset",
147149
action="publicized",
148150
)
@@ -176,13 +178,11 @@ def create(
176178
177179
It can create two datasets:
178180
179-
* `<dataset_id>` (mode = `prod`)
180-
* `<dataset_id>_staging` (mode = `staging`)
181-
182-
If `mode` is `all`, it creates both.
181+
* `<dataset_id>` (project_gcp = `prod`)
182+
* `<dataset_id>_staging` (project_gcp = `staging`)
183183
184184
Args:
185-
mode: Which dataset to create [`prod`|`staging`|`all`].
185+
project_gcp: Which dataset to create [`prod`|`staging`].
186186
if_exists: What to do if dataset exists
187187
* `raise`: Raises Conflict exception
188188
* `replace`: Drop all tables and replace dataset
@@ -213,21 +213,24 @@ def create(
213213
if not self.exists(project_gcp=m["mode"]):
214214
# Construct a full Dataset object to send to the API.
215215
dataset_obj = self._setup_dataset_object(
216-
dataset_id=m["id"], location=location, mode=m["mode"]
216+
dataset_id=m["id"],
217+
location=location,
218+
project_gcp=m["mode"],
217219
)
218220
m["client"].create_dataset(
219221
dataset_obj
220222
) # Make an API request.
221223
logger.success(
222-
" {object} {object_id}_{mode} was {action}!",
224+
" {object} {object_id}_{project_gcp} was {action}!",
223225
object_id=self.dataset_id,
224-
mode=m["mode"],
226+
project_gcp=m["mode"],
225227
object="Dataset",
226228
action="created",
227229
)
228230
# Make prod dataset public
229231
self.publicize(
230-
dataset_is_public=dataset_is_public, mode=m["mode"]
232+
dataset_is_public=dataset_is_public,
233+
project_gcp=m["mode"],
231234
)
232235
except Conflict as e:
233236
if if_exists == "pass":

python-package/basedosdados/upload/table.py

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -271,7 +271,7 @@ def _parser_blobs_to_partition_dict(self) -> Optional[dict[Any, Any]]:
271271
Extract the partition information from the blobs.
272272
"""
273273

274-
if not self.table_exists(mode="staging"):
274+
if not self.table_exists(project_gcp="staging"):
275275
return
276276
blobs = (
277277
self.client["storage_staging"]
@@ -417,7 +417,7 @@ def table_exists(self, project_gcp: str = "staging") -> bool:
417417
Check if table exists in BigQuery.
418418
419419
Args:
420-
mode: Which dataset to check [`prod`|`staging`].
420+
project_gcp: Which dataset to check [`prod`|`staging`].
421421
"""
422422

423423
try:
@@ -436,7 +436,7 @@ def _get_biglake_connection(
436436
Get or create BigLake connection and set permissions if needed.
437437
"""
438438
connection = Connection(
439-
name="biglake", location=location, mode="staging"
439+
name="biglake", location=location, project_gcp="staging"
440440
)
441441
if not connection.exists:
442442
try:
@@ -727,47 +727,47 @@ def create(
727727
) from exc
728728

729729
logger.success(
730-
"{object} {object_id} was {action} in {mode}!",
730+
"{object} {object_id} was {action} in {project_gcp}!",
731731
object_id=self.table_id,
732-
mode="staging",
732+
project_gcp="staging",
733733
object="Table",
734734
action="created",
735735
)
736736
# return None
737737

738738
def update(
739739
self,
740-
mode: str = "prod",
740+
project_gcp: str = "prod",
741741
custom_schema: Optional[list[dict[str, str]]] = None,
742742
) -> None:
743743
"""
744744
Updates BigQuery schema and description.
745745
746746
Args:
747-
mode: Table of which table to update [`prod`].
747+
project_gcp: Table of which table to update [`prod`].
748748
not_found_ok: What to do if table is not found.
749749
"""
750750

751-
table = self._get_table_obj("prod")
751+
table = self._get_table_obj(project_gcp="prod")
752752

753753
table.description = self._get_table_description()
754754

755755
# when mode is staging the table schema already exists
756-
if mode == "prod" and custom_schema is None:
756+
if project_gcp == "prod" and custom_schema is None:
757757
table.schema = self._load_schema_from_json(
758758
columns=self._get_cross_columns_from_bq_api()
759759
)
760-
if mode == "prod" and custom_schema is not None:
760+
if project_gcp == "prod" and custom_schema is not None:
761761
table.schema = self._load_schema_from_json(custom_schema)
762762

763763
fields = ["description", "schema"]
764764

765765
self.client["bigquery_prod"].update_table(table, fields=fields)
766766

767767
logger.success(
768-
" {object} {object_id} was {action} in {mode}!",
768+
" {object} {object_id} was {action} in {project_gcp}!",
769769
object_id=self.table_id,
770-
mode=mode,
770+
project_gcp=project_gcp,
771771
object="Table",
772772
action="updated",
773773
)
@@ -798,15 +798,15 @@ def publish(
798798
"""
799799
# TODO: review this method. Check if all required fields are filled
800800

801-
if if_exists == "replace" and self.table_exists(mode="prod"):
802-
self.delete(mode="prod")
801+
if if_exists == "replace" and self.table_exists(project_gcp="prod"):
802+
self.delete(project_gcp="prod")
803803

804804
publish_sql = self._make_publish_sql()
805805

806806
# create view using API metadata
807807
if custom_publish_sql is None:
808808
self.client["bigquery_prod"].query(publish_sql).result()
809-
self.update(mode="prod")
809+
self.update(project_gcp="prod")
810810

811811
# create view using custon query
812812
if custom_publish_sql is not None:

0 commit comments

Comments
 (0)