diff --git a/cmd/gochan/main.go b/cmd/gochan/main.go index 4a734c8d..3738b20c 100644 --- a/cmd/gochan/main.go +++ b/cmd/gochan/main.go @@ -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() diff --git a/pkg/gcsql/provisioning.go b/pkg/gcsql/provisioning.go index 905530eb..cd9591ba 100644 --- a/pkg/gcsql/provisioning.go +++ b/pkg/gcsql/provisioning.go @@ -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) } diff --git a/pkg/gcsql/views.go b/pkg/gcsql/views.go new file mode 100644 index 00000000..9d36d17d --- /dev/null +++ b/pkg/gcsql/views.go @@ -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 +} diff --git a/plugin_api.md b/plugin_api.md index fa37f03a..78225f65 100644 --- a/plugin_api.md +++ b/plugin_api.md @@ -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 diff --git a/sql/db_views.sql b/sql/reset_views.sql similarity index 100% rename from sql/db_views.sql rename to sql/reset_views.sql