summaryrefslogtreecommitdiff
path: root/models.go
diff options
context:
space:
mode:
authorJan Dittberner <jan@dittberner.info>2017-04-22 00:06:16 +0200
committerJan Dittberner <jan@dittberner.info>2017-04-22 00:14:38 +0200
commit5a449926f417f75acff415d1a359571d86be0983 (patch)
tree0457a5ab88841156e379c977442a6e87e64e3419 /models.go
parentfd0a8ed972f6b9327d118292b46ab5c601a48ac3 (diff)
downloadcacert-boardvoting-5a449926f417f75acff415d1a359571d86be0983.tar.gz
cacert-boardvoting-5a449926f417f75acff415d1a359571d86be0983.tar.xz
cacert-boardvoting-5a449926f417f75acff415d1a359571d86be0983.zip
Use loggo for logging0.1.0
Diffstat (limited to 'models.go')
-rw-r--r--models.go123
1 files changed, 65 insertions, 58 deletions
diff --git a/models.go b/models.go
index f6dc884..35511ed 100644
--- a/models.go
+++ b/models.go
@@ -126,14 +126,19 @@ WHERE decision=$1 AND voter=$2`,
var db *sqlx.DB
func init() {
+ failed_statements := make([]string, 0)
for _, sqlStatement := range sqlStatements {
var stmt *sqlx.Stmt
stmt, err := db.Preparex(sqlStatement)
if err != nil {
- logger.Fatalf("ERROR parsing statement %s: %s", sqlStatement, err)
+ logger.Criticalf("ERROR parsing statement %s: %s", sqlStatement, err)
+ failed_statements = append(failed_statements, sqlStatement)
}
stmt.Close()
}
+ if len(failed_statements) > 0 {
+ panic(fmt.Sprintf("%d statements failed", len(failed_statements)))
+ }
migrateConf := &goose.DBConf{
MigrationsDir: config.MigrationsPath,
@@ -148,12 +153,14 @@ func init() {
latest, err := goose.GetMostRecentDBVersion(migrateConf.MigrationsDir)
if err != nil {
- logger.Panicln(err)
+ logger.Criticalf("getting the most recent database repository version failed: %s", err)
+ panic(err)
}
err = goose.RunMigrationsOnDb(migrateConf, migrateConf.MigrationsDir, latest, db.DB)
if err != nil {
- logger.Panicln(err)
+ logger.Criticalf("running database migration failed: %s", err)
+ panic(err)
}
}
@@ -278,27 +285,27 @@ type Vote struct {
func (v *Vote) Save() (err error) {
insertVoteStmt, err := db.PrepareNamed(sqlStatements[sqlCreateVote])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer insertVoteStmt.Close()
_, err = insertVoteStmt.Exec(v)
if err != nil {
- logger.Println("Error saving vote:", err)
+ logger.Errorf("saving vote failed: %s", err)
return
}
getVoteStmt, err := db.Preparex(sqlStatements[sqlLoadVote])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer getVoteStmt.Close()
err = getVoteStmt.Get(v, v.DecisionId, v.VoterId)
if err != nil {
- logger.Println("Error getting inserted vote:", err)
+ logger.Errorf("getting inserted vote failed: %s", err)
}
return
@@ -341,7 +348,7 @@ type DecisionForDisplay struct {
func FindDecisionForDisplayByTag(tag string) (decision *DecisionForDisplay, err error) {
decisionStmt, err := db.Preparex(sqlStatements[sqlLoadDecisionByTag])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer decisionStmt.Close()
@@ -352,7 +359,7 @@ func FindDecisionForDisplayByTag(tag string) (decision *DecisionForDisplay, err
decision = nil
err = nil
} else {
- logger.Printf("Error getting motion %s: %v\n", tag, err)
+ logger.Errorf("getting motion %s failed: %v", tag, err)
}
}
decision.VoteSums, err = decision.Decision.VoteSums()
@@ -372,7 +379,7 @@ func FindDecisionsForDisplayOnPage(page int64, unvoted bool, voter *Voter) (deci
decisionsStmt, err = db.Preparex(sqlStatements[sqlLoadDecisions])
}
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer decisionsStmt.Close()
@@ -384,7 +391,7 @@ func FindDecisionsForDisplayOnPage(page int64, unvoted bool, voter *Voter) (deci
rows, err = decisionsStmt.Queryx(page - 1)
}
if err != nil {
- logger.Printf("Error loading motions for page %d: %v\n", page, err)
+ logger.Errorf("loading motions for page %d failed: %v", page, err)
return
}
defer rows.Close()
@@ -392,7 +399,7 @@ func FindDecisionsForDisplayOnPage(page int64, unvoted bool, voter *Voter) (deci
for rows.Next() {
var d DecisionForDisplay
if err = rows.StructScan(&d); err != nil {
- logger.Printf("Error loading motions for page %d: %v\n", page, err)
+ logger.Errorf("loading motions for page %d failed: %v", page, err)
return
}
d.VoteSums, err = d.Decision.VoteSums()
@@ -407,14 +414,14 @@ func FindDecisionsForDisplayOnPage(page int64, unvoted bool, voter *Voter) (deci
func (d *Decision) VoteSums() (sums *VoteSums, err error) {
votesStmt, err := db.Preparex(sqlStatements[sqlLoadVoteCountsForDecision])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer votesStmt.Close()
voteRows, err := votesStmt.Queryx(d.Id)
if err != nil {
- logger.Printf("Error fetching vote sums for motion %s: %v\n", d.Tag, err)
+ logger.Errorf("fetching vote sums for motion %s failed: %v", d.Tag, err)
return
}
defer voteRows.Close()
@@ -424,7 +431,7 @@ func (d *Decision) VoteSums() (sums *VoteSums, err error) {
var vote VoteChoice
var count int
if err = voteRows.Scan(&vote, &count); err != nil {
- logger.Printf("Error fetching vote sums for motion %s: %v\n", d.Tag, err)
+ logger.Errorf("fetching vote sums for motion %s failed: %v", d.Tag, err)
return
}
switch vote {
@@ -442,13 +449,13 @@ func (d *Decision) VoteSums() (sums *VoteSums, err error) {
func (d *DecisionForDisplay) LoadVotes() (err error) {
votesStmt, err := db.Preparex(sqlStatements[sqlLoadVotesForDecision])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer votesStmt.Close()
err = votesStmt.Select(&d.Votes, d.Id)
if err != nil {
- logger.Printf("Error selecting votes for motion %s: %v\n", d.Tag, err)
+ logger.Errorf("selecting votes for motion %s failed: %v", d.Tag, err)
}
return
}
@@ -461,18 +468,18 @@ func (d *Decision) OlderExists(unvoted bool, voter *Voter) (result bool, err err
olderStmt, err = db.Preparex(sqlStatements[sqlCountOlderThanDecision])
}
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer olderStmt.Close()
if unvoted && voter != nil {
if err = olderStmt.Get(&result, d.Proposed, voter.Id); err != nil {
- logger.Printf("Error finding older motions than %s: %v\n", d.Tag, err)
+ logger.Errorf("finding older motions than %s failed: %v", d.Tag, err)
}
} else {
if err = olderStmt.Get(&result, d.Proposed); err != nil {
- logger.Printf("Error finding older motions than %s: %v\n", d.Tag, err)
+ logger.Errorf("finding older motions than %s failed: %v", d.Tag, err)
}
}
@@ -482,34 +489,34 @@ func (d *Decision) OlderExists(unvoted bool, voter *Voter) (result bool, err err
func (d *Decision) Create() (err error) {
insertDecisionStmt, err := db.PrepareNamed(sqlStatements[sqlCreateDecision])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer insertDecisionStmt.Close()
result, err := insertDecisionStmt.Exec(d)
if err != nil {
- logger.Println("Error creating motion:", err)
+ logger.Errorf("creating motion failed: %s", err)
return
}
lastInsertId, err := result.LastInsertId()
if err != nil {
- logger.Println("Error getting id of inserted motion:", err)
+ logger.Errorf("getting id of inserted motion failed: %s", err)
return
}
rescheduleChannel <- JobIdCloseDecisions
getDecisionStmt, err := db.Preparex(sqlStatements[sqlLoadDecisionById])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer getDecisionStmt.Close()
err = getDecisionStmt.Get(d, lastInsertId)
if err != nil {
- logger.Println("Error getting inserted motion:", err)
+ logger.Errorf("getting inserted motion failed: %s", err)
}
return
@@ -518,14 +525,14 @@ func (d *Decision) Create() (err error) {
func (d *Decision) LoadWithId() (err error) {
getDecisionStmt, err := db.Preparex(sqlStatements[sqlLoadDecisionById])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer getDecisionStmt.Close()
err = getDecisionStmt.Get(d, d.Id)
if err != nil {
- logger.Println("Error loading updated motion:", err)
+ logger.Errorf("loading updated motion failed: %s", err)
}
return
@@ -534,22 +541,22 @@ func (d *Decision) LoadWithId() (err error) {
func (d *Decision) Update() (err error) {
updateDecisionStmt, err := db.PrepareNamed(sqlStatements[sqlUpdateDecision])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer updateDecisionStmt.Close()
result, err := updateDecisionStmt.Exec(d)
if err != nil {
- logger.Println("Error updating motion:", err)
+ logger.Errorf("updating motion failed: %s", err)
return
}
affectedRows, err := result.RowsAffected()
if err != nil {
- logger.Print("Problem determining the affected rows")
+ logger.Errorf("Problem determining the affected rows")
return
} else if affectedRows != 1 {
- logger.Printf("WARNING wrong number of affected rows: %d (1 expected)\n", affectedRows)
+ logger.Warningf("wrong number of affected rows: %d (1 expected)", affectedRows)
}
rescheduleChannel <- JobIdCloseDecisions
@@ -560,22 +567,22 @@ func (d *Decision) Update() (err error) {
func (d *Decision) UpdateStatus() (err error) {
updateStatusStmt, err := db.PrepareNamed(sqlStatements[sqlUpdateDecisionStatus])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer updateStatusStmt.Close()
result, err := updateStatusStmt.Exec(d)
if err != nil {
- logger.Println("Error setting motion status:", err)
+ logger.Errorf("setting motion status failed: %s", err)
return
}
affectedRows, err := result.RowsAffected()
if err != nil {
- logger.Print("Problem determining the affected rows")
+ logger.Errorf("determining the affected rows failed: %s", err)
return
} else if affectedRows != 1 {
- logger.Printf("WARNING wrong number of affected rows: %d (1 expected)\n", affectedRows)
+ logger.Warningf("wrong number of affected rows: %d (1 expected)", affectedRows)
}
rescheduleChannel <- JobIdCloseDecisions
@@ -590,7 +597,7 @@ func (d *Decision) String() string {
func FindVoterByAddress(emailAddress string) (voter *Voter, err error) {
findVoterStmt, err := db.Preparex(sqlStatements[sqlLoadEnabledVoterByEmail])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer findVoterStmt.Close()
@@ -598,7 +605,7 @@ func FindVoterByAddress(emailAddress string) (voter *Voter, err error) {
voter = &Voter{}
if err = findVoterStmt.Get(voter, emailAddress); err != nil {
if err != sql.ErrNoRows {
- logger.Printf("Error getting voter for address %s: %v\n", emailAddress, err)
+ logger.Errorf("getting voter for address %s failed: %v", emailAddress, err)
} else {
err = nil
voter = nil
@@ -613,7 +620,7 @@ func (d *Decision) Close() (err error) {
voteSums, err := d.VoteSums()
if err != nil {
- logger.Println("Error getting vote sums")
+ logger.Errorf("getting vote sums failed: %s", err)
return
}
votes := voteSums.VoteCount()
@@ -631,22 +638,22 @@ func (d *Decision) Close() (err error) {
closeDecisionStmt, err := db.PrepareNamed(sqlStatements[sqlUpdateDecisionStatus])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer closeDecisionStmt.Close()
result, err := closeDecisionStmt.Exec(d)
if err != nil {
- logger.Println("Error closing vote:", err)
+ logger.Errorf("closing vote failed: %s", err)
return
}
affectedRows, err := result.RowsAffected()
if err != nil {
- logger.Println("Error getting affected rows:", err)
+ logger.Errorf("getting affected rows failed: %s", err)
}
if affectedRows != 1 {
- logger.Printf("WARNING wrong number of affected rows: %d (1 expected)\n", affectedRows)
+ logger.Warningf("wrong number of affected rows: %d (1 expected)", affectedRows)
}
NotifyMailChannel <- NewNotificationClosedDecision(d, voteSums)
@@ -657,7 +664,7 @@ func (d *Decision) Close() (err error) {
func CloseDecisions() (err error) {
getClosableDecisionsStmt, err := db.PrepareNamed(sqlStatements[sqlSelectClosableDecisions])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer getClosableDecisionsStmt.Close()
@@ -665,14 +672,14 @@ func CloseDecisions() (err error) {
decisions := make([]*Decision, 0)
rows, err := getClosableDecisionsStmt.Queryx(struct{ Now time.Time }{time.Now().UTC()})
if err != nil {
- logger.Println("Error fetching closable decisions", err)
+ logger.Errorf("fetching closable decisions failed: %s", err)
return
}
defer rows.Close()
for rows.Next() {
decision := &Decision{}
if err = rows.StructScan(decision); err != nil {
- logger.Println("Error scanning row", err)
+ logger.Errorf("scanning row failed: %s", err)
return
}
decisions = append(decisions, decision)
@@ -680,9 +687,9 @@ func CloseDecisions() (err error) {
rows.Close()
for _, decision := range decisions {
- logger.Println("DEBUG found closable decision", decision)
+ logger.Debugf("found closable decision %s", decision.Tag)
if err = decision.Close(); err != nil {
- logger.Printf("Error closing decision %s: %s\n", decision, err)
+ logger.Errorf("closing decision %s failed: %s", decision.Tag, err)
return
}
}
@@ -693,7 +700,7 @@ func CloseDecisions() (err error) {
func GetNextPendingDecisionDue() (due *time.Time, err error) {
getNextPendingDecisionDueStmt, err := db.Preparex(sqlStatements[sqlGetNextPendingDecisionDue])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer getNextPendingDecisionDueStmt.Close()
@@ -703,10 +710,10 @@ func GetNextPendingDecisionDue() (due *time.Time, err error) {
var dueTimestamp time.Time
if err = row.Scan(&dueTimestamp); err != nil {
if err == sql.ErrNoRows {
- logger.Println("DEBUG No pending decisions")
+ logger.Debugf("No pending decisions")
return nil, nil
}
- logger.Println("Error parsing result", err)
+ logger.Errorf("parsing result failed: %s", err)
return
}
due = &dueTimestamp
@@ -717,7 +724,7 @@ func GetNextPendingDecisionDue() (due *time.Time, err error) {
func GetReminderVoters() (voters *[]Voter, err error) {
getReminderVotersStmt, err := db.Preparex(sqlStatements[sqlGetReminderVoters])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer getReminderVotersStmt.Close()
@@ -725,7 +732,7 @@ func GetReminderVoters() (voters *[]Voter, err error) {
voterSlice := make([]Voter, 0)
if err = getReminderVotersStmt.Select(&voterSlice); err != nil {
- logger.Println("Error getting voters:", err)
+ logger.Errorf("getting voters failed: %s", err)
return
}
voters = &voterSlice
@@ -736,7 +743,7 @@ func GetReminderVoters() (voters *[]Voter, err error) {
func FindUnvotedDecisionsForVoter(voter *Voter) (decisions *[]Decision, err error) {
findUnvotedDecisionsForVoterStmt, err := db.Preparex(sqlStatements[sqlFindUnvotedDecisionsForVoter])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer findUnvotedDecisionsForVoterStmt.Close()
@@ -744,7 +751,7 @@ func FindUnvotedDecisionsForVoter(voter *Voter) (decisions *[]Decision, err erro
decisionsSlice := make([]Decision, 0)
if err = findUnvotedDecisionsForVoterStmt.Select(&decisionsSlice, voter.Id); err != nil {
- logger.Println("Error getting unvoted decisions:", err)
+ logger.Errorf("getting unvoted decisions failed: %s", err)
return
}
decisions = &decisionsSlice
@@ -755,14 +762,14 @@ func FindUnvotedDecisionsForVoter(voter *Voter) (decisions *[]Decision, err erro
func GetVoterById(id int64) (voter *Voter, err error) {
getVoterByIdStmt, err := db.Preparex(sqlStatements[sqlGetEnabledVoterById])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer getVoterByIdStmt.Close()
voter = &Voter{}
if err = getVoterByIdStmt.Get(voter, id); err != nil {
- logger.Println("Error getting voter:", err)
+ logger.Errorf("getting voter failed: %s", err)
return
}
@@ -772,7 +779,7 @@ func GetVoterById(id int64) (voter *Voter, err error) {
func GetVotersForProxy(proxy *Voter, decision *Decision) (voters *[]Voter, err error) {
getVotersForProxyStmt, err := db.Preparex(sqlStatements[sqlGetVotersForProxy])
if err != nil {
- logger.Println("Error preparing statement:", err)
+ logger.Errorf("preparing statement failed: %s", err)
return
}
defer getVotersForProxyStmt.Close()
@@ -780,7 +787,7 @@ func GetVotersForProxy(proxy *Voter, decision *Decision) (voters *[]Voter, err e
votersSlice := make([]Voter, 0)
if err = getVotersForProxyStmt.Select(&votersSlice, proxy.Id, decision.Id); err != nil {
- logger.Println("Error getting voters for proxy:", err)
+ logger.Errorf("Error getting voters for proxy failed: %s", err)
return
}
voters = &votersSlice