/*
* 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"
"git.src.quest/~skye/felu-ddns/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")
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...
return nil
}
// Since we're here, it's assumed no admin accounts exist
err = CreateAdmin(config.FeluConfig.InitialAdminEmail, config.FeluConfig.InitialAdminPwd)
if err != nil {
return err
}
return nil
}