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"
|
|
|
|
|
2018-11-14 12:45:49 +00:00
|
|
|
"storj.io/storj/internal/migrate"
|
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
|
|
|
}
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
// NewConsoleDB - constructor for ConsoleDB
|
|
|
|
func NewConsoleDB(driver, source string) (*ConsoleDB, error) {
|
2018-11-08 14:19:42 +00:00
|
|
|
db, err := dbx.Open(driver, source)
|
|
|
|
if err != nil {
|
2018-12-07 14:46:42 +00:00
|
|
|
return nil, Error.New("failed opening database %q, %q: %v",
|
|
|
|
driver, source, err)
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
2019-01-15 13:03:24 +00:00
|
|
|
database := &ConsoleDB{
|
2018-12-10 12:29:01 +00:00
|
|
|
db: db,
|
|
|
|
methods: db,
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return database, nil
|
|
|
|
}
|
|
|
|
|
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-01-16 20:23:28 +00:00
|
|
|
// Buckets is a getter for Buckets repository
|
|
|
|
func (db *ConsoleDB) Buckets() console.Buckets {
|
|
|
|
return &buckets{db.methods}
|
|
|
|
}
|
|
|
|
|
2018-11-08 14:19:42 +00:00
|
|
|
// CreateTables is a method for creating all tables for satellitedb
|
2019-01-15 13:03:24 +00:00
|
|
|
func (db *ConsoleDB) CreateTables() error {
|
2018-12-10 12:29:01 +00:00
|
|
|
if db.db == nil {
|
|
|
|
return errs.New("Connection is closed")
|
|
|
|
}
|
2018-11-14 12:45:49 +00:00
|
|
|
return migrate.Create("satellitedb", db.db)
|
2018-11-08 14:19:42 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Close is used to close db connection
|
2019-01-15 13:03:24 +00:00
|
|
|
func (db *ConsoleDB) Close() error {
|
2018-12-10 12:29:01 +00:00
|
|
|
if db.db == nil {
|
|
|
|
return errs.New("Connection is closed")
|
|
|
|
}
|
2018-11-08 14:19:42 +00:00
|
|
|
return db.db.Close()
|
|
|
|
}
|
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()
|
|
|
|
}
|