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-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-11-04 14:37:39 +00:00
|
|
|
// ensures that ConsoleDB implements console.DB.
|
|
|
|
var _ console.DB = (*ConsoleDB)(nil)
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// ConsoleDB contains access to different satellite databases.
|
2019-01-15 13:03:24 +00:00
|
|
|
type ConsoleDB struct {
|
2019-12-14 02:29:54 +00:00
|
|
|
db *satelliteDB
|
2018-12-10 12:29:01 +00:00
|
|
|
tx *dbx.Tx
|
|
|
|
|
|
|
|
methods dbx.Methods
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +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
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +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
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +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
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// APIKeys is a getter for APIKeys repository.
|
2019-01-15 13:03:24 +00:00
|
|
|
func (db *ConsoleDB) APIKeys() console.APIKeys {
|
2019-09-12 15:19:30 +01:00
|
|
|
return &apikeys{db.methods, db.db}
|
2018-12-26 14:00:53 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// RegistrationTokens is a getter for RegistrationTokens repository.
|
2019-03-19 17:55:43 +00:00
|
|
|
func (db *ConsoleDB) RegistrationTokens() console.RegistrationTokens {
|
|
|
|
return ®istrationTokens{db.methods}
|
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// ResetPasswordTokens is a getter for ResetPasswordTokens repository.
|
2019-05-13 16:53:52 +01:00
|
|
|
func (db *ConsoleDB) ResetPasswordTokens() console.ResetPasswordTokens {
|
|
|
|
return &resetPasswordTokens{db.methods}
|
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// UserCredits is a getter for console.UserCredits repository.
|
2019-06-18 16:55:47 +01:00
|
|
|
func (db *ConsoleDB) UserCredits() console.UserCredits {
|
2019-07-30 14:21:00 +01:00
|
|
|
return &usercredits{db.db, db.tx}
|
2019-06-18 16:55:47 +01:00
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +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{
|
2019-07-30 14:21:00 +01:00
|
|
|
// Need to expose dbx.DB for when database methods need access to check database driver type
|
|
|
|
db: db.db,
|
2018-12-10 12:29:01 +00:00
|
|
|
tx: tx,
|
|
|
|
methods: tx,
|
|
|
|
},
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// DBTx extends Database with transaction scope.
|
2018-12-10 12:29:01 +00:00
|
|
|
type DBTx struct {
|
2019-01-15 13:03:24 +00:00
|
|
|
*ConsoleDB
|
2018-12-10 12:29:01 +00:00
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// Commit is a method for committing and closing transaction.
|
2018-12-10 12:29:01 +00:00
|
|
|
func (db *DBTx) Commit() error {
|
|
|
|
if db.tx == nil {
|
|
|
|
return errs.New("begin transaction before commit it!")
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.tx.Commit()
|
|
|
|
}
|
|
|
|
|
2019-11-15 14:27:44 +00:00
|
|
|
// Rollback is a method for rollback and closing transaction.
|
2018-12-10 12:29:01 +00:00
|
|
|
func (db *DBTx) Rollback() error {
|
|
|
|
if db.tx == nil {
|
|
|
|
return errs.New("begin transaction before rollback it!")
|
|
|
|
}
|
|
|
|
|
|
|
|
return db.tx.Rollback()
|
|
|
|
}
|