sqlmodel-session 0.2.2

Session and Unit of Work for SQLModel Rust
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! N+1 Query Detection for SQLModel Rust.
//!
//! This module provides detection and warning for the N+1 query anti-pattern,
//! which occurs when code loads N objects and then lazily loads a relationship
//! for each, resulting in N+1 database queries instead of 2.
//!
//! # Example
//!
//! ```ignore
//! // Enable N+1 detection
//! session.enable_n1_detection(3);  // Warn after 3 lazy loads
//!
//! // This will trigger a warning:
//! for hero in &mut heroes {
//!     hero.team.load(&mut session).await?;  // N queries!
//! }
//!
//! // This is the fix:
//! session.load_many(&mut heroes, |h| &mut h.team).await?;  // 1 query
//! ```

use std::collections::HashMap;
use std::sync::atomic::{AtomicUsize, Ordering};

/// Tracks lazy load queries for N+1 detection.
#[derive(Debug)]
pub struct N1QueryTracker {
    /// (parent_type, relationship_name) -> query count
    counts: HashMap<(&'static str, &'static str), AtomicUsize>,
    /// Threshold for warning (queries per relationship)
    threshold: usize,
    /// Whether detection is enabled
    enabled: bool,
    /// Captured call sites for debugging
    call_sites: Vec<CallSite>,
}

impl Default for N1QueryTracker {
    fn default() -> Self {
        Self::new()
    }
}

/// Information about where a lazy load was triggered.
#[derive(Debug, Clone)]
pub struct CallSite {
    /// The parent model type name
    pub parent_type: &'static str,
    /// The relationship field name
    pub relationship: &'static str,
    /// Source file where the load was triggered
    pub file: &'static str,
    /// Line number in the source file
    pub line: u32,
    /// When the load occurred
    pub timestamp: std::time::Instant,
}

/// Statistics about N+1 detection.
#[derive(Debug, Clone, Default)]
pub struct N1Stats {
    /// Total number of lazy loads recorded
    pub total_loads: usize,
    /// Number of distinct relationships loaded
    pub relationships_loaded: usize,
    /// Number of relationships that exceeded the threshold
    pub potential_n1: usize,
}

impl N1QueryTracker {
    /// Create a new tracker with default threshold (3).
    #[must_use]
    pub fn new() -> Self {
        Self {
            counts: HashMap::new(),
            threshold: 3,
            enabled: true,
            call_sites: Vec::new(),
        }
    }

    /// Set the threshold for N+1 warnings.
    ///
    /// A warning is emitted when the number of lazy loads for a single
    /// relationship reaches this threshold.
    #[must_use]
    pub fn with_threshold(mut self, threshold: usize) -> Self {
        self.threshold = threshold;
        self
    }

    /// Get the current threshold.
    #[must_use]
    pub fn threshold(&self) -> usize {
        self.threshold
    }

    /// Check if detection is enabled.
    #[must_use]
    pub fn is_enabled(&self) -> bool {
        self.enabled
    }

    /// Disable N+1 detection.
    pub fn disable(&mut self) {
        self.enabled = false;
    }

    /// Enable N+1 detection.
    pub fn enable(&mut self) {
        self.enabled = true;
    }

    /// Record a lazy load query.
    ///
    /// This should be called whenever a lazy relationship is loaded.
    /// When the count for a (parent_type, relationship) pair reaches
    /// the threshold, a warning is emitted.
    #[track_caller]
    pub fn record_load(&mut self, parent_type: &'static str, relationship: &'static str) {
        if !self.enabled {
            return;
        }

        let key = (parent_type, relationship);
        let count = self
            .counts
            .entry(key)
            .or_insert_with(|| AtomicUsize::new(0))
            .fetch_add(1, Ordering::Relaxed)
            + 1;

        // Capture call site
        let caller = std::panic::Location::caller();
        self.call_sites.push(CallSite {
            parent_type,
            relationship,
            file: caller.file(),
            line: caller.line(),
            timestamp: std::time::Instant::now(),
        });

        // Check threshold
        if count == self.threshold {
            self.emit_warning(parent_type, relationship, count);
        }
    }

    /// Emit a warning about potential N+1 query pattern.
    fn emit_warning(&self, parent_type: &'static str, relationship: &'static str, count: usize) {
        tracing::warn!(
            target: "sqlmodel::n1",
            parent = parent_type,
            relationship = relationship,
            queries = count,
            threshold = self.threshold,
            "N+1 QUERY PATTERN DETECTED! Consider using Session::load_many() for batch loading."
        );

        // Log recent call sites for this relationship
        let sites: Vec<_> = self
            .call_sites
            .iter()
            .filter(|s| s.parent_type == parent_type && s.relationship == relationship)
            .take(5)
            .collect();

        for (i, site) in sites.iter().enumerate() {
            tracing::debug!(
                target: "sqlmodel::n1",
                index = i,
                file = site.file,
                line = site.line,
                "  [{}] {}:{}",
                i,
                site.file,
                site.line
            );
        }
    }

    /// Reset all counts and call sites.
    ///
    /// Call this at the start of a new request or transaction scope.
    pub fn reset(&mut self) {
        self.counts.clear();
        self.call_sites.clear();
    }

    /// Get the current count for a specific relationship.
    #[must_use]
    pub fn count_for(&self, parent_type: &'static str, relationship: &'static str) -> usize {
        self.counts
            .get(&(parent_type, relationship))
            .map_or(0, |c| c.load(Ordering::Relaxed))
    }

    /// Get statistics about N+1 detection.
    #[must_use]
    pub fn stats(&self) -> N1Stats {
        N1Stats {
            total_loads: self
                .counts
                .values()
                .map(|c| c.load(Ordering::Relaxed))
                .sum(),
            relationships_loaded: self.counts.len(),
            potential_n1: self
                .counts
                .iter()
                .filter(|(_, c)| c.load(Ordering::Relaxed) >= self.threshold)
                .count(),
        }
    }

    /// Get all call sites (for debugging).
    #[must_use]
    pub fn call_sites(&self) -> &[CallSite] {
        &self.call_sites
    }
}

// ============================================================================
// N1DetectionScope - RAII Guard
// ============================================================================

/// Scope helper for N+1 detection tracking.
///
/// This helper captures the initial N+1 stats when created, allowing you to
/// compare against final stats and log a summary of issues detected within
/// the scope.
///
/// **Note:** This is NOT an automatic RAII guard - you must call `log_summary()`
/// manually with the final stats. For automatic logging, wrap your code in a
/// block and call `log_summary` at the end.
///
/// # Example
///
/// ```ignore
/// // Capture initial state
/// let scope = N1DetectionScope::from_tracker(session.n1_tracker());
///
/// // Do work that might cause N+1...
/// for hero in &mut heroes {
///     hero.team.load(&mut session).await?;
/// }
///
/// // Manually log summary with final stats
/// scope.log_summary(&session.n1_stats());
/// ```
pub struct N1DetectionScope {
    /// Stats captured when the scope was created (for comparison)
    initial_stats: N1Stats,
    /// Threshold used for this scope
    threshold: usize,
    /// Whether to log on drop even if no issues
    verbose: bool,
}

impl N1DetectionScope {
    /// Create a new detection scope.
    ///
    /// This does NOT automatically enable detection on a Session - the caller
    /// should have already called `session.enable_n1_detection()`. This scope
    /// captures the initial state and logs a summary on drop.
    ///
    /// # Arguments
    ///
    /// * `initial_stats` - The current N1Stats (from `session.n1_stats()`)
    /// * `threshold` - The threshold being used for detection
    #[must_use]
    pub fn new(initial_stats: N1Stats, threshold: usize) -> Self {
        tracing::debug!(
            target: "sqlmodel::n1",
            threshold = threshold,
            "N+1 detection scope started"
        );

        Self {
            initial_stats,
            threshold,
            verbose: false,
        }
    }

    /// Create a scope from a tracker reference.
    ///
    /// Convenience method that extracts stats and threshold from an existing tracker.
    #[must_use]
    pub fn from_tracker(tracker: &N1QueryTracker) -> Self {
        Self::new(tracker.stats(), tracker.threshold())
    }

    /// Enable verbose logging (log summary even if no issues).
    #[must_use]
    pub fn verbose(mut self) -> Self {
        self.verbose = true;
        self
    }

    /// Log a summary of the detection results.
    ///
    /// Called automatically on drop, but can be called manually for
    /// intermediate reporting.
    pub fn log_summary(&self, final_stats: &N1Stats) {
        let new_loads = final_stats
            .total_loads
            .saturating_sub(self.initial_stats.total_loads);
        let new_relationships = final_stats
            .relationships_loaded
            .saturating_sub(self.initial_stats.relationships_loaded);
        let new_n1 = final_stats
            .potential_n1
            .saturating_sub(self.initial_stats.potential_n1);

        if new_n1 > 0 {
            tracing::warn!(
                target: "sqlmodel::n1",
                potential_n1 = new_n1,
                total_loads = new_loads,
                relationships = new_relationships,
                threshold = self.threshold,
                "N+1 ISSUES DETECTED in this scope! Consider using Session::load_many() for batch loading."
            );
        } else if self.verbose {
            tracing::info!(
                target: "sqlmodel::n1",
                total_loads = new_loads,
                relationships = new_relationships,
                "N+1 detection scope completed (no issues)"
            );
        } else {
            tracing::debug!(
                target: "sqlmodel::n1",
                total_loads = new_loads,
                relationships = new_relationships,
                "N+1 detection scope completed (no issues)"
            );
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_tracker_new_defaults() {
        let tracker = N1QueryTracker::new();
        assert_eq!(tracker.threshold(), 3);
        assert!(tracker.is_enabled());
    }

    #[test]
    fn test_tracker_with_threshold() {
        let tracker = N1QueryTracker::new().with_threshold(5);
        assert_eq!(tracker.threshold(), 5);
    }

    #[test]
    fn test_tracker_enable_disable() {
        let mut tracker = N1QueryTracker::new();
        assert!(tracker.is_enabled());

        tracker.disable();
        assert!(!tracker.is_enabled());

        tracker.enable();
        assert!(tracker.is_enabled());
    }

    #[test]
    fn test_tracker_records_single_load() {
        let mut tracker = N1QueryTracker::new();
        tracker.record_load("Hero", "team");
        assert_eq!(tracker.count_for("Hero", "team"), 1);
    }

    #[test]
    fn test_tracker_records_multiple_loads() {
        let mut tracker = N1QueryTracker::new().with_threshold(10);
        for _ in 0..5 {
            tracker.record_load("Hero", "team");
        }
        assert_eq!(tracker.count_for("Hero", "team"), 5);
    }

    #[test]
    fn test_tracker_records_multiple_relationships() {
        let mut tracker = N1QueryTracker::new();
        tracker.record_load("Hero", "team");
        tracker.record_load("Hero", "team");
        tracker.record_load("Hero", "powers");
        tracker.record_load("Team", "heroes");

        assert_eq!(tracker.count_for("Hero", "team"), 2);
        assert_eq!(tracker.count_for("Hero", "powers"), 1);
        assert_eq!(tracker.count_for("Team", "heroes"), 1);
    }

    #[test]
    fn test_tracker_disabled_no_recording() {
        let mut tracker = N1QueryTracker::new();
        tracker.disable();
        tracker.record_load("Hero", "team");
        assert_eq!(tracker.count_for("Hero", "team"), 0);
    }

    #[test]
    fn test_tracker_reset_clears_counts() {
        let mut tracker = N1QueryTracker::new();
        tracker.record_load("Hero", "team");
        tracker.record_load("Hero", "team");
        assert_eq!(tracker.count_for("Hero", "team"), 2);

        tracker.reset();
        assert_eq!(tracker.count_for("Hero", "team"), 0);
        assert!(tracker.call_sites().is_empty());
    }

    #[test]
    fn test_callsite_captures_location() {
        let mut tracker = N1QueryTracker::new();
        tracker.record_load("Hero", "team");

        assert_eq!(tracker.call_sites().len(), 1);
        let site = &tracker.call_sites()[0];
        assert_eq!(site.parent_type, "Hero");
        assert_eq!(site.relationship, "team");
        assert!(site.file.contains("n1_detection.rs"));
        assert!(site.line > 0);
    }

    #[test]
    fn test_callsite_timestamp_monotonic() {
        let mut tracker = N1QueryTracker::new();
        tracker.record_load("Hero", "team");
        tracker.record_load("Hero", "team");

        let sites = tracker.call_sites();
        assert!(sites[1].timestamp >= sites[0].timestamp);
    }

    #[test]
    fn test_stats_total_loads_accurate() {
        let mut tracker = N1QueryTracker::new().with_threshold(10);
        tracker.record_load("Hero", "team");
        tracker.record_load("Hero", "team");
        tracker.record_load("Hero", "powers");

        let stats = tracker.stats();
        assert_eq!(stats.total_loads, 3);
    }

    #[test]
    fn test_stats_relationships_count() {
        let mut tracker = N1QueryTracker::new();
        tracker.record_load("Hero", "team");
        tracker.record_load("Hero", "powers");
        tracker.record_load("Team", "heroes");

        let stats = tracker.stats();
        assert_eq!(stats.relationships_loaded, 3);
    }

    #[test]
    fn test_stats_potential_n1_count() {
        let mut tracker = N1QueryTracker::new().with_threshold(2);
        tracker.record_load("Hero", "team");
        tracker.record_load("Hero", "team"); // Reaches threshold
        tracker.record_load("Hero", "powers"); // Only 1

        let stats = tracker.stats();
        assert_eq!(stats.potential_n1, 1);
    }

    #[test]
    fn test_stats_default() {
        let stats = N1Stats::default();
        assert_eq!(stats.total_loads, 0);
        assert_eq!(stats.relationships_loaded, 0);
        assert_eq!(stats.potential_n1, 0);
    }

    // ========================================================================
    // N1DetectionScope Tests
    // ========================================================================

    #[test]
    fn test_scope_new_captures_initial_state() {
        let initial = N1Stats {
            total_loads: 5,
            relationships_loaded: 2,
            potential_n1: 1,
        };
        let scope = N1DetectionScope::new(initial.clone(), 3);
        assert_eq!(scope.initial_stats.total_loads, 5);
        assert_eq!(scope.threshold, 3);
    }

    #[test]
    fn test_scope_from_tracker() {
        let mut tracker = N1QueryTracker::new().with_threshold(5);
        tracker.record_load("Hero", "team");
        tracker.record_load("Hero", "team");

        let scope = N1DetectionScope::from_tracker(&tracker);
        assert_eq!(scope.threshold, 5);
        assert_eq!(scope.initial_stats.total_loads, 2);
    }

    #[test]
    fn test_scope_verbose_flag() {
        let initial = N1Stats::default();
        let scope = N1DetectionScope::new(initial, 3);
        assert!(!scope.verbose);

        let verbose_scope = scope.verbose();
        assert!(verbose_scope.verbose);
    }

    #[test]
    fn test_scope_log_summary_no_issues() {
        let initial = N1Stats::default();
        let scope = N1DetectionScope::new(initial, 3);

        // Final stats same as initial - no issues
        let final_stats = N1Stats {
            total_loads: 2,
            relationships_loaded: 1,
            potential_n1: 0,
        };

        // Should not panic and should log at debug level
        scope.log_summary(&final_stats);
    }

    #[test]
    fn test_scope_log_summary_with_issues() {
        let initial = N1Stats::default();
        let scope = N1DetectionScope::new(initial, 3);

        // Final stats show N+1 issues
        let final_stats = N1Stats {
            total_loads: 10,
            relationships_loaded: 2,
            potential_n1: 1,
        };

        // Should log warning
        scope.log_summary(&final_stats);
    }

    #[test]
    fn test_scope_calculates_delta() {
        let initial = N1Stats {
            total_loads: 5,
            relationships_loaded: 2,
            potential_n1: 0,
        };
        let scope = N1DetectionScope::new(initial, 3);

        let final_stats = N1Stats {
            total_loads: 15,
            relationships_loaded: 4,
            potential_n1: 2,
        };

        // The scope should calculate: 15-5=10 new loads, 4-2=2 new relationships, 2-0=2 new N+1s
        // We can't directly test the calculation, but the log_summary should use deltas
        scope.log_summary(&final_stats);
    }
}