Revert "db: prepare moving to comfylite3"

This reverts commit cd1959d824.
This commit is contained in:
Tigor Hutasuhut 2024-05-27 14:24:15 +07:00
parent b90d5c7601
commit 4db333848b
4 changed files with 14 additions and 41 deletions

View file

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

View file

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

View file

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

View file

@ -1,16 +0,0 @@
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
}