fix(db): add FK constraints, indexes, transaction fixes, and hook_executions tenant isolation (migrations 082-083) (#1246)

Co-authored-by: Bruno Clermont <bruno.clermont@gmail.com>
This commit is contained in:
Bruno ClermontandBruno Clermont authored and GitHub committed 2026-06-21 10:13:35 +07:00
1 parent c31dd21934
commit 756c4dce71
12 files changed
+397 -27

No files matched your search

+4 -2
View File
@@ -314,14 +314,16 @@ func (s *SQLiteTracingStore) GetCostSummary(ctx context.Context, opts store.Cost
}
// DeleteTracesOlderThan deletes traces and their spans older than cutoff.
// SQLite's spans.trace_id has no FK reference to traces(id), so spans must be
// deleted explicitly before the parent traces to avoid leaving orphaned span rows.
// Returns total traces deleted.
func (s *SQLiteTracingStore) DeleteTracesOlderThan(ctx context.Context, cutoff time.Time) (int64, error) {
// Delete spans belonging to old traces.
// Delete spans first — SQLite has no CASCADE on spans.trace_id.
_, err := s.db.ExecContext(ctx,
`DELETE FROM spans WHERE trace_id IN (SELECT id FROM traces WHERE created_at < ?)`, cutoff)
if err != nil {
return 0, fmt.Errorf("delete old spans: %w", err)
}
res, err := s.db.ExecContext(ctx, `DELETE FROM traces WHERE created_at < ?`, cutoff)
if err != nil {
return 0, fmt.Errorf("delete old traces: %w", err)