Persistance and recovery
This commit is contained in:
@ -2,7 +2,9 @@
|
||||
package store
|
||||
|
||||
import (
|
||||
_ "embed"
|
||||
"log/slog"
|
||||
"strings"
|
||||
"time"
|
||||
|
||||
"quantex.com.ar/multidb"
|
||||
@ -11,6 +13,9 @@ import (
|
||||
"quantex.com/qfixdpl/src/common/tracerr"
|
||||
)
|
||||
|
||||
//go:embed db.sql
|
||||
var schemaSQL string
|
||||
|
||||
const dbPingSeconds = 30
|
||||
|
||||
type Store struct {
|
||||
@ -45,9 +50,31 @@ func New(config Config) (*Store, error) {
|
||||
|
||||
go s.db.PeriodicDBPing(time.Second * dbPingSeconds)
|
||||
|
||||
if err := s.ensureTables(); err != nil {
|
||||
return nil, tracerr.Errorf("error ensuring tables: %w", err)
|
||||
}
|
||||
|
||||
return s, nil
|
||||
}
|
||||
|
||||
func (p *Store) ensureTables() error {
|
||||
statements := strings.Split(schemaSQL, ";")
|
||||
for _, stmt := range statements {
|
||||
stmt = strings.TrimSpace(stmt)
|
||||
if stmt == "" {
|
||||
continue
|
||||
}
|
||||
|
||||
if _, err := p.db.Exec(stmt); err != nil {
|
||||
return tracerr.Errorf("error executing schema statement: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
slog.Info("database tables ensured")
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (p *Store) CloseDB() {
|
||||
p.db.Close()
|
||||
slog.Info("closing database connection.")
|
||||
|
||||
Reference in New Issue
Block a user