-
Notifications
You must be signed in to change notification settings - Fork 4
/
bq-utils.go
58 lines (48 loc) · 1.31 KB
/
bq-utils.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
package statistics
import (
"context"
"fmt"
"net/http"
"cloud.google.com/go/bigquery"
"github.com/ansel1/merry/v2"
"google.golang.org/api/googleapi"
"gopkg.in/guregu/null.v4"
)
func bqTableExistsOrCreate(ctx context.Context, dataset *bigquery.Dataset, tableName string) error {
t := dataset.Table(tableName)
_, err := t.Metadata(ctx)
if err == nil {
// Table Exists
return nil
}
if e, ok := err.(*googleapi.Error); !ok || e.Code != http.StatusNotFound {
// Unknown error
return merry.Wrap(err)
}
// Table does not exist
schemaBytes, err := bqMigrationsJson.ReadFile(fmt.Sprintf("bq-schema/%s.json", tableName))
if err != nil {
return merry.Wrap(err)
}
schema, err := bigquery.SchemaFromJSON(schemaBytes)
if err != nil {
return merry.Wrap(err)
}
tm := &bigquery.TableMetadata{
RequirePartitionFilter: false,
}
tm.Name = tableName
tm.Clustering = &bigquery.Clustering{Fields: []string{"Updated"}}
tm.TimePartitioning = &bigquery.TimePartitioning{Field: "Updated", Type: bigquery.YearPartitioningType}
tm.Schema = schema.Relax() // Make sure everyting is nullable. This is ok for stats
err = t.Create(ctx, tm)
return merry.Wrap(err)
}
func nullIntToBQNullString(i null.Int) bigquery.NullString {
var s *string
if i.Valid {
v := fmt.Sprintf("%d", i.Int64)
s = &v
}
return nullStr(s)
}