Finn
054008eb1f
Connects to zwave-js, syncs all locks and codeslots with database, and records an event log. No support for updating code slots.
53 lines
872 B
Go
53 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,
|
|
}
|
|
}
|