feat: Add ent-based postgres storage

Signed-off-by: m.nabokikh <maksim.nabokikh@flant.com>
This commit is contained in:
m.nabokikh
2021-05-14 23:14:38 +04:00
parent 81c4dc7994
commit 19884d92ac
14 changed files with 401 additions and 20 deletions

View File

@@ -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{}