mirror of
https://github.com/Eggbertx/gochan.git
synced 2025-08-02 02:36:24 -07:00
Reset views on gochan startup
This commit is contained in:
parent
c7fa173d3a
commit
7a3130635a
5 changed files with 50 additions and 4 deletions
|
@ -77,6 +77,14 @@ func main() {
|
|||
gcutil.LogFatal().Err(err).Msg("Failed to initialize the database")
|
||||
}
|
||||
events.TriggerEvent("db-initialized")
|
||||
events.RegisterEvent([]string{"db-views-reset"}, func(trigger string, i ...interface{}) error {
|
||||
gcutil.LogInfo().Msg("SQL views reset")
|
||||
return nil
|
||||
})
|
||||
if err = gcsql.ResetViews(); err != nil {
|
||||
gcutil.LogFatal().Err(err).Send()
|
||||
}
|
||||
|
||||
parseCommandLine()
|
||||
serverutil.InitMinifier()
|
||||
siteCfg := config.GetSiteConfig()
|
||||
|
|
|
@ -3,6 +3,7 @@ package gcsql
|
|||
import (
|
||||
"errors"
|
||||
"fmt"
|
||||
"path"
|
||||
"strconv"
|
||||
|
||||
"github.com/gochan-org/gochan/pkg/config"
|
||||
|
@ -28,11 +29,15 @@ var (
|
|||
targetDatabaseVersion = -1
|
||||
)
|
||||
|
||||
func findSQLFile(filename string) string {
|
||||
return gcutil.FindResource(filename,
|
||||
path.Join("./sql/", filename),
|
||||
path.Join("/usr/local/share/gochan/", filename),
|
||||
path.Join("/usr/share/gochan/", filename))
|
||||
}
|
||||
|
||||
func initDB(initFile string) error {
|
||||
filePath := gcutil.FindResource(initFile,
|
||||
"./sql/"+initFile,
|
||||
"/usr/local/share/gochan/"+initFile,
|
||||
"/usr/share/gochan/"+initFile)
|
||||
filePath := findSQLFile(initFile)
|
||||
if filePath == "" {
|
||||
return fmt.Errorf("missing SQL database initialization file (%s), please reinstall gochan", initFile)
|
||||
}
|
||||
|
|
30
pkg/gcsql/views.go
Normal file
30
pkg/gcsql/views.go
Normal file
|
@ -0,0 +1,30 @@
|
|||
package gcsql
|
||||
|
||||
import (
|
||||
"errors"
|
||||
|
||||
"github.com/gochan-org/gochan/pkg/events"
|
||||
)
|
||||
|
||||
var (
|
||||
errMissingViewFile = errors.New("unable to find reset_views.sql, please reinstall gochan")
|
||||
)
|
||||
|
||||
func ResetViews() error {
|
||||
viewsFile := findSQLFile("reset_views.sql")
|
||||
if viewsFile == "" {
|
||||
return errMissingViewFile
|
||||
}
|
||||
err := RunSQLFile(viewsFile)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
_, err, recovered := events.TriggerEvent("db-views-reset")
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if recovered {
|
||||
return errors.New("recovered from panic while running reset views event")
|
||||
}
|
||||
return nil
|
||||
}
|
|
@ -109,6 +109,9 @@ This is a list of events that gochan may trigger at some point and can be used i
|
|||
- **db-initialized**
|
||||
- Triggered after the database is successfully initialized (db version checking, provisioning, etc)
|
||||
|
||||
- **db-views-reset**
|
||||
- Triggered after the SQL views have been successfully reset, either immediately after the database is initialized, or by a staff member
|
||||
|
||||
- **incoming-upload**
|
||||
- Triggered by the `gcsql` package when an upload is attached to a post. It is triggered before the upload is entered in the database
|
||||
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue