dbrest-core 0.8.6

Database-agnostic core for the dbrest REST API
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
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! Schema Cache module
//!
//! The schema cache is the heart of dbrest. It introspects the PostgreSQL database
//! and caches:
//! - Tables/Views metadata
//! - Column information
//! - Foreign key relationships
//! - Functions/Procedures
//!
//! # Architecture
//!
//! The cache is immutable and wrapped in `ArcSwap` for lock-free reads and atomic
//! replacement during schema reload.
//!
//! ```text
//! ┌─────────────────────────────────────────────────┐
//! │                  SchemaCache                     │
//! ├─────────────────────────────────────────────────┤
//! │  tables: HashMap<QualifiedIdentifier, Table>    │
//! │  relationships: HashMap<..., Vec<Relationship>> │
//! │  routines: HashMap<QualifiedIdentifier, Vec>    │
//! │  timezones: HashSet<String>                     │
//! └─────────────────────────────────────────────────┘
//! ```

pub mod db;
pub mod media_handler;
pub mod queries;
pub mod relationship;
pub mod representations;
pub mod routine;
pub mod table;

// Re-export main types
pub use db::{ComputedFieldRow, DbIntrospector, RelationshipRow, RoutineRow, TableRow};
pub use media_handler::{MediaHandler, MediaHandlerMap, ResolvedHandler};
pub use relationship::{
    AnyRelationship, Cardinality, ComputedRelationship, Junction, Relationship,
};
pub use representations::{DataRepresentation, RepresentationsMap};
pub use routine::{PgType, ReturnType, Routine, RoutineParam, Volatility};
pub use table::{Column, ComputedField, Table};

use std::collections::{HashMap, HashSet};
use std::sync::Arc;

use arc_swap::ArcSwap;

use crate::config::AppConfig;
use crate::error::Error;
use crate::types::QualifiedIdentifier;

/// Type alias for the tables map
pub type TablesMap = HashMap<QualifiedIdentifier, Table>;

/// Type alias for the relationships map
/// Key: (source_table, schema) -> list of relationships from that table
pub type RelationshipsMap = HashMap<(QualifiedIdentifier, String), Vec<AnyRelationship>>;

/// Type alias for the routines map
/// Key: QualifiedIdentifier -> list of overloaded functions
pub type RoutinesMap = HashMap<QualifiedIdentifier, Vec<Routine>>;

/// Immutable schema cache
///
/// This structure holds all introspected database metadata. It is designed to be
/// immutable and wrapped in `ArcSwap` for lock-free reads.
#[derive(Debug, Clone)]
pub struct SchemaCache {
    /// All tables and views by qualified name
    pub tables: Arc<TablesMap>,
    /// Relationships indexed by source table
    pub relationships: Arc<RelationshipsMap>,
    /// Functions/procedures indexed by qualified name
    pub routines: Arc<RoutinesMap>,
    /// Available PostgreSQL timezones
    pub timezones: Arc<HashSet<String>>,
    /// Data representation mappings
    pub representations: Arc<RepresentationsMap>,
    /// Media handler mappings
    pub media_handlers: Arc<MediaHandlerMap>,
}

impl Default for SchemaCache {
    fn default() -> Self {
        Self::empty()
    }
}

impl SchemaCache {
    /// Create an empty schema cache
    pub fn empty() -> Self {
        Self {
            tables: Arc::new(HashMap::new()),
            relationships: Arc::new(HashMap::new()),
            routines: Arc::new(HashMap::new()),
            timezones: Arc::new(HashSet::new()),
            representations: Arc::new(HashMap::new()),
            media_handlers: Arc::new(HashMap::new()),
        }
    }

    /// Load schema cache from database using the provided introspector
    pub async fn load<I: DbIntrospector + ?Sized>(
        introspector: &I,
        config: &AppConfig,
    ) -> Result<Self, Error> {
        let schemas = &config.db_schemas;

        tracing::info!("Loading schema cache for schemas: {:?}", schemas);

        // Combine exposed schemas with extra search path for computed fields query
        let mut all_schemas = config.db_schemas.clone();
        for extra_schema in &config.db_extra_search_path {
            if !all_schemas.contains(extra_schema) {
                all_schemas.push(extra_schema.clone());
            }
        }

        tracing::debug!("All schemas for computed fields query: {:?}", all_schemas);

        // Query all data concurrently
        let (tables_rows, rel_rows, routine_rows, computed_fields_rows, timezones) = tokio::try_join!(
            introspector.query_tables(schemas),
            introspector.query_relationships(),
            introspector.query_routines(schemas),
            introspector.query_computed_fields(&all_schemas),
            introspector.query_timezones(),
        )?;

        tracing::debug!(
            "Loaded: {} tables, {} relationships, {} routines, {} computed fields, {} timezones",
            tables_rows.len(),
            rel_rows.len(),
            routine_rows.len(),
            computed_fields_rows.len(),
            timezones.len()
        );

        // Build tables map
        let mut tables = HashMap::with_capacity(tables_rows.len());
        for row in tables_rows {
            let table = row.into_table()?;
            let qi = table.qi();
            tables.insert(qi.clone(), table);
        }

        // Group computed fields by table and attach them
        use crate::schema_cache::table::ComputedField;
        use crate::types::QualifiedIdentifier as QI;

        let mut attached_count = 0;
        let mut not_found_count = 0;

        for row in computed_fields_rows {
            let table_qi = QI::new(&row.table_schema, &row.table_name);
            if let Some(table) = tables.get_mut(&table_qi) {
                let function_qi = QI::new(&row.function_schema, &row.function_name);
                let computed_field = ComputedField {
                    function: function_qi,
                    return_type: row.return_type.into(),
                    returns_set: row.returns_set,
                };
                // Use function name as the key (not qualified, matching PostgREST behavior)
                table
                    .computed_fields
                    .insert(row.function_name.clone().into(), computed_field);
                tracing::trace!(
                    "Attached computed field '{}' to table {}.{}",
                    row.function_name,
                    row.table_schema,
                    row.table_name
                );
                attached_count += 1;
            } else {
                tracing::warn!(
                    "Computed field function {}.{} references non-existent table {}.{}",
                    row.function_schema,
                    row.function_name,
                    row.table_schema,
                    row.table_name
                );
                not_found_count += 1;
            }
        }

        tracing::debug!(
            "Attached {} computed fields to tables, {} referenced non-existent tables",
            attached_count,
            not_found_count
        );

        // Build relationships map — store both forward (M2O) and reverse (O2M)
        // directions so that resource embedding works in either direction.
        let mut relationships: RelationshipsMap = HashMap::new();
        for row in rel_rows {
            let rel = row.into_relationship();

            // Forward direction (M2O / O2O): keyed under the FK-holding table
            let fwd_key = (rel.table.clone(), rel.table.schema.to_string());
            let reverse = rel.reverse();
            relationships
                .entry(fwd_key)
                .or_default()
                .push(AnyRelationship::ForeignKey(rel));

            // Reverse direction (O2M / O2O-parent): keyed under the referenced table
            let rev_key = (reverse.table.clone(), reverse.table.schema.to_string());
            relationships
                .entry(rev_key)
                .or_default()
                .push(AnyRelationship::ForeignKey(reverse));
        }

        // Build routines map
        let mut routines: RoutinesMap = HashMap::new();
        for row in routine_rows {
            let routine = row.into_routine()?;
            let qi = routine.qi();
            routines.entry(qi).or_default().push(routine);
        }

        // Convert timezones to HashSet, ensuring UTC is always included
        let mut timezone_set: HashSet<String> = timezones.into_iter().collect();
        timezone_set.insert("UTC".to_string());

        Ok(Self {
            tables: Arc::new(tables),
            relationships: Arc::new(relationships),
            routines: Arc::new(routines),
            timezones: Arc::new(timezone_set),
            representations: Arc::new(HashMap::new()),
            media_handlers: Arc::new(HashMap::new()),
        })
    }

    /// Get a table by qualified identifier
    pub fn get_table(&self, qi: &QualifiedIdentifier) -> Option<&Table> {
        self.tables.get(qi)
    }

    /// Get a table by schema and name
    pub fn get_table_by_name(&self, schema: &str, name: &str) -> Option<&Table> {
        let qi = QualifiedIdentifier::new(schema, name);
        self.tables.get(&qi)
    }

    /// Find relationships from a source table
    pub fn find_relationships(&self, source: &QualifiedIdentifier) -> &[AnyRelationship] {
        let key = (source.clone(), source.schema.to_string());
        self.relationships
            .get(&key)
            .map(|v| v.as_slice())
            .unwrap_or(&[])
    }

    /// Find relationships from source to a specific target
    pub fn find_relationships_to(
        &self,
        source: &QualifiedIdentifier,
        target_name: &str,
    ) -> Vec<&AnyRelationship> {
        self.find_relationships(source)
            .iter()
            .filter(|r| r.foreign_table().name.as_str() == target_name)
            .collect()
    }

    /// Get a routine by qualified identifier
    pub fn get_routines(&self, qi: &QualifiedIdentifier) -> Option<&[Routine]> {
        self.routines.get(qi).map(|v| v.as_slice())
    }

    /// Get a routine by schema and name
    pub fn get_routines_by_name(&self, schema: &str, name: &str) -> Option<&[Routine]> {
        let qi = QualifiedIdentifier::new(schema, name);
        self.routines.get(&qi).map(|v| v.as_slice())
    }

    /// Check if a timezone is valid
    pub fn is_valid_timezone(&self, tz: &str) -> bool {
        self.timezones.contains(tz)
    }

    /// Get the number of tables
    pub fn table_count(&self) -> usize {
        self.tables.len()
    }

    /// Get the number of relationships
    pub fn relationship_count(&self) -> usize {
        self.relationships.values().map(|v| v.len()).sum()
    }

    /// Get the number of routines
    pub fn routine_count(&self) -> usize {
        self.routines.values().map(|v| v.len()).sum()
    }

    /// Get a summary string for logging
    pub fn summary(&self) -> String {
        format!(
            "{} tables, {} relationships, {} routines, {} timezones",
            self.table_count(),
            self.relationship_count(),
            self.routine_count(),
            self.timezones.len(),
        )
    }

    /// Iterate over all tables
    pub fn tables_iter(&self) -> impl Iterator<Item = (&QualifiedIdentifier, &Table)> {
        self.tables.iter()
    }

    /// Iterate over all tables in a specific schema
    pub fn tables_in_schema(&self, schema: &str) -> impl Iterator<Item = &Table> {
        self.tables
            .values()
            .filter(move |t| t.schema.as_str() == schema)
    }
}

/// Schema cache holder with atomic swap capability
///
/// Wraps the schema cache in `ArcSwap` for lock-free reads and atomic updates.
#[derive(Debug)]
pub struct SchemaCacheHolder {
    inner: ArcSwap<Option<SchemaCache>>,
}

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

impl SchemaCacheHolder {
    /// Create a new empty holder
    pub fn new() -> Self {
        Self {
            inner: ArcSwap::from_pointee(None),
        }
    }

    /// Create a holder with an initial cache
    pub fn with_cache(cache: SchemaCache) -> Self {
        Self {
            inner: ArcSwap::from_pointee(Some(cache)),
        }
    }

    /// Get a reference to the current cache
    ///
    /// Returns None if the cache hasn't been loaded yet.
    pub fn get(&self) -> Option<arc_swap::Guard<Arc<Option<SchemaCache>>>> {
        let guard = self.inner.load();
        if guard.is_some() { Some(guard) } else { None }
    }

    /// Replace the cache with a new one
    pub fn replace(&self, cache: SchemaCache) {
        self.inner.store(Arc::new(Some(cache)));
    }

    /// Clear the cache
    pub fn clear(&self) {
        self.inner.store(Arc::new(None));
    }

    /// Check if the cache is loaded
    pub fn is_loaded(&self) -> bool {
        self.inner.load().is_some()
    }
}

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

    fn create_test_cache() -> SchemaCache {
        let mut tables = HashMap::new();

        let users_table = test_table()
            .schema("public")
            .name("users")
            .pk_col("id")
            .column(test_column().name("id").data_type("integer").build())
            .column(test_column().name("name").data_type("text").build())
            .build();

        let posts_table = test_table()
            .schema("public")
            .name("posts")
            .pk_col("id")
            .column(test_column().name("id").data_type("integer").build())
            .column(test_column().name("user_id").data_type("integer").build())
            .column(test_column().name("title").data_type("text").build())
            .build();

        tables.insert(users_table.qi(), users_table);
        tables.insert(posts_table.qi(), posts_table);

        // Create relationship posts -> users
        let rel = test_relationship()
            .table("public", "posts")
            .foreign_table("public", "users")
            .m2o("fk_posts_user", &[("user_id", "id")])
            .build();

        let mut relationships = HashMap::new();
        let key = (
            QualifiedIdentifier::new("public", "posts"),
            "public".to_string(),
        );
        relationships.insert(key, vec![AnyRelationship::ForeignKey(rel)]);

        // Create routine
        let routine = test_routine()
            .schema("public")
            .name("get_user")
            .param(test_param().name("user_id").pg_type("integer").build())
            .returns_setof_composite("public", "users")
            .build();

        let mut routines = HashMap::new();
        routines.insert(routine.qi(), vec![routine]);

        let mut timezones = HashSet::new();
        timezones.insert("UTC".to_string());
        timezones.insert("America/New_York".to_string());

        SchemaCache {
            tables: Arc::new(tables),
            relationships: Arc::new(relationships),
            routines: Arc::new(routines),
            timezones: Arc::new(timezones),
            representations: Arc::new(HashMap::new()),
            media_handlers: Arc::new(HashMap::new()),
        }
    }

    #[test]
    fn test_schema_cache_empty() {
        let cache = SchemaCache::empty();
        assert_eq!(cache.table_count(), 0);
        assert_eq!(cache.relationship_count(), 0);
        assert_eq!(cache.routine_count(), 0);
    }

    #[test]
    fn test_schema_cache_get_table() {
        let cache = create_test_cache();

        let qi = QualifiedIdentifier::new("public", "users");
        let table = cache.get_table(&qi).unwrap();
        assert_eq!(table.name.as_str(), "users");
    }

    #[test]
    fn test_schema_cache_get_table_by_name() {
        let cache = create_test_cache();

        let table = cache.get_table_by_name("public", "posts").unwrap();
        assert_eq!(table.name.as_str(), "posts");
        assert!(table.has_pk());
    }

    #[test]
    fn test_schema_cache_get_table_not_found() {
        let cache = create_test_cache();

        let qi = QualifiedIdentifier::new("public", "nonexistent");
        assert!(cache.get_table(&qi).is_none());
    }

    #[test]
    fn test_schema_cache_find_relationships() {
        let cache = create_test_cache();

        let source = QualifiedIdentifier::new("public", "posts");
        let rels = cache.find_relationships(&source);
        assert_eq!(rels.len(), 1);
        assert_eq!(rels[0].foreign_table().name.as_str(), "users");
    }

    #[test]
    fn test_schema_cache_find_relationships_to() {
        let cache = create_test_cache();

        let source = QualifiedIdentifier::new("public", "posts");
        let rels = cache.find_relationships_to(&source, "users");
        assert_eq!(rels.len(), 1);

        let rels = cache.find_relationships_to(&source, "nonexistent");
        assert!(rels.is_empty());
    }

    #[test]
    fn test_schema_cache_get_routines() {
        let cache = create_test_cache();

        let qi = QualifiedIdentifier::new("public", "get_user");
        let routines = cache.get_routines(&qi).unwrap();
        assert_eq!(routines.len(), 1);
        assert!(routines[0].returns_set());
    }

    #[test]
    fn test_schema_cache_get_routines_by_name() {
        let cache = create_test_cache();

        let routines = cache.get_routines_by_name("public", "get_user").unwrap();
        assert_eq!(routines.len(), 1);
    }

    #[test]
    fn test_schema_cache_is_valid_timezone() {
        let cache = create_test_cache();

        assert!(cache.is_valid_timezone("UTC"));
        assert!(cache.is_valid_timezone("America/New_York"));
        assert!(!cache.is_valid_timezone("Invalid/Zone"));
    }

    #[test]
    fn test_schema_cache_counts() {
        let cache = create_test_cache();

        assert_eq!(cache.table_count(), 2);
        assert_eq!(cache.relationship_count(), 1);
        assert_eq!(cache.routine_count(), 1);
    }

    #[test]
    fn test_schema_cache_summary() {
        let cache = create_test_cache();

        let summary = cache.summary();
        assert!(summary.contains("2 tables"));
        assert!(summary.contains("1 relationships"));
        assert!(summary.contains("1 routines"));
    }

    #[test]
    fn test_schema_cache_tables_iter() {
        let cache = create_test_cache();

        let table_names: Vec<_> = cache.tables_iter().map(|(_, t)| t.name.as_str()).collect();
        assert!(table_names.contains(&"users"));
        assert!(table_names.contains(&"posts"));
    }

    #[test]
    fn test_schema_cache_tables_in_schema() {
        let cache = create_test_cache();

        let public_tables: Vec<_> = cache.tables_in_schema("public").collect();
        assert_eq!(public_tables.len(), 2);

        let other_tables: Vec<_> = cache.tables_in_schema("other").collect();
        assert!(other_tables.is_empty());
    }

    // ========================================================================
    // SchemaCacheHolder Tests
    // ========================================================================

    #[test]
    fn test_schema_cache_holder_new() {
        let holder = SchemaCacheHolder::new();
        assert!(!holder.is_loaded());
        assert!(holder.get().is_none());
    }

    #[test]
    fn test_schema_cache_holder_with_cache() {
        let cache = create_test_cache();
        let holder = SchemaCacheHolder::with_cache(cache);
        assert!(holder.is_loaded());
        assert!(holder.get().is_some());
    }

    #[test]
    fn test_schema_cache_holder_replace() {
        let holder = SchemaCacheHolder::new();
        assert!(!holder.is_loaded());

        let cache = create_test_cache();
        holder.replace(cache);
        assert!(holder.is_loaded());
    }

    #[test]
    fn test_schema_cache_holder_clear() {
        let cache = create_test_cache();
        let holder = SchemaCacheHolder::with_cache(cache);
        assert!(holder.is_loaded());

        holder.clear();
        assert!(!holder.is_loaded());
    }

    // ========================================================================
    // Mock-based Tests
    // ========================================================================

    #[tokio::test]
    async fn test_schema_cache_load_with_mock() {
        use db::MockDbIntrospector;

        let mut mock = MockDbIntrospector::new();

        // Set up mock expectations
        mock.expect_query_tables().returning(|_| {
            Ok(vec![TableRow {
                table_schema: "public".to_string(),
                table_name: "test_table".to_string(),
                table_description: None,
                is_view: false,
                insertable: true,
                updatable: true,
                deletable: true,
                readable: true,
                pk_cols: vec!["id".to_string()],
                columns_json: r#"[{"name":"id","description":null,"nullable":false,"data_type":"integer","nominal_type":"integer","max_length":null,"default":null,"enum_values":[]}]"#.to_string(),
            }])
        });

        mock.expect_query_relationships().returning(|| Ok(vec![]));
        mock.expect_query_routines().returning(|_| Ok(vec![]));
        mock.expect_query_computed_fields()
            .returning(|_| Ok(vec![]));
        mock.expect_query_timezones()
            .returning(|| Ok(vec!["UTC".to_string()]));

        let config = AppConfig::default();
        let cache = SchemaCache::load(&mock, &config).await.unwrap();

        assert_eq!(cache.table_count(), 1);
        let table = cache.get_table_by_name("public", "test_table").unwrap();
        assert!(table.has_pk());
    }

    #[tokio::test]
    async fn test_schema_cache_load_with_relationships() {
        use db::MockDbIntrospector;

        let mut mock = MockDbIntrospector::new();

        mock.expect_query_tables().returning(|_| {
            Ok(vec![
                TableRow {
                    table_schema: "public".to_string(),
                    table_name: "users".to_string(),
                    table_description: None,
                    is_view: false,
                    insertable: true,
                    updatable: true,
                    deletable: true,
                    readable: true,
                    pk_cols: vec!["id".to_string()],
                    columns_json: r#"[{"name":"id","description":null,"nullable":false,"data_type":"integer","nominal_type":"integer","max_length":null,"default":null,"enum_values":[]}]"#.to_string(),
                },
                TableRow {
                    table_schema: "public".to_string(),
                    table_name: "posts".to_string(),
                    table_description: None,
                    is_view: false,
                    insertable: true,
                    updatable: true,
                    deletable: true,
                    readable: true,
                    pk_cols: vec!["id".to_string()],
                    columns_json: r#"[{"name":"id","description":null,"nullable":false,"data_type":"integer","nominal_type":"integer","max_length":null,"default":null,"enum_values":[]},{"name":"user_id","description":null,"nullable":false,"data_type":"integer","nominal_type":"integer","max_length":null,"default":null,"enum_values":[]}]"#.to_string(),
                },
            ])
        });

        mock.expect_query_relationships().returning(|| {
            Ok(vec![RelationshipRow {
                table_schema: "public".to_string(),
                table_name: "posts".to_string(),
                foreign_table_schema: "public".to_string(),
                foreign_table_name: "users".to_string(),
                is_self: false,
                constraint_name: "fk_posts_user".to_string(),
                cols_and_fcols: vec![("user_id".to_string(), "id".to_string())],
                one_to_one: false,
            }])
        });

        mock.expect_query_routines().returning(|_| Ok(vec![]));
        mock.expect_query_computed_fields()
            .returning(|_| Ok(vec![]));
        mock.expect_query_timezones().returning(|| Ok(vec![]));

        let config = AppConfig::default();
        let cache = SchemaCache::load(&mock, &config).await.unwrap();

        assert_eq!(cache.table_count(), 2);
        // 2 relationships: forward M2O (posts→users) + reverse O2M (users→posts)
        assert_eq!(cache.relationship_count(), 2);

        // Forward: posts → users (M2O)
        let source = QualifiedIdentifier::new("public", "posts");
        let rels = cache.find_relationships(&source);
        assert_eq!(rels.len(), 1);
        assert!(rels[0].is_to_one()); // M2O

        // Reverse: users → posts (O2M)
        let source_rev = QualifiedIdentifier::new("public", "users");
        let rels_rev = cache.find_relationships(&source_rev);
        assert_eq!(rels_rev.len(), 1);
        assert!(!rels_rev[0].is_to_one()); // O2M is not to-one
    }
}