2017-06-14 07:09:44 +00:00
|
|
|
package main
|
|
|
|
|
2017-11-11 04:06:16 +00:00
|
|
|
import (
|
|
|
|
"database/sql"
|
|
|
|
"log"
|
2017-09-15 22:20:01 +00:00
|
|
|
|
2019-05-17 08:40:41 +00:00
|
|
|
c "github.com/Azareal/Gosora/common"
|
2018-08-29 03:23:02 +00:00
|
|
|
"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) {
|
2017-11-05 09:55:34 +00:00
|
|
|
stmts = &Stmts{Mocks: false}
|
|
|
|
|
2017-09-03 04:50:31 +00:00
|
|
|
// Engine specific code
|
|
|
|
err = _initDatabase()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2017-11-05 09:55:34 +00:00
|
|
|
globs = &Globs{stmts}
|
2021-04-08 05:01:27 +00:00
|
|
|
ws := errors.WithStack
|
2017-06-14 07:09:44 +00:00
|
|
|
|
2017-11-11 23:34:27 +00:00
|
|
|
log.Print("Running the db handlers.")
|
2019-05-17 08:40:41 +00:00
|
|
|
err = c.DbInits.Run()
|
2017-11-02 13:35:19 +00:00
|
|
|
if err != nil {
|
2021-04-08 05:01:27 +00:00
|
|
|
return ws(err)
|
2017-11-02 13:35:19 +00:00
|
|
|
}
|
2017-11-11 04:06:16 +00:00
|
|
|
|
|
|
|
log.Print("Loading the usergroups.")
|
2019-05-17 08:40:41 +00:00
|
|
|
c.Groups, err = c.NewMemoryGroupStore()
|
2017-06-14 07:09:44 +00:00
|
|
|
if err != nil {
|
2021-04-08 05:01:27 +00:00
|
|
|
return ws(err)
|
2017-06-14 07:09:44 +00:00
|
|
|
}
|
2019-05-17 08:40:41 +00:00
|
|
|
err2 := c.Groups.LoadGroups()
|
2017-11-11 04:06:16 +00:00
|
|
|
if err2 != nil {
|
2021-04-08 05:01:27 +00:00
|
|
|
return ws(err2)
|
2017-11-11 04:06:16 +00:00
|
|
|
}
|
2017-06-14 07:09:44 +00:00
|
|
|
|
2017-09-28 22:16:34 +00:00
|
|
|
// 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")
|
2017-11-23 05:37:08 +00:00
|
|
|
|
2019-05-17 08:40:41 +00:00
|
|
|
var ucache c.UserCache
|
|
|
|
if c.Config.UserCache == "static" {
|
|
|
|
ucache = c.NewMemoryUserCache(c.Config.UserCacheCapacity)
|
2018-06-17 07:28:18 +00:00
|
|
|
}
|
2019-05-17 08:40:41 +00:00
|
|
|
var tcache c.TopicCache
|
|
|
|
if c.Config.TopicCache == "static" {
|
|
|
|
tcache = c.NewMemoryTopicCache(c.Config.TopicCacheCapacity)
|
2017-11-11 04:06:16 +00:00
|
|
|
}
|
2017-11-23 05:37:08 +00:00
|
|
|
|
2019-05-17 08:40:41 +00:00
|
|
|
c.Users, err = c.NewDefaultUserStore(ucache)
|
2017-11-11 04:06:16 +00:00
|
|
|
if err != nil {
|
2021-04-08 05:01:27 +00:00
|
|
|
return ws(err)
|
2017-11-11 04:06:16 +00:00
|
|
|
}
|
2019-05-17 08:40:41 +00:00
|
|
|
c.Topics, err = c.NewDefaultTopicStore(tcache)
|
2017-11-23 05:37:08 +00:00
|
|
|
if err != nil {
|
2021-04-08 05:01:27 +00:00
|
|
|
return ws(err)
|
2017-09-28 22:16:34 +00:00
|
|
|
}
|
|
|
|
|
2017-06-14 07:09:44 +00:00
|
|
|
log.Print("Loading the forums.")
|
2019-05-17 08:40:41 +00:00
|
|
|
c.Forums, err = c.NewMemoryForumStore()
|
2017-11-02 13:35:19 +00:00
|
|
|
if err != nil {
|
2021-04-08 05:01:27 +00:00
|
|
|
return ws(err)
|
2017-11-02 13:35:19 +00:00
|
|
|
}
|
2019-05-17 08:40:41 +00:00
|
|
|
err = c.Forums.LoadForums()
|
2017-06-14 07:09:44 +00:00
|
|
|
if err != nil {
|
2021-04-08 05:01:27 +00:00
|
|
|
return ws(err)
|
2017-06-14 07:09:44 +00:00
|
|
|
}
|
|
|
|
|
2017-06-14 09:55:47 +00:00
|
|
|
log.Print("Loading the forum permissions.")
|
2019-05-17 08:40:41 +00:00
|
|
|
c.FPStore, err = c.NewMemoryForumPermsStore()
|
2017-11-06 04:02:35 +00:00
|
|
|
if err != nil {
|
2021-04-08 05:01:27 +00:00
|
|
|
return ws(err)
|
2017-11-06 04:02:35 +00:00
|
|
|
}
|
2019-05-17 08:40:41 +00:00
|
|
|
err = c.FPStore.Init()
|
2017-06-14 07:09:44 +00:00
|
|
|
if err != nil {
|
2021-04-08 05:01:27 +00:00
|
|
|
return ws(err)
|
2017-06-14 07:09:44 +00:00
|
|
|
}
|
2017-06-28 12:05:26 +00:00
|
|
|
|
2017-06-14 07:09:44 +00:00
|
|
|
log.Print("Loading the settings.")
|
2019-05-17 08:40:41 +00:00
|
|
|
err = c.LoadSettings()
|
2017-06-14 07:09:44 +00:00
|
|
|
if err != nil {
|
2021-04-08 05:01:27 +00:00
|
|
|
return ws(err)
|
2017-06-14 07:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Print("Loading the plugins.")
|
2019-05-17 08:40:41 +00:00
|
|
|
err = c.InitExtend()
|
2017-06-14 07:09:44 +00:00
|
|
|
if err != nil {
|
2021-04-08 05:01:27 +00:00
|
|
|
return ws(err)
|
2017-06-14 07:09:44 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
log.Print("Loading the themes.")
|
2019-05-17 08:40:41 +00:00
|
|
|
err = c.Themes.LoadActiveStatus()
|
2018-08-29 03:23:02 +00:00
|
|
|
if err != nil {
|
2021-04-08 05:01:27 +00:00
|
|
|
return ws(err)
|
2018-08-29 03:23:02 +00:00
|
|
|
}
|
|
|
|
return nil
|
2017-06-14 07:09:44 +00:00
|
|
|
}
|