54 lines
872 B
Go
54 lines
872 B
Go
|
package db
|
||
|
|
||
|
import (
|
||
|
"database/sql"
|
||
|
"embed"
|
||
|
|
||
|
_ "github.com/mattn/go-sqlite3"
|
||
|
goose "github.com/pressly/goose/v3"
|
||
|
"github.com/sirupsen/logrus"
|
||
|
|
||
|
"git.janky.solutions/finn/lockserver/config"
|
||
|
)
|
||
|
|
||
|
func Get() (*Queries, *sql.DB, error) {
|
||
|
db, err := sql.Open("sqlite3", config.C.Database)
|
||
|
if err != nil {
|
||
|
return nil, nil, err
|
||
|
}
|
||
|
|
||
|
return New(db), db, nil
|
||
|
}
|
||
|
|
||
|
//go:embed migrations
|
||
|
var migrations embed.FS
|
||
|
|
||
|
func Migrate() error {
|
||
|
logrus.WithField("dbfile", config.C.Database).Info("migrating database")
|
||
|
|
||
|
_, conn, err := Get()
|
||
|
if err != nil {
|
||
|
return err
|
||
|
}
|
||
|
defer conn.Close()
|
||
|
|
||
|
goose.SetBaseFS(migrations)
|
||
|
|
||
|
if err := goose.SetDialect("sqlite3"); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
if err := goose.Up(conn, "migrations"); err != nil {
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func NullString(s string) sql.NullString {
|
||
|
return sql.NullString{
|
||
|
Valid: s != "",
|
||
|
String: s,
|
||
|
}
|
||
|
}
|