-
Notifications
You must be signed in to change notification settings - Fork 111
/
registry.go
165 lines (140 loc) · 4.43 KB
/
registry.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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
package sqlite
import (
"context"
"encoding/json"
"fmt"
"time"
"github.com/google/uuid"
"github.com/rilldata/rill/runtime/drivers"
)
// FindInstances implements drivers.RegistryStore.
func (c *connection) FindInstances(ctx context.Context) ([]*drivers.Instance, error) {
return c.findInstances(ctx, "")
}
// FindInstance implements drivers.RegistryStore.
func (c *connection) FindInstance(ctx context.Context, id string) (*drivers.Instance, error) {
is, err := c.findInstances(ctx, "WHERE id = $1", id)
if err != nil {
return nil, err
}
if len(is) == 0 {
return nil, drivers.ErrNotFound
}
return is[0], nil
}
func (c *connection) findInstances(_ context.Context, whereClause string, args ...any) ([]*drivers.Instance, error) {
// Override ctx because sqlite sometimes segfaults on context cancellation
ctx := context.Background()
sql := fmt.Sprintf("SELECT id, olap_driver, olap_dsn, repo_driver, repo_dsn, embed_catalog, created_on, updated_on, variables, project_variables, ingestion_limit_bytes FROM instances %s ORDER BY id", whereClause)
rows, err := c.db.QueryxContext(ctx, sql, args...)
if err != nil {
return nil, err
}
defer rows.Close()
var res []*drivers.Instance
for rows.Next() {
// sqlite doesn't support maps need to read as bytes and convert to map
var variables, projectVariables []byte
i := &drivers.Instance{}
err := rows.Scan(&i.ID, &i.OLAPDriver, &i.OLAPDSN, &i.RepoDriver, &i.RepoDSN, &i.EmbedCatalog, &i.CreatedOn, &i.UpdatedOn, &variables, &projectVariables, &i.IngestionLimitBytes)
if err != nil {
return nil, err
}
i.Variables, err = mapFromJSON(variables)
if err != nil {
return nil, err
}
i.ProjectVariables, err = mapFromJSON(projectVariables)
if err != nil {
return nil, err
}
res = append(res, i)
}
return res, nil
}
// CreateInstance implements drivers.RegistryStore.
func (c *connection) CreateInstance(_ context.Context, inst *drivers.Instance) error {
// Override ctx because sqlite sometimes segfaults on context cancellation
ctx := context.Background()
if inst.ID == "" {
inst.ID = uuid.NewString()
}
// sqlite doesn't support maps need to convert to json and write as bytes array
variables, err := mapToJSON(inst.Variables)
if err != nil {
return err
}
projectVariables, err := mapToJSON(inst.ProjectVariables)
if err != nil {
return err
}
now := time.Now()
_, err = c.db.ExecContext(
ctx,
"INSERT INTO instances(id, olap_driver, olap_dsn, repo_driver, repo_dsn, embed_catalog, created_on, updated_on, variables, project_variables, ingestion_limit_bytes) "+
"VALUES ($1, $2, $3, $4, $5, $6, $7, $7, $8, $9, $10)",
inst.ID,
inst.OLAPDriver,
inst.OLAPDSN,
inst.RepoDriver,
inst.RepoDSN,
inst.EmbedCatalog,
now,
variables,
projectVariables,
inst.IngestionLimitBytes,
)
if err != nil {
return err
}
// We assign manually instead of using RETURNING because it doesn't work for timestamps in SQLite
inst.CreatedOn = now
inst.UpdatedOn = now
return nil
}
// CreateInstance implements drivers.RegistryStore.
func (c *connection) EditInstance(_ context.Context, inst *drivers.Instance) error {
// Override ctx because sqlite sometimes segfaults on context cancellation
ctx := context.Background()
// sqlite doesn't support maps need to convert to json and write as bytes array
variables, err := mapToJSON(inst.Variables)
if err != nil {
return err
}
now := time.Now()
_, err = c.db.ExecContext(
ctx,
"UPDATE instances SET olap_driver = $2, olap_dsn = $3, repo_driver = $4, repo_dsn = $5, embed_catalog = $6, variables = $7, updated_on = $8, ingestion_limit_bytes = $9 "+
"WHERE id = $1",
inst.ID,
inst.OLAPDriver,
inst.OLAPDSN,
inst.RepoDriver,
inst.RepoDSN,
inst.EmbedCatalog,
variables,
now,
inst.IngestionLimitBytes,
)
if err != nil {
return err
}
// We assign manually instead of using RETURNING because it doesn't work for timestamps in SQLite
inst.UpdatedOn = now
return nil
}
// DeleteInstance implements drivers.RegistryStore.
func (c *connection) DeleteInstance(_ context.Context, id string) error {
// Override ctx because sqlite sometimes segfaults on context cancellation
ctx := context.Background()
_, err := c.db.ExecContext(ctx, "DELETE FROM instances WHERE id=$1", id)
return err
}
func mapToJSON(data map[string]string) ([]byte, error) {
return json.Marshal(data)
}
func mapFromJSON(data []byte) (map[string]string, error) {
var m map[string]string
err := json.Unmarshal(data, &m)
return m, err
}