Skip to content

Commit 2096d28

Browse files
committed
bigquery: add Table.Update
Table.Update will replace Table.Patch. We use the same design that we developed for storage: a struct TableMetadataToUpdate holds all the updatable fields, using the types of the optional package. Change-Id: Ie8d828722fc29f12881a3e976fb276561702fd65 Reviewed-on: https://code-review.googlesource.com/8352 Reviewed-by: Michael McGreevy <[email protected]>
1 parent 3a5d9c1 commit 2096d28

File tree

2 files changed

+56
-3
lines changed

2 files changed

+56
-3
lines changed

bigquery/integration_test.go

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,26 @@ func TestIntegration(t *testing.T) {
161161

162162
checkRead(job2)
163163

164-
// TODO(jba): patch the table
164+
// Test Update.
165+
tm, err := table.Metadata(ctx)
166+
if err != nil {
167+
t.Fatal(err)
168+
}
169+
wantDescription := tm.Description + "more"
170+
wantName := tm.Name + "more"
171+
got, err := table.Update(ctx, TableMetadataToUpdate{
172+
Description: wantDescription,
173+
Name: wantName,
174+
})
175+
if err != nil {
176+
t.Fatal(err)
177+
}
178+
if got.Description != wantDescription {
179+
t.Errorf("Description: got %q, want %q", got.Description, wantDescription)
180+
}
181+
if got.Name != wantName {
182+
t.Errorf("Name: got %q, want %q", got.Name, wantName)
183+
}
165184
}
166185

167186
func hasStatusCode(err error, code int) bool {

bigquery/table.go

Lines changed: 36 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ import (
2020

2121
"golang.org/x/net/context"
2222

23+
"cloud.google.com/go/internal/optional"
2324
bq "google.golang.org/api/bigquery/v2"
2425
)
2526

@@ -245,6 +246,8 @@ type TableMetadataPatch struct {
245246

246247
// Patch returns a *TableMetadataPatch, which can be used to modify specific Table metadata fields.
247248
// In order to apply the changes, the TableMetadataPatch's Apply method must be called.
249+
//
250+
// Deprecated: use Table.Update instead.
248251
func (t *Table) Patch() *TableMetadataPatch {
249252
return &TableMetadataPatch{
250253
s: t.c.service,
@@ -255,22 +258,53 @@ func (t *Table) Patch() *TableMetadataPatch {
255258
}
256259

257260
// Description sets the table description.
261+
//
262+
// Deprecated: use Table.Update instead.
258263
func (p *TableMetadataPatch) Description(desc string) {
259264
p.conf.Description = &desc
260265
}
261266

262267
// Name sets the table name.
268+
//
269+
// Deprecated: use Table.Update instead.
263270
func (p *TableMetadataPatch) Name(name string) {
264271
p.conf.Name = &name
265272
}
266273

267-
// TODO(mcgreevy): support patching the schema.
268-
269274
// Apply applies the patch operation.
275+
//
276+
// Deprecated: use Table.Update instead.
270277
func (p *TableMetadataPatch) Apply(ctx context.Context) (*TableMetadata, error) {
271278
return p.s.patchTable(ctx, p.projectID, p.datasetID, p.tableID, &p.conf)
272279
}
273280

281+
// Update modifies specific Table metadata fields.
282+
func (t *Table) Update(ctx context.Context, tm TableMetadataToUpdate) (*TableMetadata, error) {
283+
var conf patchTableConf
284+
if tm.Description != nil {
285+
s := optional.ToString(tm.Description)
286+
conf.Description = &s
287+
}
288+
if tm.Name != nil {
289+
s := optional.ToString(tm.Name)
290+
conf.Name = &s
291+
}
292+
return t.c.service.patchTable(ctx, t.ProjectID, t.DatasetID, t.TableID, &conf)
293+
}
294+
295+
// TableMetadataToUpdate is used when updating a table's metadata.
296+
// Only non-nil fields will be updated.
297+
type TableMetadataToUpdate struct {
298+
// Description is the user-friendly description of this table.
299+
Description optional.String
300+
301+
// Name is the user-friendly name for this table.
302+
Name optional.String
303+
304+
// TODO(jba): support updating the schema
305+
// TODO(jba): support updating the view
306+
}
307+
274308
// NewUploader returns an *Uploader that can be used to append rows to t.
275309
func (t *Table) NewUploader(opts ...UploadOption) *Uploader {
276310
uploader := &Uploader{t: t}

0 commit comments

Comments
 (0)