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"
|
2020-01-10 01:12:27 +00:00
|
|
|
"sync"
|
2018-12-10 12:29:01 +00:00
|
|
|
|
2019-01-08 14:05:14 +00:00
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2021-08-12 17:26:43 +01:00
|
|
|
"storj.io/common/lrucache"
|
2019-01-15 13:03:24 +00:00
|
|
|
"storj.io/storj/satellite/console"
|
2022-05-12 16:12:22 +01:00
|
|
|
"storj.io/storj/satellite/console/consoleauth"
|
2020-01-15 02:29:51 +00:00
|
|
|
"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 {
|
2021-04-23 13:59:10 +01:00
|
|
|
apikeysLRUOptions lrucache.Options
|
2020-01-10 01:12:27 +00:00
|
|
|
|
2019-12-14 02:29:54 +00:00
|
|
|
db *satelliteDB
|
2018-12-10 12:29:01 +00:00
|
|
|
tx *dbx.Tx
|
|
|
|
|
|
|
|
methods dbx.Methods
|
2020-01-10 01:12:27 +00:00
|
|
|
|
|
|
|
apikeysOnce *sync.Once
|
|
|
|
apikeys *apikeys
|
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 {
|
2022-01-06 19:46:53 +00:00
|
|
|
return &users{db.db}
|
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 {
|
2020-01-29 00:57:15 +00:00
|
|
|
return &projects{db: db.methods, sdb: db.db}
|
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
|
|
|
}
|
|
|
|
|
2023-04-26 05:46:48 +01:00
|
|
|
// ProjectInvitations is a getter for ProjectInvitations repository.
|
|
|
|
func (db *ConsoleDB) ProjectInvitations() console.ProjectInvitations {
|
2023-05-05 05:48:01 +01:00
|
|
|
return &projectInvitations{db.db}
|
2023-04-26 05:46:48 +01: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 {
|
2020-01-10 01:12:27 +00:00
|
|
|
db.apikeysOnce.Do(func() {
|
2023-04-04 10:11:06 +01:00
|
|
|
options := db.apikeysLRUOptions
|
|
|
|
options.Name = "satellitedb-apikeys"
|
2020-01-10 01:12:27 +00:00
|
|
|
db.apikeys = &apikeys{
|
|
|
|
methods: db.methods,
|
2023-04-18 23:15:00 +01:00
|
|
|
lru: lrucache.NewOf[*dbx.ApiKey_Project_PublicId_Row](options),
|
2020-01-10 01:12:27 +00:00
|
|
|
db: db.db,
|
|
|
|
}
|
|
|
|
})
|
|
|
|
|
|
|
|
return db.apikeys
|
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}
|
|
|
|
}
|
|
|
|
|
2022-05-12 16:12:22 +01:00
|
|
|
// WebappSessions is a getter for WebappSessions repository.
|
|
|
|
func (db *ConsoleDB) WebappSessions() consoleauth.WebappSessions {
|
|
|
|
return &webappSessions{db.methods}
|
|
|
|
}
|
|
|
|
|
2022-12-15 03:56:11 +00:00
|
|
|
// AccountFreezeEvents is a getter for AccountFreezeEvents repository.
|
|
|
|
func (db *ConsoleDB) AccountFreezeEvents() console.AccountFreezeEvents {
|
|
|
|
return &accountFreezeEvents{db.methods}
|
|
|
|
}
|
|
|
|
|
2019-12-19 10:07:56 +00:00
|
|
|
// WithTx is a method for executing and retrying transaction.
|
|
|
|
func (db *ConsoleDB) WithTx(ctx context.Context, fn func(context.Context, console.DBTx) error) error {
|
2018-12-10 12:29:01 +00:00
|
|
|
if db.db == nil {
|
2019-12-19 10:07:56 +00:00
|
|
|
return errs.New("DB is not initialized!")
|
2018-12-10 12:29:01 +00:00
|
|
|
}
|
|
|
|
|
2019-12-19 10:07:56 +00:00
|
|
|
return db.db.WithTx(ctx, func(ctx context.Context, tx *dbx.Tx) error {
|
|
|
|
dbTx := &DBTx{
|
|
|
|
ConsoleDB: &ConsoleDB{
|
2020-01-10 01:12:27 +00:00
|
|
|
apikeysLRUOptions: db.apikeysLRUOptions,
|
|
|
|
|
2019-12-19 10:07:56 +00:00
|
|
|
// Need to expose dbx.DB for when database methods need access to check database driver type
|
|
|
|
db: db.db,
|
|
|
|
tx: tx,
|
|
|
|
methods: tx,
|
2020-01-10 01:12:27 +00:00
|
|
|
|
|
|
|
apikeysOnce: db.apikeysOnce,
|
|
|
|
apikeys: db.apikeys,
|
2019-12-19 10:07:56 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
return fn(ctx, dbTx)
|
|
|
|
})
|
2018-12-10 12:29:01 +00:00
|
|
|
}
|
|
|
|
|
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()
|
|
|
|
}
|