Experimenting with greater use of pkg/errors so we don't waste as much time as we're wasting here on pinning things down.

This commit is contained in:
Azareal 2018-08-29 13:23:02 +10:00
parent adb447ae06
commit 6839dd5ffb
4 changed files with 28 additions and 23 deletions

View File

@ -5,6 +5,7 @@ import (
"log"
"./common"
"github.com/pkg/errors"
)
var stmts *Stmts
@ -30,17 +31,17 @@ func InitDatabase() (err error) {
log.Print("Running the db handlers.")
err = common.DbInits.Run()
if err != nil {
return err
return errors.WithStack(err)
}
log.Print("Loading the usergroups.")
common.Groups, err = common.NewMemoryGroupStore()
if err != nil {
return err
return errors.WithStack(err)
}
err2 := common.Groups.LoadGroups()
if err2 != nil {
return err2
return errors.WithStack(err2)
}
// We have to put this here, otherwise LoadForums() won't be able to get the last poster data when building it's forums
@ -58,45 +59,48 @@ func InitDatabase() (err error) {
common.Users, err = common.NewDefaultUserStore(ucache)
if err != nil {
return err
return errors.WithStack(err)
}
common.Topics, err = common.NewDefaultTopicStore(tcache)
if err != nil {
return err2
return errors.WithStack(err2)
}
log.Print("Loading the forums.")
common.Forums, err = common.NewMemoryForumStore()
if err != nil {
return err
return errors.WithStack(err)
}
err = common.Forums.LoadForums()
if err != nil {
return err
return errors.WithStack(err)
}
log.Print("Loading the forum permissions.")
common.FPStore, err = common.NewMemoryForumPermsStore()
if err != nil {
return err
return errors.WithStack(err)
}
err = common.FPStore.Init()
if err != nil {
return err
return errors.WithStack(err)
}
log.Print("Loading the settings.")
err = common.LoadSettings()
if err != nil {
return err
return errors.WithStack(err)
}
log.Print("Loading the plugins.")
err = common.InitExtend()
if err != nil {
return err
return errors.WithStack(err)
}
log.Print("Loading the themes.")
return common.Themes.LoadActiveStatus()
if err != nil {
return errors.WithStack(common.Themes.LoadActiveStatus())
}
return nil
}

View File

@ -85,7 +85,7 @@ func gloinit() (err error) {
}
err = InitDatabase()
if err != nil {
return errors.WithStack(err)
return err
}
err = afterDBInit()
if err != nil {

View File

@ -14,6 +14,7 @@ import (
"./common"
"./query_gen/lib"
_ "github.com/go-sql-driver/mysql"
"github.com/pkg/errors"
)
var dbCollation = "utf8mb4_general_ci"
@ -33,7 +34,7 @@ func initMySQL() (err error) {
"collation": dbCollation,
})
if err != nil {
return err
return errors.WithStack(err)
}
// Set the number of max open connections
@ -47,45 +48,45 @@ func initMySQL() (err error) {
// Build the generated prepared statements, we are going to slowly move the queries over to the query generator rather than writing them all by hand, this'll make it easier for us to implement database adapters for other databases like PostgreSQL, MSSQL, SQlite, etc.
err = _gen_mysql()
if err != nil {
return err
return errors.WithStack(err)
}
// TODO: Is there a less noisy way of doing this for tests?
log.Print("Preparing getActivityFeedByWatcher statement.")
stmts.getActivityFeedByWatcher, err = db.Prepare("SELECT activity_stream_matches.asid, activity_stream.actor, activity_stream.targetUser, activity_stream.event, activity_stream.elementType, activity_stream.elementID FROM `activity_stream_matches` INNER JOIN `activity_stream` ON activity_stream_matches.asid = activity_stream.asid AND activity_stream_matches.watcher != activity_stream.actor WHERE `watcher` = ? ORDER BY activity_stream.asid DESC LIMIT 8")
if err != nil {
return err
return errors.WithStack(err)
}
log.Print("Preparing getActivityCountByWatcher statement.")
stmts.getActivityCountByWatcher, err = db.Prepare("SELECT count(*) FROM `activity_stream_matches` INNER JOIN `activity_stream` ON activity_stream_matches.asid = activity_stream.asid AND activity_stream_matches.watcher != activity_stream.actor WHERE `watcher` = ?")
if err != nil {
return err
return errors.WithStack(err)
}
log.Print("Preparing todaysPostCount statement.")
stmts.todaysPostCount, err = db.Prepare("select count(*) from replies where createdAt BETWEEN (utc_timestamp() - interval 1 day) and utc_timestamp()")
if err != nil {
return err
return errors.WithStack(err)
}
log.Print("Preparing todaysTopicCount statement.")
stmts.todaysTopicCount, err = db.Prepare("select count(*) from topics where createdAt BETWEEN (utc_timestamp() - interval 1 day) and utc_timestamp()")
if err != nil {
return err
return errors.WithStack(err)
}
log.Print("Preparing todaysTopicCountByForum statement.")
// TODO: Stop hard-coding this query
stmts.todaysTopicCountByForum, err = db.Prepare("select count(*) from topics where createdAt BETWEEN (utc_timestamp() - interval 1 day) and utc_timestamp() and parentID = ?")
if err != nil {
return err
return errors.WithStack(err)
}
log.Print("Preparing todaysNewUserCount statement.")
stmts.todaysNewUserCount, err = db.Prepare("select count(*) from users where createdAt BETWEEN (utc_timestamp() - interval 1 day) and utc_timestamp()")
if err != nil {
return err
return errors.WithStack(err)
}
return nil

View File

@ -5,6 +5,7 @@ import (
"database/sql"
"errors"
"fmt"
"log"
"runtime"
"strconv"
"strings"
@ -65,11 +66,10 @@ func (adapter *MysqlAdapter) BuildConn(config map[string]string) (*sql.DB, error
}
db, err := sql.Open("mysql", config["username"]+dbpassword+"@unix("+dbsocket+")/"+config["name"]+"?collation="+dbCollation+"&parseTime=true")
log.Print("err: ", err)
if err == nil {
// Make sure that the connection is alive
return db, db.Ping()
} else {
fmt.Println(err)
}
}