2023-09-18 23:57:18 +00:00
|
|
|
package database
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2024-01-26 00:22:57 +00:00
|
|
|
"embed"
|
2023-09-18 23:57:18 +00:00
|
|
|
_ "embed"
|
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"
|
2023-10-24 00:18:16 +00:00
|
|
|
_ "modernc.org/sqlite"
|
2024-01-11 01:23:36 +00:00
|
|
|
"reichard.io/antholume/config"
|
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-01-26 00:22:57 +00:00
|
|
|
//go:embed migrations/*
|
|
|
|
var migrations embed.FS
|
|
|
|
|
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-27 19:56:01 +00:00
|
|
|
dbm.init()
|
|
|
|
|
|
|
|
return dbm
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init manager
|
|
|
|
func (dbm *DBManager) init() {
|
|
|
|
if dbm.cfg.DBType == "sqlite" || dbm.cfg.DBType == "memory" {
|
2023-10-24 00:18:16 +00:00
|
|
|
var dbLocation string = ":memory:"
|
2024-01-27 19:56:01 +00:00
|
|
|
if dbm.cfg.DBType == "sqlite" {
|
|
|
|
dbLocation = filepath.Join(dbm.cfg.ConfigPath, fmt.Sprintf("%s.db", dbm.cfg.DBName))
|
2023-10-24 00:18:16 +00:00
|
|
|
}
|
|
|
|
|
2023-09-18 23:57:18 +00:00
|
|
|
var err error
|
2023-10-05 23:56:19 +00:00
|
|
|
dbm.DB, err = sql.Open("sqlite", dbLocation)
|
2023-09-18 23:57:18 +00:00
|
|
|
if err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Fatalf("Unable to open DB: %v", err)
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
2023-10-05 23:56:19 +00:00
|
|
|
|
|
|
|
// Single Open Connection
|
|
|
|
dbm.DB.SetMaxOpenConns(1)
|
2024-01-26 00:22:57 +00:00
|
|
|
|
|
|
|
// Execute DDL
|
2023-10-05 23:56:19 +00:00
|
|
|
if _, err := dbm.DB.Exec(ddl, nil); err != nil {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Fatalf("Error executing schema: %v", err)
|
2024-01-26 00:22:57 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Perform Migrations
|
|
|
|
err = dbm.performMigrations()
|
|
|
|
if err != nil && err != goose.ErrNoMigrationFiles {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Fatalf("Error running DB migrations: %v", err)
|
2023-10-05 23:56:19 +00:00
|
|
|
}
|
2024-01-26 00:22:57 +00:00
|
|
|
|
|
|
|
// Cache Tables
|
|
|
|
dbm.CacheTempTables()
|
2023-09-18 23:57:18 +00:00
|
|
|
} else {
|
2024-01-27 01:45:07 +00:00
|
|
|
log.Fatal("Unsupported Database")
|
2023-09-18 23:57:18 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
dbm.Queries = New(dbm.DB)
|
|
|
|
}
|
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
|
|
|
|
dbm.init()
|
|
|
|
|
|
|
|
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
|
|
|
|
|
|
|
func (dbm *DBManager) performMigrations() error {
|
|
|
|
// Set DB Migration
|
|
|
|
goose.SetBaseFS(migrations)
|
|
|
|
|
|
|
|
// Run Migrations
|
|
|
|
goose.SetLogger(log.StandardLogger())
|
|
|
|
goose.SetDialect("sqlite")
|
|
|
|
return goose.Up(dbm.DB, "migrations")
|
|
|
|
}
|