Skip to content

Commit d615e12

Browse files
committed
Merge pull request #1 from sarang4/table_update_patch
Update Table and Patch Table support has been added.
2 parents b3ec17f + e30bf51 commit d615e12

File tree

1 file changed

+99
-0
lines changed

1 file changed

+99
-0
lines changed

bigquery/client.py

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -384,6 +384,105 @@ def get_table(self, dataset, table):
384384

385385
return table
386386

387+
388+
def update_table(self, dataset, table, schema, expiration_time=None):
389+
"""Updates information in an existing table. The update method
390+
replaces the entire table resource, whereas the patch method only
391+
replaces fields that are provided in the submitted table resource.
392+
393+
Args:
394+
dataset: the dataset to update the table in.
395+
table: the name of table to update.
396+
schema: table schema dict. Schema Should have older as well as new fields.
397+
expiration_time: the expiry time in milliseconds since the epoch.
398+
399+
Returns:
400+
bool indicating if the table was successfully updated or not,
401+
or response from BigQuery if swallow_results is set for False.
402+
"""
403+
404+
body = {
405+
'schema': {'fields': schema},
406+
'tableReference': {
407+
'tableId': table,
408+
'projectId': self.project_id,
409+
'datasetId': dataset
410+
}
411+
}
412+
413+
if expiration_time is not None:
414+
body['expirationTime'] = expiration_time
415+
416+
try:
417+
table = self.bigquery.tables().update(
418+
projectId=self.project_id,
419+
tableId=table,
420+
datasetId=dataset,
421+
body=body
422+
).execute()
423+
if self.swallow_results:
424+
return True
425+
else:
426+
return table
427+
428+
except HttpError as e:
429+
logging.error(('Cannot update table {0}.{1}\n'
430+
'Http Error: {2}').format(dataset, table,
431+
e.content))
432+
if self.swallow_results:
433+
return False
434+
else:
435+
return {}
436+
437+
def patch_table(self, dataset, table, schema, expiration_time=None):
438+
"""Updates information in an existing dataset. The update method
439+
replaces the entire dataset resource, whereas the patch method only
440+
replaces fields that are provided in the submitted dataset resource.
441+
442+
Args:
443+
dataset: the dataset to patch the table in.
444+
table: the name of table to patch.
445+
schema: table schema dict. Schema Should have older as well as new fields.
446+
expiration_time: the expiry time in milliseconds since the epoch.
447+
448+
Returns:
449+
bool indicating if the table was successfully updated or not,
450+
or response from BigQuery if swallow_results is set for False.
451+
"""
452+
453+
body = {
454+
'schema': {'fields': schema},
455+
'tableReference': {
456+
'tableId': table,
457+
'projectId': self.project_id,
458+
'datasetId': dataset
459+
}
460+
}
461+
462+
if expiration_time is not None:
463+
body['expirationTime'] = expiration_time
464+
465+
try:
466+
table = self.bigquery.tables().patch(
467+
projectId=self.project_id,
468+
tableId=table,
469+
datasetId=dataset,
470+
body=body
471+
).execute()
472+
if self.swallow_results:
473+
return True
474+
else:
475+
return table
476+
477+
except HttpError as e:
478+
logging.error(('Cannot patch table {0}.{1}\n'
479+
'Http Error: {2}').format(dataset, table,
480+
e.content))
481+
if self.swallow_results:
482+
return False
483+
else:
484+
return {}
485+
387486
def create_table(self, dataset, table, schema, expiration_time=None):
388487
"""Create a new table in the dataset.
389488

0 commit comments

Comments
 (0)