feat: batches with dry run, undo, and CSV import and export

- POST /_seo/api/v1/batch runs changes in one transaction: dry_run
  answers the diff and the conflicts (loops, chains, noindex on a
  published record, canonicals to redirected or noindex addresses) and
  rolls back; a refused change refuses the batch (D-12)
- undo of a change or a whole batch from History and through MCP, with
  the undo_stale, undo_loop and undo_gone refusals (D-13)
- CSV export and import of redirects, rules and records by URL: BOM,
  comma or semicolon, formula cells escaped, a dry-run preview, all or
  nothing with via import; the screen's apply token is HMAC-SHA256 over
  the file and a fingerprint of the data (transfer.stale)
- Import and export singleton with the seo_transfer widget action,
  History record actions undo and undo_batch, navigation entry
- MCP tools undo_change, undo_batch, apply_batch, export_csv, import_csv
- TestBatch, TestUndo, TestCSVTransfer, TestLoopRefused mcp and import
This commit is contained in:
2026-10-27 12:35:26 +01:00
parent aa0c3bcacf
commit ab3b219fec
27 changed files with 3319 additions and 96 deletions

View File

@@ -18,6 +18,21 @@ import (
type viaKey struct{}
type batchKey struct{}
// WithBatch marks ctx as part of batch id: every history row written
// through it carries the batch id, so the batch can be listed and undone
// as a whole (D-12, D-13).
func WithBatch(ctx context.Context, id string) context.Context {
return context.WithValue(ctx, batchKey{}, id)
}
// BatchOf is the batch id of ctx, "" outside a batch.
func BatchOf(ctx context.Context) string {
id, _ := ctx.Value(batchKey{}).(string)
return id
}
// WithVia marks ctx as a write through via (import, auto, undo), which
// history records instead of admin or mcp.
func WithVia(ctx context.Context, via string) context.Context {
@@ -182,10 +197,14 @@ func (s *Service) author(ctx context.Context, db *gorm.DB, via string) (*uint, s
}
// Record appends one history row per change whose values differ, inside
// db (the caller's transaction): author and via from ctx, What as given,
// before and after of the changed fields only.
// db (the caller's transaction): author, via and batch from ctx, What as
// given, before and after of the changed fields only.
func (s *Service) Record(ctx context.Context, db *gorm.DB, changes ...Change) error {
via := ViaOf(ctx)
var batch *string
if id := BatchOf(ctx); id != "" {
batch = &id
}
var authorID *uint
var authorLabel string
authored := false
@@ -199,6 +218,7 @@ func (s *Service) Record(ctx context.Context, db *gorm.DB, changes ...Change) er
authored = true
}
row := models.Revision{
BatchID: batch,
AuthorID: authorID,
AuthorLabel: authorLabel,
Via: via,