summaryrefslogtreecommitdiff
path: root/models.go
diff options
context:
space:
mode:
authorJan Dittberner <jandd@cacert.org>2021-02-28 18:25:17 +0100
committerJan Dittberner <jandd@cacert.org>2021-03-07 19:42:11 +0100
commit70cc0942ca017007d5da66870fa20185fd8fbbaa (patch)
treea4a68fc5dc302b2f337db32f7c909049533c6706 /models.go
parent03827874cfc51b4be6709459d5263ef87f31dbe4 (diff)
downloadcacert-boardvoting-70cc0942ca017007d5da66870fa20185fd8fbbaa.tar.gz
cacert-boardvoting-70cc0942ca017007d5da66870fa20185fd8fbbaa.tar.xz
cacert-boardvoting-70cc0942ca017007d5da66870fa20185fd8fbbaa.zip
Upgrade to Go 1.16 and newer libraries
- use embed from the standard library instead of packr - upgrade to sprig v3 - upgrade gomail version - use golang-migrate instead of sql-migrate to get embed support - use statigz to deliver compressed static assets
Diffstat (limited to 'models.go')
-rw-r--r--models.go52
1 files changed, 47 insertions, 5 deletions
diff --git a/models.go b/models.go
index 3647097..0a567d5 100644
--- a/models.go
+++ b/models.go
@@ -18,15 +18,17 @@ package main
import (
"database/sql"
+ "embed"
"errors"
"fmt"
"time"
+ "github.com/golang-migrate/migrate/v4"
+ "github.com/golang-migrate/migrate/v4/database/sqlite3"
"github.com/jmoiron/sqlx"
- migrate "github.com/rubenv/sql-migrate"
- log "github.com/sirupsen/logrus"
+ "github.com/johejo/golang-migrate-extra/source/iofs"
- migrations "git.cacert.org/cacert-boardvoting/db"
+ log "github.com/sirupsen/logrus"
)
type sqlKey int
@@ -282,12 +284,38 @@ type DbHandler struct {
var db *DbHandler
+//go:embed boardvoting/migrations/*
+var migrations embed.FS
+
func NewDB(database *sql.DB) *DbHandler {
handler := &DbHandler{db: sqlx.NewDb(database, "sqlite3")}
- _, err := migrate.Exec(database, "sqlite3", migrations.Migrations(), migrate.Up)
+ source, err := iofs.New(migrations, "boardvoting/migrations")
+ if err != nil {
+ log.Panicf("could not create migration source: %v", err)
+ }
+
+ driver, err := sqlite3.WithInstance(database, &sqlite3.Config{})
if err != nil {
- log.Panicf("running database migration failed: %v", err)
+ log.Panicf("could not create migration driver: %v", err)
+ }
+
+ m, err := migrate.NewWithInstance("iofs", source, "sqlite3", driver)
+ if err != nil {
+ log.Panicf("could not create migration instance: %v", err)
+ }
+
+ m.Log = NewLogger()
+
+ err = m.Up()
+ if err != nil {
+ if !errors.Is(err, migrate.ErrNoChange) {
+ log.Panicf("running database migration failed: %v", err)
+ }
+
+ log.Info("no database migrations required")
+ } else {
+ log.Info("applied database migrations")
}
failedStatements := make([]string, 0)
@@ -312,6 +340,20 @@ func NewDB(database *sql.DB) *DbHandler {
return handler
}
+type migrationLogger struct{}
+
+func (m migrationLogger) Printf(format string, v ...interface{}) {
+ log.Printf(format, v...)
+}
+
+func (m migrationLogger) Verbose() bool {
+ return log.IsLevelEnabled(log.DebugLevel)
+}
+
+func NewLogger() migrate.Logger {
+ return &migrationLogger{}
+}
+
func (d *DbHandler) Close() error {
return d.db.Close()
}