-
Notifications
You must be signed in to change notification settings - Fork 0
/
models.go
60 lines (52 loc) · 1.31 KB
/
models.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
59
60
package bqmigrator
import (
"context"
"fmt"
"cloud.google.com/go/bigquery"
)
type Dataset struct {
Name string
Tables []string
}
type Target struct {
ProjectID string
Datasets []Dataset
}
type Migration struct {
Name string
number int
Description string
Target Target
Setup func(ctx context.Context, bigquery *bigquery.Client, migration *Migration) error
Run func(ctx context.Context, bigquery *bigquery.Client, migration Migration) error
}
func (d *Dataset) getDatasetString() string {
var tables string
for _, table := range d.Tables {
if tables == "" {
tables = fmt.Sprintf("'%s'", table)
continue
}
tables = fmt.Sprintf("%s, '%s'", tables, table)
}
tableStr := fmt.Sprintf("ARRAY<STRING>[%s]", tables)
return fmt.Sprintf("STRUCT<STRING, ARRAY<STRING>>('%s', %s)", d.Name, tableStr)
}
type latestMigration struct {
migrationNumber int `bigquery:"migration_number"`
}
func (c *latestMigration) Load(v []bigquery.Value, s bigquery.Schema) error {
for i, schema := range s {
switch schema.Name {
case "migration_number":
migrationNumber, ok := v[i].(int64)
if !ok {
return fmt.Errorf("migration_number is not an int64")
}
c.migrationNumber = int(migrationNumber)
default:
return fmt.Errorf("unknown field: [%s]", schema.Name)
}
}
return nil
}