BrainMinder/internal/database/db.go
2024-08-22 10:13:16 +02:00

64 lines
1.2 KiB
Go

package database
import (
"context"
"errors"
"time"
"brainminder.speedtech.it/assets"
"github.com/golang-migrate/migrate/v4"
"github.com/golang-migrate/migrate/v4/source/iofs"
"github.com/jmoiron/sqlx"
_ "github.com/golang-migrate/migrate/v4/database/sqlite3"
_ "github.com/mattn/go-sqlite3"
)
const defaultTimeout = 3 * time.Second
type DB struct {
*sqlx.DB
}
func GetContext() (context.Context, context.CancelFunc) {
return context.WithTimeout(context.Background(), defaultTimeout)
}
func New(dsn string, automigrate bool) (*DB, error) {
ctx, cancel := context.WithTimeout(context.Background(), defaultTimeout)
defer cancel()
db, err := sqlx.ConnectContext(ctx, "sqlite3", dsn)
if err != nil {
return nil, err
}
db.SetMaxOpenConns(25)
db.SetMaxIdleConns(25)
db.SetConnMaxIdleTime(5 * time.Minute)
db.SetConnMaxLifetime(2 * time.Hour)
if automigrate {
iofsDriver, err := iofs.New(assets.EmbeddedFiles, "migrations")
if err != nil {
return nil, err
}
migrator, err := migrate.NewWithSourceInstance("iofs", iofsDriver, "sqlite3://"+dsn)
if err != nil {
return nil, err
}
err = migrator.Up()
switch {
case errors.Is(err, migrate.ErrNoChange):
break
case err != nil:
return nil, err
}
}
return &DB{db}, nil
}