From fd0a8ed972f6b9327d118292b46ab5c601a48ac3 Mon Sep 17 00:00:00 2001 From: Jan Dittberner Date: Fri, 21 Apr 2017 21:41:25 +0200 Subject: [PATCH] Run goose migration on application startup --- boardvoting.go | 7 ++++--- config.yaml.example | 7 ++++--- models.go | 22 ++++++++++++++++++++++ notifications.go | 4 ++-- 4 files changed, 32 insertions(+), 8 deletions(-) diff --git a/boardvoting.go b/boardvoting.go index e2e9838..539013a 100644 --- a/boardvoting.go +++ b/boardvoting.go @@ -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"` diff --git a/config.yaml.example b/config.yaml.example index d2c24e3..19e96ea 100644 --- a/config.yaml.example +++ b/config.yaml.example @@ -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 diff --git a/models.go b/models.go index d124387..f6dc884 100644 --- 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 diff --git a/notifications.go b/notifications.go index d4c405a..aa8c7a8 100644 --- a/notifications.go +++ b/notifications.go @@ -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 {