reddb-io-server 1.12.0

RedDB server-side engine: storage, runtime, replication, MCP, AI, and the gRPC/HTTP/RedWire/PG-wire dispatchers. Re-exported by the umbrella `reddb` crate.
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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
//! Table Definition
//!
//! Defines table structure including columns, primary keys, indexes, and constraints.
//! Tables are the primary data organization unit in RedDB.

use super::types::DataType;
use std::collections::HashMap;
use std::fmt;

/// Table definition containing all metadata
#[derive(Debug, Clone)]
pub struct TableDef {
    /// Table name (unique within database)
    pub name: String,
    /// Column definitions in order
    pub columns: Vec<ColumnDef>,
    /// Primary key column names (can be composite)
    pub primary_key: Vec<String>,
    /// Index definitions
    pub indexes: Vec<IndexDef>,
    /// Table-level constraints
    pub constraints: Vec<Constraint>,
    /// Schema version (for migrations)
    pub version: u32,
    /// Creation timestamp
    pub created_at: u64,
    /// Last modification timestamp
    pub updated_at: u64,
}

impl TableDef {
    /// Create a new table definition
    pub fn new(name: impl Into<String>) -> Self {
        let now = std::time::SystemTime::now()
            .duration_since(std::time::UNIX_EPOCH)
            .unwrap_or_default()
            .as_secs();

        Self {
            name: name.into(),
            columns: Vec::new(),
            primary_key: Vec::new(),
            indexes: Vec::new(),
            constraints: Vec::new(),
            version: 1,
            created_at: now,
            updated_at: now,
        }
    }

    /// Add a column to the table
    pub fn add_column(mut self, column: ColumnDef) -> Self {
        self.columns.push(column);
        self
    }

    /// Set primary key columns
    pub fn primary_key(mut self, columns: Vec<String>) -> Self {
        self.primary_key = columns;
        self
    }

    /// Add an index
    pub fn add_index(mut self, index: IndexDef) -> Self {
        self.indexes.push(index);
        self
    }

    /// Add a constraint
    pub fn add_constraint(mut self, constraint: Constraint) -> Self {
        self.constraints.push(constraint);
        self
    }

    /// Get column by name
    pub fn get_column(&self, name: &str) -> Option<&ColumnDef> {
        self.columns.iter().find(|c| c.name == name)
    }

    /// Get column index by name
    pub fn column_index(&self, name: &str) -> Option<usize> {
        self.columns.iter().position(|c| c.name == name)
    }

    /// Check if a column is part of the primary key
    pub fn is_primary_key_column(&self, name: &str) -> bool {
        self.primary_key.iter().any(|pk| pk == name)
    }

    /// Validate table definition
    pub fn validate(&self) -> Result<(), TableDefError> {
        // Check table name
        if self.name.is_empty() {
            return Err(TableDefError::EmptyTableName);
        }

        // Check for duplicate column names
        let mut seen = HashMap::new();
        for col in &self.columns {
            if seen.insert(&col.name, true).is_some() {
                return Err(TableDefError::DuplicateColumn(col.name.clone()));
            }
        }

        // Validate primary key columns exist
        for pk_col in &self.primary_key {
            if self.get_column(pk_col).is_none() {
                return Err(TableDefError::InvalidPrimaryKey(pk_col.clone()));
            }
        }

        // Validate index columns exist
        for index in &self.indexes {
            for col in &index.columns {
                if self.get_column(col).is_none() {
                    return Err(TableDefError::InvalidIndexColumn(col.clone()));
                }
            }
        }

        // Validate constraints reference existing columns
        for constraint in &self.constraints {
            for col in &constraint.columns {
                if self.get_column(col).is_none() {
                    return Err(TableDefError::InvalidConstraintColumn(col.clone()));
                }
            }
        }

        Ok(())
    }

    /// Serialize table definition to bytes.
    ///
    /// The `RTBL` payload byte layout is owned by `reddb-file` (ADR 0046); this
    /// projects the typed definition into [`reddb_file::TableDefLayout`], mapping
    /// the SQL-level type/index/constraint enums to their on-disk discriminant
    /// bytes. The framing (magic, version, varints, strings) lives in the codec.
    pub fn to_bytes(&self) -> Vec<u8> {
        let columns = self
            .columns
            .iter()
            .map(|col| reddb_file::ColumnLayout {
                name: col.name.clone(),
                data_type: col.data_type.to_byte(),
                nullable: col.nullable,
                default: col.default.clone(),
                vector_dim: col.vector_dim,
                compress: col.compress,
                enum_variants: col.enum_variants.clone(),
                decimal_precision: col.decimal_precision,
                element_type: col.element_type.map(|et| et.to_byte()),
                metadata: col
                    .metadata
                    .iter()
                    .map(|(k, v)| (k.clone(), v.clone()))
                    .collect(),
            })
            .collect();
        let indexes = self
            .indexes
            .iter()
            .map(|idx| reddb_file::IndexLayout {
                name: idx.name.clone(),
                index_type: idx.index_type as u8,
                unique: idx.unique,
                columns: idx.columns.clone(),
            })
            .collect();
        let constraints = self
            .constraints
            .iter()
            .map(|c| reddb_file::ConstraintLayout {
                name: c.name.clone(),
                constraint_type: c.constraint_type as u8,
                columns: c.columns.clone(),
                ref_table: c.ref_table.clone(),
                ref_columns: c.ref_columns.clone(),
            })
            .collect();
        let layout = reddb_file::TableDefLayout {
            version: self.version,
            name: self.name.clone(),
            created_at: self.created_at,
            updated_at: self.updated_at,
            columns,
            primary_key: self.primary_key.clone(),
            indexes,
            constraints,
        };
        reddb_file::encode_table_def(&layout)
    }

    /// Deserialize table definition from bytes via the `reddb-file` codec.
    pub fn from_bytes(data: &[u8]) -> Result<Self, TableDefError> {
        let layout = reddb_file::decode_table_def(data).map_err(TableDefError::from_codec)?;

        let mut columns = Vec::with_capacity(layout.columns.len());
        for col in layout.columns {
            let data_type =
                DataType::from_byte(col.data_type).ok_or(TableDefError::InvalidDataType)?;
            let element_type = match col.element_type {
                Some(byte) => {
                    Some(DataType::from_byte(byte).ok_or(TableDefError::InvalidDataType)?)
                }
                None => None,
            };
            columns.push(ColumnDef {
                name: col.name,
                data_type,
                nullable: col.nullable,
                default: col.default,
                vector_dim: col.vector_dim,
                compress: col.compress,
                enum_variants: col.enum_variants,
                decimal_precision: col.decimal_precision,
                element_type,
                metadata: col.metadata.into_iter().collect(),
            });
        }

        let mut indexes = Vec::with_capacity(layout.indexes.len());
        for idx in layout.indexes {
            let index_type =
                IndexType::from_byte(idx.index_type).ok_or(TableDefError::InvalidIndexType)?;
            indexes.push(IndexDef {
                name: idx.name,
                columns: idx.columns,
                index_type,
                unique: idx.unique,
            });
        }

        let mut constraints = Vec::with_capacity(layout.constraints.len());
        for c in layout.constraints {
            let constraint_type = ConstraintType::from_byte(c.constraint_type)
                .ok_or(TableDefError::InvalidConstraintType)?;
            constraints.push(Constraint {
                name: c.name,
                constraint_type,
                columns: c.columns,
                ref_table: c.ref_table,
                ref_columns: c.ref_columns,
            });
        }

        Ok(Self {
            name: layout.name,
            columns,
            primary_key: layout.primary_key,
            indexes,
            constraints,
            version: layout.version,
            created_at: layout.created_at,
            updated_at: layout.updated_at,
        })
    }
}

impl fmt::Display for TableDef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        writeln!(f, "TABLE {} (version {})", self.name, self.version)?;
        writeln!(f, "  Columns:")?;
        for col in &self.columns {
            writeln!(f, "    {}", col)?;
        }
        if !self.primary_key.is_empty() {
            writeln!(f, "  Primary Key: ({})", self.primary_key.join(", "))?;
        }
        if !self.indexes.is_empty() {
            writeln!(f, "  Indexes:")?;
            for idx in &self.indexes {
                writeln!(f, "    {}", idx)?;
            }
        }
        Ok(())
    }
}

/// Column definition
#[derive(Debug, Clone)]
pub struct ColumnDef {
    /// Column name
    pub name: String,
    /// Data type
    pub data_type: DataType,
    /// Whether NULL values are allowed
    pub nullable: bool,
    /// Default value (serialized)
    pub default: Option<Vec<u8>>,
    /// Vector dimension (for Vector type)
    pub vector_dim: Option<u32>,
    /// Whether to compress this column's data (e.g., brotli for text)
    pub compress: bool,
    /// For Enum type: list of valid variants
    pub enum_variants: Vec<String>,
    /// For Decimal type: number of decimal places (default 4)
    pub decimal_precision: u8,
    /// For Array type: element data type
    pub element_type: Option<DataType>,
    /// Additional column metadata
    pub metadata: HashMap<String, String>,
}

impl ColumnDef {
    /// Create a new column definition
    pub fn new(name: impl Into<String>, data_type: DataType) -> Self {
        Self {
            name: name.into(),
            data_type,
            nullable: true,
            default: None,
            vector_dim: None,
            compress: false,
            enum_variants: Vec::new(),
            decimal_precision: 4,
            element_type: None,
            metadata: HashMap::new(),
        }
    }

    /// Create a non-nullable column
    pub fn not_null(mut self) -> Self {
        self.nullable = false;
        self
    }

    /// Set default value
    pub fn with_default(mut self, default: Vec<u8>) -> Self {
        self.default = Some(default);
        self
    }

    /// Set vector dimension
    pub fn with_vector_dim(mut self, dim: u32) -> Self {
        self.vector_dim = Some(dim);
        self
    }

    /// Add metadata
    pub fn with_metadata(mut self, key: impl Into<String>, value: impl Into<String>) -> Self {
        self.metadata.insert(key.into(), value.into());
        self
    }

    /// Enable per-column compression
    pub fn compressed(mut self) -> Self {
        self.compress = true;
        self
    }

    /// Set enum variants (for Enum type columns)
    pub fn with_variants(mut self, variants: Vec<String>) -> Self {
        self.enum_variants = variants;
        self
    }

    /// Set decimal precision (for Decimal type columns)
    pub fn with_precision(mut self, precision: u8) -> Self {
        self.decimal_precision = precision;
        self
    }

    /// Set element type (for Array type columns)
    pub fn with_element_type(mut self, dt: DataType) -> Self {
        self.element_type = Some(dt);
        self
    }
}

impl fmt::Display for ColumnDef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{} {}", self.name, self.data_type)?;
        if let Some(dim) = self.vector_dim {
            write!(f, "({})", dim)?;
        }
        if !self.nullable {
            write!(f, " NOT NULL")?;
        }
        if self.default.is_some() {
            write!(f, " DEFAULT <value>")?;
        }
        Ok(())
    }
}

/// Index definition
#[derive(Debug, Clone)]
pub struct IndexDef {
    /// Index name
    pub name: String,
    /// Column names in order
    pub columns: Vec<String>,
    /// Index type
    pub index_type: IndexType,
    /// Whether values must be unique
    pub unique: bool,
}

impl IndexDef {
    /// Create a new index
    pub fn new(name: impl Into<String>, columns: Vec<String>) -> Self {
        Self {
            name: name.into(),
            columns,
            index_type: IndexType::BTree,
            unique: false,
        }
    }

    /// Create a unique index
    pub fn unique(mut self) -> Self {
        self.unique = true;
        self
    }

    /// Set index type
    pub fn with_type(mut self, index_type: IndexType) -> Self {
        self.index_type = index_type;
        self
    }
}

impl fmt::Display for IndexDef {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        if self.unique {
            write!(f, "UNIQUE ")?;
        }
        write!(
            f,
            "INDEX {} ({}) USING {:?}",
            self.name,
            self.columns.join(", "),
            self.index_type
        )
    }
}

/// Index type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum IndexType {
    /// B-tree index (default, good for range queries)
    BTree = 1,
    /// Hash index (exact match only, faster for point queries)
    Hash = 2,
    /// IVF index for vector similarity search
    IvfFlat = 3,
    /// HNSW index for vector similarity search
    Hnsw = 4,
}

impl IndexType {
    fn from_byte(b: u8) -> Option<Self> {
        match b {
            1 => Some(IndexType::BTree),
            2 => Some(IndexType::Hash),
            3 => Some(IndexType::IvfFlat),
            4 => Some(IndexType::Hnsw),
            _ => None,
        }
    }
}

/// Constraint definition
#[derive(Debug, Clone)]
pub struct Constraint {
    /// Constraint name
    pub name: String,
    /// Constraint type
    pub constraint_type: ConstraintType,
    /// Columns involved
    pub columns: Vec<String>,
    /// Reference table (for foreign keys)
    pub ref_table: Option<String>,
    /// Reference columns (for foreign keys)
    pub ref_columns: Option<Vec<String>>,
}

impl Constraint {
    /// Create a new constraint
    pub fn new(name: impl Into<String>, constraint_type: ConstraintType) -> Self {
        Self {
            name: name.into(),
            constraint_type,
            columns: Vec::new(),
            ref_table: None,
            ref_columns: None,
        }
    }

    /// Set columns
    pub fn on_columns(mut self, columns: Vec<String>) -> Self {
        self.columns = columns;
        self
    }

    /// Set foreign key reference
    pub fn references(mut self, table: String, columns: Vec<String>) -> Self {
        self.ref_table = Some(table);
        self.ref_columns = Some(columns);
        self
    }
}

/// Constraint type
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum ConstraintType {
    /// Primary key constraint
    PrimaryKey = 1,
    /// Unique constraint
    Unique = 2,
    /// Foreign key constraint
    ForeignKey = 3,
    /// Check constraint
    Check = 4,
    /// Not null constraint
    NotNull = 5,
}

impl ConstraintType {
    fn from_byte(b: u8) -> Option<Self> {
        match b {
            1 => Some(ConstraintType::PrimaryKey),
            2 => Some(ConstraintType::Unique),
            3 => Some(ConstraintType::ForeignKey),
            4 => Some(ConstraintType::Check),
            5 => Some(ConstraintType::NotNull),
            _ => None,
        }
    }
}

/// Errors that can occur with table definitions
#[derive(Debug, Clone, PartialEq)]
pub enum TableDefError {
    /// Empty table name
    EmptyTableName,
    /// Duplicate column name
    DuplicateColumn(String),
    /// Invalid primary key column
    InvalidPrimaryKey(String),
    /// Invalid index column
    InvalidIndexColumn(String),
    /// Invalid constraint column
    InvalidConstraintColumn(String),
    /// Truncated data
    TruncatedData,
    /// Invalid magic bytes
    InvalidMagic,
    /// Invalid data type
    InvalidDataType,
    /// Invalid index type
    InvalidIndexType,
    /// Invalid constraint type
    InvalidConstraintType,
    /// Varint overflow
    VarintOverflow,
}

impl fmt::Display for TableDefError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            TableDefError::EmptyTableName => write!(f, "empty table name"),
            TableDefError::DuplicateColumn(name) => write!(f, "duplicate column: {}", name),
            TableDefError::InvalidPrimaryKey(name) => {
                write!(f, "invalid primary key column: {}", name)
            }
            TableDefError::InvalidIndexColumn(name) => write!(f, "invalid index column: {}", name),
            TableDefError::InvalidConstraintColumn(name) => {
                write!(f, "invalid constraint column: {}", name)
            }
            TableDefError::TruncatedData => write!(f, "truncated data"),
            TableDefError::InvalidMagic => write!(f, "invalid magic bytes"),
            TableDefError::InvalidDataType => write!(f, "invalid data type"),
            TableDefError::InvalidIndexType => write!(f, "invalid index type"),
            TableDefError::InvalidConstraintType => write!(f, "invalid constraint type"),
            TableDefError::VarintOverflow => write!(f, "varint overflow"),
        }
    }
}

impl std::error::Error for TableDefError {}

impl TableDefError {
    /// Map a `reddb-file` table-def codec error onto the server error type,
    /// preserving the legacy variant semantics (invalid UTF-8 surfaces as
    /// truncated data, as the previous hand-rolled reader did).
    fn from_codec(err: reddb_file::TableDefCodecError) -> Self {
        match err {
            reddb_file::TableDefCodecError::TruncatedData => TableDefError::TruncatedData,
            reddb_file::TableDefCodecError::InvalidMagic => TableDefError::InvalidMagic,
            reddb_file::TableDefCodecError::VarintOverflow => TableDefError::VarintOverflow,
            reddb_file::TableDefCodecError::InvalidUtf8 => TableDefError::TruncatedData,
        }
    }
}

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

    #[test]
    fn test_table_def_basic() {
        let table = TableDef::new("port_scans")
            .add_column(ColumnDef::new("id", DataType::UnsignedInteger).not_null())
            .add_column(ColumnDef::new("ip", DataType::IpAddr).not_null())
            .add_column(ColumnDef::new("port", DataType::UnsignedInteger).not_null())
            .add_column(ColumnDef::new("status", DataType::Text))
            .add_column(ColumnDef::new("timestamp", DataType::Timestamp).not_null())
            .primary_key(vec!["id".to_string()]);

        assert_eq!(table.name, "port_scans");
        assert_eq!(table.columns.len(), 5);
        assert_eq!(table.primary_key, vec!["id"]);
        assert!(table.validate().is_ok());
    }

    #[test]
    fn test_table_def_with_indexes() {
        let table = TableDef::new("subdomains")
            .add_column(ColumnDef::new("id", DataType::UnsignedInteger).not_null())
            .add_column(ColumnDef::new("domain", DataType::Text).not_null())
            .add_column(ColumnDef::new("subdomain", DataType::Text).not_null())
            .add_column(ColumnDef::new("ip", DataType::IpAddr))
            .primary_key(vec!["id".to_string()])
            .add_index(IndexDef::new("idx_domain", vec!["domain".to_string()]))
            .add_index(IndexDef::new("idx_subdomain", vec!["subdomain".to_string()]).unique());

        assert_eq!(table.indexes.len(), 2);
        assert!(table.indexes[1].unique);
        assert!(table.validate().is_ok());
    }

    #[test]
    fn test_table_def_with_vector() {
        let table = TableDef::new("embeddings")
            .add_column(ColumnDef::new("id", DataType::UnsignedInteger).not_null())
            .add_column(
                ColumnDef::new("embedding", DataType::Vector)
                    .not_null()
                    .with_vector_dim(384),
            )
            .add_column(ColumnDef::new("text", DataType::Text))
            .primary_key(vec!["id".to_string()])
            .add_index(
                IndexDef::new("idx_embedding", vec!["embedding".to_string()])
                    .with_type(IndexType::IvfFlat),
            );

        let col = table.get_column("embedding").unwrap();
        assert_eq!(col.vector_dim, Some(384));
        assert!(table.validate().is_ok());
    }

    #[test]
    fn test_table_def_validation_duplicate_column() {
        let table = TableDef::new("test")
            .add_column(ColumnDef::new("id", DataType::Integer))
            .add_column(ColumnDef::new("id", DataType::Text)); // Duplicate

        assert!(matches!(
            table.validate(),
            Err(TableDefError::DuplicateColumn(_))
        ));
    }

    #[test]
    fn test_table_def_validation_invalid_pk() {
        let table = TableDef::new("test")
            .add_column(ColumnDef::new("id", DataType::Integer))
            .primary_key(vec!["nonexistent".to_string()]);

        assert!(matches!(
            table.validate(),
            Err(TableDefError::InvalidPrimaryKey(_))
        ));
    }

    #[test]
    fn test_table_def_roundtrip() {
        let table = TableDef::new("hosts")
            .add_column(ColumnDef::new("id", DataType::UnsignedInteger).not_null())
            .add_column(ColumnDef::new("ip", DataType::IpAddr).not_null())
            .add_column(ColumnDef::new("hostname", DataType::Text))
            .add_column(ColumnDef::new("last_seen", DataType::Timestamp))
            .add_column(ColumnDef::new("fingerprint", DataType::Vector).with_vector_dim(128))
            .primary_key(vec!["id".to_string()])
            .add_index(IndexDef::new("idx_ip", vec!["ip".to_string()]).unique())
            .add_index(
                IndexDef::new("idx_fingerprint", vec!["fingerprint".to_string()])
                    .with_type(IndexType::IvfFlat),
            );

        let bytes = table.to_bytes();
        let recovered = TableDef::from_bytes(&bytes).unwrap();

        assert_eq!(table.name, recovered.name);
        assert_eq!(table.columns.len(), recovered.columns.len());
        assert_eq!(table.primary_key, recovered.primary_key);
        assert_eq!(table.indexes.len(), recovered.indexes.len());

        for (orig, rec) in table.columns.iter().zip(recovered.columns.iter()) {
            assert_eq!(orig.name, rec.name);
            assert_eq!(orig.data_type, rec.data_type);
            assert_eq!(orig.nullable, rec.nullable);
            assert_eq!(orig.vector_dim, rec.vector_dim);
        }

        for (orig, rec) in table.indexes.iter().zip(recovered.indexes.iter()) {
            assert_eq!(orig.name, rec.name);
            assert_eq!(orig.columns, rec.columns);
            assert_eq!(orig.unique, rec.unique);
            assert_eq!(orig.index_type, rec.index_type);
        }
    }

    #[test]
    fn test_column_def_metadata() {
        let col = ColumnDef::new("ip", DataType::IpAddr)
            .not_null()
            .with_metadata("description", "Target IP address")
            .with_metadata("indexed", "true");

        assert_eq!(
            col.metadata.get("description"),
            Some(&"Target IP address".to_string())
        );
        assert_eq!(col.metadata.get("indexed"), Some(&"true".to_string()));
    }

    #[test]
    fn test_constraint_foreign_key() {
        let constraint = Constraint::new("fk_host", ConstraintType::ForeignKey)
            .on_columns(vec!["host_id".to_string()])
            .references("hosts".to_string(), vec!["id".to_string()]);

        assert_eq!(constraint.constraint_type, ConstraintType::ForeignKey);
        assert_eq!(constraint.columns, vec!["host_id"]);
        assert_eq!(constraint.ref_table, Some("hosts".to_string()));
        assert_eq!(constraint.ref_columns, Some(vec!["id".to_string()]));
    }

    #[test]
    fn test_table_display() {
        let table = TableDef::new("test")
            .add_column(ColumnDef::new("id", DataType::Integer).not_null())
            .add_column(ColumnDef::new("name", DataType::Text))
            .primary_key(vec!["id".to_string()]);

        let display = format!("{}", table);
        assert!(display.contains("TABLE test"));
        assert!(display.contains("id INTEGER NOT NULL"));
        assert!(display.contains("name TEXT"));
        assert!(display.contains("Primary Key: (id)"));
    }
}