/
store.go
342 lines (312 loc) · 10.1 KB
/
store.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
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
// Copyright © 2019 The Things Network Foundation, The Things Industries B.V.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package store
import (
"context"
"fmt"
"net/url"
"os"
"path/filepath"
"regexp"
"runtime/debug"
"runtime/trace"
"strings"
"time"
"github.com/jinzhu/gorm"
"github.com/lib/pq"
"go.thethings.network/lorawan-stack/v3/pkg/errors"
"go.thethings.network/lorawan-stack/v3/pkg/log"
"go.thethings.network/lorawan-stack/v3/pkg/ttnpb"
)
func newStore(db *gorm.DB) *store { return &store{DB: db} }
type store struct {
DB *gorm.DB
}
func (s *store) query(ctx context.Context, model interface{}, funcs ...func(*gorm.DB) *gorm.DB) *gorm.DB {
query := s.DB.Model(model).Scopes(withContext(ctx))
if len(funcs) > 0 {
query = query.Scopes(funcs...)
}
return query
}
func (s *store) findEntity(ctx context.Context, entityID ttnpb.Identifiers, fields ...string) (modelInterface, error) {
model := modelForID(entityID)
query := s.query(ctx, model, withID(entityID))
if len(fields) == 1 && fields[0] == "id" {
fields[0] = s.DB.NewScope(model).TableName() + ".id"
}
if len(fields) > 0 {
query = query.Select(fields)
}
if err := query.First(model).Error; err != nil {
if gorm.IsRecordNotFoundError(err) {
return nil, errNotFoundForID(entityID)
}
return nil, convertError(err)
}
return model, nil
}
func (s *store) createEntity(ctx context.Context, model interface{}) error {
if model, ok := model.(modelInterface); ok {
model.SetContext(ctx)
}
return s.DB.Create(model).Error
}
func (s *store) updateEntity(ctx context.Context, model interface{}, columns ...string) error {
query := s.query(ctx, model)
query = query.Select(append(columns, "updated_at"))
return query.Save(model).Error
}
func (s *store) deleteEntity(ctx context.Context, entityID ttnpb.Identifiers) error {
model, err := s.findEntity(ctx, entityID, "id")
if err != nil {
return err
}
return s.DB.Delete(model).Error
}
var (
errDatabase = errors.DefineInternal("database", "database error")
errAlreadyExists = errors.DefineAlreadyExists("already_exists", "entity already exists")
// ErrIDTaken is returned when an entity can not be created because the ID is already taken.
ErrIDTaken = errors.DefineAlreadyExists("id_taken", "ID already taken")
// ErrEUITaken is returned when an entity can not be created because the EUI is already taken.
ErrEUITaken = errors.DefineAlreadyExists("eui_taken", "EUI already taken")
)
var uniqueViolationRegex = regexp.MustCompile(`duplicate key value( .+)? violates unique constraint "([a-z_]+)"`)
func convertError(err error) error {
switch err {
case nil, context.Canceled, context.DeadlineExceeded:
return err
}
if ttnErr, ok := errors.From(err); ok {
return ttnErr
}
if pqErr, ok := err.(*pq.Error); ok {
switch pqErr.Code.Name() {
case "unique_violation":
if match := uniqueViolationRegex.FindStringSubmatch(pqErr.Message); match != nil {
switch {
case strings.HasSuffix(match[2], "_id_index"):
return ErrIDTaken.WithCause(err)
case strings.HasSuffix(match[2], "_eui_index"):
return ErrEUITaken.WithCause(err)
default:
return errAlreadyExists.WithCause(err).WithAttributes("index", match[2])
}
}
return errAlreadyExists.WithCause(err)
default:
return errDatabase.WithCause(err).WithAttributes("code", pqErr.Code.Name())
}
}
return errDatabase.WithCause(err)
}
// Open opens a new database connection.
func Open(ctx context.Context, dsn string) (*gorm.DB, error) {
dbURI, err := url.Parse(dsn)
if err != nil {
return nil, err
}
dbName := strings.TrimPrefix(dbURI.Path, "/")
db, err := gorm.Open("postgres", dsn)
if err != nil {
return nil, err
}
db = db.Set("db:name", dbName)
var dbVersion string
err = db.Raw("SELECT version()").Row().Scan(&dbVersion)
if err != nil {
return nil, err
}
db = db.Set("db:version", dbVersion)
switch {
case strings.Contains(dbVersion, "CockroachDB"):
db = db.Set("db:kind", "CockroachDB")
case strings.Contains(dbVersion, "PostgreSQL"):
db = db.Set("db:kind", "PostgreSQL")
}
SetLogger(db, log.FromContext(ctx))
return db, nil
}
// Initialize initializes the database.
func Initialize(db *gorm.DB) error {
if dbKind, ok := db.Get("db:kind"); ok {
switch dbKind {
case "CockroachDB":
if dbName, ok := db.Get("db:name"); ok {
if err := db.Exec(fmt.Sprintf("CREATE DATABASE IF NOT EXISTS %s;", dbName)).Error; err != nil {
return err
}
}
case "PostgreSQL":
if err := db.Exec("CREATE EXTENSION IF NOT EXISTS pgcrypto").Error; err != nil {
return err
}
}
}
return nil
}
// ErrTransactionRecovered is returned when a panic is caught from a SQL transaction.
var ErrTransactionRecovered = errors.DefineInternal("transaction_recovered", "Internal Server Error")
// Transact executes f in a db transaction.
func Transact(ctx context.Context, db *gorm.DB, f func(db *gorm.DB) error) (err error) {
defer trace.StartRegion(ctx, "database transaction").End()
tx := db.Begin()
if tx.Error != nil {
return convertError(tx.Error)
}
defer func() {
if p := recover(); p != nil {
fmt.Fprintln(os.Stderr, p)
os.Stderr.Write(debug.Stack())
if pErr, ok := p.(error); ok {
switch pErr {
case context.Canceled, context.DeadlineExceeded:
err = pErr
default:
err = ErrTransactionRecovered.WithCause(pErr)
}
} else {
err = ErrTransactionRecovered.WithAttributes("panic", p)
}
}
if err != nil {
tx.Rollback()
} else {
err = tx.Commit().Error
}
err = convertError(err)
}()
SetLogger(tx, log.FromContext(ctx).WithField("namespace", "db"))
return f(tx)
}
func entityTypeForID(id ttnpb.Identifiers) string {
return strings.Replace(id.EntityType(), " ", "_", -1)
}
func modelForEntityType(entityType string) modelInterface {
switch entityType {
case "application":
return &Application{}
case "client":
return &Client{}
case "end_device":
return &EndDevice{}
case "gateway":
return &Gateway{}
case "organization":
return &Organization{}
case "user":
return &User{}
default:
panic(fmt.Sprintf("can't find model for entity type %s", entityType))
}
}
func modelForID(id ttnpb.Identifiers) modelInterface {
return modelForEntityType(entityTypeForID(id))
}
var (
errApplicationNotFound = errors.DefineNotFound("application_not_found", "application `{application_id}` not found")
errClientNotFound = errors.DefineNotFound("client_not_found", "client `{client_id}` not found")
errGatewayNotFound = errors.DefineNotFound("gateway_not_found", "gateway `{gateway_id}` not found")
errEndDeviceNotFound = errors.DefineNotFound("end_device_not_found", "end device `{application_id}:{device_id}` not found")
errOrganizationNotFound = errors.DefineNotFound("organization_not_found", "organization `{organization_id}` not found")
errUserNotFound = errors.DefineNotFound("user_not_found", "user `{user_id}` not found")
errSessionNotFound = errors.DefineNotFound("session_not_found", "session `{session_id}` for user `{user_id}` not found")
errAuthorizationNotFound = errors.DefineNotFound("authorization_not_found", "authorization of `{user_id}` for `{client_id}` not found")
errAuthorizationCodeNotFound = errors.DefineNotFound("authorization_code_not_found", "authorization code not found")
errAccessTokenNotFound = errors.DefineNotFound("access_token_not_found", "access token not found")
errAPIKeyNotFound = errors.DefineNotFound("api_key_not_found", "API key not found")
errMigrationNotFound = errors.DefineNotFound("migration_not_found", "migration not found")
)
func errNotFoundForID(id ttnpb.Identifiers) error {
switch t := entityTypeForID(id); t {
case "application":
return errApplicationNotFound.WithAttributes("application_id", id.IDString())
case "client":
return errClientNotFound.WithAttributes("client_id", id.IDString())
case "end_device":
appID, devID := splitEndDeviceIDString(id.IDString())
return errEndDeviceNotFound.WithAttributes("application_id", appID, "device_id", devID)
case "gateway":
return errGatewayNotFound.WithAttributes("gateway_id", id.IDString())
case "organization":
return errOrganizationNotFound.WithAttributes("organization_id", id.IDString())
case "user":
return errUserNotFound.WithAttributes("user_id", id.IDString())
default:
panic(fmt.Sprintf("can't find errNotFound for entity type %s", t))
}
}
// SetLogger sets the database logger.
func SetLogger(db *gorm.DB, log log.Interface) {
db.SetLogger(logger{Interface: log})
}
type logger struct {
log.Interface
}
// Print implements the gorm.logger interface.
func (l logger) Print(v ...interface{}) {
if len(v) < 3 {
l.Error(fmt.Sprint(v...))
return
}
logger := l.Interface
if source, ok := v[1].(string); ok {
logger = logger.WithField("source", filepath.Base(source))
} else {
l.Error(fmt.Sprint(v...))
return
}
switch v[0] {
case "log", "error":
if err, ok := v[2].(error); ok {
err = convertError(err)
if errors.IsAlreadyExists(err) {
return // no problem.
}
logger.WithError(err).Error("Database error")
return
}
logger.Error(fmt.Sprint(v[2:]...))
return
case "sql":
if len(v) != 6 {
return
}
duration, _ := v[2].(time.Duration)
query, _ := v[3].(string)
values, _ := v[4].([]interface{})
rows, _ := v[5].(int64)
logger.WithFields(log.Fields(
"duration", duration,
"query", query,
"values", values,
"rows", rows,
)).Debug("Run database query")
default:
l.Error(fmt.Sprint(v...))
}
}
func cleanFields(fields ...string) []string {
seen := make(map[string]struct{}, len(fields))
out := make([]string, 0, len(fields))
for _, field := range fields {
if _, ok := seen[field]; ok {
continue
}
seen[field] = struct{}{}
out = append(out, field)
}
return out
}