Skip to content

Commit

Permalink
bigquery: add Table.Update
Browse files Browse the repository at this point in the history
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 <mcgreevy@golang.org>
  • Loading branch information
jba committed Oct 12, 2016
1 parent 3a5d9c1 commit 2096d28
Show file tree
Hide file tree
Showing 2 changed files with 56 additions and 3 deletions.
21 changes: 20 additions & 1 deletion bigquery/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,26 @@ func TestIntegration(t *testing.T) {

checkRead(job2)

// TODO(jba): patch the table
// Test Update.
tm, err := table.Metadata(ctx)
if err != nil {
t.Fatal(err)
}
wantDescription := tm.Description + "more"
wantName := tm.Name + "more"
got, err := table.Update(ctx, TableMetadataToUpdate{
Description: wantDescription,
Name: wantName,
})
if err != nil {
t.Fatal(err)
}
if got.Description != wantDescription {
t.Errorf("Description: got %q, want %q", got.Description, wantDescription)
}
if got.Name != wantName {
t.Errorf("Name: got %q, want %q", got.Name, wantName)
}
}

func hasStatusCode(err error, code int) bool {
Expand Down
38 changes: 36 additions & 2 deletions bigquery/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (

"golang.org/x/net/context"

"cloud.google.com/go/internal/optional"
bq "google.golang.org/api/bigquery/v2"
)

Expand Down Expand Up @@ -245,6 +246,8 @@ type TableMetadataPatch struct {

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

// Description sets the table description.
//
// Deprecated: use Table.Update instead.
func (p *TableMetadataPatch) Description(desc string) {
p.conf.Description = &desc
}

// Name sets the table name.
//
// Deprecated: use Table.Update instead.
func (p *TableMetadataPatch) Name(name string) {
p.conf.Name = &name
}

// TODO(mcgreevy): support patching the schema.

// Apply applies the patch operation.
//
// Deprecated: use Table.Update instead.
func (p *TableMetadataPatch) Apply(ctx context.Context) (*TableMetadata, error) {
return p.s.patchTable(ctx, p.projectID, p.datasetID, p.tableID, &p.conf)
}

// Update modifies specific Table metadata fields.
func (t *Table) Update(ctx context.Context, tm TableMetadataToUpdate) (*TableMetadata, error) {
var conf patchTableConf
if tm.Description != nil {
s := optional.ToString(tm.Description)
conf.Description = &s
}
if tm.Name != nil {
s := optional.ToString(tm.Name)
conf.Name = &s
}
return t.c.service.patchTable(ctx, t.ProjectID, t.DatasetID, t.TableID, &conf)
}

// TableMetadataToUpdate is used when updating a table's metadata.
// Only non-nil fields will be updated.
type TableMetadataToUpdate struct {
// Description is the user-friendly description of this table.
Description optional.String

// Name is the user-friendly name for this table.
Name optional.String

// TODO(jba): support updating the schema
// TODO(jba): support updating the view
}

// NewUploader returns an *Uploader that can be used to append rows to t.
func (t *Table) NewUploader(opts ...UploadOption) *Uploader {
uploader := &Uploader{t: t}
Expand Down

0 comments on commit 2096d28

Please sign in to comment.