-
Notifications
You must be signed in to change notification settings - Fork 311
/
local.go
94 lines (74 loc) · 2.42 KB
/
local.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
package schemarepository
import (
"fmt"
warehouseutils "github.com/rudderlabs/rudder-server/warehouse/utils"
)
type LocalSchemaRepository struct {
warehouse warehouseutils.Warehouse
uploader warehouseutils.UploaderI
}
func NewLocalSchemaRepository(wh warehouseutils.Warehouse, uploader warehouseutils.UploaderI) (*LocalSchemaRepository, error) {
ls := LocalSchemaRepository{
warehouse: wh,
uploader: uploader,
}
return &ls, nil
}
func (ls *LocalSchemaRepository) FetchSchema(_ warehouseutils.Warehouse) (warehouseutils.SchemaT, warehouseutils.SchemaT, error) {
schema := ls.uploader.GetLocalSchema()
if schema == nil {
schema = warehouseutils.SchemaT{}
}
return schema, warehouseutils.SchemaT{}, nil
}
func (*LocalSchemaRepository) CreateSchema() (err error) {
return nil
}
func (ls *LocalSchemaRepository) CreateTable(tableName string, columnMap map[string]string) (err error) {
// fetch schema from local db
schema, _, err := ls.FetchSchema(ls.warehouse)
if err != nil {
return err
}
if _, ok := schema[tableName]; ok {
return fmt.Errorf("failed to create table: table %s already exists", tableName)
}
// add table to schema
schema[tableName] = columnMap
// update schema
return ls.uploader.UpdateLocalSchema(schema)
}
func (ls *LocalSchemaRepository) AddColumns(tableName string, columnsInfo []warehouseutils.ColumnInfo) (err error) {
// fetch schema from local db
schema, _, err := ls.FetchSchema(ls.warehouse)
if err != nil {
return err
}
// check if table exists
if _, ok := schema[tableName]; !ok {
return fmt.Errorf("failed to add column: table %s does not exist", tableName)
}
for _, columnInfo := range columnsInfo {
schema[tableName][columnInfo.Name] = columnInfo.Type
}
// update schema
return ls.uploader.UpdateLocalSchema(schema)
}
func (ls *LocalSchemaRepository) AlterColumn(tableName, columnName, columnType string) (err error) {
// fetch schema from local db
schema, _, err := ls.FetchSchema(ls.warehouse)
if err != nil {
return err
}
// check if table exists
if _, ok := schema[tableName]; !ok {
return fmt.Errorf("failed to add column: table %s does not exist", tableName)
}
// check if column exists
if _, ok := schema[tableName][columnName]; !ok {
return fmt.Errorf("failed to alter column: column %s does not exist in table %s", columnName, tableName)
}
schema[tableName][columnName] = columnType
// update schema
return ls.uploader.UpdateLocalSchema(schema)
}