DEVELOPMENT ENVIRONMENT

~liljamo/felu

ref: 6b83684da956b580c5c32129848a1ff66f42e0c6 felu/internal/db/db.go -rw-r--r-- 923 bytes
6b83684dJonni Liljamo docs: add README.md 1 year, 29 days ago
                                                                                
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
/*
 * 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/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...
		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
}