summaryrefslogtreecommitdiff
path: root/database.go
blob: ffc44e34d050089943945e7a776a579f4f13b05f (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
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main

import (
	"database/sql"
	"fmt"
	"io"
	"log"
	"os"
	"os/exec"
	"os/user"
	"path/filepath"
	"strconv"

	_ "github.com/go-sql-driver/mysql"
)

const neededSchemaVersion int = 2

func setupDatabase() *sql.DB {

	u, err := user.Current()
	if err != nil {
		log.Fatal(err)
	}
	target := fmt.Sprintf("%s@unix(%s)/%s", u.Username, config.Database.Socket,
		config.Database.Database)

	db, err := sql.Open("mysql", target)
	if err != nil {
		log.Fatal(err)
	}

	err = db.Ping()
	if err != nil {
		log.Fatal(err)
	}

	migrate(db)

	log.Printf("Connected to database: %s\n", target)

	return db
}

func migrate(db *sql.DB) {

	for {
		v := schemaVersion(db)

		if v > neededSchemaVersion {
			log.Fatalf(
				"Current database schema version is %d but newest is %d!", v,
				neededSchemaVersion)
		}

		if v == neededSchemaVersion {
			break
		}

		log.Printf("Starting database schema migration to version %d.\n", v+1)
		path := filepath.Join(config.Database.Migrations,
			fmt.Sprintf("%04d_migration.sql", v+1))
		RunSqlScript(path)
		log.Printf("Finished database schema migration to version %d.\n", v+1)
	}
}

func RunSqlScript(path string) {

	script, err := os.Open(path)
	if err != nil {
		log.Fatalf("Could not open SQL script '%s'!\n", path)
	}

	cmd := exec.Command("mariadb")
	stdin, err := cmd.StdinPipe()
	if err != nil {
		log.Fatalf("Could not open stdin of mariadb process!\n%v", err)
	}

	err = cmd.Start()
	if err != nil {
		log.Fatalf("Could not start mariadb process!\n%v", err)
	}
	io.Copy(stdin, script)
	stdin.Close()

	err = cmd.Wait()
	if err != nil {
		log.Fatalf("Failed to wait for SQL script to finish!\n%v", err)
	}
}

func schemaVersion(db *sql.DB) int {

	cmd := "SELECT value FROM meta WHERE (identifier='version');"
	rows, err := db.Query(cmd)
	if err != nil {
		log.Fatal(err)
	}
	defer rows.Close()

	rows.Next()
	var version string
	err = rows.Scan(&version)
	if err != nil {
		log.Fatal(err)
	}

	v, err := strconv.Atoi(version)
	if err != nil {
		log.Fatalf("Could not convert database schema version '%s' to int.\n",
			version)
	}

	return v
}

func dbCleanup(db *sql.DB) {
	err := db.Close()
	if err != nil {
		log.Println("Could not close database connection")
	} else {
		log.Println("Closed database connection")
	}
}