Skip to content

Commit

Permalink
Reset views on gochan startup
Browse files Browse the repository at this point in the history
  • Loading branch information
Eggbertx committed Oct 18, 2024
1 parent c7fa173 commit 7a31306
Show file tree
Hide file tree
Showing 5 changed files with 50 additions and 4 deletions.
8 changes: 8 additions & 0 deletions cmd/gochan/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
13 changes: 9 additions & 4 deletions pkg/gcsql/provisioning.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package gcsql
import (
"errors"
"fmt"
"path"
"strconv"

"github.com/gochan-org/gochan/pkg/config"
Expand All @@ -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)
}
Expand Down
30 changes: 30 additions & 0 deletions pkg/gcsql/views.go
Original file line number Diff line number Diff line change
@@ -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
}
3 changes: 3 additions & 0 deletions plugin_api.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
File renamed without changes.

0 comments on commit 7a31306

Please sign in to comment.