summaryrefslogtreecommitdiff
path: root/model/database.go
diff options
context:
space:
mode:
Diffstat (limited to 'model/database.go')
-rw-r--r--model/database.go38
1 files changed, 19 insertions, 19 deletions
diff --git a/model/database.go b/model/database.go
index 4740899..0e31882 100644
--- a/model/database.go
+++ b/model/database.go
@@ -9,12 +9,10 @@ import (
"xengineering.eu/ceres/model/migrations"
)
-var db *sql.DB
+type DB sql.DB
-func ConnectDatabase(path string) {
- var err error
-
- db, err = sql.Open("sqlite3", path)
+func OpenDB(path string) *DB {
+ db, err := sql.Open("sqlite3", path)
if err != nil {
log.Fatal(err)
}
@@ -23,10 +21,12 @@ func ConnectDatabase(path string) {
if err != nil {
log.Fatal(err)
}
+
+ return (*DB)(db)
}
-func Transaction(f func(*sql.Tx) error) error {
- tx, err := db.Begin()
+func (db *DB) Transaction(f func(*sql.Tx) error) error {
+ tx, err := (*sql.DB)(db).Begin()
if err != nil {
log.Printf("Failed to start database transaction: %v", err)
return err
@@ -46,7 +46,7 @@ func Transaction(f func(*sql.Tx) error) error {
return tx.Commit()
}
-func isDatabaseEmpty(tx *sql.Tx) bool {
+func (db *DB) IsEmpty(tx *sql.Tx) bool {
cmd := `SELECT COUNT(*) FROM sqlite_master WHERE type='table'`
rows, err := tx.Query(cmd)
if err != nil {
@@ -67,7 +67,7 @@ func isDatabaseEmpty(tx *sql.Tx) bool {
return number == 0
}
-func setupMinimalDatabase(tx *sql.Tx, execVersion string) error {
+func (db *DB) setupMinimal(tx *sql.Tx, execVersion string) error {
cmd := `
CREATE TABLE metadata (
key TEXT PRIMARY KEY,
@@ -82,7 +82,7 @@ VALUES
return err
}
-func getDatabaseVersion(tx *sql.Tx) string {
+func (db *DB) Version(tx *sql.Tx) string {
rows, err := tx.Query(`SELECT value FROM metadata WHERE key='version';`)
if err != nil {
log.Fatal(err)
@@ -102,11 +102,11 @@ func getDatabaseVersion(tx *sql.Tx) string {
return version
}
-func MigrateDatabase(execVersion string) {
- err := Transaction(func(tx *sql.Tx) error {
- if isDatabaseEmpty(tx) {
+func (db *DB) Migrate(execVersion string) {
+ err := db.Transaction(func(tx *sql.Tx) error {
+ if db.IsEmpty(tx) {
log.Println("Starting with empty database")
- err := setupMinimalDatabase(tx, execVersion)
+ err := db.setupMinimal(tx, execVersion)
if err != nil {
log.Fatalf("Failed to setup minimal database schema: %v", err)
}
@@ -118,7 +118,7 @@ func MigrateDatabase(execVersion string) {
}
}
- dbVersion := getDatabaseVersion(tx)
+ dbVersion := db.Version(tx)
if dbVersion != execVersion {
log.Fatalf(
"Database version '%s' does not match executable version '%s'",
@@ -134,8 +134,8 @@ func MigrateDatabase(execVersion string) {
}
}
-func InjectExampleRecipes() {
- err := Transaction(func(tx *sql.Tx) error {
+func (db *DB) CreateExamples() {
+ err := db.Transaction(func(tx *sql.Tx) error {
recipes := RecipeTestData()
for _, recipe := range recipes {
@@ -152,8 +152,8 @@ func InjectExampleRecipes() {
}
}
-func DisconnectDatabase() {
- var err error = db.Close()
+func (db *DB) Close() {
+ err := (*sql.DB)(db).Close()
if err != nil {
log.Printf("Failed to close database: %v\n", err)
} else {