Files
housecarl/internal/store/history.go
T
abrown38andClaude Opus 5.5 92147c8681 Diffs name their chain; history shows the ruleset's diff
The ruleset's diff names the chain each hunk is in on its @@ line, as
git names the function. A history entry shows what it did to the
ruleset (web page, hc history diff, GET /history/{id}/diff), rebuilt
from the current configuration by stepping back through the later
entries. A changed setting is listed by its own fields
(builtins.drop_invalid) instead of the whole object.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-22 23:39:03 +01:00

84 lines
2.2 KiB
Go

package store
import (
"context"
"database/sql"
"encoding/json"
"errors"
"housecarl/internal/model"
)
func scanHistory(sc interface{ Scan(...any) error }) (*model.HistoryEntry, error) {
e := &model.HistoryEntry{}
var t, changes string
var uid, revertOf, revertedBy sql.NullInt64
if err := sc.Scan(&e.ID, &t, &uid, &e.Username, &e.Via, &e.Description, &changes, &revertOf, &revertedBy); err != nil {
return nil, err
}
e.Time = parseTS(t)
e.UserID, e.RevertOf, e.RevertedBy = uid.Int64, revertOf.Int64, revertedBy.Int64
if err := json.Unmarshal([]byte(changes), &e.Changes); err != nil {
return nil, err
}
return e, nil
}
const historyCols = `id, time, user_id, username, via, description, changes, revert_of, reverted_by`
// ListHistory lists entries, newest first; before > 0 pages back.
func (s *Store) ListHistory(ctx context.Context, limit int, before int64) ([]*model.HistoryEntry, error) {
if limit <= 0 || limit > 500 {
limit = 100
}
q := `SELECT ` + historyCols + ` FROM history`
var args []any
if before > 0 {
q += ` WHERE id < ?`
args = append(args, before)
}
q += ` ORDER BY id DESC LIMIT ?`
args = append(args, limit)
rows, err := s.db.QueryContext(ctx, q, args...)
if err != nil {
return nil, err
}
defer rows.Close()
out := []*model.HistoryEntry{}
for rows.Next() {
e, err := scanHistory(rows)
if err != nil {
return nil, err
}
out = append(out, e)
}
return out, rows.Err()
}
// GetHistory loads one entry.
func (s *Store) GetHistory(ctx context.Context, id int64) (*model.HistoryEntry, error) {
e, err := scanHistory(s.db.QueryRowContext(ctx, `SELECT `+historyCols+` FROM history WHERE id = ?`, id))
if errors.Is(err, sql.ErrNoRows) {
return nil, ErrNotFound
}
return e, err
}
// HistorySince lists the entries from id on, newest first.
func (s *Store) HistorySince(ctx context.Context, id int64) ([]*model.HistoryEntry, error) {
rows, err := s.db.QueryContext(ctx, `SELECT `+historyCols+` FROM history WHERE id >= ? ORDER BY id DESC`, id)
if err != nil {
return nil, err
}
defer rows.Close()
var out []*model.HistoryEntry
for rows.Next() {
e, err := scanHistory(rows)
if err != nil {
return nil, err
}
out = append(out, e)
}
return out, rows.Err()
}