BUGFIX: FTS5 -- make sure only minimum updates happen

This commit is contained in:
Simon Martens
2026-01-10 23:36:10 +01:00
parent 57bbd68123
commit 4c2fcb991f
5 changed files with 79 additions and 20 deletions

View File

@@ -232,6 +232,11 @@ func HasScans(contents []*dbmodels.Content) bool {
} }
func updateEntryFTS5(app core.App, entry *dbmodels.Entry) error { func updateEntryFTS5(app core.App, entry *dbmodels.Entry) error {
// Always update contents for backward compatibility
return updateEntryFTS5WithContents(app, entry, true)
}
func updateEntryFTS5WithContents(app core.App, entry *dbmodels.Entry, updateContents bool) error {
if entry == nil { if entry == nil {
return nil return nil
} }
@@ -267,6 +272,9 @@ func updateEntryFTS5(app core.App, entry *dbmodels.Entry) error {
} }
} }
// Update entry and all related contents // Update entry and conditionally update related contents
if updateContents {
return dbmodels.UpdateFTS5EntryAndRelatedContents(app, entry, places, agents, series) return dbmodels.UpdateFTS5EntryAndRelatedContents(app, entry, places, agents, series)
}
return dbmodels.UpdateFTS5Entry(app, entry, places, agents, series)
} }

View File

@@ -192,6 +192,13 @@ func (p *AlmanachEditPage) POSTSave(engine *templating.Engine, app core.App) Han
} }
} }
// Capture old values that affect content FTS5 records
oldPreferredTitle := entry.PreferredTitle()
oldYear := entry.Year()
// Check if agent relations will change (affects contents)
agentRelationsChanged := len(payload.NewAgentRelations) > 0 || len(payload.DeletedAgentRelationIDs) > 0
user := req.User() user := req.User()
if err := app.RunInTransaction(func(tx core.App) error { if err := app.RunInTransaction(func(tx core.App) error {
if err := applyEntryChanges(tx, entry, &payload, user); err != nil { if err := applyEntryChanges(tx, entry, &payload, user); err != nil {
@@ -214,17 +221,22 @@ func (p *AlmanachEditPage) POSTSave(engine *templating.Engine, app core.App) Han
}) })
} }
// Check if fields that affect contents changed
contentsNeedUpdate := entry.PreferredTitle() != oldPreferredTitle ||
entry.Year() != oldYear ||
agentRelationsChanged
// Update FTS5 index asynchronously // Update FTS5 index asynchronously
go func(appInstance core.App, entryID string) { go func(appInstance core.App, entryID string, updateContents bool) {
freshEntry, err := dbmodels.Entries_ID(appInstance, entryID) freshEntry, err := dbmodels.Entries_ID(appInstance, entryID)
if err != nil { if err != nil {
appInstance.Logger().Error("Failed to load entry for FTS5 update", "entry_id", entryID, "error", err) appInstance.Logger().Error("Failed to load entry for FTS5 update", "entry_id", entryID, "error", err)
return return
} }
if err := updateEntryFTS5(appInstance, freshEntry); err != nil { if err := updateEntryFTS5WithContents(appInstance, freshEntry, updateContents); err != nil {
appInstance.Logger().Error("Failed to update FTS5 index for entry", "entry_id", entryID, "error", err) appInstance.Logger().Error("Failed to update FTS5 index for entry", "entry_id", entryID, "error", err)
} }
}(app, entry.Id) }(app, entry.Id, contentsNeedUpdate)
freshEntry, err := dbmodels.Entries_MusenalmID(app, id) freshEntry, err := dbmodels.Entries_MusenalmID(app, id)
if err == nil { if err == nil {

View File

@@ -197,6 +197,9 @@ func (p *OrtEditPage) POST(engine *templating.Engine, app core.App) HandleFunc {
return p.renderError(engine, app, e, "Ungültiger Status.") return p.renderError(engine, app, e, "Ungültiger Status.")
} }
// Capture old name (entries depend on place name)
oldName := place.Name()
user := req.User() user := req.User()
if err := app.RunInTransaction(func(tx core.App) error { if err := app.RunInTransaction(func(tx core.App) error {
applyPlaceForm(place, formdata, name, status, user) applyPlaceForm(place, formdata, name, status, user)
@@ -206,17 +209,27 @@ func (p *OrtEditPage) POST(engine *templating.Engine, app core.App) HandleFunc {
return p.renderError(engine, app, e, "Speichern fehlgeschlagen.") return p.renderError(engine, app, e, "Speichern fehlgeschlagen.")
} }
// Update FTS5 index for place and all related entries asynchronously // Check if name changed (entries store place name)
go func(appInstance core.App, placeID string) { nameChanged := place.Name() != oldName
// Update FTS5 index for place and conditionally update related entries asynchronously
go func(appInstance core.App, placeID string, updateEntries bool) {
freshPlace, err := dbmodels.Places_ID(appInstance, placeID) freshPlace, err := dbmodels.Places_ID(appInstance, placeID)
if err != nil { if err != nil {
appInstance.Logger().Error("Failed to load place for FTS5 update", "place_id", placeID, "error", err) appInstance.Logger().Error("Failed to load place for FTS5 update", "place_id", placeID, "error", err)
return return
} }
// If name changed, update place + entries. Otherwise just update place.
if updateEntries {
if err := dbmodels.UpdateFTS5PlaceAndRelatedEntries(appInstance, freshPlace); err != nil { 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) appInstance.Logger().Error("Failed to update FTS5 index for place and entries", "place_id", placeID, "error", err)
} }
}(app, place.Id) } else {
if err := dbmodels.UpdateFTS5Place(appInstance, freshPlace); err != nil {
appInstance.Logger().Error("Failed to update FTS5 index for place", "place_id", placeID, "error", err)
}
}
}(app, place.Id, nameChanged)
redirect := fmt.Sprintf("/ort/%s/edit?saved_message=%s", id, url.QueryEscape("Änderungen gespeichert.")) redirect := fmt.Sprintf("/ort/%s/edit?saved_message=%s", id, url.QueryEscape("Änderungen gespeichert."))
return e.Redirect(http.StatusSeeOther, redirect) return e.Redirect(http.StatusSeeOther, redirect)

View File

@@ -303,6 +303,9 @@ func (p *PersonEditPage) POST(engine *templating.Engine, app core.App) HandleFun
return p.renderError(engine, app, e, "Ungültiger Status.") return p.renderError(engine, app, e, "Ungültiger Status.")
} }
// Capture old name (entries and contents depend on agent name)
oldName := agent.Name()
user := req.User() user := req.User()
if err := app.RunInTransaction(func(tx core.App) error { if err := app.RunInTransaction(func(tx core.App) error {
applyPersonForm(agent, formdata, name, status, user) applyPersonForm(agent, formdata, name, status, user)
@@ -312,17 +315,27 @@ func (p *PersonEditPage) POST(engine *templating.Engine, app core.App) HandleFun
return p.renderError(engine, app, e, "Speichern fehlgeschlagen.") return p.renderError(engine, app, e, "Speichern fehlgeschlagen.")
} }
// Update FTS5 index for agent and all related entries/contents asynchronously // Check if name changed (entries and contents store agent name)
go func(appInstance core.App, agentID string) { nameChanged := agent.Name() != oldName
// Update FTS5 index for agent and conditionally update related entries/contents asynchronously
go func(appInstance core.App, agentID string, updateRelated bool) {
freshAgent, err := dbmodels.Agents_ID(appInstance, agentID) freshAgent, err := dbmodels.Agents_ID(appInstance, agentID)
if err != nil { if err != nil {
appInstance.Logger().Error("Failed to load agent for FTS5 update", "agent_id", agentID, "error", err) appInstance.Logger().Error("Failed to load agent for FTS5 update", "agent_id", agentID, "error", err)
return return
} }
// If name changed, update agent + entries + contents. Otherwise just update agent.
if updateRelated {
if err := dbmodels.UpdateFTS5AgentAndRelated(appInstance, freshAgent); err != nil { 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) appInstance.Logger().Error("Failed to update FTS5 index for agent and related records", "agent_id", agentID, "error", err)
} }
}(app, agent.Id) } else {
if err := dbmodels.UpdateFTS5Agent(appInstance, freshAgent); err != nil {
appInstance.Logger().Error("Failed to update FTS5 index for agent", "agent_id", agentID, "error", err)
}
}
}(app, agent.Id, nameChanged)
redirect := fmt.Sprintf("/person/%s/edit?saved_message=%s", id, url.QueryEscape("Änderungen gespeichert.")) redirect := fmt.Sprintf("/person/%s/edit?saved_message=%s", id, url.QueryEscape("Änderungen gespeichert."))
return e.Redirect(http.StatusSeeOther, redirect) return e.Redirect(http.StatusSeeOther, redirect)

View File

@@ -409,6 +409,9 @@ func (p *ReiheEditPage) POST(engine *templating.Engine, app core.App) HandleFunc
return p.renderError(engine, app, e, "Ungültiger Status.") return p.renderError(engine, app, e, "Ungültiger Status.")
} }
// Capture old title (entries depend on series title)
oldTitle := series.Title()
user := req.User() user := req.User()
if err := app.RunInTransaction(func(tx core.App) error { if err := app.RunInTransaction(func(tx core.App) error {
applySeriesForm(series, formdata, title, status, user) applySeriesForm(series, formdata, title, status, user)
@@ -418,17 +421,27 @@ func (p *ReiheEditPage) POST(engine *templating.Engine, app core.App) HandleFunc
return p.renderError(engine, app, e, "Speichern fehlgeschlagen.") return p.renderError(engine, app, e, "Speichern fehlgeschlagen.")
} }
// Update FTS5 index for series and all related entries asynchronously // Check if title changed (entries store series title)
go func(appInstance core.App, seriesID string) { titleChanged := series.Title() != oldTitle
// Update FTS5 index for series and conditionally update related entries asynchronously
go func(appInstance core.App, seriesID string, updateEntries bool) {
freshSeries, err := dbmodels.Series_ID(appInstance, seriesID) freshSeries, err := dbmodels.Series_ID(appInstance, seriesID)
if err != nil { if err != nil {
appInstance.Logger().Error("Failed to load series for FTS5 update", "series_id", seriesID, "error", err) appInstance.Logger().Error("Failed to load series for FTS5 update", "series_id", seriesID, "error", err)
return return
} }
// If title changed, update series + entries. Otherwise just update series.
if updateEntries {
if err := dbmodels.UpdateFTS5SeriesAndRelatedEntries(appInstance, freshSeries); err != nil { 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) appInstance.Logger().Error("Failed to update FTS5 index for series and entries", "series_id", seriesID, "error", err)
} }
}(app, series.Id) } else {
if err := dbmodels.UpdateFTS5Series(appInstance, freshSeries); err != nil {
appInstance.Logger().Error("Failed to update FTS5 index for series", "series_id", seriesID, "error", err)
}
}
}(app, series.Id, titleChanged)
redirect := fmt.Sprintf("/reihe/%s/edit?saved_message=%s", id, url.QueryEscape("Änderungen gespeichert.")) redirect := fmt.Sprintf("/reihe/%s/edit?saved_message=%s", id, url.QueryEscape("Änderungen gespeichert."))
return e.Redirect(http.StatusSeeOther, redirect) return e.Redirect(http.StatusSeeOther, redirect)