Skip to content

Commit 22bc7cd

Browse files
committed
Update 'Table.patch()' to support patching schema.
See: #1023 (comment).
1 parent d22d7da commit 22bc7cd

2 files changed

Lines changed: 50 additions & 3 deletions

File tree

gcloud/bigquery/table.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -423,8 +423,14 @@ def reload(self, client=None):
423423
method='GET', path=self.path)
424424
self._set_properties(api_response)
425425

426-
def patch(self, client=None, friendly_name=_MARKER, description=_MARKER,
427-
location=_MARKER, expires=_MARKER, view_query=_MARKER):
426+
def patch(self,
427+
client=None,
428+
friendly_name=_MARKER,
429+
description=_MARKER,
430+
location=_MARKER,
431+
expires=_MARKER,
432+
view_query=_MARKER,
433+
schema=_MARKER):
428434
"""API call: update individual table properties via a PATCH request
429435
430436
See
@@ -449,6 +455,9 @@ def patch(self, client=None, friendly_name=_MARKER, description=_MARKER,
449455
:type view_query: string
450456
:param view_query: SQL query defining the table as a view
451457
458+
:type schema: list of :class:`SchemaField`
459+
:param schema: fields describing the schema
460+
452461
:raises: ValueError for invalid value types.
453462
"""
454463
client = self._require_client(client)
@@ -476,6 +485,13 @@ def patch(self, client=None, friendly_name=_MARKER, description=_MARKER,
476485
else:
477486
partial['view'] = {'query': view_query}
478487

488+
if schema is not _MARKER:
489+
if schema is None:
490+
partial['schema'] = None
491+
else:
492+
partial['schema'] = {
493+
'fields': self._build_schema_resource(schema)}
494+
479495
api_response = client.connection.api_request(
480496
method='PATCH', path=self.path, data=partial)
481497
self._set_properties(api_response)

gcloud/bigquery/test_table.py

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -569,6 +569,7 @@ def test_patch_w_alternate_client(self):
569569
import datetime
570570
import pytz
571571
from gcloud.bigquery._helpers import _millis
572+
from gcloud.bigquery.table import SchemaField
572573
PATH = 'projects/%s/datasets/%s/tables/%s' % (
573574
self.PROJECT, self.DS_NAME, self.TABLE_NAME)
574575
QUERY = 'select fullname, age from person_ages'
@@ -586,9 +587,11 @@ def test_patch_w_alternate_client(self):
586587
client2 = _Client(project=self.PROJECT, connection=conn2)
587588
dataset = _Dataset(client1)
588589
table = self._makeOne(self.TABLE_NAME, dataset=dataset)
590+
full_name = SchemaField('full_name', 'STRING', mode='REQUIRED')
591+
age = SchemaField('age', 'INTEGER', mode='OPTIONAL')
589592

590593
table.patch(client=client2, view_query=QUERY, location=LOCATION,
591-
expires=self.EXP_TIME)
594+
expires=self.EXP_TIME, schema=[full_name, age])
592595

593596
self.assertEqual(len(conn1._requested), 0)
594597
self.assertEqual(len(conn2._requested), 1)
@@ -599,10 +602,38 @@ def test_patch_w_alternate_client(self):
599602
'view': {'query': QUERY},
600603
'location': LOCATION,
601604
'expirationTime': _millis(self.EXP_TIME),
605+
'schema': {'fields': [
606+
{'name': 'full_name', 'type': 'STRING', 'mode': 'REQUIRED'},
607+
{'name': 'age', 'type': 'INTEGER', 'mode': 'OPTIONAL'}]},
602608
}
603609
self.assertEqual(req['data'], SENT)
604610
self._verifyResourceProperties(table, RESOURCE)
605611

612+
def test_patch_w_schema_None(self):
613+
# Simulate deleting schema: not sure if back-end will actually
614+
# allow this operation, but the spec says it is optional.
615+
PATH = 'projects/%s/datasets/%s/tables/%s' % (
616+
self.PROJECT, self.DS_NAME, self.TABLE_NAME)
617+
DESCRIPTION = 'DESCRIPTION'
618+
TITLE = 'TITLE'
619+
RESOURCE = self._makeResource()
620+
RESOURCE['description'] = DESCRIPTION
621+
RESOURCE['friendlyName'] = TITLE
622+
conn = _Connection(RESOURCE)
623+
client = _Client(project=self.PROJECT, connection=conn)
624+
dataset = _Dataset(client)
625+
table = self._makeOne(self.TABLE_NAME, dataset=dataset)
626+
627+
table.patch(schema=None)
628+
629+
self.assertEqual(len(conn._requested), 1)
630+
req = conn._requested[0]
631+
self.assertEqual(req['method'], 'PATCH')
632+
SENT = {'schema': None}
633+
self.assertEqual(req['data'], SENT)
634+
self.assertEqual(req['path'], '/%s' % PATH)
635+
self._verifyResourceProperties(table, RESOURCE)
636+
606637
def test_update_w_bound_client(self):
607638
from gcloud.bigquery.table import SchemaField
608639
PATH = 'projects/%s/datasets/%s/tables/%s' % (

0 commit comments

Comments
 (0)