mirror of
https://github.com/Theodor-Springmann-Stiftung/musenalm.git
synced 2026-02-04 02:25:30 +00:00
BUGFIX: keep fts5 tables syncronized
This commit is contained in:
@@ -230,3 +230,43 @@ func HasScans(contents []*dbmodels.Content) bool {
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
func updateEntryFTS5(app core.App, entry *dbmodels.Entry) error {
|
||||
if entry == nil {
|
||||
return nil
|
||||
}
|
||||
|
||||
// Load related data for FTS5
|
||||
places := []*dbmodels.Place{}
|
||||
for _, placeID := range entry.Places() {
|
||||
place, err := dbmodels.Places_ID(app, placeID)
|
||||
if err == nil && place != nil {
|
||||
places = append(places, place)
|
||||
}
|
||||
}
|
||||
|
||||
agents := []*dbmodels.Agent{}
|
||||
agentRelations, err := dbmodels.REntriesAgents_Entry(app, entry.Id)
|
||||
if err == nil {
|
||||
for _, relation := range agentRelations {
|
||||
agent, err := dbmodels.Agents_ID(app, relation.Agent())
|
||||
if err == nil && agent != nil {
|
||||
agents = append(agents, agent)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
series := []*dbmodels.Series{}
|
||||
seriesRelations, err := dbmodels.REntriesSeries_Entry(app, entry.Id)
|
||||
if err == nil {
|
||||
for _, relation := range seriesRelations {
|
||||
s, err := dbmodels.Series_ID(app, relation.Series())
|
||||
if err == nil && s != nil {
|
||||
series = append(series, s)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// Update entry and all related contents
|
||||
return dbmodels.UpdateFTS5EntryAndRelatedContents(app, entry, places, agents, series)
|
||||
}
|
||||
|
||||
@@ -214,6 +214,18 @@ func (p *AlmanachEditPage) POSTSave(engine *templating.Engine, app core.App) Han
|
||||
})
|
||||
}
|
||||
|
||||
// Update FTS5 index asynchronously
|
||||
go func(appInstance core.App, entryID string) {
|
||||
freshEntry, err := dbmodels.Entries_ID(appInstance, entryID)
|
||||
if err != nil {
|
||||
appInstance.Logger().Error("Failed to load entry for FTS5 update", "entry_id", entryID, "error", err)
|
||||
return
|
||||
}
|
||||
if err := updateEntryFTS5(appInstance, freshEntry); err != nil {
|
||||
appInstance.Logger().Error("Failed to update FTS5 index for entry", "entry_id", entryID, "error", err)
|
||||
}
|
||||
}(app, entry.Id)
|
||||
|
||||
freshEntry, err := dbmodels.Entries_MusenalmID(app, id)
|
||||
if err == nil {
|
||||
entry = freshEntry
|
||||
@@ -297,6 +309,13 @@ func (p *AlmanachEditPage) POSTDelete(engine *templating.Engine, app core.App) H
|
||||
})
|
||||
}
|
||||
|
||||
// Delete from FTS5 index asynchronously
|
||||
go func(appInstance core.App, entryID string) {
|
||||
if err := dbmodels.DeleteFTS5Entry(appInstance, entryID); err != nil {
|
||||
appInstance.Logger().Error("Failed to delete FTS5 entry", "entry_id", entryID, "error", err)
|
||||
}
|
||||
}(app, entry.Id)
|
||||
|
||||
return e.JSON(http.StatusOK, map[string]any{
|
||||
"success": true,
|
||||
"redirect": "/suche/baende",
|
||||
|
||||
@@ -151,6 +151,18 @@ func (p *AlmanachNewPage) POSTSave(engine *templating.Engine, app core.App) Hand
|
||||
})
|
||||
}
|
||||
|
||||
// Update FTS5 index asynchronously
|
||||
go func(appInstance core.App, entryID string) {
|
||||
freshEntry, err := dbmodels.Entries_ID(appInstance, entryID)
|
||||
if err != nil {
|
||||
appInstance.Logger().Error("Failed to load entry for FTS5 update", "entry_id", entryID, "error", err)
|
||||
return
|
||||
}
|
||||
if err := updateEntryFTS5(appInstance, freshEntry); err != nil {
|
||||
appInstance.Logger().Error("Failed to update FTS5 index for new entry", "entry_id", entryID, "error", err)
|
||||
}
|
||||
}(app, entry.Id)
|
||||
|
||||
redirect := "/"
|
||||
if entry != nil {
|
||||
redirect = "/almanach/" + strconv.Itoa(entry.MusenalmID()) + "/edit?saved_message=" + url.QueryEscape("Änderungen gespeichert.")
|
||||
|
||||
@@ -206,6 +206,18 @@ func (p *OrtEditPage) POST(engine *templating.Engine, app core.App) HandleFunc {
|
||||
return p.renderError(engine, app, e, "Speichern fehlgeschlagen.")
|
||||
}
|
||||
|
||||
// Update FTS5 index for place and all related entries asynchronously
|
||||
go func(appInstance core.App, placeID string) {
|
||||
freshPlace, err := dbmodels.Places_ID(appInstance, placeID)
|
||||
if err != nil {
|
||||
appInstance.Logger().Error("Failed to load place for FTS5 update", "place_id", placeID, "error", err)
|
||||
return
|
||||
}
|
||||
if err := dbmodels.UpdateFTS5PlaceAndRelatedEntries(appInstance, freshPlace); err != nil {
|
||||
appInstance.Logger().Error("Failed to update FTS5 index for place and related records", "place_id", placeID, "error", err)
|
||||
}
|
||||
}(app, place.Id)
|
||||
|
||||
redirect := fmt.Sprintf("/ort/%s/edit?saved_message=%s", id, url.QueryEscape("Änderungen gespeichert."))
|
||||
return e.Redirect(http.StatusSeeOther, redirect)
|
||||
}
|
||||
@@ -288,6 +300,20 @@ func (p *OrtEditPage) POSTDelete(engine *templating.Engine, app core.App) Handle
|
||||
})
|
||||
}
|
||||
|
||||
// Delete place from FTS5 and update all affected entries asynchronously
|
||||
go func(appInstance core.App, placeID string, affectedEntries []*dbmodels.Entry) {
|
||||
if err := dbmodels.DeleteFTS5Place(appInstance, placeID); err != nil {
|
||||
appInstance.Logger().Error("Failed to delete place from FTS5", "place_id", placeID, "error", err)
|
||||
}
|
||||
|
||||
// Update FTS5 for all entries that had this place
|
||||
for _, entry := range affectedEntries {
|
||||
if err := updateEntryFTS5(appInstance, entry); err != nil {
|
||||
appInstance.Logger().Error("Failed to update FTS5 for entry after place deletion", "entry_id", entry.Id, "error", err)
|
||||
}
|
||||
}
|
||||
}(app, place.Id, entries)
|
||||
|
||||
return e.JSON(http.StatusOK, map[string]any{
|
||||
"success": true,
|
||||
"redirect": "/orte",
|
||||
|
||||
@@ -130,6 +130,18 @@ func (p *OrtNewPage) POST(engine *templating.Engine, app core.App) HandleFunc {
|
||||
return p.renderPage(engine, app, e, req, "Speichern fehlgeschlagen.")
|
||||
}
|
||||
|
||||
// Update FTS5 index for new place (no related entries yet) asynchronously
|
||||
go func(appInstance core.App, placeID string) {
|
||||
freshPlace, err := dbmodels.Places_ID(appInstance, placeID)
|
||||
if err != nil {
|
||||
appInstance.Logger().Error("Failed to load place for FTS5 update", "place_id", placeID, "error", err)
|
||||
return
|
||||
}
|
||||
if err := dbmodels.UpdateFTS5Place(appInstance, freshPlace); err != nil {
|
||||
appInstance.Logger().Error("Failed to update FTS5 index for new place", "place_id", placeID, "error", err)
|
||||
}
|
||||
}(app, createdPlace.Id)
|
||||
|
||||
redirect := fmt.Sprintf(
|
||||
"/ort/%s/edit?saved_message=%s",
|
||||
createdPlace.Id,
|
||||
|
||||
@@ -312,6 +312,18 @@ func (p *PersonEditPage) POST(engine *templating.Engine, app core.App) HandleFun
|
||||
return p.renderError(engine, app, e, "Speichern fehlgeschlagen.")
|
||||
}
|
||||
|
||||
// Update FTS5 index for agent and all related entries/contents asynchronously
|
||||
go func(appInstance core.App, agentID string) {
|
||||
freshAgent, err := dbmodels.Agents_ID(appInstance, agentID)
|
||||
if err != nil {
|
||||
appInstance.Logger().Error("Failed to load agent for FTS5 update", "agent_id", agentID, "error", err)
|
||||
return
|
||||
}
|
||||
if err := dbmodels.UpdateFTS5AgentAndRelated(appInstance, freshAgent); err != nil {
|
||||
appInstance.Logger().Error("Failed to update FTS5 index for agent and related records", "agent_id", agentID, "error", err)
|
||||
}
|
||||
}(app, agent.Id)
|
||||
|
||||
redirect := fmt.Sprintf("/person/%s/edit?saved_message=%s", id, url.QueryEscape("Änderungen gespeichert."))
|
||||
return e.Redirect(http.StatusSeeOther, redirect)
|
||||
}
|
||||
|
||||
@@ -134,6 +134,18 @@ func (p *PersonNewPage) POST(engine *templating.Engine, app core.App) HandleFunc
|
||||
return p.renderPage(engine, app, e, req, "Speichern fehlgeschlagen.")
|
||||
}
|
||||
|
||||
// Update FTS5 index for agent (no related records for new agent) asynchronously
|
||||
go func(appInstance core.App, agentID string) {
|
||||
freshAgent, err := dbmodels.Agents_ID(appInstance, agentID)
|
||||
if err != nil {
|
||||
appInstance.Logger().Error("Failed to load agent for FTS5 update", "agent_id", agentID, "error", err)
|
||||
return
|
||||
}
|
||||
if err := dbmodels.UpdateFTS5Agent(appInstance, freshAgent); err != nil {
|
||||
appInstance.Logger().Error("Failed to update FTS5 index for new agent", "agent_id", agentID, "error", err)
|
||||
}
|
||||
}(app, createdAgent.Id)
|
||||
|
||||
redirect := fmt.Sprintf(
|
||||
"/person/%s/edit?saved_message=%s",
|
||||
createdAgent.Id,
|
||||
|
||||
@@ -253,6 +253,19 @@ func (p *ReiheEditPage) POSTDelete(engine *templating.Engine, app core.App) Hand
|
||||
})
|
||||
}
|
||||
|
||||
// Delete series and entries from FTS5 asynchronously
|
||||
go func(appInstance core.App, seriesID string, deletedEntries []*dbmodels.Entry) {
|
||||
if err := dbmodels.DeleteFTS5Series(appInstance, seriesID); err != nil {
|
||||
appInstance.Logger().Error("Failed to delete series from FTS5", "series_id", seriesID, "error", err)
|
||||
}
|
||||
|
||||
for _, entry := range deletedEntries {
|
||||
if err := dbmodels.DeleteFTS5Entry(appInstance, entry.Id); err != nil {
|
||||
appInstance.Logger().Error("Failed to delete FTS5 entry", "entry_id", entry.Id, "error", err)
|
||||
}
|
||||
}
|
||||
}(app, series.Id, preferredEntries)
|
||||
|
||||
return e.JSON(http.StatusOK, map[string]any{
|
||||
"success": true,
|
||||
"redirect": "/reihen",
|
||||
@@ -405,6 +418,18 @@ func (p *ReiheEditPage) POST(engine *templating.Engine, app core.App) HandleFunc
|
||||
return p.renderError(engine, app, e, "Speichern fehlgeschlagen.")
|
||||
}
|
||||
|
||||
// Update FTS5 index for series and all related entries asynchronously
|
||||
go func(appInstance core.App, seriesID string) {
|
||||
freshSeries, err := dbmodels.Series_ID(appInstance, seriesID)
|
||||
if err != nil {
|
||||
appInstance.Logger().Error("Failed to load series for FTS5 update", "series_id", seriesID, "error", err)
|
||||
return
|
||||
}
|
||||
if err := dbmodels.UpdateFTS5SeriesAndRelatedEntries(appInstance, freshSeries); err != nil {
|
||||
appInstance.Logger().Error("Failed to update FTS5 index for series and related records", "series_id", seriesID, "error", err)
|
||||
}
|
||||
}(app, series.Id)
|
||||
|
||||
redirect := fmt.Sprintf("/reihe/%s/edit?saved_message=%s", id, url.QueryEscape("Änderungen gespeichert."))
|
||||
return e.Redirect(http.StatusSeeOther, redirect)
|
||||
}
|
||||
|
||||
@@ -134,6 +134,18 @@ func (p *ReiheNewPage) POST(engine *templating.Engine, app core.App) HandleFunc
|
||||
return p.renderPage(engine, app, e, req, "Speichern fehlgeschlagen.")
|
||||
}
|
||||
|
||||
// Update FTS5 index for new series (no related entries yet) asynchronously
|
||||
go func(appInstance core.App, seriesID string) {
|
||||
freshSeries, err := dbmodels.Series_ID(appInstance, seriesID)
|
||||
if err != nil {
|
||||
appInstance.Logger().Error("Failed to load series for FTS5 update", "series_id", seriesID, "error", err)
|
||||
return
|
||||
}
|
||||
if err := dbmodels.UpdateFTS5Series(appInstance, freshSeries); err != nil {
|
||||
appInstance.Logger().Error("Failed to update FTS5 index for new series", "series_id", seriesID, "error", err)
|
||||
}
|
||||
}(app, createdSeries.Id)
|
||||
|
||||
redirect := fmt.Sprintf(
|
||||
"/reihe/%d/edit?saved_message=%s",
|
||||
createdSeries.MusenalmID(),
|
||||
|
||||
Reference in New Issue
Block a user