-
Notifications
You must be signed in to change notification settings - Fork 307
/
local.go
101 lines (78 loc) · 2.82 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
95
96
97
98
99
100
101
package schemarepository
import (
"fmt"
"github.com/rudderlabs/rudder-server/warehouse/internal/model"
warehouseutils "github.com/rudderlabs/rudder-server/warehouse/utils"
)
type LocalSchemaRepository struct {
warehouse model.Warehouse
uploader warehouseutils.Uploader
}
func NewLocalSchemaRepository(wh model.Warehouse, uploader warehouseutils.Uploader) (*LocalSchemaRepository, error) {
ls := LocalSchemaRepository{
warehouse: wh,
uploader: uploader,
}
return &ls, nil
}
func (ls *LocalSchemaRepository) FetchSchema(_ model.Warehouse) (model.Schema, model.Schema, error) {
schema, err := ls.uploader.GetLocalSchema()
if err != nil {
return model.Schema{}, model.Schema{}, fmt.Errorf("fetching local schema: %w", err)
}
return schema, model.Schema{}, nil
}
func (*LocalSchemaRepository) CreateSchema() (err error) {
return nil
}
func (ls *LocalSchemaRepository) CreateTable(tableName string, columnMap model.TableSchema) (err error) {
// fetch schema from local db
schema, err := ls.uploader.GetLocalSchema()
if err != nil {
return fmt.Errorf("fetching local schema: %w", 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.uploader.GetLocalSchema()
if err != nil {
return fmt.Errorf("fetching local schema: %w", 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) (model.AlterTableResponse, error) {
// fetch schema from local db
schema, err := ls.uploader.GetLocalSchema()
if err != nil {
return model.AlterTableResponse{}, fmt.Errorf("fetching local schema: %w", err)
}
// check if table exists
if _, ok := schema[tableName]; !ok {
return model.AlterTableResponse{}, fmt.Errorf("failed to add column: table %s does not exist", tableName)
}
// check if column exists
if _, ok := schema[tableName][columnName]; !ok {
return model.AlterTableResponse{}, fmt.Errorf("failed to alter column: column %s does not exist in table %s", columnName, tableName)
}
schema[tableName][columnName] = columnType
// update schema
return model.AlterTableResponse{}, ls.uploader.UpdateLocalSchema(schema)
}
func (*LocalSchemaRepository) RefreshPartitions(_ string, _ []warehouseutils.LoadFile) error {
return nil
}