summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Dittberner <jandd@cacert.org>2019-07-31 17:42:12 +0200
committerJan Dittberner <jandd@cacert.org>2019-07-31 17:42:12 +0200
commit41a8261552d9922da3a5759f6d0c1f856c95c2ce (patch)
tree761a05fd7747915b6324c37e91762fb82d7bf020
parent56741a1089705fc31f4e947a012972174b34f897 (diff)
downloadcacert-boardvoting-41a8261552d9922da3a5759f6d0c1f856c95c2ce.tar.gz
cacert-boardvoting-41a8261552d9922da3a5759f6d0c1f856c95c2ce.tar.xz
cacert-boardvoting-41a8261552d9922da3a5759f6d0c1f856c95c2ce.zip
Ensure that the application can start with a new database
This commit changes the NewDB function to run migrations before preparing SQL statements.
-rw-r--r--models.go9
1 files changed, 5 insertions, 4 deletions
diff --git a/models.go b/models.go
index 57515a7..19d701c 100644
--- a/models.go
+++ b/models.go
@@ -262,6 +262,11 @@ var db *dbHandler
func NewDB(database *sql.DB) *dbHandler {
handler := &dbHandler{db: sqlx.NewDb(database, "sqlite3")}
+ _, err := migrate.Exec(database, "sqlite3", migrations.Migrations(), migrate.Up)
+ if err != nil {
+ log.Panicf("running database migration failed: %v", err)
+ }
+
failedStatements := make([]string, 0)
for _, sqlStatement := range sqlStatements {
var stmt *sqlx.Stmt
@@ -276,10 +281,6 @@ func NewDB(database *sql.DB) *dbHandler {
log.Panicf("%d statements failed to prepare", len(failedStatements))
}
- _, err := migrate.Exec(database, "sqlite3", migrations.Migrations(), migrate.Up)
- if err != nil {
- log.Panicf("running database migration failed: %v", err)
- }
return handler
}