2024-04-07 00:28:29 +07:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"io/fs"
|
2024-05-11 20:23:05 +07:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
2024-04-07 00:28:29 +07:00
|
|
|
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
|
|
|
"github.com/pressly/goose/v3"
|
2024-04-08 15:48:45 +07:00
|
|
|
sqldblogger "github.com/simukti/sqldb-logger"
|
2024-04-07 00:28:29 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/config"
|
2024-04-07 23:41:00 +07:00
|
|
|
"github.com/tigorlazuardi/redmage/pkg/errs"
|
2024-04-13 00:15:31 +07:00
|
|
|
semconv "go.opentelemetry.io/otel/semconv/v1.4.0"
|
|
|
|
|
|
|
|
"github.com/XSAM/otelsql"
|
2024-04-07 00:28:29 +07:00
|
|
|
)
|
|
|
|
|
|
|
|
var Migrations fs.FS
|
|
|
|
|
|
|
|
func Open(cfg *config.Config) (*sql.DB, error) {
|
2024-04-25 20:22:05 +07:00
|
|
|
driver := cfg.String("db.driver")
|
2024-04-25 13:05:41 +07:00
|
|
|
dsn := cfg.String("db.string")
|
2024-05-11 20:23:05 +07:00
|
|
|
if driver == "sqlite3" {
|
|
|
|
path, err := filepath.Abs(dsn)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrapw(err, "failed to get absolute path of sqlite3 database", "path", dsn)
|
|
|
|
}
|
|
|
|
dir := filepath.Dir(path)
|
|
|
|
err = os.MkdirAll(dir, 0777)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errs.Wrapw(err, "failed to create directory for sqlite3 database", "dir", dir)
|
|
|
|
}
|
|
|
|
}
|
2024-04-27 15:16:14 +07:00
|
|
|
db, err := otelsql.Open(driver, dsn, otelsql.WithAttributes(
|
|
|
|
semconv.DBSystemSqlite,
|
|
|
|
))
|
2024-04-25 13:05:41 +07:00
|
|
|
if err != nil {
|
2024-05-11 20:23:05 +07:00
|
|
|
return db, errs.Wrapw(err, "failed to open database", "driver", driver, "db.string", dsn)
|
2024-04-25 13:05:41 +07:00
|
|
|
}
|
2024-05-22 08:00:25 +07:00
|
|
|
if driver == "sqlite3" {
|
|
|
|
db.SetMaxOpenConns(1) // SQLITE is not thread safe. This is to prevent database is locked error.
|
|
|
|
}
|
2024-04-25 20:22:05 +07:00
|
|
|
if cfg.Bool("db.automigrate") {
|
2024-04-27 15:16:14 +07:00
|
|
|
goose.SetLogger(goose.NopLogger())
|
2024-04-25 20:22:05 +07:00
|
|
|
goose.SetBaseFS(Migrations)
|
|
|
|
|
|
|
|
if err := goose.SetDialect(driver); err != nil {
|
2024-05-12 11:56:20 +07:00
|
|
|
return db, errs.Wrapw(err, "failed to set goose dialect", "dialect", driver, "dsn", dsn)
|
2024-04-25 20:22:05 +07:00
|
|
|
}
|
|
|
|
|
|
|
|
if err := goose.Up(db, "db/migrations"); err != nil {
|
2024-05-12 11:56:20 +07:00
|
|
|
return db, errs.Wrapw(err, "failed to migrate database", "dialect", driver, "dsn", dsn)
|
2024-04-25 20:22:05 +07:00
|
|
|
}
|
|
|
|
}
|
2024-04-25 13:05:41 +07:00
|
|
|
return db, err
|
|
|
|
}
|
|
|
|
|
2024-04-27 15:16:14 +07:00
|
|
|
func OpenPubsub(cfg *config.Config) (*sql.DB, error) {
|
|
|
|
driver := cfg.String("pubsub.db.driver")
|
|
|
|
dsn := cfg.String("pubsub.db.string")
|
|
|
|
db, err := sql.Open(driver, dsn)
|
2024-04-07 00:28:29 +07:00
|
|
|
if err != nil {
|
2024-04-07 23:41:00 +07:00
|
|
|
return db, errs.Wrapw(err, "failed to open database", "driver", driver)
|
2024-04-07 00:28:29 +07:00
|
|
|
}
|
|
|
|
return db, err
|
|
|
|
}
|
2024-04-27 15:16:14 +07:00
|
|
|
|
|
|
|
func ApplyLogger(cfg *config.Config, db *sql.DB) *sql.DB {
|
|
|
|
dsn := cfg.String("db.string")
|
|
|
|
return sqldblogger.OpenDriver(dsn, db.Driver(), sqlLogger{},
|
|
|
|
sqldblogger.WithSQLQueryAsMessage(true),
|
|
|
|
)
|
|
|
|
}
|