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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
|
package model
import (
"database/sql"
"embed"
"fmt"
"log"
_ "github.com/mattn/go-sqlite3"
)
type DB sql.DB
func OpenDB(path string) (*DB, error) {
db, err := sql.Open("sqlite3", path)
if err != nil {
return nil, fmt.Errorf("Failed to open SQLite3 database: %w", err)
}
err = db.Ping()
if err != nil {
return nil, fmt.Errorf("Failed to ping SQLite3 database: %w", err)
}
return (*DB)(db), nil
}
func (db *DB) Transaction(f func(*sql.Tx) error) error {
tx, err := (*sql.DB)(db).Begin()
if err != nil {
return fmt.Errorf("Failed to start database transaction: %w", err)
}
defer func() {
if tx.Rollback() == nil {
log.Println("Rolled back transaction")
}
}()
err = f(tx)
if err != nil {
return fmt.Errorf("Failed transaction: %w", err)
}
return tx.Commit()
}
func (db *DB) isEmpty(tx *sql.Tx) (bool, error) {
cmd := `SELECT COUNT(*) FROM sqlite_master WHERE type='table'`
rows, err := tx.Query(cmd)
if err != nil {
return false, fmt.Errorf("Select call failed: %w", err)
}
defer rows.Close()
if !rows.Next() {
return false, fmt.Errorf("Result set is empty")
}
var number int
err = rows.Scan(&number)
if err != nil {
return false, fmt.Errorf("Failed to scan numerical value: %w", err)
}
return number == 0, nil
}
func (db *DB) schemaVersion(tx *sql.Tx) (int, error) {
empty, err := db.isEmpty(tx)
if err != nil {
return 0, fmt.Errorf("Failed to check if DB is empty: %w", err)
}
if empty {
return 0, nil
}
rows, err := tx.Query(`SELECT value FROM metadata WHERE key='version';`)
if err != nil {
return 0, fmt.Errorf("Select call for version failed: %w", err)
}
defer rows.Close()
if rows.Next() {
return 1, nil // version field was only present in one schema version
}
rows, err = tx.Query(`SELECT value FROM metadata WHERE key='schema_version';`)
if err != nil {
return 0, fmt.Errorf("Select call for schema_version failed: %w", err)
}
defer rows.Close()
if !rows.Next() {
return 0, fmt.Errorf("No schema_version entry in metadata table")
}
var number int
err = rows.Scan(&number)
if err != nil {
return 0, fmt.Errorf("Failed to scan schema_version: %w", err)
}
return number, nil
}
//go:embed sql/migration*.sql
var migrationSQL embed.FS
func getMigrations() ([]func(tx *sql.Tx) error, error) {
migrations := make([]func(tx *sql.Tx) error, 0)
entries, err := migrationSQL.ReadDir("sql")
if err != nil {
return nil, fmt.Errorf("Failed to read embedded migration SQL FS: %w", err)
}
amount := len(entries)
for i := 0; i < amount; i++ {
file := fmt.Sprintf("sql/migration%03d.sql", i)
data, err := migrationSQL.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("Failed to read migration SQL code: %w", err)
}
migrations = append(
migrations,
func(tx *sql.Tx) error {
_, err := tx.Exec(string(data))
return err
},
)
}
return migrations, nil
}
func (db *DB) Migrate() error {
migrations, err := getMigrations()
if err != nil {
return fmt.Errorf("Failed to get migrations: %w", err)
}
return db.Transaction(func(tx *sql.Tx) error {
var version int
for index, migration := range migrations {
var err error
version, err = db.schemaVersion(tx)
if err != nil {
return fmt.Errorf("Failed to get DB schema version: %w", err)
}
if version == index {
log.Printf("Starting database migration for schema version %d", version)
err = migration(tx)
if err != nil {
return err
}
}
}
version, err := db.schemaVersion(tx)
if err != nil {
return fmt.Errorf("Failed to get DB schema version: %w", err)
}
target := len(migrations)
if version != target {
return fmt.Errorf("Expected schema version %d but detected %d", target, version)
}
log.Printf("Database schema version: %d\n", version)
return nil
})
}
func (db *DB) CreateExamples() error {
return db.Transaction(func(tx *sql.Tx) error {
recipes := RecipeTestData()
for _, recipe := range recipes {
err := recipe.Create(tx)
if err != nil {
return err
}
}
return nil
})
}
func (db *DB) Close() error {
return (*sql.DB)(db).Close()
}
|