mdql-core 0.5.27

Core library for MDQL — a queryable database backed by markdown files
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
//! CASCADE and RESTRICT delete: FK-aware deletion with BFS graph walk.

use std::collections::{HashMap, HashSet, VecDeque};
use std::path::Path;

use crate::database::{DatabaseConfig, ForeignKey};
use crate::model::{Row, Value};
use crate::schema::Schema;
use crate::txn::{TableLock, TableTransaction, atomic_write};

#[derive(Debug, Clone)]
pub enum CascadeAction {
    Delete { table: String, filename: String },
    PruneList { table: String, filename: String, column: String, value_to_remove: String },
}

#[derive(Debug, Clone)]
pub struct CascadePlan {
    pub primary_deletes: Vec<(String, String)>,
    pub cascade_actions: Vec<CascadeAction>,
    pub restrict_violations: Vec<String>,
}

impl CascadePlan {
    pub fn total_deletes(&self) -> usize {
        self.primary_deletes.len()
            + self.cascade_actions.iter()
                .filter(|a| matches!(a, CascadeAction::Delete { .. }))
                .count()
    }
}

pub fn build_cascade_plan(
    target_table: &str,
    matched_filenames: &[String],
    config: &DatabaseConfig,
    tables_data: &HashMap<String, (Schema, Vec<Row>)>,
) -> CascadePlan {
    let mut plan = CascadePlan {
        primary_deletes: matched_filenames.iter()
            .map(|f| (target_table.to_string(), f.clone()))
            .collect(),
        cascade_actions: Vec::new(),
        restrict_violations: Vec::new(),
    };

    let mut queue: VecDeque<(String, String)> = VecDeque::new();
    let mut visited: HashSet<(String, String)> = HashSet::new();

    for f in matched_filenames {
        let key = (target_table.to_string(), f.clone());
        visited.insert(key);
        queue.push_back((target_table.to_string(), f.clone()));
    }

    while let Some((deleted_table, deleted_filename)) = queue.pop_front() {
        let referencing_fks: Vec<&ForeignKey> = config
            .foreign_keys
            .iter()
            .filter(|fk| fk.to_table == deleted_table)
            .collect();

        for fk in referencing_fks {
            let from_rows = match tables_data.get(&fk.from_table) {
                Some(d) => &d.1,
                None => continue,
            };

            let match_value = resolve_fk_value(
                &deleted_table, &deleted_filename, &fk.to_column, tables_data,
            );
            let match_value = match match_value {
                Some(v) => v,
                None => continue,
            };

            for row in from_rows {
                let filename = match row.get("path").and_then(|v| v.as_str()) {
                    Some(f) => f.to_string(),
                    None => continue,
                };

                let fk_value = match row.get(&fk.from_column) {
                    Some(v) if !v.is_null() => v,
                    _ => continue,
                };

                match fk_value {
                    Value::List(items) => {
                        if items.iter().any(|item| item == &match_value) {
                            plan.cascade_actions.push(CascadeAction::PruneList {
                                table: fk.from_table.clone(),
                                filename: filename.clone(),
                                column: fk.from_column.clone(),
                                value_to_remove: match_value.clone(),
                            });
                        }
                    }
                    _ => {
                        if fk_value.to_display_string() == match_value {
                            let key = (fk.from_table.clone(), filename.clone());
                            if visited.insert(key) {
                                plan.cascade_actions.push(CascadeAction::Delete {
                                    table: fk.from_table.clone(),
                                    filename: filename.clone(),
                                });
                                queue.push_back((fk.from_table.clone(), filename.clone()));
                            }
                        }
                    }
                }
            }
        }
    }

    plan
}

pub fn build_restrict_plan(
    target_table: &str,
    matched_filenames: &[String],
    config: &DatabaseConfig,
    tables_data: &HashMap<String, (Schema, Vec<Row>)>,
) -> CascadePlan {
    let mut plan = CascadePlan {
        primary_deletes: matched_filenames.iter()
            .map(|f| (target_table.to_string(), f.clone()))
            .collect(),
        cascade_actions: Vec::new(),
        restrict_violations: Vec::new(),
    };

    for filename in matched_filenames {
        let referencing_fks: Vec<&ForeignKey> = config
            .foreign_keys
            .iter()
            .filter(|fk| fk.to_table == target_table)
            .collect();

        for fk in &referencing_fks {
            let from_rows = match tables_data.get(&fk.from_table) {
                Some(d) => &d.1,
                None => continue,
            };

            let match_value = resolve_fk_value(
                target_table, filename, &fk.to_column, tables_data,
            );
            let match_value = match match_value {
                Some(v) => v,
                None => continue,
            };

            for row in from_rows {
                let ref_filename = row.get("path")
                    .and_then(|v| v.as_str())
                    .unwrap_or("");

                let fk_value = match row.get(&fk.from_column) {
                    Some(v) if !v.is_null() => v,
                    _ => continue,
                };

                let matches = match fk_value {
                    Value::List(items) => items.iter().any(|i| i == &match_value),
                    _ => fk_value.to_display_string() == match_value,
                };

                if matches {
                    plan.restrict_violations.push(format!(
                        "{}/{} references {}/{} via {}.{}",
                        fk.from_table, ref_filename,
                        target_table, filename,
                        fk.from_table, fk.from_column,
                    ));
                }
            }
        }
    }

    plan
}

fn resolve_fk_value(
    table: &str,
    filename: &str,
    to_column: &str,
    tables_data: &HashMap<String, (Schema, Vec<Row>)>,
) -> Option<String> {
    if to_column == "path" {
        return Some(filename.to_string());
    }
    let rows = &tables_data.get(table)?.1;
    let row = rows.iter().find(|r| {
        r.get("path").and_then(|v| v.as_str()).map_or(false, |p| p == filename)
    })?;
    row.get(to_column).map(|v| v.to_display_string())
}

pub fn execute_cascade_plan(
    plan: &CascadePlan,
    db_path: &Path,
) -> crate::errors::Result<String> {
    if plan.primary_deletes.is_empty() {
        return Ok("DELETE 0".to_string());
    }

    let mut affected_tables: HashSet<String> = HashSet::new();
    for (table, _) in &plan.primary_deletes {
        affected_tables.insert(table.clone());
    }
    for action in &plan.cascade_actions {
        match action {
            CascadeAction::Delete { table, .. } | CascadeAction::PruneList { table, .. } => {
                affected_tables.insert(table.clone());
            }
        }
    }

    let mut table_names: Vec<String> = affected_tables.into_iter().collect();
    table_names.sort();

    let _locks: Vec<TableLock> = table_names
        .iter()
        .map(|name| TableLock::acquire(&db_path.join(name)))
        .collect::<Result<Vec<_>, _>>()?;

    let mut txns: HashMap<String, TableTransaction> = HashMap::new();
    for name in &table_names {
        txns.insert(name.clone(), TableTransaction::new(&db_path.join(name), "CASCADE DELETE")?);
    }

    let result = (|| -> crate::errors::Result<(usize, usize, usize)> {
        let mut delete_count = 0;
        let mut cascade_delete_count = 0;
        let mut prune_count = 0;

        for (table, filename) in &plan.primary_deletes {
            let filepath = db_path.join(table).join(filename);
            if filepath.exists() {
                let content = std::fs::read_to_string(&filepath)?;
                txns.get_mut(table).unwrap().record_delete(&filepath, &content)?;
                std::fs::remove_file(&filepath)?;
                crate::checksums::remove_checksum(&db_path.join(table), filename)?;
                delete_count += 1;
            }
        }

        for action in &plan.cascade_actions {
            match action {
                CascadeAction::Delete { table, filename } => {
                    let filepath = db_path.join(table).join(filename);
                    if filepath.exists() {
                        let content = std::fs::read_to_string(&filepath)?;
                        txns.get_mut(table).unwrap().record_delete(&filepath, &content)?;
                        std::fs::remove_file(&filepath)?;
                        crate::checksums::remove_checksum(&db_path.join(table), filename)?;
                        cascade_delete_count += 1;
                    }
                }
                CascadeAction::PruneList { table, filename, column, value_to_remove } => {
                    let filepath = db_path.join(table).join(filename);
                    if filepath.exists() {
                        let content = std::fs::read_to_string(&filepath)?;
                        txns.get_mut(table).unwrap().record_delete(&filepath, &content)?;
                        let updated = prune_list_value(&content, column, value_to_remove);
                        atomic_write(&filepath, &updated)?;
                        prune_count += 1;
                    }
                }
            }
        }

        Ok((delete_count, cascade_delete_count, prune_count))
    })();

    match result {
        Ok((delete_count, cascade_delete_count, prune_count)) => {
            for (_, txn) in txns {
                txn.commit()?;
            }
            let mut msg = format!("DELETE {}", delete_count);
            if cascade_delete_count > 0 || prune_count > 0 {
                msg.push_str(" (cascade:");
                if cascade_delete_count > 0 {
                    msg.push_str(&format!(" {} deleted", cascade_delete_count));
                }
                if prune_count > 0 {
                    if cascade_delete_count > 0 { msg.push(','); }
                    msg.push_str(&format!(" {} list refs pruned", prune_count));
                }
                msg.push(')');
            }
            Ok(msg)
        }
        Err(e) => {
            for (_, txn) in txns {
                let _ = txn.rollback();
            }
            Err(e)
        }
    }
}

fn prune_list_value(content: &str, column: &str, value_to_remove: &str) -> String {
    let lines: Vec<&str> = content.lines().collect();
    let mut result = Vec::new();
    let mut in_target_list = false;
    let mut found_column = false;

    for line in &lines {
        if in_target_list {
            let trimmed = line.trim();
            if trimmed.starts_with("- ") {
                let item = trimmed.strip_prefix("- ").unwrap().trim();
                let item = item.trim_matches('"').trim_matches('\'');
                if item == value_to_remove {
                    continue;
                }
                result.push(*line);
            } else {
                in_target_list = false;
                result.push(*line);
            }
        } else if !found_column && line.trim_start().starts_with(&format!("{}:", column)) {
            found_column = true;
            let after_colon = line.trim_start()
                .strip_prefix(&format!("{}:", column))
                .unwrap_or("")
                .trim();
            if after_colon.is_empty() {
                in_target_list = true;
            }
            result.push(*line);
        } else {
            result.push(*line);
        }
    }

    let mut out = result.join("\n");
    if content.ends_with('\n') && !out.ends_with('\n') {
        out.push('\n');
    }
    out
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::model::Value;
    use crate::schema::Schema;

    fn test_schema(table: &str) -> Schema {
        Schema {
            table: table.to_string(),
            primary_key: "path".to_string(),
            frontmatter: indexmap::IndexMap::new(),
            sections: indexmap::IndexMap::new(),
            h1_required: false,
            rules: crate::schema::Rules {
                reject_unknown_frontmatter: false,
                reject_unknown_sections: false,
                reject_duplicate_sections: false,
                normalize_numbered_headings: false,
            },
        }
    }

    fn make_row(path: &str, fields: &[(&str, Value)]) -> Row {
        let mut row = Row::new();
        row.insert("path".to_string(), Value::String(path.to_string()));
        for (k, v) in fields {
            row.insert(k.to_string(), v.clone());
        }
        row
    }

    #[test]
    fn test_cascade_no_dependents() {
        let config = DatabaseConfig {
            name: "test".into(),
            foreign_keys: vec![],
            views: vec![],
            sync: None,
        };
        let mut tables: HashMap<String, (Schema, Vec<Row>)> = HashMap::new();
        tables.insert("strats".into(), (test_schema("strats"), vec![
            make_row("alpha.md", &[("title", Value::String("Alpha".into()))]),
        ]));

        let plan = build_cascade_plan("strats", &["alpha.md".into()], &config, &tables);
        assert_eq!(plan.primary_deletes.len(), 1);
        assert!(plan.cascade_actions.is_empty());
    }

    #[test]
    fn test_cascade_single_level() {
        let config = DatabaseConfig {
            name: "test".into(),
            foreign_keys: vec![ForeignKey {
                from_table: "backtests".into(),
                from_column: "strategy".into(),
                to_table: "strats".into(),
                to_column: "path".into(),
            }],
            views: vec![],
            sync: None,
        };
        let mut tables: HashMap<String, (Schema, Vec<Row>)> = HashMap::new();
        tables.insert("strats".into(), (test_schema("strats"), vec![
            make_row("alpha.md", &[]),
        ]));
        tables.insert("backtests".into(), (test_schema("backtests"), vec![
            make_row("bt-alpha.md", &[("strategy", Value::String("alpha.md".into()))]),
            make_row("bt-beta.md", &[("strategy", Value::String("beta.md".into()))]),
        ]));

        let plan = build_cascade_plan("strats", &["alpha.md".into()], &config, &tables);
        assert_eq!(plan.primary_deletes.len(), 1);
        assert_eq!(plan.cascade_actions.len(), 1);
        assert!(matches!(&plan.cascade_actions[0], CascadeAction::Delete { table, filename }
            if table == "backtests" && filename == "bt-alpha.md"));
    }

    #[test]
    fn test_cascade_multi_level() {
        let config = DatabaseConfig {
            name: "test".into(),
            foreign_keys: vec![
                ForeignKey {
                    from_table: "backtests".into(),
                    from_column: "strategy".into(),
                    to_table: "strats".into(),
                    to_column: "path".into(),
                },
                ForeignKey {
                    from_table: "events".into(),
                    from_column: "backtest".into(),
                    to_table: "backtests".into(),
                    to_column: "path".into(),
                },
            ],
            views: vec![],
            sync: None,
        };
        let mut tables: HashMap<String, (Schema, Vec<Row>)> = HashMap::new();
        tables.insert("strats".into(), (test_schema("strats"), vec![
            make_row("alpha.md", &[]),
        ]));
        tables.insert("backtests".into(), (test_schema("backtests"), vec![
            make_row("bt-alpha.md", &[("strategy", Value::String("alpha.md".into()))]),
        ]));
        tables.insert("events".into(), (test_schema("events"), vec![
            make_row("ev-1.md", &[("backtest", Value::String("bt-alpha.md".into()))]),
        ]));

        let plan = build_cascade_plan("strats", &["alpha.md".into()], &config, &tables);
        assert_eq!(plan.primary_deletes.len(), 1);
        assert_eq!(plan.cascade_actions.len(), 2);
    }

    #[test]
    fn test_cascade_list_prune() {
        let config = DatabaseConfig {
            name: "test".into(),
            foreign_keys: vec![ForeignKey {
                from_table: "strats".into(),
                from_column: "ancestry".into(),
                to_table: "strats".into(),
                to_column: "path".into(),
            }],
            views: vec![],
            sync: None,
        };
        let mut tables: HashMap<String, (Schema, Vec<Row>)> = HashMap::new();
        tables.insert("strats".into(), (test_schema("strats"), vec![
            make_row("alpha.md", &[]),
            make_row("beta.md", &[("ancestry", Value::List(vec![
                "alpha.md".to_string(), "gamma.md".to_string(),
            ]))]),
        ]));

        let plan = build_cascade_plan("strats", &["alpha.md".into()], &config, &tables);
        assert_eq!(plan.primary_deletes.len(), 1);
        assert_eq!(plan.cascade_actions.len(), 1);
        assert!(matches!(&plan.cascade_actions[0], CascadeAction::PruneList { column, value_to_remove, .. }
            if column == "ancestry" && value_to_remove == "alpha.md"));
    }

    #[test]
    fn test_cascade_self_referential_no_loop() {
        let config = DatabaseConfig {
            name: "test".into(),
            foreign_keys: vec![ForeignKey {
                from_table: "strats".into(),
                from_column: "parent".into(),
                to_table: "strats".into(),
                to_column: "path".into(),
            }],
            views: vec![],
            sync: None,
        };
        let mut tables: HashMap<String, (Schema, Vec<Row>)> = HashMap::new();
        tables.insert("strats".into(), (test_schema("strats"), vec![
            make_row("alpha.md", &[("parent", Value::String("beta.md".into()))]),
            make_row("beta.md", &[("parent", Value::String("alpha.md".into()))]),
        ]));

        let plan = build_cascade_plan("strats", &["alpha.md".into()], &config, &tables);
        assert_eq!(plan.primary_deletes.len(), 1);
        assert_eq!(plan.cascade_actions.len(), 1);
        assert!(matches!(&plan.cascade_actions[0], CascadeAction::Delete { filename, .. }
            if filename == "beta.md"));
    }

    #[test]
    fn test_restrict_blocks() {
        let config = DatabaseConfig {
            name: "test".into(),
            foreign_keys: vec![ForeignKey {
                from_table: "backtests".into(),
                from_column: "strategy".into(),
                to_table: "strats".into(),
                to_column: "path".into(),
            }],
            views: vec![],
            sync: None,
        };
        let mut tables: HashMap<String, (Schema, Vec<Row>)> = HashMap::new();
        tables.insert("strats".into(), (test_schema("strats"), vec![
            make_row("alpha.md", &[]),
        ]));
        tables.insert("backtests".into(), (test_schema("backtests"), vec![
            make_row("bt-alpha.md", &[("strategy", Value::String("alpha.md".into()))]),
        ]));

        let plan = build_restrict_plan("strats", &["alpha.md".into()], &config, &tables);
        assert!(!plan.restrict_violations.is_empty());
    }

    #[test]
    fn test_restrict_allows() {
        let config = DatabaseConfig {
            name: "test".into(),
            foreign_keys: vec![ForeignKey {
                from_table: "backtests".into(),
                from_column: "strategy".into(),
                to_table: "strats".into(),
                to_column: "path".into(),
            }],
            views: vec![],
            sync: None,
        };
        let mut tables: HashMap<String, (Schema, Vec<Row>)> = HashMap::new();
        tables.insert("strats".into(), (test_schema("strats"), vec![
            make_row("alpha.md", &[]),
        ]));
        tables.insert("backtests".into(), (test_schema("backtests"), vec![
            make_row("bt-beta.md", &[("strategy", Value::String("beta.md".into()))]),
        ]));

        let plan = build_restrict_plan("strats", &["alpha.md".into()], &config, &tables);
        assert!(plan.restrict_violations.is_empty());
    }

    #[test]
    fn test_prune_list_value() {
        let content = "---\ntitle: Test\nancestry:\n  - alpha.md\n  - gamma.md\n---\n\n# Test\n";
        let result = prune_list_value(content, "ancestry", "alpha.md");
        assert!(!result.contains("alpha.md"));
        assert!(result.contains("gamma.md"));
        assert!(result.contains("title: Test"));
    }
}