diff options
Diffstat (limited to 'model/database.go')
-rw-r--r-- | model/database.go | 25 |
1 files changed, 25 insertions, 0 deletions
diff --git a/model/database.go b/model/database.go index 3953c92..16a6cea 100644 --- a/model/database.go +++ b/model/database.go @@ -57,8 +57,33 @@ func Transaction(f func(*sql.Tx) error) error { return tx.Commit() } +func isDatabaseEmpty(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() + + if !rows.Next() { + log.Fatalf("No rows on request of database table number") + } + + var number int + err = rows.Scan(&number) + if err != nil { + log.Fatalf("Failed to scan number of database tables to integer") + } + + return number == 0 +} + func MigrateDatabase() { err := Transaction(func(tx *sql.Tx) error { + if isDatabaseEmpty(tx) { + log.Println("Starting with empty database") + } + query, err := GetSql(`migrate`) if err != nil { log.Fatal(err) |