2017-06-14 07:09:44 +00:00
|
|
|
package main
|
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
import (
|
2022-02-21 03:32:53 +00:00
|
|
|
"database/sql"
|
|
|
|
"log"
|
2017-09-15 22:20:01 +00:00
|
|
|
|
2022-02-21 03:32:53 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
|
|
|
"github.com/pkg/errors"
|
2017-11-11 04:06:16 +00:00
|
|
|
)
|
2017-06-15 11:40:35 +00:00
|
|
|
|
2017-11-05 09:55:34 +00:00
|
|
|
var stmts *Stmts
|
|
|
|
|
2017-06-15 11:40:35 +00:00
|
|
|
var db *sql.DB
|
2017-09-03 04:50:31 +00:00
|
|
|
var dbAdapter string
|
2017-06-14 07:09:44 +00:00
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
// ErrNoRows is an alias of sql.ErrNoRows, just in case we end up with non-database/sql datastores
|
2017-06-28 12:05:26 +00:00
|
|
|
var ErrNoRows = sql.ErrNoRows
|
|
|
|
|
2017-10-14 07:39:22 +00:00
|
|
|
var _initDatabase func() error
|
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
func InitDatabase() (err error) {
|
2022-02-21 03:32:53 +00:00
|
|
|
stmts = &Stmts{Mocks: false}
|
|
|
|
|
|
|
|
// Engine specific code
|
|
|
|
err = _initDatabase()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
globs = &Globs{stmts}
|
|
|
|
ws := errors.WithStack
|
|
|
|
|
|
|
|
log.Print("Running the db handlers.")
|
|
|
|
err = c.DbInits.Run()
|
|
|
|
if err != nil {
|
|
|
|
return ws(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Print("Loading the usergroups.")
|
|
|
|
c.Groups, err = c.NewMemoryGroupStore()
|
|
|
|
if err != nil {
|
|
|
|
return ws(err)
|
|
|
|
}
|
|
|
|
err2 := c.Groups.LoadGroups()
|
|
|
|
if err2 != nil {
|
|
|
|
return ws(err2)
|
|
|
|
}
|
|
|
|
|
|
|
|
// We have to put this here, otherwise LoadForums() won't be able to get the last poster data when building it's forums
|
|
|
|
log.Print("Initialising the user and topic stores")
|
|
|
|
|
|
|
|
var ucache c.UserCache
|
|
|
|
if c.Config.UserCache == "static" {
|
|
|
|
ucache = c.NewMemoryUserCache(c.Config.UserCacheCapacity)
|
|
|
|
}
|
|
|
|
var tcache c.TopicCache
|
|
|
|
if c.Config.TopicCache == "static" {
|
|
|
|
tcache = c.NewMemoryTopicCache(c.Config.TopicCacheCapacity)
|
|
|
|
}
|
|
|
|
|
|
|
|
c.Users, err = c.NewDefaultUserStore(ucache)
|
|
|
|
if err != nil {
|
|
|
|
return ws(err)
|
|
|
|
}
|
|
|
|
c.Topics, err = c.NewDefaultTopicStore(tcache)
|
|
|
|
if err != nil {
|
|
|
|
return ws(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Print("Loading the forums.")
|
|
|
|
c.Forums, err = c.NewMemoryForumStore()
|
|
|
|
if err != nil {
|
|
|
|
return ws(err)
|
|
|
|
}
|
|
|
|
err = c.Forums.LoadForums()
|
|
|
|
if err != nil {
|
|
|
|
return ws(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Print("Loading the forum permissions.")
|
|
|
|
c.FPStore, err = c.NewMemoryForumPermsStore()
|
|
|
|
if err != nil {
|
|
|
|
return ws(err)
|
|
|
|
}
|
|
|
|
err = c.FPStore.Init()
|
|
|
|
if err != nil {
|
|
|
|
return ws(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Print("Loading the settings.")
|
|
|
|
err = c.LoadSettings()
|
|
|
|
if err != nil {
|
|
|
|
return ws(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Print("Loading the plugins.")
|
|
|
|
err = c.InitExtend()
|
|
|
|
if err != nil {
|
|
|
|
return ws(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Print("Loading the themes.")
|
|
|
|
err = c.Themes.LoadActiveStatus()
|
|
|
|
if err != nil {
|
|
|
|
return ws(err)
|
|
|
|
}
|
|
|
|
return nil
|
2017-06-14 07:09:44 +00:00
|
|
|
}
|