/*
* Copyright (C) 2023 Jonni Liljamo <jonni@liljamo.com>
*
* This file is licensed under AGPL-3.0-or-later, see NOTICE and LICENSE for
* more information.
*/
package db
import (
"database/sql"
"log/slog"
"git.src.quest/~skye/felu-ddns/internal/config"
_ "github.com/mattn/go-sqlite3"
)
var DBConn *sql.DB
func InitDB() error {
var err error
DBConn, err = sql.Open("sqlite3",
config.FeluConfig.DataDir + "felu.db?_foreign_keys=true")
if err != nil {
return err
}
runMigrations()
return nil
}
func InitAdminUser() error {
rows, err := DBConn.Query(`SELECT id FROM users WHERE is_admin = TRUE`)
if err != nil {
return err
}
defer rows.Close()
if rows.Next() {
// There is at least one...
slog.Info("Existing admin user found")
return nil
}
// Since we're here, it's assumed no admin accounts exist
slog.Info("Creating initial admin user")
err = CreateAdmin(config.FeluConfig.InitialAdminEmail, config.FeluConfig.InitialAdminPwd)
if err != nil {
return err
}
return nil
}