feat(PROJ-44): ocr_chars-Spalte + SetOCRResult-Helper
DB-Schema bekommt eine idempotente ocr_chars BIGINT-Spalte (Default 0). SetOCRResult schreibt status und chars atomar; GetOCRMeta liest beide mit COALESCE-Defaults. Der OCR-Worker ersetzt jeden SetOCRStatus-Call durch SetOCRResult und uebergibt die extrahierte Zeichenzahl bei 'done'.
This commit is contained in:
@@ -40,6 +40,57 @@ func (s *Store) SetOCRStatus(ctx context.Context, id, status string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
// SetOCRResult atomically writes both ocr_status and ocr_chars in one UPDATE.
|
||||
// Used by the OCR worker after a job completes (PROJ-44).
|
||||
// chars must be >= 0; for status='failed'/'skipped'/'disabled' callers pass 0.
|
||||
// Silently no-ops when no DB is configured.
|
||||
func (s *Store) SetOCRResult(ctx context.Context, id, status string, chars int64) error {
|
||||
if s.db == nil {
|
||||
return nil
|
||||
}
|
||||
if id == "" {
|
||||
return errors.New("storage: SetOCRResult: empty id")
|
||||
}
|
||||
switch status {
|
||||
case "pending", "done", "failed", "skipped", "disabled":
|
||||
default:
|
||||
return fmt.Errorf("storage: SetOCRResult: invalid status %q", status)
|
||||
}
|
||||
if chars < 0 {
|
||||
chars = 0
|
||||
}
|
||||
_, err := s.db.Exec(ctx,
|
||||
`UPDATE emails SET ocr_status = $1, ocr_chars = $2 WHERE id = $3`,
|
||||
status, chars, id,
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("storage: set ocr result: %w", err)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
// GetOCRMeta returns ocr_status (defaulting to "pending" if NULL) and
|
||||
// ocr_chars (defaulting to 0) for a single mail. Returns "", 0, nil when no
|
||||
// DB is configured or the mail is not found.
|
||||
func (s *Store) GetOCRMeta(ctx context.Context, id string) (status string, chars int64, err error) {
|
||||
if s.db == nil {
|
||||
return "", 0, nil
|
||||
}
|
||||
if id == "" {
|
||||
return "", 0, errors.New("storage: GetOCRMeta: empty id")
|
||||
}
|
||||
row := s.db.QueryRow(ctx,
|
||||
`SELECT COALESCE(ocr_status, 'pending'), COALESCE(ocr_chars, 0)
|
||||
FROM emails WHERE id = $1`, id)
|
||||
if scanErr := row.Scan(&status, &chars); scanErr != nil {
|
||||
if errors.Is(scanErr, pgx.ErrNoRows) {
|
||||
return "", 0, nil
|
||||
}
|
||||
return "", 0, fmt.Errorf("storage: get ocr meta: %w", scanErr)
|
||||
}
|
||||
return status, chars, nil
|
||||
}
|
||||
|
||||
// OCREnabled reports whether OCR processing should run for the given tenant.
|
||||
// Defaults to true when:
|
||||
// - no DB is configured (DB-less mode)
|
||||
|
||||
Reference in New Issue
Block a user