Skip to content
Merged
Show file tree
Hide file tree
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
24 changes: 12 additions & 12 deletions bigquery/dataset.go
Original file line number Diff line number Diff line change
Expand Up @@ -255,23 +255,23 @@ func (dm *DatasetMetadata) toBQ() (*bq.Dataset, error) {
ds.StorageBillingModel = string(dm.StorageBillingModel)
ds.IsCaseInsensitive = dm.IsCaseInsensitive
ds.Labels = dm.Labels
var err error
ds.Access, err = accessListToBQ(dm.Access)
if err != nil {
return nil, err
}
// These fields are read-only, but we'll populate them should there be a change in the service behavior.
if !dm.CreationTime.IsZero() {
return nil, errors.New("bigquery: Dataset.CreationTime is not writable")
ds.CreationTime = dm.CreationTime.UnixMilli()
}
if !dm.LastModifiedTime.IsZero() {
return nil, errors.New("bigquery: Dataset.LastModifiedTime is not writable")
}
if dm.FullID != "" {
return nil, errors.New("bigquery: Dataset.FullID is not writable")
ds.LastModifiedTime = dm.LastModifiedTime.UnixMilli()
}
if dm.ETag != "" {
return nil, errors.New("bigquery: Dataset.ETag is not writable")
ds.Etag = dm.ETag
ds.Id = dm.FullID

// access list
var err error
ds.Access, err = accessListToBQ(dm.Access)
if err != nil {
return nil, err
}

if dm.DefaultEncryptionConfig != nil {
ds.DefaultEncryptionConfiguration = dm.DefaultEncryptionConfig.toBQ()
}
Expand Down
13 changes: 0 additions & 13 deletions bigquery/dataset_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,19 +382,6 @@ func TestDatasetToBQ(t *testing.T) {
t.Errorf("got=-, want=+:\n%s", diff)
}
}

// Check that non-writeable fields are unset.
aTime := time.Date(2017, 1, 26, 0, 0, 0, 0, time.Local)
for _, dm := range []*DatasetMetadata{
{CreationTime: aTime},
{LastModifiedTime: aTime},
{FullID: "x"},
{ETag: "e"},
} {
if _, err := dm.toBQ(); err == nil {
t.Errorf("%+v: got nil, want error", dm)
}
}
}

func TestBQToDatasetMetadata(t *testing.T) {
Expand Down
9 changes: 3 additions & 6 deletions bigquery/routine.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,6 @@ package bigquery

import (
"context"
"errors"
"fmt"
"strings"
"time"
Expand Down Expand Up @@ -310,14 +309,12 @@ func (rm *RoutineMetadata) toBQ() (*bq.Routine, error) {
r.RemoteFunctionOptions = rm.RemoteFunctionOptions.toBQ()
}
if !rm.CreationTime.IsZero() {
return nil, errors.New("cannot set CreationTime on create")
r.CreationTime = rm.CreationTime.UnixMilli()
}
if !rm.LastModifiedTime.IsZero() {
return nil, errors.New("cannot set LastModifiedTime on create")
}
if rm.ETag != "" {
return nil, errors.New("cannot set ETag on create")
r.LastModifiedTime = rm.LastModifiedTime.UnixMilli()
}
r.Etag = rm.ETag
return r, nil
}

Expand Down
32 changes: 9 additions & 23 deletions bigquery/table.go
Original file line number Diff line number Diff line change
Expand Up @@ -855,32 +855,18 @@ func (tm *TableMetadata) toBQ() (*bq.Table, error) {
t.ExternalDataConfiguration = &edc
}
t.EncryptionConfiguration = tm.EncryptionConfig.toBQ()
if tm.FullID != "" {
return nil, errors.New("cannot set FullID on create")
}
if tm.Type != "" {
return nil, errors.New("cannot set Type on create")
}
// Propagate read-only fields.
t.Id = tm.FullID
t.Type = string(tm.Type)
t.NumBytes = tm.NumBytes
t.NumLongTermBytes = tm.NumLongTermBytes
t.NumRows = tm.NumRows
t.Etag = tm.ETag
if !tm.CreationTime.IsZero() {
return nil, errors.New("cannot set CreationTime on create")
t.CreationTime = tm.CreationTime.UnixMilli()
}
if !tm.LastModifiedTime.IsZero() {
return nil, errors.New("cannot set LastModifiedTime on create")
}
if tm.NumBytes != 0 {
return nil, errors.New("cannot set NumBytes on create")
}
if tm.NumLongTermBytes != 0 {
return nil, errors.New("cannot set NumLongTermBytes on create")
}
if tm.NumRows != 0 {
return nil, errors.New("cannot set NumRows on create")
}
if tm.StreamingBuffer != nil {
return nil, errors.New("cannot set StreamingBuffer on create")
}
if tm.ETag != "" {
return nil, errors.New("cannot set ETag on create")
t.LastModifiedTime = uint64(tm.LastModifiedTime.UnixMilli())
}
t.DefaultCollation = string(tm.DefaultCollation)

Expand Down
10 changes: 0 additions & 10 deletions bigquery/table_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -361,16 +361,6 @@ func TestTableMetadataToBQ(t *testing.T) {
{Schema: sc, ViewQuery: "q"}, // can't have both schema and query
{UseLegacySQL: true}, // UseLegacySQL without query
{UseStandardSQL: true}, // UseStandardSQL without query
// read-only fields
{FullID: "x"},
{Type: "x"},
{CreationTime: aTime},
{LastModifiedTime: aTime},
{NumBytes: 1},
{NumLongTermBytes: 1},
{NumRows: 1},
{StreamingBuffer: &StreamingBuffer{}},
{ETag: "x"},
// expiration time outside allowable range is invalid
// See https://godoc.org/time#Time.UnixNano
{ExpirationTime: time.Date(1677, 9, 21, 0, 12, 43, 145224192, time.UTC).Add(-1)},
Expand Down
Loading