feat: Add ent-based postgres storage
Signed-off-by: m.nabokikh <maksim.nabokikh@flant.com>
This commit is contained in:
@@ -58,7 +58,7 @@ func (d *Database) DeleteAuthRequest(id string) error {
|
||||
|
||||
// UpdateAuthRequest changes an auth request by id using an updater function and saves it to the database.
|
||||
func (d *Database) UpdateAuthRequest(id string, updater func(old storage.AuthRequest) (storage.AuthRequest, error)) error {
|
||||
tx, err := d.client.Tx(context.TODO())
|
||||
tx, err := d.BeginTx(context.TODO())
|
||||
if err != nil {
|
||||
return fmt.Errorf("update auth request tx: %w", err)
|
||||
}
|
||||
|
@@ -57,7 +57,7 @@ func (d *Database) DeleteClient(id string) error {
|
||||
|
||||
// UpdateClient changes an oauth2 client by id using an updater function and saves it to the database.
|
||||
func (d *Database) UpdateClient(id string, updater func(old storage.Client) (storage.Client, error)) error {
|
||||
tx, err := d.client.Tx(context.TODO())
|
||||
tx, err := d.BeginTx(context.TODO())
|
||||
if err != nil {
|
||||
return convertDBError("update client tx: %w", err)
|
||||
}
|
||||
|
@@ -55,7 +55,7 @@ func (d *Database) DeleteConnector(id string) error {
|
||||
|
||||
// UpdateConnector changes a connector by id using an updater function and saves it to the database.
|
||||
func (d *Database) UpdateConnector(id string, updater func(old storage.Connector) (storage.Connector, error)) error {
|
||||
tx, err := d.client.Tx(context.TODO())
|
||||
tx, err := d.BeginTx(context.TODO())
|
||||
if err != nil {
|
||||
return convertDBError("update connector tx: %w", err)
|
||||
}
|
||||
|
@@ -37,7 +37,7 @@ func (d *Database) GetDeviceToken(deviceCode string) (storage.DeviceToken, error
|
||||
|
||||
// UpdateDeviceToken changes a token by device code using an updater function and saves it to the database.
|
||||
func (d *Database) UpdateDeviceToken(deviceCode string, updater func(old storage.DeviceToken) (storage.DeviceToken, error)) error {
|
||||
tx, err := d.client.Tx(context.TODO())
|
||||
tx, err := d.BeginTx(context.TODO())
|
||||
if err != nil {
|
||||
return convertDBError("update device token tx: %w", err)
|
||||
}
|
||||
|
@@ -26,7 +26,7 @@ func (d *Database) GetKeys() (storage.Keys, error) {
|
||||
func (d *Database) UpdateKeys(updater func(old storage.Keys) (storage.Keys, error)) error {
|
||||
firstUpdate := false
|
||||
|
||||
tx, err := d.client.Tx(context.TODO())
|
||||
tx, err := d.BeginTx(context.TODO())
|
||||
if err != nil {
|
||||
return convertDBError("update keys tx: %w", err)
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@ package client
|
||||
|
||||
import (
|
||||
"context"
|
||||
"database/sql"
|
||||
"hash"
|
||||
"time"
|
||||
|
||||
@@ -17,7 +18,9 @@ import (
|
||||
var _ storage.Storage = (*Database)(nil)
|
||||
|
||||
type Database struct {
|
||||
client *db.Client
|
||||
client *db.Client
|
||||
txOptions *sql.TxOptions
|
||||
|
||||
hasher func() hash.Hash
|
||||
}
|
||||
|
||||
@@ -44,6 +47,13 @@ func WithHasher(h func() hash.Hash) func(*Database) {
|
||||
}
|
||||
}
|
||||
|
||||
// WithTxIsolationLevel sets correct isolation level for database transactions.
|
||||
func WithTxIsolationLevel(level sql.IsolationLevel) func(*Database) {
|
||||
return func(s *Database) {
|
||||
s.txOptions = &sql.TxOptions{Isolation: level}
|
||||
}
|
||||
}
|
||||
|
||||
// Schema exposes migration schema to perform migrations.
|
||||
func (d *Database) Schema() *migrate.Schema {
|
||||
return d.client.Schema
|
||||
@@ -54,6 +64,11 @@ func (d *Database) Close() error {
|
||||
return d.client.Close()
|
||||
}
|
||||
|
||||
// BeginTx is a wrapper to begin transaction with defined options.
|
||||
func (d *Database) BeginTx(ctx context.Context) (*db.Tx, error) {
|
||||
return d.client.BeginTx(ctx, d.txOptions)
|
||||
}
|
||||
|
||||
// GarbageCollect removes expired entities from the database.
|
||||
func (d *Database) GarbageCollect(now time.Time) (storage.GCResult, error) {
|
||||
result := storage.GCResult{}
|
||||
|
@@ -55,7 +55,7 @@ func (d *Database) DeleteOfflineSessions(userID, connID string) error {
|
||||
func (d *Database) UpdateOfflineSessions(userID string, connID string, updater func(s storage.OfflineSessions) (storage.OfflineSessions, error)) error {
|
||||
id := offlineSessionID(userID, connID, d.hasher)
|
||||
|
||||
tx, err := d.client.Tx(context.TODO())
|
||||
tx, err := d.BeginTx(context.TODO())
|
||||
if err != nil {
|
||||
return convertDBError("update offline session tx: %w", err)
|
||||
}
|
||||
|
@@ -64,7 +64,7 @@ func (d *Database) DeletePassword(email string) error {
|
||||
func (d *Database) UpdatePassword(email string, updater func(old storage.Password) (storage.Password, error)) error {
|
||||
email = strings.ToLower(email)
|
||||
|
||||
tx, err := d.client.Tx(context.TODO())
|
||||
tx, err := d.BeginTx(context.TODO())
|
||||
if err != nil {
|
||||
return convertDBError("update connector tx: %w", err)
|
||||
}
|
||||
|
@@ -67,7 +67,7 @@ func (d *Database) DeleteRefresh(id string) error {
|
||||
|
||||
// UpdateRefreshToken changes a refresh token by id using an updater function and saves it to the database.
|
||||
func (d *Database) UpdateRefreshToken(id string, updater func(old storage.RefreshToken) (storage.RefreshToken, error)) error {
|
||||
tx, err := d.client.Tx(context.TODO())
|
||||
tx, err := d.BeginTx(context.TODO())
|
||||
if err != nil {
|
||||
return convertDBError("update refresh token tx: %w", err)
|
||||
}
|
||||
|
Reference in New Issue
Block a user