2019-01-24 16:26:36 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-08 14:19:42 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
2018-12-10 12:29:01 +00:00
|
|
|
"context"
|
|
|
|
|
2019-01-08 14:05:14 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-03-06 15:54:48 +00:00
|
|
|
"storj.io/storj/pkg/accounting"
|
2019-01-15 13:03:24 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2019-01-16 20:23:28 +00:00
|
|
|
dbx "storj.io/storj/satellite/satellitedb/dbx"
|
2018-11-08 14:19:42 +00:00
|
|
|
)
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
// ConsoleDB contains access to different satellite databases
|
|
|
|
type ConsoleDB struct {
|
2018-11-08 14:19:42 +00:00
|
|
|
db *dbx.DB
|
2018-12-10 12:29:01 +00:00
|
|
|
tx *dbx.Tx
|
|
|
|
|
|
|
|
methods dbx.Methods
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 14:50:47 +00:00
|
|
|
// Users is getter a for Users repository
|
2019-01-15 13:03:24 +00:00
|
|
|
func (db *ConsoleDB) Users() console.Users {
|
2018-12-10 12:29:01 +00:00
|
|
|
return &users{db.methods}
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 14:50:47 +00:00
|
|
|
// Projects is a getter for Projects repository
|
2019-01-15 13:03:24 +00:00
|
|
|
func (db *ConsoleDB) Projects() console.Projects {
|
2018-12-10 12:29:01 +00:00
|
|
|
return &projects{db.methods}
|
2018-11-13 08:27:42 +00:00
|
|
|
}
|
|
|
|
|
2018-11-20 14:50:47 +00:00
|
|
|
// ProjectMembers is a getter for ProjectMembers repository
|
2019-01-15 13:03:24 +00:00
|
|
|
func (db *ConsoleDB) ProjectMembers() console.ProjectMembers {
|
2018-12-28 12:07:35 +00:00
|
|
|
return &projectMembers{db.methods, db.db}
|
2018-11-20 14:50:47 +00:00
|
|
|
}
|
|
|
|
|
2018-12-26 14:00:53 +00:00
|
|
|
// APIKeys is a getter for APIKeys repository
|
2019-01-15 13:03:24 +00:00
|
|
|
func (db *ConsoleDB) APIKeys() console.APIKeys {
|
2018-12-26 14:00:53 +00:00
|
|
|
return &apikeys{db.methods}
|
|
|
|
}
|
|
|
|
|
2019-03-06 15:54:48 +00:00
|
|
|
// BucketUsage is a getter for accounting.BucketUsage repository
|
|
|
|
func (db *ConsoleDB) BucketUsage() accounting.BucketUsage {
|
|
|
|
return &bucketusage{db.methods}
|
|
|
|
}
|
|
|
|
|
2019-03-19 17:55:43 +00:00
|
|
|
// RegistrationTokens is a getter for RegistrationTokens repository
|
|
|
|
func (db *ConsoleDB) RegistrationTokens() console.RegistrationTokens {
|
|
|
|
return ®istrationTokens{db.methods}
|
|
|
|
}
|
|
|
|
|
2018-12-10 12:29:01 +00:00
|
|
|
// BeginTx is a method for opening transaction
|
2019-01-15 13:03:24 +00:00
|
|
|
func (db *ConsoleDB) BeginTx(ctx context.Context) (console.DBTx, error) {
|
2018-12-10 12:29:01 +00:00
|
|
|
if db.db == nil {
|
|
|
|
return nil, errs.New("DB is not initialized!")
|
|
|
|
}
|
|
|
|
|
|
|
|
tx, err := db.db.Open(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &DBTx{
|
2019-01-15 13:03:24 +00:00
|
|
|
ConsoleDB: &ConsoleDB{
|
2018-12-10 12:29:01 +00:00
|
|
|
tx: tx,
|
|
|
|
methods: tx,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// DBTx extends Database with transaction scope
|
|
|
|
type DBTx struct {
|
2019-01-15 13:03:24 +00:00
|
|
|
*ConsoleDB
|
2018-12-10 12:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// 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()
|
|
|
|
}
|