0c5e381434
Transactions in our code that might need to work against CockroachDB need to be retried in the event of a retryable error. The transaction helper functions in dbutil do that automatically. I am changing this code to use those helpers instead. I also fleshed out consoledb_test.go to do actual inserts and gets to make sure things were working correctly. Change-Id: I089bf4c776d15dc8578080e26760bd6dff4beec9
102 lines
2.5 KiB
Go
102 lines
2.5 KiB
Go
// Copyright (C) 2019 Storj Labs, Inc.
|
|
// See LICENSE for copying information.
|
|
|
|
package satellitedb
|
|
|
|
import (
|
|
"context"
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
"storj.io/storj/satellite/console"
|
|
dbx "storj.io/storj/satellite/satellitedb/dbx"
|
|
)
|
|
|
|
// ensures that ConsoleDB implements console.DB.
|
|
var _ console.DB = (*ConsoleDB)(nil)
|
|
|
|
// ConsoleDB contains access to different satellite databases.
|
|
type ConsoleDB struct {
|
|
db *satelliteDB
|
|
tx *dbx.Tx
|
|
|
|
methods dbx.Methods
|
|
}
|
|
|
|
// Users is getter a for Users repository.
|
|
func (db *ConsoleDB) Users() console.Users {
|
|
return &users{db.methods}
|
|
}
|
|
|
|
// Projects is a getter for Projects repository.
|
|
func (db *ConsoleDB) Projects() console.Projects {
|
|
return &projects{db.methods}
|
|
}
|
|
|
|
// ProjectMembers is a getter for ProjectMembers repository.
|
|
func (db *ConsoleDB) ProjectMembers() console.ProjectMembers {
|
|
return &projectMembers{db.methods, db.db}
|
|
}
|
|
|
|
// APIKeys is a getter for APIKeys repository.
|
|
func (db *ConsoleDB) APIKeys() console.APIKeys {
|
|
return &apikeys{db.methods, db.db}
|
|
}
|
|
|
|
// RegistrationTokens is a getter for RegistrationTokens repository.
|
|
func (db *ConsoleDB) RegistrationTokens() console.RegistrationTokens {
|
|
return ®istrationTokens{db.methods}
|
|
}
|
|
|
|
// ResetPasswordTokens is a getter for ResetPasswordTokens repository.
|
|
func (db *ConsoleDB) ResetPasswordTokens() console.ResetPasswordTokens {
|
|
return &resetPasswordTokens{db.methods}
|
|
}
|
|
|
|
// UserCredits is a getter for console.UserCredits repository.
|
|
func (db *ConsoleDB) UserCredits() console.UserCredits {
|
|
return &usercredits{db.db, db.tx}
|
|
}
|
|
|
|
// WithTx is a method for executing and retrying transaction.
|
|
func (db *ConsoleDB) WithTx(ctx context.Context, fn func(context.Context, console.DBTx) error) error {
|
|
if db.db == nil {
|
|
return errs.New("DB is not initialized!")
|
|
}
|
|
|
|
return db.db.WithTx(ctx, func(ctx context.Context, tx *dbx.Tx) error {
|
|
dbTx := &DBTx{
|
|
ConsoleDB: &ConsoleDB{
|
|
// Need to expose dbx.DB for when database methods need access to check database driver type
|
|
db: db.db,
|
|
tx: tx,
|
|
methods: tx,
|
|
},
|
|
}
|
|
return fn(ctx, dbTx)
|
|
})
|
|
}
|
|
|
|
// DBTx extends Database with transaction scope.
|
|
type DBTx struct {
|
|
*ConsoleDB
|
|
}
|
|
|
|
// Commit is a method for committing and closing transaction.
|
|
func (db *DBTx) Commit() error {
|
|
if db.tx == nil {
|
|
return errs.New("begin transaction before commit it!")
|
|
}
|
|
|
|
return db.tx.Commit()
|
|
}
|
|
|
|
// Rollback is a method for rollback and closing transaction.
|
|
func (db *DBTx) Rollback() error {
|
|
if db.tx == nil {
|
|
return errs.New("begin transaction before rollback it!")
|
|
}
|
|
|
|
return db.tx.Rollback()
|
|
}
|