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" "log"
"./common" "./common"
"github.com/pkg/errors"
) )
var stmts *Stmts var stmts *Stmts
@ -30,17 +31,17 @@ func InitDatabase() (err error) {
log.Print("Running the db handlers.") log.Print("Running the db handlers.")
err = common.DbInits.Run() err = common.DbInits.Run()
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
log.Print("Loading the usergroups.") log.Print("Loading the usergroups.")
common.Groups, err = common.NewMemoryGroupStore() common.Groups, err = common.NewMemoryGroupStore()
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
err2 := common.Groups.LoadGroups() err2 := common.Groups.LoadGroups()
if err2 != nil { 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 // 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) common.Users, err = common.NewDefaultUserStore(ucache)
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
common.Topics, err = common.NewDefaultTopicStore(tcache) common.Topics, err = common.NewDefaultTopicStore(tcache)
if err != nil { if err != nil {
return err2 return errors.WithStack(err2)
} }
log.Print("Loading the forums.") log.Print("Loading the forums.")
common.Forums, err = common.NewMemoryForumStore() common.Forums, err = common.NewMemoryForumStore()
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
err = common.Forums.LoadForums() err = common.Forums.LoadForums()
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
log.Print("Loading the forum permissions.") log.Print("Loading the forum permissions.")
common.FPStore, err = common.NewMemoryForumPermsStore() common.FPStore, err = common.NewMemoryForumPermsStore()
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
err = common.FPStore.Init() err = common.FPStore.Init()
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
log.Print("Loading the settings.") log.Print("Loading the settings.")
err = common.LoadSettings() err = common.LoadSettings()
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
log.Print("Loading the plugins.") log.Print("Loading the plugins.")
err = common.InitExtend() err = common.InitExtend()
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
log.Print("Loading the themes.") 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() err = InitDatabase()
if err != nil { if err != nil {
return errors.WithStack(err) return err
} }
err = afterDBInit() err = afterDBInit()
if err != nil { if err != nil {

View File

@ -14,6 +14,7 @@ import (
"./common" "./common"
"./query_gen/lib" "./query_gen/lib"
_ "github.com/go-sql-driver/mysql" _ "github.com/go-sql-driver/mysql"
"github.com/pkg/errors"
) )
var dbCollation = "utf8mb4_general_ci" var dbCollation = "utf8mb4_general_ci"
@ -33,7 +34,7 @@ func initMySQL() (err error) {
"collation": dbCollation, "collation": dbCollation,
}) })
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
// Set the number of max open connections // 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. // 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() err = _gen_mysql()
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
// TODO: Is there a less noisy way of doing this for tests? // TODO: Is there a less noisy way of doing this for tests?
log.Print("Preparing getActivityFeedByWatcher statement.") 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") 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 { if err != nil {
return err return errors.WithStack(err)
} }
log.Print("Preparing getActivityCountByWatcher statement.") 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` = ?") 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 { if err != nil {
return err return errors.WithStack(err)
} }
log.Print("Preparing todaysPostCount statement.") 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()") stmts.todaysPostCount, err = db.Prepare("select count(*) from replies where createdAt BETWEEN (utc_timestamp() - interval 1 day) and utc_timestamp()")
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
log.Print("Preparing todaysTopicCount statement.") 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()") stmts.todaysTopicCount, err = db.Prepare("select count(*) from topics where createdAt BETWEEN (utc_timestamp() - interval 1 day) and utc_timestamp()")
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
log.Print("Preparing todaysTopicCountByForum statement.") log.Print("Preparing todaysTopicCountByForum statement.")
// TODO: Stop hard-coding this query // 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 = ?") 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 { if err != nil {
return err return errors.WithStack(err)
} }
log.Print("Preparing todaysNewUserCount statement.") 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()") stmts.todaysNewUserCount, err = db.Prepare("select count(*) from users where createdAt BETWEEN (utc_timestamp() - interval 1 day) and utc_timestamp()")
if err != nil { if err != nil {
return err return errors.WithStack(err)
} }
return nil return nil

View File

@ -5,6 +5,7 @@ import (
"database/sql" "database/sql"
"errors" "errors"
"fmt" "fmt"
"log"
"runtime" "runtime"
"strconv" "strconv"
"strings" "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") db, err := sql.Open("mysql", config["username"]+dbpassword+"@unix("+dbsocket+")/"+config["name"]+"?collation="+dbCollation+"&parseTime=true")
log.Print("err: ", err)
if err == nil { if err == nil {
// Make sure that the connection is alive // Make sure that the connection is alive
return db, db.Ping() return db, db.Ping()
} else {
fmt.Println(err)
} }
} }