Make WHERE query for deleting presaved entries stricter and re-add in the error check that was removed in 713ee96a96

This commit is contained in:
David Dworken 2023-09-17 14:38:48 -07:00
parent 4c912cfaed
commit e6c76eb222
No known key found for this signature in database

View File

@ -139,14 +139,21 @@ func saveHistoryEntry(ctx context.Context) {
// Drop any entries from pre-saving since they're no longer needed // Drop any entries from pre-saving since they're no longer needed
if config.BetaMode { if config.BetaMode {
deletePresavedEntryFunc := func() error { deletePresavedEntryFunc := func() error {
tx, err := lib.MakeWhereQueryFromSearch(ctx, db, "cwd:"+entry.CurrentWorkingDirectory+" start_time:"+strconv.FormatInt(entry.StartTime.Unix(), 10)+" end_time:1970/01/01_00:00:00_+00:00") query := "cwd:" + entry.CurrentWorkingDirectory
query += " start_time:" + strconv.FormatInt(entry.StartTime.Unix(), 10)
query += " end_time:1970/01/01_00:00:00_+00:00"
tx, err := lib.MakeWhereQueryFromSearch(ctx, db, query)
if err != nil { if err != nil {
return fmt.Errorf("failed to query for pre-saved history entry: %w", err) return fmt.Errorf("failed to query for pre-saved history entry: %w", err)
} }
tx.Where("command = ?", entry.Command)
res := tx.Delete(&data.HistoryEntry{}) res := tx.Delete(&data.HistoryEntry{})
if res.Error != nil { if res.Error != nil {
return fmt.Errorf("failed to delete pre-saved history entry (expected command=%#v): %w", entry.Command, res.Error) return fmt.Errorf("failed to delete pre-saved history entry (expected command=%#v): %w", entry.Command, res.Error)
} }
if res.RowsAffected > 1 {
return fmt.Errorf("attempted to delete pre-saved entry, but something went wrong since we deleted %d rows", res.RowsAffected)
}
return nil return nil
} }
lib.CheckFatalError(lib.RetryingDbFunction(deletePresavedEntryFunc)) lib.CheckFatalError(lib.RetryingDbFunction(deletePresavedEntryFunc))