2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-12-05 09:35:50 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
2020-01-15 02:29:51 +00:00
|
|
|
package dbx
|
2018-12-05 09:35:50 +00:00
|
|
|
|
2018-12-07 14:46:42 +00:00
|
|
|
import (
|
2019-03-12 13:08:23 +00:00
|
|
|
"context"
|
2019-04-23 12:13:57 +01:00
|
|
|
"fmt"
|
2019-03-12 13:08:23 +00:00
|
|
|
|
2018-12-07 14:46:42 +00:00
|
|
|
"github.com/zeebo/errs"
|
2019-12-17 20:07:54 +00:00
|
|
|
"gopkg.in/spacemonkeygo/monkit.v2"
|
2020-01-08 13:40:19 +00:00
|
|
|
|
2020-01-07 22:20:20 +00:00
|
|
|
"storj.io/storj/private/dbutil/txutil"
|
2020-01-03 19:13:57 +00:00
|
|
|
|
|
|
|
// load our cockroach sql driver for anywhere that uses this dbx.Open
|
|
|
|
_ "storj.io/storj/private/dbutil/cockroachutil"
|
2018-12-07 14:46:42 +00:00
|
|
|
)
|
|
|
|
|
2020-01-15 07:25:26 +00:00
|
|
|
//go:generate dbx schema -d postgres -d cockroach satellitedb.dbx .
|
2020-01-15 02:29:51 +00:00
|
|
|
//go:generate dbx golang -d postgres -d cockroach -p dbx -t templates satellitedb.dbx .
|
2020-01-15 07:25:26 +00:00
|
|
|
//go:generate bash -c "( echo '//lint:file-ignore * generated file'; cat satellitedb.dbx.go ) > satellitedb.dbx.go.tmp && mv satellitedb.dbx.go{.tmp,}"
|
|
|
|
//go:generate perl -p0i -e "s,^(\\s*\"github.com/lib/pq\")\\n\\n\\1,\\1,gm" satellitedb.dbx.go
|
2018-12-17 20:14:16 +00:00
|
|
|
|
2019-12-17 20:07:54 +00:00
|
|
|
var mon = monkit.Package()
|
|
|
|
|
2018-12-07 14:46:42 +00:00
|
|
|
func init() {
|
|
|
|
// catch dbx errors
|
2019-04-23 12:13:57 +01:00
|
|
|
class := errs.Class("satellitedb")
|
2018-12-17 20:14:16 +00:00
|
|
|
WrapErr = func(e *Error) error {
|
2019-04-01 21:14:58 +01:00
|
|
|
switch e.Code {
|
|
|
|
case ErrorCode_NoRows:
|
2018-12-17 20:14:16 +00:00
|
|
|
return e.Err
|
2019-04-01 21:14:58 +01:00
|
|
|
case ErrorCode_ConstraintViolation:
|
2019-04-23 12:13:57 +01:00
|
|
|
return class.Wrap(&constraintError{e.Constraint, e.Err})
|
2018-12-17 20:14:16 +00:00
|
|
|
}
|
2019-04-23 12:13:57 +01:00
|
|
|
return class.Wrap(e)
|
2018-12-17 20:14:16 +00:00
|
|
|
}
|
2018-12-07 14:46:42 +00:00
|
|
|
}
|
2019-03-12 13:08:23 +00:00
|
|
|
|
2019-04-23 12:13:57 +01:00
|
|
|
// Unwrap returns the underlying error.
|
2019-04-23 20:48:57 +01:00
|
|
|
func (e *Error) Unwrap() error { return e.Err }
|
|
|
|
|
|
|
|
// Cause returns the underlying error.
|
|
|
|
func (e *Error) Cause() error { return e.Err }
|
2019-04-23 12:13:57 +01:00
|
|
|
|
|
|
|
type constraintError struct {
|
|
|
|
constraint string
|
|
|
|
err error
|
|
|
|
}
|
|
|
|
|
|
|
|
// Unwrap returns the underlying error.
|
2019-04-23 20:48:57 +01:00
|
|
|
func (err *constraintError) Unwrap() error { return err.err }
|
|
|
|
|
|
|
|
// Cause returns the underlying error.
|
|
|
|
func (err *constraintError) Cause() error { return err.err }
|
2019-04-23 12:13:57 +01:00
|
|
|
|
|
|
|
// Error implements the error interface.
|
|
|
|
func (err *constraintError) Error() string {
|
|
|
|
return fmt.Sprintf("violates constraint %q: %v", err.constraint, err.err)
|
|
|
|
}
|
|
|
|
|
2019-03-12 13:08:23 +00:00
|
|
|
// WithTx wraps DB code in a transaction
|
|
|
|
func (db *DB) WithTx(ctx context.Context, fn func(context.Context, *Tx) error) (err error) {
|
|
|
|
tx, err := db.Open(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-01-15 07:25:26 +00:00
|
|
|
return txutil.ExecuteInTx(ctx, db.Driver(), tx.Tx, func() error {
|
2020-01-07 22:20:20 +00:00
|
|
|
return fn(ctx, tx)
|
|
|
|
})
|
2019-03-12 13:08:23 +00:00
|
|
|
}
|