summaryrefslogtreecommitdiff
path: root/models.go
diff options
context:
space:
mode:
authorJan Dittberner <jandd@cacert.org>2018-03-29 20:08:41 +0200
committerJan Dittberner <jandd@cacert.org>2018-03-29 20:08:41 +0200
commit4dd5e0982050ae92a9a37cb62e606b2807a85c27 (patch)
treed2b9900068afe3d747524b43fbae49b86feaa7c8 /models.go
parentaea93c328e76fba300f5dd2c380b46c62a6db904 (diff)
downloadcacert-boardvoting-4dd5e0982050ae92a9a37cb62e606b2807a85c27.tar.gz
cacert-boardvoting-4dd5e0982050ae92a9a37cb62e606b2807a85c27.tar.xz
cacert-boardvoting-4dd5e0982050ae92a9a37cb62e606b2807a85c27.zip
Embed database migrations
- switch from goose to github.com/rubenv/sql-migrate - move assets (static, templates, migrations) to boardvoting package - add generated boardvoting/assets.go - remove unused static files from static directory - add package db with db migration configuration
Diffstat (limited to 'models.go')
-rw-r--r--models.go42
1 files changed, 12 insertions, 30 deletions
diff --git a/models.go b/models.go
index 75473b4..eeb49fc 100644
--- a/models.go
+++ b/models.go
@@ -1,10 +1,11 @@
package main
import (
- "bitbucket.org/liamstask/goose/lib/goose"
"database/sql"
"fmt"
+ dbmig "git.cacert.org/cacert-boardvoting/db"
"github.com/jmoiron/sqlx"
+ "github.com/rubenv/sql-migrate"
"time"
)
@@ -141,11 +142,6 @@ type Decision struct {
VoteType VoteType
}
-type Email struct {
- VoterId int64 `db:"voter"`
- Address string
-}
-
type Voter struct {
Id int64
Name string
@@ -247,41 +243,27 @@ type dbHandler struct {
var db *dbHandler
-func NewDB(database *sqlx.DB) *dbHandler {
- handler := &dbHandler{db: database}
- failed_statements := make([]string, 0)
+func NewDB(database *sql.DB) *dbHandler {
+ handler := &dbHandler{db: sqlx.NewDb(database, "sqlite3")}
+ failedStatements := make([]string, 0)
for _, sqlStatement := range sqlStatements {
var stmt *sqlx.Stmt
- stmt, err := database.Preparex(sqlStatement)
+ stmt, err := handler.db.Preparex(sqlStatement)
if err != nil {
log.Critical("ERROR parsing statement %s: %s", sqlStatement, err)
- failed_statements = append(failed_statements, sqlStatement)
+ failedStatements = append(failedStatements, sqlStatement)
}
stmt.Close()
}
- if len(failed_statements) > 0 {
- log.Panicf("%d statements failed to prepare", len(failed_statements))
- }
-
- migrateConf := &goose.DBConf{
- MigrationsDir: config.MigrationsPath,
- Env: "production",
- Driver: goose.DBDriver{
- Name: "sqlite3",
- OpenStr: config.DatabaseFile,
- Import: "github.com/mattn/go-sqlite3",
- Dialect: &goose.Sqlite3Dialect{},
- },
- }
-
- latest, err := goose.GetMostRecentDBVersion(migrateConf.MigrationsDir)
- if err != nil {
- log.Panicf("getting the most recent database repository version failed: %v", err)
+ if len(failedStatements) > 0 {
+ log.Panicf("%d statements failed to prepare", len(failedStatements))
}
- err = goose.RunMigrationsOnDb(migrateConf, migrateConf.MigrationsDir, latest, database.DB)
+ version, err := migrate.Exec(database, "sqlite3", dbmig.Migrations(), migrate.Up)
if err != nil {
log.Panicf("running database migration failed: %v", err)
+ } else {
+ log.Infof("database is now at version %d", version)
}
return handler
}