lure/cmd/lure-api-server/db.go

36 lines
801 B
Go
Raw Normal View History

2022-12-18 05:01:50 +00:00
package main
import (
"os"
2022-12-24 20:56:02 +00:00
"github.com/jmoiron/sqlx"
2022-12-18 05:01:50 +00:00
"go.arsenm.dev/logger/log"
"go.arsenm.dev/lure/internal/config"
"go.arsenm.dev/lure/internal/db"
"modernc.org/sqlite"
2022-12-18 05:01:50 +00:00
)
2022-12-24 20:56:02 +00:00
var gdb *sqlx.DB
2022-12-18 05:01:50 +00:00
func init() {
fi, err := os.Stat(config.DBPath)
2022-12-24 22:03:19 +00:00
if err == nil {
// TODO: This should be removed by the first stable release.
if fi.IsDir() {
log.Fatal("Your package cache database is using the old database engine. Please remove ~/.cache/lure and then run `lure ref`.").Send()
}
}
2022-12-24 22:03:19 +00:00
sqlite.MustRegisterScalarFunction("json_array_contains", 2, db.JsonArrayContains)
2022-12-24 20:56:02 +00:00
gdb, err = sqlx.Open("sqlite", config.DBPath)
2022-12-18 05:01:50 +00:00
if err != nil {
log.Fatal("Error opening database").Err(err).Send()
}
err = db.Init(gdb)
if err != nil {
log.Fatal("Error initializing database").Err(err).Send()
}
}