diff --git a/api/api.go b/api/api.go index fd9ba8f..18f2e9c 100644 --- a/api/api.go +++ b/api/api.go @@ -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 - txAble txAble + db bob.Executor + sqldb *sql.DB scheduler *scheduler.Scheduler @@ -38,7 +38,7 @@ type API struct { } type Dependencies struct { - DB *comfylite3.ComfyDB + DB *sql.DB 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), - txAble: deps.DB, + sqldb: deps.DB, downloadBroadcast: broadcast.NewRelay[bmessage.ImageDownloadMessage](), config: deps.Config, imageSemaphore: make(chan struct{}, deps.Config.Int("download.concurrency.images")), diff --git a/api/transaction.go b/api/transaction.go index 31fb915..c1342f2 100644 --- a/api/transaction.go +++ b/api/transaction.go @@ -2,20 +2,15 @@ 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.txAble.BeginTx(ctx, nil) + tx, err := api.sqldb.BeginTx(ctx, nil) if err != nil { return errs.Wrapw(err, "failed to begin transaction") } diff --git a/cli/serve.go b/cli/serve.go index 58546b3..ed138c5 100644 --- a/cli/serve.go +++ b/cli/serve.go @@ -28,18 +28,12 @@ var serveCmd = &cobra.Command{ } defer tele.Close() - database, err := db.NewComfy(cfg) + database, err := db.Open(cfg) 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) } - // 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") @@ -57,18 +51,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: database, + DB: loggedDb, Config: cfg, Reddit: red, Publisher: publisher, diff --git a/db/comfylite.go b/db/comfylite.go deleted file mode 100644 index 4e15fee..0000000 --- a/db/comfylite.go +++ /dev/null @@ -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 -}