summaryrefslogtreecommitdiff
path: root/internal/server/api/database.go
blob: 06175158a99ab32dc78901d87637c8134967c0e6 (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
36
37
package api

import (
	"database/sql"
	"log"

	_ "github.com/mattn/go-sqlite3"

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

var db *sql.DB

func ConnectToDatabase() {
	var err error
	db, err = sql.Open("sqlite3", "file:server.db?cache=shared")
	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 DB() *sql.DB {
	return db
}