summaryrefslogtreecommitdiff
path: root/internal/server/api/database.go
blob: 7057393d011bbc40660f222c1213f526e23e3560 (plain)
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
package api

import (
	"database/sql"
	"log"

	"github.com/kyren223/eko/pkg/assert"
)

var db *sql.DB

func ConnectToDatabase() {
	db, err := sql.Open("sqlite3", "server.db")
	assert.NoError(err, "DB should always be accessible")
	assert.AddFlush(db)
	log.Println("established connection with the database")

	pragmas := []string{
		"PRAGMA journal_mode = WAL;",
		"PRAGMA synchronous = NORMAL;",
		"PRAGMA temp_store = MEMORY;",
		"PRAGMA mmap_size = 30000000000;",
	}
	for _, pragma := range pragmas {
		_, err := db.Exec(pragma)
		assert.NoError(err, "DB pragmas should always execute with no errors")
	}

	log.Println("database connection ready to be used")
}

func CloseDatabase() {
	db.Close()
	log.Println("connection with database closed")
}