db: prepare moving to comfylite3

This commit is contained in:
Tigor Hutasuhut 2024-05-23 16:34:39 +07:00
parent f5f5c7ae9e
commit cd1959d824
4 changed files with 41 additions and 14 deletions

View file

@ -2,9 +2,9 @@ package api
import (
"context"
"database/sql"
"sync"
"github.com/davidroman0O/comfylite3"
"github.com/stephenafamo/bob"
"github.com/teivah/broadcast"
"github.com/tigorlazuardi/redmage/api/bmessage"
@ -18,8 +18,8 @@ import (
)
type API struct {
db bob.Executor
sqldb *sql.DB
db bob.Executor
txAble txAble
scheduler *scheduler.Scheduler
@ -38,7 +38,7 @@ type API struct {
}
type Dependencies struct {
DB *sql.DB
DB *comfylite3.ComfyDB
Config *config.Config
Reddit *reddit.Reddit
Publisher message.Publisher
@ -55,7 +55,7 @@ func New(deps Dependencies) *API {
api := &API{
db: bob.New(deps.DB),
sqldb: deps.DB,
txAble: deps.DB,
downloadBroadcast: broadcast.NewRelay[bmessage.ImageDownloadMessage](),
config: deps.Config,
imageSemaphore: make(chan struct{}, deps.Config.Int("download.concurrency.images")),

View file

@ -2,15 +2,20 @@ package api
import (
"context"
"database/sql"
"github.com/stephenafamo/bob"
"github.com/tigorlazuardi/redmage/pkg/errs"
)
type txAble interface {
BeginTx(ctx context.Context, opts *sql.TxOptions) (*sql.Tx, error)
}
type executor func(exec bob.Executor) error
func (api *API) withTransaction(ctx context.Context, f executor) (err error) {
tx, err := api.sqldb.BeginTx(ctx, nil)
tx, err := api.txAble.BeginTx(ctx, nil)
if err != nil {
return errs.Wrapw(err, "failed to begin transaction")
}

View file

@ -28,12 +28,18 @@ var serveCmd = &cobra.Command{
}
defer tele.Close()
database, err := db.Open(cfg)
database, err := db.NewComfy(cfg)
if err != nil {
log.New(cmd.Context()).Err(err).Error("failed to open connection to database")
log.New(cmd.Context()).Err(err).Error("failed to open database")
os.Exit(1)
}
// database, err := db.Open(cfg)
// if err != nil {
// log.New(cmd.Context()).Err(err).Error("failed to open connection to database")
// os.Exit(1)
// }
pubsubDB, err := pubsub.New(cfg)
if err != nil {
log.New(cmd.Context()).Err(err).Error("failed to open connection to pubsub database")
@ -51,18 +57,18 @@ var serveCmd = &cobra.Command{
os.Exit(1)
}
loggedDb := db.ApplyLogger(cfg, database)
if err != nil {
log.New(cmd.Context()).Err(err).Error("failed to connect database")
os.Exit(1)
}
// loggedDb := db.ApplyLogger(cfg, database)
// if err != nil {
// log.New(cmd.Context()).Err(err).Error("failed to connect database")
// os.Exit(1)
// }
red := &reddit.Reddit{
Client: reddit.NewRedditHTTPClient(cfg),
Config: cfg,
}
api := api.New(api.Dependencies{
DB: loggedDb,
DB: database,
Config: cfg,
Reddit: red,
Publisher: publisher,

16
db/comfylite.go Normal file
View file

@ -0,0 +1,16 @@
package db
import (
"github.com/davidroman0O/comfylite3"
"github.com/tigorlazuardi/redmage/config"
"github.com/tigorlazuardi/redmage/pkg/errs"
)
func NewComfy(cfg *config.Config) (*comfylite3.ComfyDB, error) {
target := cfg.String("db.string")
db, err := comfylite3.Comfy(comfylite3.WithPath(target))
if err != nil {
return db, errs.Wrapf(err, "failed to create/open comfy db at %q", target)
}
return db, nil
}