go.chromium.org/luci@v0.0.0-20240309015107-7cdc2e660f33/tools/cmd/bqschemaupdater/local_table_store.go (about)

     1  // Copyright 2018 The LUCI Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //      http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package main
    16  
    17  import (
    18  	"context"
    19  	"net/http"
    20  
    21  	"cloud.google.com/go/bigquery"
    22  
    23  	"google.golang.org/api/googleapi"
    24  )
    25  
    26  type tableKey struct {
    27  	datasetID, tableID string
    28  }
    29  
    30  type localTableStore map[tableKey]*bigquery.TableMetadata
    31  
    32  func (ts localTableStore) getTableMetadata(ctx context.Context, datasetID, tableID string) (*bigquery.TableMetadata, error) {
    33  	if md, ok := ts[tableKey{datasetID, tableID}]; ok {
    34  		return md, nil
    35  	}
    36  	return nil, &googleapi.Error{Code: http.StatusNotFound}
    37  }
    38  
    39  func (ts localTableStore) createTable(ctx context.Context, datasetID, tableID string, md *bigquery.TableMetadata) error {
    40  	key := tableKey{datasetID, tableID}
    41  	if _, ok := ts[key]; ok {
    42  		return &googleapi.Error{Code: http.StatusConflict}
    43  	}
    44  	var cpy bigquery.TableMetadata
    45  	if md != nil {
    46  		cpy = *md
    47  	}
    48  	ts[key] = &cpy
    49  	return nil
    50  }
    51  
    52  func (ts localTableStore) updateTable(ctx context.Context, datasetID, tableID string, toUpdate bigquery.TableMetadataToUpdate) error {
    53  	md, ok := ts[tableKey{datasetID, tableID}]
    54  	if !ok {
    55  		return &googleapi.Error{Code: http.StatusNotFound}
    56  	}
    57  	if toUpdate.Description != nil {
    58  		md.Description = toUpdate.Description.(string)
    59  	}
    60  	if toUpdate.Name != nil {
    61  		md.Name = toUpdate.Name.(string)
    62  	}
    63  	if toUpdate.Schema != nil {
    64  		md.Schema = toUpdate.Schema
    65  	}
    66  	return nil
    67  }