2023-09-18 23:57:18 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2024-03-11 17:13:26 +00:00
|
|
|
"database/sql/driver"
|
2024-01-26 00:22:57 +00:00
|
|
|
"embed"
|
2023-09-18 23:57:18 +00:00
|
|
|
_ "embed"
|
2024-03-11 17:13:26 +00:00
|
|
|
"errors"
|
2023-09-19 23:29:55 +00:00
|
|
|
"fmt"
|
2024-01-27 03:07:30 +00:00
|
|
|
"path/filepath"
|
2024-01-21 16:29:26 +00:00
|
|
|
"time"
|
|
|
|
|
2024-01-26 00:22:57 +00:00
|
|
|
"github.com/pressly/goose/v3"
|
2023-09-18 23:57:18 +00:00
|
|
|
log "github.com/sirupsen/logrus"
|
2024-03-11 17:13:26 +00:00
|
|
|
sqlite "modernc.org/sqlite"
|
2024-01-11 01:23:36 +00:00
|
|
|
"reichard.io/antholume/config"
|
2024-01-28 02:02:08 +00:00
|
|
|
_ "reichard.io/antholume/database/migrations"
|
2023-09-18 23:57:18 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
type DBManager struct {
|
|
|
|
DB *sql.DB
|
|
|
|
Ctx context.Context
|
|
|
|
Queries *Queries
|
2024-01-27 19:56:01 +00:00
|
|
|
cfg *config.Config
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
//go:embed schema.sql
|
|
|
|
var ddl string
|
|
|
|
|
2024-03-11 17:13:26 +00:00
|
|
|
//go:embed views.sql
|
|
|
|
var views string
|
|
|
|
|
2024-01-26 00:22:57 +00:00
|
|
|
//go:embed migrations/*
|
|
|
|
var migrations embed.FS
|
|
|
|
|
2024-03-11 17:13:26 +00:00
|
|
|
// Register scalar sqlite function on init
|
|
|
|
func init() {
|
|
|
|
sqlite.MustRegisterFunction("LOCAL_TIME", &sqlite.FunctionImpl{
|
|
|
|
NArgs: 2,
|
|
|
|
Deterministic: true,
|
|
|
|
Scalar: localTime,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
// Returns an initialized manager
|
2023-09-18 23:57:18 +00:00
|
|
|
func NewMgr(c *config.Config) *DBManager {
|
|
|
|
// Create Manager
|
|
|
|
dbm := &DBManager{
|
|
|
|
Ctx: context.Background(),
|
2024-01-27 19:56:01 +00:00
|
|
|
cfg: c,
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2024-01-28 02:02:08 +00:00
|
|
|
if err := dbm.init(); err != nil {
|
|
|
|
log.Panic("Unable to init DB")
|
|
|
|
}
|
2024-01-27 19:56:01 +00:00
|
|
|
|
|
|
|
return dbm
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init manager
|
2024-01-28 02:02:08 +00:00
|
|
|
func (dbm *DBManager) init() error {
|
2024-02-02 01:05:35 +00:00
|
|
|
// Build DB Location
|
|
|
|
var dbLocation string
|
|
|
|
switch dbm.cfg.DBType {
|
|
|
|
case "sqlite":
|
|
|
|
dbLocation = filepath.Join(dbm.cfg.ConfigPath, fmt.Sprintf("%s.db", dbm.cfg.DBName))
|
|
|
|
case "memory":
|
|
|
|
dbLocation = ":memory:"
|
|
|
|
default:
|
2024-01-28 02:02:08 +00:00
|
|
|
return fmt.Errorf("unsupported database")
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
2024-02-02 01:05:35 +00:00
|
|
|
var err error
|
|
|
|
dbm.DB, err = sql.Open("sqlite", dbLocation)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("Unable to open DB: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Single open connection
|
|
|
|
dbm.DB.SetMaxOpenConns(1)
|
|
|
|
|
|
|
|
// Check if DB is new
|
|
|
|
isNew, err := isEmpty(dbm.DB)
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("Unable to determine db info: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init SQLc
|
2023-09-18 23:57:18 +00:00
|
|
|
dbm.Queries = New(dbm.DB)
|
2024-01-28 02:02:08 +00:00
|
|
|
|
2024-02-02 01:05:35 +00:00
|
|
|
// Execute schema
|
|
|
|
if _, err := dbm.DB.Exec(ddl, nil); err != nil {
|
|
|
|
log.Panicf("Error executing schema: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Perform migrations
|
|
|
|
err = dbm.performMigrations(isNew)
|
|
|
|
if err != nil && err != goose.ErrNoMigrationFiles {
|
|
|
|
log.Panicf("Error running DB migrations: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-03-11 17:13:26 +00:00
|
|
|
// Execute views
|
|
|
|
if _, err := dbm.DB.Exec(views, nil); err != nil {
|
|
|
|
log.Panicf("Error executing views: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2024-02-02 01:05:35 +00:00
|
|
|
// Update settings
|
|
|
|
err = dbm.updateSettings()
|
|
|
|
if err != nil {
|
|
|
|
log.Panicf("Error running DB settings update: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Cache tables
|
2024-05-26 23:56:59 +00:00
|
|
|
if err := dbm.CacheTempTables(); err != nil {
|
|
|
|
log.Warn("Refreshing temp table cache failed: ", err)
|
|
|
|
}
|
2024-02-02 01:05:35 +00:00
|
|
|
|
2024-01-28 02:02:08 +00:00
|
|
|
return nil
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
2023-09-19 23:29:55 +00:00
|
|
|
|
2024-01-27 19:56:01 +00:00
|
|
|
// Reload manager (close DB & reinit)
|
|
|
|
func (dbm *DBManager) Reload() error {
|
|
|
|
// Close handle
|
|
|
|
err := dbm.DB.Close()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Reinit DB
|
2024-01-28 02:02:08 +00:00
|
|
|
if err := dbm.init(); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2024-01-27 19:56:01 +00:00
|
|
|
|
|
|
|
return nil
|
2023-11-03 23:38:31 +00:00
|
|
|
}
|
|
|
|
|
2024-01-21 16:29:26 +00:00
|
|
|
func (dbm *DBManager) CacheTempTables() error {
|
|
|
|
start := time.Now()
|
|
|
|
user_streaks_sql := `
|
|
|
|
DELETE FROM user_streaks;
|
|
|
|
INSERT INTO user_streaks SELECT * FROM view_user_streaks;
|
|
|
|
`
|
|
|
|
if _, err := dbm.DB.ExecContext(dbm.Ctx, user_streaks_sql); err != nil {
|
2023-11-04 01:37:26 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Debug("Cached 'user_streaks' in: ", time.Since(start))
|
2023-11-04 01:37:26 +00:00
|
|
|
|
2024-01-21 16:29:26 +00:00
|
|
|
start = time.Now()
|
|
|
|
document_statistics_sql := `
|
|
|
|
DELETE FROM document_user_statistics;
|
|
|
|
INSERT INTO document_user_statistics SELECT * FROM view_document_user_statistics;
|
|
|
|
`
|
|
|
|
if _, err := dbm.DB.ExecContext(dbm.Ctx, document_statistics_sql); err != nil {
|
2023-11-04 01:37:26 +00:00
|
|
|
return err
|
|
|
|
}
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Debug("Cached 'document_user_statistics' in: ", time.Since(start))
|
2023-11-04 01:37:26 +00:00
|
|
|
|
2023-09-19 23:29:55 +00:00
|
|
|
return nil
|
|
|
|
}
|
2024-01-26 00:22:57 +00:00
|
|
|
|
2024-02-02 01:05:35 +00:00
|
|
|
func (dbm *DBManager) updateSettings() error {
|
|
|
|
// Set SQLite PRAGMA Settings
|
|
|
|
pragmaQuery := `
|
|
|
|
PRAGMA foreign_keys = ON;
|
|
|
|
PRAGMA journal_mode = WAL;
|
|
|
|
`
|
|
|
|
if _, err := dbm.DB.Exec(pragmaQuery, nil); err != nil {
|
|
|
|
log.Errorf("Error executing pragma: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update Antholume Version in DB
|
|
|
|
if _, err := dbm.Queries.UpdateSettings(dbm.Ctx, UpdateSettingsParams{
|
|
|
|
Name: "version",
|
|
|
|
Value: dbm.cfg.Version,
|
|
|
|
}); err != nil {
|
|
|
|
log.Errorf("Error updating DB settings: %v", err)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (dbm *DBManager) performMigrations(isNew bool) error {
|
|
|
|
// Create context
|
2024-05-26 23:56:59 +00:00
|
|
|
ctx := context.WithValue(context.Background(), "isNew", isNew) // nolint
|
2024-02-02 01:05:35 +00:00
|
|
|
|
|
|
|
// Set DB migration
|
2024-01-26 00:22:57 +00:00
|
|
|
goose.SetBaseFS(migrations)
|
|
|
|
|
2024-02-02 01:05:35 +00:00
|
|
|
// Run migrations
|
2024-01-26 00:22:57 +00:00
|
|
|
goose.SetLogger(log.StandardLogger())
|
2024-02-02 01:05:35 +00:00
|
|
|
if err := goose.SetDialect("sqlite"); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return goose.UpContext(ctx, dbm.DB, "migrations")
|
|
|
|
}
|
|
|
|
|
2024-03-11 17:13:26 +00:00
|
|
|
// Determines whether the database is empty
|
2024-02-02 01:05:35 +00:00
|
|
|
func isEmpty(db *sql.DB) (bool, error) {
|
|
|
|
var tableCount int
|
|
|
|
err := db.QueryRow("SELECT COUNT(*) FROM sqlite_master WHERE type='table';").Scan(&tableCount)
|
|
|
|
if err != nil {
|
|
|
|
return false, err
|
|
|
|
}
|
|
|
|
return tableCount == 0, nil
|
2024-01-26 00:22:57 +00:00
|
|
|
}
|
2024-03-11 17:13:26 +00:00
|
|
|
|
|
|
|
// LOCAL_TIME custom SQL function
|
|
|
|
func localTime(ctx *sqlite.FunctionContext, args []driver.Value) (driver.Value, error) {
|
|
|
|
timeStr, ok := args[0].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("both arguments to TZTime must be strings")
|
|
|
|
}
|
|
|
|
|
|
|
|
timeZoneStr, ok := args[1].(string)
|
|
|
|
if !ok {
|
|
|
|
return nil, errors.New("both arguments to TZTime must be strings")
|
|
|
|
}
|
|
|
|
|
|
|
|
timeZone, err := time.LoadLocation(timeZoneStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("unable to parse timezone")
|
|
|
|
}
|
|
|
|
|
|
|
|
formattedTime, err := time.ParseInLocation(time.RFC3339, timeStr, time.UTC)
|
|
|
|
if err != nil {
|
|
|
|
return nil, errors.New("unable to parse time")
|
|
|
|
}
|
|
|
|
|
|
|
|
return formattedTime.In(timeZone).Format("2006-01-02 15:04:05.000"), nil
|
|
|
|
}
|