toasty-core 0.3.0

Core types, schema representations, and driver interface for Toasty
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
use super::{DiffContext, TableId, Type, table};
use crate::stmt;

use std::{
    collections::{HashMap, HashSet},
    fmt,
    ops::Deref,
};

/// A column in a database table.
///
/// Each column has a logical type ([`stmt::Type`]) used by the query engine and
/// a storage type ([`Type`]) representing how the value is stored in the database.
///
/// # Examples
///
/// ```ignore
/// use toasty_core::schema::db::{Column, ColumnId, TableId, Type};
/// use toasty_core::stmt;
///
/// let column = Column {
///     id: ColumnId { table: TableId(0), index: 0 },
///     name: "email".to_string(),
///     ty: stmt::Type::String,
///     storage_ty: Type::VarChar(255),
///     nullable: false,
///     primary_key: false,
///     auto_increment: false,
/// };
///
/// assert_eq!(column.name, "email");
/// assert!(!column.nullable);
/// ```
#[derive(Debug, Clone, PartialEq)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct Column {
    /// Uniquely identifies the column in the schema.
    pub id: ColumnId,

    /// The name of the column in the database.
    pub name: String,

    /// The column type, from Toasty's point of view.
    pub ty: stmt::Type,

    /// The database storage type of the column.
    pub storage_ty: Type,

    /// Whether or not the column is nullable
    #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "is_false"))]
    pub nullable: bool,

    /// True if the column is part of the table's primary key
    #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "is_false"))]
    pub primary_key: bool,

    /// True if the column is an integer that should be auto-incremented
    /// with each insertion of a new row. This should be false if a `storage_ty`
    /// of type `Serial` is used.
    #[cfg_attr(feature = "serde", serde(default, skip_serializing_if = "is_false"))]
    pub auto_increment: bool,
}

#[cfg(feature = "serde")]
fn is_false(b: &bool) -> bool {
    !*b
}

/// Uniquely identifies a column within a schema.
///
/// A `ColumnId` combines the [`TableId`] of the owning table with the column's
/// positional index within that table's column list.
///
/// # Examples
///
/// ```ignore
/// use toasty_core::schema::db::{ColumnId, TableId};
///
/// let id = ColumnId { table: TableId(0), index: 2 };
/// assert_eq!(id.index, 2);
/// ```
#[derive(PartialEq, Eq, Clone, Copy, Hash)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub struct ColumnId {
    /// The table this column belongs to.
    pub table: TableId,
    /// Zero-based position of this column in the table's column list.
    pub index: usize,
}

impl ColumnId {
    pub(crate) fn placeholder() -> Self {
        Self {
            table: table::TableId::placeholder(),
            index: usize::MAX,
        }
    }
}

impl From<&Column> for ColumnId {
    fn from(value: &Column) -> Self {
        value.id
    }
}

impl fmt::Debug for ColumnId {
    fn fmt(&self, fmt: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(fmt, "ColumnId({}/{})", self.table.0, self.index)
    }
}

/// The set of differences between two column lists.
///
/// Computed by [`ColumnsDiff::from`] and dereferences to
/// `Vec<ColumnsDiffItem>` for iteration.
///
/// # Examples
///
/// ```ignore
/// use toasty_core::schema::db::{ColumnsDiff, DiffContext, RenameHints, Schema};
///
/// let previous = Schema::default();
/// let next = Schema::default();
/// let hints = RenameHints::new();
/// let cx = DiffContext::new(&previous, &next, &hints);
/// let diff = ColumnsDiff::from(&cx, &[], &[]);
/// assert!(diff.is_empty());
/// ```
pub struct ColumnsDiff<'a> {
    items: Vec<ColumnsDiffItem<'a>>,
}

impl<'a> ColumnsDiff<'a> {
    /// Computes the diff between two column slices.
    ///
    /// Uses [`DiffContext`] to resolve rename hints. Columns matched by name
    /// (or by rename hint) are compared field-by-field; unmatched columns in
    /// `previous` become drops, and unmatched columns in `next` become adds.
    pub fn from(cx: &DiffContext<'a>, previous: &'a [Column], next: &'a [Column]) -> Self {
        fn has_diff(previous: &Column, next: &Column) -> bool {
            previous.name != next.name
                || previous.storage_ty != next.storage_ty
                || previous.nullable != next.nullable
                || previous.primary_key != next.primary_key
                || previous.auto_increment != next.auto_increment
        }

        let mut items = vec![];
        let mut add_ids: HashSet<_> = next.iter().map(|next| next.id).collect();

        let next_map =
            HashMap::<&str, &'a Column>::from_iter(next.iter().map(|to| (to.name.as_str(), to)));

        for previous in previous {
            let next = if let Some(next_id) = cx.rename_hints().get_column(previous.id) {
                cx.next().column(next_id)
            } else if let Some(next) = next_map.get(previous.name.as_str()) {
                next
            } else {
                items.push(ColumnsDiffItem::DropColumn(previous));
                continue;
            };

            add_ids.remove(&next.id);

            if has_diff(previous, next) {
                items.push(ColumnsDiffItem::AlterColumn { previous, next });
            }
        }

        for column_id in add_ids {
            items.push(ColumnsDiffItem::AddColumn(cx.next().column(column_id)));
        }

        Self { items }
    }

    /// Returns `true` if there are no column changes.
    pub const fn is_empty(&self) -> bool {
        self.items.is_empty()
    }
}

impl<'a> Deref for ColumnsDiff<'a> {
    type Target = Vec<ColumnsDiffItem<'a>>;

    fn deref(&self) -> &Self::Target {
        &self.items
    }
}

/// A single change detected between two column lists.
pub enum ColumnsDiffItem<'a> {
    /// A new column was added.
    AddColumn(&'a Column),
    /// An existing column was removed.
    DropColumn(&'a Column),
    /// A column was modified (name, type, nullability, or other property changed).
    AlterColumn {
        /// The column definition before the change.
        previous: &'a Column,
        /// The column definition after the change.
        next: &'a Column,
    },
}

#[cfg(test)]
mod tests {
    use crate::schema::db::{
        Column, ColumnId, ColumnsDiff, ColumnsDiffItem, DiffContext, PrimaryKey, RenameHints,
        Schema, Table, TableId, Type,
    };
    use crate::stmt;

    fn make_column(
        table_id: usize,
        index: usize,
        name: &str,
        storage_ty: Type,
        nullable: bool,
    ) -> Column {
        Column {
            id: ColumnId {
                table: TableId(table_id),
                index,
            },
            name: name.to_string(),
            ty: stmt::Type::String, // Simplified for tests
            storage_ty,
            nullable,
            primary_key: false,
            auto_increment: false,
        }
    }

    fn make_schema_with_columns(table_id: usize, columns: Vec<Column>) -> Schema {
        let mut schema = Schema::default();
        schema.tables.push(Table {
            id: TableId(table_id),
            name: "test_table".to_string(),
            columns,
            primary_key: PrimaryKey {
                columns: vec![],
                index: super::super::IndexId {
                    table: TableId(table_id),
                    index: 0,
                },
            },
            indices: vec![],
        });
        schema
    }

    #[test]
    fn test_no_diff_same_columns() {
        let from_cols = vec![
            make_column(0, 0, "id", Type::Integer(8), false),
            make_column(0, 1, "name", Type::Text, false),
        ];
        let to_cols = vec![
            make_column(0, 0, "id", Type::Integer(8), false),
            make_column(0, 1, "name", Type::Text, false),
        ];

        let from_schema = make_schema_with_columns(0, from_cols.clone());
        let to_schema = make_schema_with_columns(0, to_cols.clone());
        let hints = RenameHints::new();
        let cx = DiffContext::new(&from_schema, &to_schema, &hints);

        let diff = ColumnsDiff::from(&cx, &from_cols, &to_cols);
        assert!(diff.is_empty());
    }

    #[test]
    fn test_add_column() {
        let from_cols = vec![make_column(0, 0, "id", Type::Integer(8), false)];
        let to_cols = vec![
            make_column(0, 0, "id", Type::Integer(8), false),
            make_column(0, 1, "name", Type::Text, false),
        ];

        let from_schema = make_schema_with_columns(0, from_cols.clone());
        let to_schema = make_schema_with_columns(0, to_cols.clone());
        let hints = RenameHints::new();
        let cx = DiffContext::new(&from_schema, &to_schema, &hints);

        let diff = ColumnsDiff::from(&cx, &from_cols, &to_cols);
        assert_eq!(diff.items.len(), 1);
        assert!(matches!(diff.items[0], ColumnsDiffItem::AddColumn(_)));
        if let ColumnsDiffItem::AddColumn(col) = diff.items[0] {
            assert_eq!(col.name, "name");
        }
    }

    #[test]
    fn test_drop_column() {
        let from_cols = vec![
            make_column(0, 0, "id", Type::Integer(8), false),
            make_column(0, 1, "name", Type::Text, false),
        ];
        let to_cols = vec![make_column(0, 0, "id", Type::Integer(8), false)];

        let from_schema = make_schema_with_columns(0, from_cols.clone());
        let to_schema = make_schema_with_columns(0, to_cols.clone());
        let hints = RenameHints::new();
        let cx = DiffContext::new(&from_schema, &to_schema, &hints);

        let diff = ColumnsDiff::from(&cx, &from_cols, &to_cols);
        assert_eq!(diff.items.len(), 1);
        assert!(matches!(diff.items[0], ColumnsDiffItem::DropColumn(_)));
        if let ColumnsDiffItem::DropColumn(col) = diff.items[0] {
            assert_eq!(col.name, "name");
        }
    }

    #[test]
    fn test_alter_column_type() {
        let from_cols = vec![make_column(0, 0, "id", Type::Integer(8), false)];
        let to_cols = vec![make_column(0, 0, "id", Type::Text, false)];

        let from_schema = make_schema_with_columns(0, from_cols.clone());
        let to_schema = make_schema_with_columns(0, to_cols.clone());
        let hints = RenameHints::new();
        let cx = DiffContext::new(&from_schema, &to_schema, &hints);

        let diff = ColumnsDiff::from(&cx, &from_cols, &to_cols);
        assert_eq!(diff.items.len(), 1);
        assert!(matches!(diff.items[0], ColumnsDiffItem::AlterColumn { .. }));
    }

    #[test]
    fn test_alter_column_nullable() {
        let from_cols = vec![make_column(0, 0, "id", Type::Integer(8), false)];
        let to_cols = vec![make_column(0, 0, "id", Type::Integer(8), true)];

        let from_schema = make_schema_with_columns(0, from_cols.clone());
        let to_schema = make_schema_with_columns(0, to_cols.clone());
        let hints = RenameHints::new();
        let cx = DiffContext::new(&from_schema, &to_schema, &hints);

        let diff = ColumnsDiff::from(&cx, &from_cols, &to_cols);
        assert_eq!(diff.items.len(), 1);
        assert!(matches!(diff.items[0], ColumnsDiffItem::AlterColumn { .. }));
    }

    #[test]
    fn test_rename_column_with_hint() {
        // Column renamed from "old_name" to "new_name"
        let from_cols = vec![make_column(0, 0, "old_name", Type::Text, false)];
        let to_cols = vec![make_column(0, 0, "new_name", Type::Text, false)];

        let from_schema = make_schema_with_columns(0, from_cols.clone());
        let to_schema = make_schema_with_columns(0, to_cols.clone());

        let mut hints = RenameHints::new();
        hints.add_column_hint(
            ColumnId {
                table: TableId(0),
                index: 0,
            },
            ColumnId {
                table: TableId(0),
                index: 0,
            },
        );
        let cx = DiffContext::new(&from_schema, &to_schema, &hints);

        let diff = ColumnsDiff::from(&cx, &from_cols, &to_cols);
        assert_eq!(diff.items.len(), 1);
        assert!(matches!(diff.items[0], ColumnsDiffItem::AlterColumn { .. }));
        if let ColumnsDiffItem::AlterColumn { previous, next } = diff.items[0] {
            assert_eq!(previous.name, "old_name");
            assert_eq!(next.name, "new_name");
        }
    }

    #[test]
    fn test_rename_column_without_hint_is_drop_and_add() {
        // Column renamed from "old_name" to "new_name", but no hint provided
        // Should be treated as drop + add
        let from_cols = vec![make_column(0, 0, "old_name", Type::Text, false)];
        let to_cols = vec![make_column(0, 0, "new_name", Type::Text, false)];

        let from_schema = make_schema_with_columns(0, from_cols.clone());
        let to_schema = make_schema_with_columns(0, to_cols.clone());
        let hints = RenameHints::new();
        let cx = DiffContext::new(&from_schema, &to_schema, &hints);

        let diff = ColumnsDiff::from(&cx, &from_cols, &to_cols);
        assert_eq!(diff.items.len(), 2);

        let has_drop = diff
            .items
            .iter()
            .any(|item| matches!(item, ColumnsDiffItem::DropColumn(_)));
        let has_add = diff
            .items
            .iter()
            .any(|item| matches!(item, ColumnsDiffItem::AddColumn(_)));
        assert!(has_drop);
        assert!(has_add);
    }

    #[cfg(feature = "serde")]
    mod serde_tests {
        use crate::schema::db::{Column, ColumnId, TableId, Type};
        use crate::stmt;

        fn base_column() -> Column {
            Column {
                id: ColumnId {
                    table: TableId(0),
                    index: 0,
                },
                name: "test".to_string(),
                ty: stmt::Type::String,
                storage_ty: Type::Text,
                nullable: false,
                primary_key: false,
                auto_increment: false,
            }
        }

        #[test]
        fn false_booleans_are_omitted() {
            let toml = toml::to_string(&base_column()).unwrap();
            assert!(!toml.contains("nullable"), "toml: {toml}");
            assert!(!toml.contains("primary_key"), "toml: {toml}");
            assert!(!toml.contains("auto_increment"), "toml: {toml}");
        }

        #[test]
        fn nullable_true_is_included() {
            let col = Column {
                nullable: true,
                ..base_column()
            };
            let toml = toml::to_string(&col).unwrap();
            assert!(toml.contains("nullable = true"), "toml: {toml}");
        }

        #[test]
        fn primary_key_true_is_included() {
            let col = Column {
                primary_key: true,
                ..base_column()
            };
            let toml = toml::to_string(&col).unwrap();
            assert!(toml.contains("primary_key = true"), "toml: {toml}");
        }

        #[test]
        fn auto_increment_true_is_included() {
            let col = Column {
                auto_increment: true,
                ..base_column()
            };
            let toml = toml::to_string(&col).unwrap();
            assert!(toml.contains("auto_increment = true"), "toml: {toml}");
        }

        #[test]
        fn missing_bool_fields_deserialize_as_false() {
            let toml = "name = \"test\"\nty = \"String\"\nstorage_ty = \"Text\"\n\n[id]\ntable = 0\nindex = 0\n";
            let col: Column = toml::from_str(toml).unwrap();
            assert!(!col.nullable);
            assert!(!col.primary_key);
            assert!(!col.auto_increment);
        }

        #[test]
        fn round_trip_all_true() {
            let original = Column {
                nullable: true,
                primary_key: true,
                auto_increment: true,
                ..base_column()
            };
            let decoded: Column = toml::from_str(&toml::to_string(&original).unwrap()).unwrap();
            assert_eq!(original, decoded);
        }
    }

    #[test]
    fn test_multiple_operations() {
        let from_cols = vec![
            make_column(0, 0, "id", Type::Integer(8), false),
            make_column(0, 1, "old_name", Type::Text, false),
            make_column(0, 2, "to_drop", Type::Text, false),
        ];
        let to_cols = vec![
            make_column(0, 0, "id", Type::Text, false), // type changed
            make_column(0, 1, "new_name", Type::Text, false), // renamed
            make_column(0, 2, "added", Type::Integer(8), false), // new column
        ];

        let from_schema = make_schema_with_columns(0, from_cols.clone());
        let to_schema = make_schema_with_columns(0, to_cols.clone());

        let mut hints = RenameHints::new();
        hints.add_column_hint(
            ColumnId {
                table: TableId(0),
                index: 1,
            },
            ColumnId {
                table: TableId(0),
                index: 1,
            },
        );
        let cx = DiffContext::new(&from_schema, &to_schema, &hints);

        let diff = ColumnsDiff::from(&cx, &from_cols, &to_cols);
        // Should have: 1 alter (id type changed), 1 alter (renamed), 1 drop (to_drop), 1 add (added)
        assert_eq!(diff.items.len(), 4);
    }
}