diff options
-rw-r--r-- | model/database.go | 84 |
1 files changed, 48 insertions, 36 deletions
diff --git a/model/database.go b/model/database.go index 0e31882..d3fc115 100644 --- a/model/database.go +++ b/model/database.go @@ -46,29 +46,35 @@ func (db *DB) Transaction(f func(*sql.Tx) error) error { return tx.Commit() } -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 { - log.Fatal(err) - } - defer rows.Close() +func (db *DB) IsEmpty() bool { + var number int - if !rows.Next() { - log.Fatalf("No rows on request of database table number") - } + _ = db.Transaction(func(tx *sql.Tx) error { + cmd := `SELECT COUNT(*) FROM sqlite_master WHERE type='table'` + rows, err := tx.Query(cmd) + if err != nil { + log.Fatal(err) + } + defer rows.Close() - var number int - err = rows.Scan(&number) - if err != nil { - log.Fatalf("Failed to scan number of database tables to integer") - } + if !rows.Next() { + log.Fatalf("No rows on request of database table number") + } + + err = rows.Scan(&number) + if err != nil { + log.Fatalf("Failed to scan number of database tables to integer") + } + + return nil + }) return number == 0 } -func (db *DB) setupMinimal(tx *sql.Tx, execVersion string) error { - cmd := ` +func (db *DB) setupMinimal(execVersion string) error { + return db.Transaction(func(tx *sql.Tx) error { + cmd := ` CREATE TABLE metadata ( key TEXT PRIMARY KEY, value TEXT @@ -78,35 +84,41 @@ INSERT INTO metadata VALUES ('version', ?); ` - _, err := tx.Exec(cmd, execVersion) - return err + _, err := tx.Exec(cmd, execVersion) + return err + }) } -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) - } - defer rows.Close() +func (db *DB) Version() string { + var version string - if !rows.Next() { - log.Fatalf("No rows on request of database version") - } + _ = db.Transaction(func(tx *sql.Tx) error { + rows, err := tx.Query(`SELECT value FROM metadata WHERE key='version';`) + if err != nil { + log.Fatal(err) + } + defer rows.Close() - var version string - err = rows.Scan(&version) - if err != nil { - log.Fatalf("Failed to scan database version to string") - } + if !rows.Next() { + log.Fatalf("No rows on request of database version") + } + + err = rows.Scan(&version) + if err != nil { + log.Fatalf("Failed to scan database version to string") + } + + return nil + }) return version } func (db *DB) Migrate(execVersion string) { err := db.Transaction(func(tx *sql.Tx) error { - if db.IsEmpty(tx) { + if db.IsEmpty() { log.Println("Starting with empty database") - err := db.setupMinimal(tx, execVersion) + err := db.setupMinimal(execVersion) if err != nil { log.Fatalf("Failed to setup minimal database schema: %v", err) } @@ -118,7 +130,7 @@ func (db *DB) Migrate(execVersion string) { } } - dbVersion := db.Version(tx) + dbVersion := db.Version() if dbVersion != execVersion { log.Fatalf( "Database version '%s' does not match executable version '%s'", |