oneiriq-surql 0.2.2

Code-first database toolkit for SurrealDB - schema definitions, migrations, query building, and typed CRUD (Rust port of oneiriq-surql). Published as the `oneiriq-surql` crate; imported as `use surql::...`.
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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
//! Filesystem watcher for schema files (debounced drift detection).
//!
//! Port of `surql/migration/watcher.py`. The Python original uses
//! `watchdog` and an asyncio debounce task; this Rust port uses
//! [`notify`](https://docs.rs/notify) for cross-platform file events
//! and surfaces debounced results via a [`tokio::sync::mpsc`] channel.
//!
//! This module is feature-gated behind the `watcher` cargo feature
//! (enables `notify`, `tokio`, and `tokio-util` as optional deps).
//!
//! ## Deviation from Python
//!
//! * The Python watcher invokes a user-supplied `async` callback and
//!   uses Python's `importlib` to dynamically load modified schema
//!   modules. Rust cannot execute arbitrary source files, so the port
//!   instead takes a [`SchemaSnapshot`] provider closure (called on each
//!   debounce tick) and a recorded snapshot. The debounced result is a
//!   [`DriftReport`] (same type returned by [`crate::migration::hooks`]).
//! * File-type filtering defaults to `.rs` / `.surql` (see
//!   [`is_schema_file`]) instead of `.py`.
//! * The debounce cadence is 500 ms by default (the py default is 1 s);
//!   callers can override via [`WatcherConfig::debounce_ms`].
//!
//! ## Example
//!
//! ```no_run
//! # #[cfg(feature = "watcher")] {
//! use std::path::PathBuf;
//! use surql::migration::diff::SchemaSnapshot;
//! use surql::migration::watcher::{SchemaWatcher, WatcherConfig};
//! # async fn run() -> Result<(), Box<dyn std::error::Error>> {
//! let recorded = SchemaSnapshot::new();
//! let (watcher, mut events) = SchemaWatcher::start(
//!     &[PathBuf::from("schemas")],
//!     &WatcherConfig::new(),
//!     SchemaSnapshot::new,
//!     recorded,
//! )?;
//! // ... later in another task
//! while let Some(report) = events.recv().await {
//!     if report.drift_detected {
//!         // regenerate migration
//!     }
//! }
//! watcher.stop();
//! # Ok(())
//! # }
//! # }
//! ```

use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::{Arc, Mutex};
use std::time::Duration;

use notify::{Event, EventKind, RecommendedWatcher, RecursiveMode, Watcher as NotifyWatcher};
use tokio::sync::mpsc::{unbounded_channel, UnboundedReceiver};

use crate::error::{Result, SurqlError};
use crate::migration::diff::SchemaSnapshot;
use crate::migration::hooks::{check_schema_drift_from_snapshots, DriftReport};

// ---------------------------------------------------------------------------
// Public configuration
// ---------------------------------------------------------------------------

/// Configuration for [`SchemaWatcher`].
#[derive(Debug, Clone)]
pub struct WatcherConfig {
    /// Debounce window (ms). Events that arrive within this window are
    /// collapsed into a single drift check. Default: 500 ms.
    pub debounce_ms: u64,
    /// Whether to watch directories recursively. Default: `true`.
    pub recursive: bool,
    /// File extensions treated as schema files. Default: `["rs", "surql"]`.
    pub extensions: Vec<String>,
}

impl WatcherConfig {
    /// Construct a default watcher configuration.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Override the debounce window (milliseconds).
    #[must_use]
    pub fn debounce_ms(mut self, ms: u64) -> Self {
        self.debounce_ms = ms;
        self
    }

    /// Toggle recursive watching of directories.
    #[must_use]
    pub fn recursive(mut self, recursive: bool) -> Self {
        self.recursive = recursive;
        self
    }

    /// Replace the file-extension allowlist.
    #[must_use]
    pub fn extensions<I, S>(mut self, exts: I) -> Self
    where
        I: IntoIterator<Item = S>,
        S: Into<String>,
    {
        self.extensions = exts.into_iter().map(Into::into).collect();
        self
    }
}

impl Default for WatcherConfig {
    fn default() -> Self {
        Self {
            debounce_ms: 500,
            recursive: true,
            extensions: vec!["rs".to_string(), "surql".to_string()],
        }
    }
}

/// Return `true` if `path` matches the default schema-file allowlist
/// (`.rs` / `.surql`).
#[must_use]
pub fn is_schema_file(path: &Path) -> bool {
    matches!(
        path.extension().and_then(|e| e.to_str()),
        Some("rs" | "surql")
    )
}

// ---------------------------------------------------------------------------
// Watcher
// ---------------------------------------------------------------------------

/// Active schema file watcher.
///
/// Owns the [`notify`] watcher handle and a background debounce task.
/// Dropping the value stops the watcher; [`SchemaWatcher::stop`] is
/// provided as an explicit shutdown helper.
pub struct SchemaWatcher {
    running: Arc<AtomicBool>,
    _watcher: RecommendedWatcher,
}

impl std::fmt::Debug for SchemaWatcher {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SchemaWatcher")
            .field("running", &self.running.load(Ordering::Relaxed))
            .finish_non_exhaustive()
    }
}

impl SchemaWatcher {
    /// Start watching `paths` for schema file changes.
    ///
    /// * `paths` — directories or files to monitor. Non-existent paths
    ///   are skipped with a warning; watching fails hard only if every
    ///   path is invalid.
    /// * `config` — knobs for debounce / recursion / extension filter.
    /// * `current_snapshot_provider` — closure invoked on each debounce
    ///   tick to produce the "code-side" schema. Must be `Send + 'static`.
    /// * `recorded_snapshot` — baseline the current snapshot is compared
    ///   against. Held for the lifetime of the watcher.
    ///
    /// Returns the watcher handle plus a receiver that yields a
    /// [`DriftReport`] every debounce tick.
    ///
    /// # Errors
    ///
    /// Returns [`SurqlError::MigrationWatcher`] if the underlying
    /// `notify` watcher cannot be constructed, or if no paths can be
    /// registered.
    pub fn start<F>(
        paths: &[PathBuf],
        config: &WatcherConfig,
        current_snapshot_provider: F,
        recorded_snapshot: SchemaSnapshot,
    ) -> Result<(Self, UnboundedReceiver<DriftReport>)>
    where
        F: Fn() -> SchemaSnapshot + Send + Sync + 'static,
    {
        let (report_tx, report_rx) = unbounded_channel::<DriftReport>();
        let running = Arc::new(AtomicBool::new(true));
        let pending_flag = Arc::new(AtomicBool::new(false));
        let (event_tx, event_rx) = unbounded_channel::<()>();

        let allow_ext = config.extensions.clone();
        let mut watcher = build_notify_watcher(Arc::clone(&pending_flag), event_tx, allow_ext)?;
        register_paths(&mut watcher, paths, config)?;

        spawn_debounce_task(
            Arc::clone(&running),
            Arc::clone(&pending_flag),
            event_rx,
            Duration::from_millis(config.debounce_ms),
            Arc::new(Mutex::new(recorded_snapshot)),
            Arc::new(current_snapshot_provider),
            report_tx,
        );

        Ok((
            Self {
                running,
                _watcher: watcher,
            },
            report_rx,
        ))
    }

    /// Stop the watcher. Safe to call multiple times.
    pub fn stop(&self) {
        self.running.store(false, Ordering::Release);
    }
}

impl Drop for SchemaWatcher {
    fn drop(&mut self) {
        self.stop();
    }
}

fn build_notify_watcher(
    pending_flag: Arc<AtomicBool>,
    event_tx: tokio::sync::mpsc::UnboundedSender<()>,
    allow_ext: Vec<String>,
) -> Result<RecommendedWatcher> {
    NotifyWatcher::new(
        move |res: notify::Result<Event>| match res {
            Ok(event) => {
                if !event_of_interest(&event, &allow_ext) {
                    return;
                }
                pending_flag.store(true, Ordering::Release);
                let _ = event_tx.send(());
            }
            Err(err) => {
                tracing::warn!(
                    target: "surql::migration::watcher",
                    error = %err,
                    "watcher_event_error",
                );
            }
        },
        notify::Config::default(),
    )
    .map_err(|e| SurqlError::MigrationWatcher {
        reason: format!("failed to construct file watcher: {e}"),
    })
}

fn register_paths(
    watcher: &mut RecommendedWatcher,
    paths: &[PathBuf],
    config: &WatcherConfig,
) -> Result<()> {
    let recursive = if config.recursive {
        RecursiveMode::Recursive
    } else {
        RecursiveMode::NonRecursive
    };
    let mut registered = 0usize;
    for path in paths {
        if !path.exists() {
            tracing::warn!(
                target: "surql::migration::watcher",
                path = %path.display(),
                "path_not_found",
            );
            continue;
        }
        let effective_mode = if path.is_dir() {
            recursive
        } else {
            RecursiveMode::NonRecursive
        };
        match watcher.watch(path, effective_mode) {
            Ok(()) => registered += 1,
            Err(e) => {
                tracing::warn!(
                    target: "surql::migration::watcher",
                    path = %path.display(),
                    error = %e,
                    "failed_to_register_watch",
                );
            }
        }
    }
    if registered == 0 {
        return Err(SurqlError::MigrationWatcher {
            reason: "no paths could be registered with the watcher".to_string(),
        });
    }
    Ok(())
}

fn spawn_debounce_task<F>(
    running: Arc<AtomicBool>,
    pending: Arc<AtomicBool>,
    mut event_rx: UnboundedReceiver<()>,
    debounce: Duration,
    recorded: Arc<Mutex<SchemaSnapshot>>,
    provider: Arc<F>,
    report_tx: tokio::sync::mpsc::UnboundedSender<DriftReport>,
) where
    F: Fn() -> SchemaSnapshot + Send + Sync + 'static,
{
    tokio::spawn(async move {
        while running.load(Ordering::Acquire) {
            if event_rx.recv().await.is_none() {
                break;
            }
            // Collapse subsequent events that arrive during the window.
            while tokio::time::timeout(debounce, event_rx.recv())
                .await
                .is_ok()
            {
                // A value (Some or None) arrived in-time; keep collapsing
                // until the window goes quiet.
            }
            if !running.load(Ordering::Acquire) {
                break;
            }
            if !pending.swap(false, Ordering::AcqRel) {
                continue;
            }
            let report = {
                let code = (provider)();
                let recorded = recorded.lock().expect("recorded mutex poisoned");
                check_schema_drift_from_snapshots(&code, &recorded)
            };
            if report_tx.send(report).is_err() {
                break;
            }
        }
    });
}

fn event_of_interest(event: &Event, extensions: &[String]) -> bool {
    matches!(
        event.kind,
        EventKind::Create(_) | EventKind::Modify(_) | EventKind::Remove(_)
    ) && event.paths.iter().any(|p| {
        matches!(
            p.extension().and_then(|e| e.to_str()),
            Some(ext) if extensions.iter().any(|allowed| allowed == ext)
        )
    })
}

// ---------------------------------------------------------------------------
// Tests
// ---------------------------------------------------------------------------

#[cfg(test)]
mod tests {
    use super::*;
    use crate::migration::diff::SchemaSnapshot;
    use crate::schema::table::table_schema;
    use notify::event::{CreateKind, EventAttributes, ModifyKind};
    use std::fs;
    use std::sync::atomic::{AtomicU64, Ordering as AtOrd};
    use std::time::{Duration, SystemTime, UNIX_EPOCH};

    static TEST_DIR_COUNTER: AtomicU64 = AtomicU64::new(0);

    fn unique_temp_dir(tag: &str) -> PathBuf {
        let nanos: u128 = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .map_or(0, |d| d.as_nanos());
        let n = TEST_DIR_COUNTER.fetch_add(1, AtOrd::Relaxed);
        let pid = std::process::id();
        let dir = std::env::temp_dir().join(format!("surql-watcher-{tag}-{pid}-{nanos}-{n}"));
        fs::create_dir_all(&dir).expect("create temp dir");
        dir
    }

    // --- is_schema_file ---------------------------------------------------

    #[test]
    fn is_schema_file_accepts_rs_and_surql() {
        assert!(is_schema_file(&PathBuf::from("user.rs")));
        assert!(is_schema_file(&PathBuf::from("schema/20260101_x.surql")));
    }

    #[test]
    fn is_schema_file_rejects_other_ext() {
        assert!(!is_schema_file(&PathBuf::from("README.md")));
        assert!(!is_schema_file(&PathBuf::from("Cargo.toml")));
        assert!(!is_schema_file(&PathBuf::from("a.py")));
    }

    // --- WatcherConfig ----------------------------------------------------

    #[test]
    fn watcher_config_defaults() {
        let c = WatcherConfig::new();
        assert_eq!(c.debounce_ms, 500);
        assert!(c.recursive);
        assert_eq!(c.extensions, vec!["rs".to_string(), "surql".to_string()]);
    }

    #[test]
    fn watcher_config_builders_override() {
        let c = WatcherConfig::new()
            .debounce_ms(50)
            .recursive(false)
            .extensions(["toml"]);
        assert_eq!(c.debounce_ms, 50);
        assert!(!c.recursive);
        assert_eq!(c.extensions, vec!["toml".to_string()]);
    }

    // --- event_of_interest ------------------------------------------------

    fn make_event(kind: EventKind, paths: Vec<PathBuf>) -> Event {
        Event {
            kind,
            paths,
            attrs: EventAttributes::new(),
        }
    }

    #[test]
    fn event_of_interest_accepts_surql_modify() {
        let e = make_event(
            EventKind::Modify(ModifyKind::Any),
            vec![PathBuf::from("schemas/user.surql")],
        );
        assert!(event_of_interest(
            &e,
            &["rs".to_string(), "surql".to_string()]
        ));
    }

    #[test]
    fn event_of_interest_rejects_non_listed_extension() {
        let e = make_event(
            EventKind::Modify(ModifyKind::Any),
            vec![PathBuf::from("schemas/README.md")],
        );
        assert!(!event_of_interest(
            &e,
            &["rs".to_string(), "surql".to_string()]
        ));
    }

    #[test]
    fn event_of_interest_rejects_access_kind() {
        // EventKind::Access is not in the create/modify/remove set.
        let e = make_event(
            EventKind::Access(notify::event::AccessKind::Read),
            vec![PathBuf::from("schemas/user.surql")],
        );
        assert!(!event_of_interest(
            &e,
            &["rs".to_string(), "surql".to_string()]
        ));
    }

    #[test]
    fn event_of_interest_accepts_create_kind() {
        let e = make_event(
            EventKind::Create(CreateKind::File),
            vec![PathBuf::from("schemas/new.rs")],
        );
        assert!(event_of_interest(
            &e,
            &["rs".to_string(), "surql".to_string()]
        ));
    }

    // --- SchemaWatcher (live file events) ---------------------------------

    #[tokio::test]
    async fn start_fails_when_all_paths_missing() {
        let missing = std::env::temp_dir().join("surql-watcher-never-xyz-1-2-3");
        let err = SchemaWatcher::start(
            &[missing],
            &WatcherConfig::new(),
            SchemaSnapshot::new,
            SchemaSnapshot::new(),
        )
        .expect_err("should fail when every path is missing");
        assert!(matches!(err, SurqlError::MigrationWatcher { .. }));
    }

    #[tokio::test]
    async fn start_succeeds_and_stop_returns_without_panic() {
        let dir = unique_temp_dir("start-stop");
        let (w, _rx) = SchemaWatcher::start(
            std::slice::from_ref(&dir),
            &WatcherConfig::new().debounce_ms(50),
            SchemaSnapshot::new,
            SchemaSnapshot::new(),
        )
        .expect("start watcher");
        w.stop();
        // Dropping should not panic either.
        drop(w);
        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn file_event_triggers_debounced_report_with_drift() {
        let dir = unique_temp_dir("drift-report");

        // Code-side snapshot exposes a `user` table; recorded is empty
        // so the resulting report must detect drift.
        let provider = || SchemaSnapshot {
            tables: vec![table_schema("user")],
            edges: vec![],
        };
        let recorded = SchemaSnapshot::new();

        let (w, mut rx) = SchemaWatcher::start(
            std::slice::from_ref(&dir),
            &WatcherConfig::new().debounce_ms(50),
            provider,
            recorded,
        )
        .expect("start watcher");

        // Trigger at least one Create event.
        let file = dir.join("user.surql");
        fs::write(&file, "-- @up\nSELECT 1;\n-- @down\nSELECT 2;\n").unwrap();

        let report = tokio::time::timeout(Duration::from_secs(5), rx.recv())
            .await
            .expect("should receive a report before timeout")
            .expect("channel should yield a report");

        assert!(report.drift_detected);
        w.stop();
        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn non_schema_file_does_not_trigger_report() {
        let dir = unique_temp_dir("non-schema");
        let (w, mut rx) = SchemaWatcher::start(
            std::slice::from_ref(&dir),
            &WatcherConfig::new().debounce_ms(50),
            SchemaSnapshot::new,
            SchemaSnapshot::new(),
        )
        .expect("start watcher");

        // Touch an unrelated file.
        fs::write(dir.join("NOTES.md"), "not a schema\n").unwrap();

        let got = tokio::time::timeout(Duration::from_millis(400), rx.recv()).await;
        assert!(got.is_err(), "should time out with no report");
        w.stop();
        let _ = fs::remove_dir_all(&dir);
    }

    #[tokio::test]
    async fn debounces_multiple_rapid_events_into_one_report() {
        let dir = unique_temp_dir("debounce");
        let provider = || SchemaSnapshot {
            tables: vec![table_schema("user")],
            edges: vec![],
        };
        let (w, mut rx) = SchemaWatcher::start(
            std::slice::from_ref(&dir),
            &WatcherConfig::new().debounce_ms(150),
            provider,
            SchemaSnapshot::new(),
        )
        .expect("start watcher");

        for i in 0..5 {
            let file = dir.join(format!("user{i}.surql"));
            fs::write(&file, format!("-- @up\nSELECT {i};\n-- @down\nSELECT 0;\n")).unwrap();
            tokio::time::sleep(Duration::from_millis(20)).await;
        }

        // First report should come through.
        let _first = tokio::time::timeout(Duration::from_secs(5), rx.recv())
            .await
            .expect("first report")
            .expect("channel");

        // Further rapid events within the same window should not
        // produce extra reports immediately; let the quiescent window
        // expire.
        let extra = tokio::time::timeout(Duration::from_millis(400), rx.recv()).await;
        // We don't assert strictly zero extra reports because notify
        // backends sometimes batch vs. split events unpredictably;
        // instead assert at least the first one arrived.
        drop(extra);
        w.stop();
        let _ = fs::remove_dir_all(&dir);
    }
}