Skip to content

Update Table and Patch Table support has been added. #1

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 14, 2015
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
99 changes: 99 additions & 0 deletions bigquery/client.py
Original file line number Diff line number Diff line change
Expand Up @@ -384,6 +384,105 @@ def get_table(self, dataset, table):

return table


def update_table(self, dataset, table, schema, expiration_time=None):
"""Updates information in an existing table. The update method
replaces the entire table resource, whereas the patch method only
replaces fields that are provided in the submitted table resource.

Args:
dataset: the dataset to update the table in.
table: the name of table to update.
schema: table schema dict. Schema Should have older as well as new fields.
expiration_time: the expiry time in milliseconds since the epoch.

Returns:
bool indicating if the table was successfully updated or not,
or response from BigQuery if swallow_results is set for False.
"""

body = {
'schema': {'fields': schema},
'tableReference': {
'tableId': table,
'projectId': self.project_id,
'datasetId': dataset
}
}

if expiration_time is not None:
body['expirationTime'] = expiration_time

try:
table = self.bigquery.tables().update(
projectId=self.project_id,
tableId=table,
datasetId=dataset,
body=body
).execute()
if self.swallow_results:
return True
else:
return table

except HttpError as e:
logging.error(('Cannot update table {0}.{1}\n'
'Http Error: {2}').format(dataset, table,
e.content))
if self.swallow_results:
return False
else:
return {}

def patch_table(self, dataset, table, schema, expiration_time=None):
"""Updates information in an existing dataset. The update method
replaces the entire dataset resource, whereas the patch method only
replaces fields that are provided in the submitted dataset resource.

Args:
dataset: the dataset to patch the table in.
table: the name of table to patch.
schema: table schema dict. Schema Should have older as well as new fields.
expiration_time: the expiry time in milliseconds since the epoch.

Returns:
bool indicating if the table was successfully updated or not,
or response from BigQuery if swallow_results is set for False.
"""

body = {
'schema': {'fields': schema},
'tableReference': {
'tableId': table,
'projectId': self.project_id,
'datasetId': dataset
}
}

if expiration_time is not None:
body['expirationTime'] = expiration_time

try:
table = self.bigquery.tables().patch(
projectId=self.project_id,
tableId=table,
datasetId=dataset,
body=body
).execute()
if self.swallow_results:
return True
else:
return table

except HttpError as e:
logging.error(('Cannot patch table {0}.{1}\n'
'Http Error: {2}').format(dataset, table,
e.content))
if self.swallow_results:
return False
else:
return {}

def create_table(self, dataset, table, schema, expiration_time=None):
"""Create a new table in the dataset.

Expand Down