Run goose migration on application startup
authorJan Dittberner <jan@dittberner.info>
Fri, 21 Apr 2017 19:41:25 +0000 (21:41 +0200)
committerJan Dittberner <jan@dittberner.info>
Fri, 21 Apr 2017 22:14:38 +0000 (00:14 +0200)
boardvoting.go
config.yaml.example
models.go
notifications.go

index e2e9838..539013a 100644 (file)
@@ -658,15 +658,16 @@ func (h *decisionVoteHandler) ServeHTTP(w http.ResponseWriter, r *http.Request)
 }
 
 type Config struct {
-       BoardMailAddress          string `yaml:"board_mail_address"`
-       VoteNoticeAddress         string `yaml:"notice_sender_address"`
-       NotificationSenderAddress string `yaml:"reminder_sender_address"`
+       NoticeMailAddress         string `yaml:"notice_mail_address"`
+       VoteNoticeMailAddress     string `yaml:"vote_notice_mail_address"`
+       NotificationSenderAddress string `yaml:"notification_sender_address"`
        DatabaseFile              string `yaml:"database_file"`
        ClientCACertificates      string `yaml:"client_ca_certificates"`
        ServerCert                string `yaml:"server_certificate"`
        ServerKey                 string `yaml:"server_key"`
        CookieSecret              string `yaml:"cookie_secret"`
        BaseURL                   string `yaml:"base_url"`
+       MigrationsPath            string `yaml:"migrations_path"`
        MailServer                struct {
                Host string `yaml:"host"`
                Port int    `yaml:"port"`
index d2c24e3..19e96ea 100644 (file)
@@ -1,13 +1,14 @@
 ---
-board_mail_address: cacert-board@lists.cacert.org
-notice_sender_address: cacert-board-votes@lists.cacert.org
-reminder_sender_address: returns@cacert.org
+notice_mail_address: cacert-board@lists.cacert.org
+vote_notice_mail_address: cacert-board-votes@lists.cacert.org
+notification_sender_address: returns@cacert.org
 database_file: database.sqlite
 client_ca_certificates: cacert_class3.pem
 server_certificate: server.crt
 server_key: server.key
 cookie_secret: base64encoded_random_byte_value_of_at_least_32_bytes
 base_url: https://motions.cacert.org
+migrations_path: db
 mail_server:
   host: localhost
   port: 25
\ No newline at end of file
index d124387..f6dc884 100644 (file)
--- a/models.go
+++ b/models.go
@@ -1,6 +1,7 @@
 package main
 
 import (
+       "bitbucket.org/liamstask/goose/lib/goose"
        "database/sql"
        "fmt"
        "github.com/jmoiron/sqlx"
@@ -133,6 +134,27 @@ func init() {
                }
                stmt.Close()
        }
+
+       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 {
+               logger.Panicln(err)
+       }
+
+       err = goose.RunMigrationsOnDb(migrateConf, migrateConf.MigrationsDir, latest, db.DB)
+       if err != nil {
+               logger.Panicln(err)
+       }
 }
 
 type VoteType uint8
index d4c405a..aa8c7a8 100644 (file)
@@ -83,7 +83,7 @@ func buildMail(templateName string, context interface{}) (mailText *bytes.Buffer
 type notificationBase struct{}
 
 func (n *notificationBase) getRecipient() recipientData {
-       return recipientData{field: "To", address: config.BoardMailAddress, name: "CAcert board mailing list"}
+       return recipientData{field: "To", address: config.NoticeMailAddress, name: "CAcert board mailing list"}
 }
 
 type decisionReplyBase struct {
@@ -228,7 +228,7 @@ func (n *RemindVoterNotification) GetNotificationContent() *notificationContent
 type voteNotificationBase struct{}
 
 func (n *voteNotificationBase) getRecipient() recipientData {
-       return recipientData{"To", config.VoteNoticeAddress, "CAcert board votes mailing list"}
+       return recipientData{"To", config.VoteNoticeMailAddress, "CAcert board votes mailing list"}
 }
 
 type notificationProxyVote struct {