github.com/Azareal/Gosora@v0.0.0-20210729070923-553e66b59003/database.go (about)

     1  package main
     2  
     3  import (
     4  	"database/sql"
     5  	"log"
     6  
     7  	c "github.com/Azareal/Gosora/common"
     8  	"github.com/pkg/errors"
     9  )
    10  
    11  var stmts *Stmts
    12  
    13  var db *sql.DB
    14  var dbAdapter string
    15  
    16  // ErrNoRows is an alias of sql.ErrNoRows, just in case we end up with non-database/sql datastores
    17  var ErrNoRows = sql.ErrNoRows
    18  
    19  var _initDatabase func() error
    20  
    21  func InitDatabase() (err error) {
    22  	stmts = &Stmts{Mocks: false}
    23  
    24  	// Engine specific code
    25  	err = _initDatabase()
    26  	if err != nil {
    27  		return err
    28  	}
    29  	globs = &Globs{stmts}
    30  	ws := errors.WithStack
    31  
    32  	log.Print("Running the db handlers.")
    33  	err = c.DbInits.Run()
    34  	if err != nil {
    35  		return ws(err)
    36  	}
    37  
    38  	log.Print("Loading the usergroups.")
    39  	c.Groups, err = c.NewMemoryGroupStore()
    40  	if err != nil {
    41  		return ws(err)
    42  	}
    43  	err2 := c.Groups.LoadGroups()
    44  	if err2 != nil {
    45  		return ws(err2)
    46  	}
    47  
    48  	// We have to put this here, otherwise LoadForums() won't be able to get the last poster data when building it's forums
    49  	log.Print("Initialising the user and topic stores")
    50  
    51  	var ucache c.UserCache
    52  	if c.Config.UserCache == "static" {
    53  		ucache = c.NewMemoryUserCache(c.Config.UserCacheCapacity)
    54  	}
    55  	var tcache c.TopicCache
    56  	if c.Config.TopicCache == "static" {
    57  		tcache = c.NewMemoryTopicCache(c.Config.TopicCacheCapacity)
    58  	}
    59  
    60  	c.Users, err = c.NewDefaultUserStore(ucache)
    61  	if err != nil {
    62  		return ws(err)
    63  	}
    64  	c.Topics, err = c.NewDefaultTopicStore(tcache)
    65  	if err != nil {
    66  		return ws(err)
    67  	}
    68  
    69  	log.Print("Loading the forums.")
    70  	c.Forums, err = c.NewMemoryForumStore()
    71  	if err != nil {
    72  		return ws(err)
    73  	}
    74  	err = c.Forums.LoadForums()
    75  	if err != nil {
    76  		return ws(err)
    77  	}
    78  
    79  	log.Print("Loading the forum permissions.")
    80  	c.FPStore, err = c.NewMemoryForumPermsStore()
    81  	if err != nil {
    82  		return ws(err)
    83  	}
    84  	err = c.FPStore.Init()
    85  	if err != nil {
    86  		return ws(err)
    87  	}
    88  
    89  	log.Print("Loading the settings.")
    90  	err = c.LoadSettings()
    91  	if err != nil {
    92  		return ws(err)
    93  	}
    94  
    95  	log.Print("Loading the plugins.")
    96  	err = c.InitExtend()
    97  	if err != nil {
    98  		return ws(err)
    99  	}
   100  
   101  	log.Print("Loading the themes.")
   102  	err = c.Themes.LoadActiveStatus()
   103  	if err != nil {
   104  		return ws(err)
   105  	}
   106  	return nil
   107  }