toasty_core/schema/db/
diff.rs

1use std::collections::HashMap;
2
3use crate::schema::db::{ColumnId, IndexId, Schema, TableId};
4
5#[derive(Default)]
6pub struct RenameHints {
7    tables: HashMap<TableId, TableId>,
8    columns: HashMap<ColumnId, ColumnId>,
9    indices: HashMap<IndexId, IndexId>,
10}
11
12impl RenameHints {
13    pub fn new() -> Self {
14        Self::default()
15    }
16
17    pub fn add_table_hint(&mut self, from: TableId, to: TableId) {
18        self.tables.insert(from, to);
19    }
20
21    pub fn add_column_hint(&mut self, from: ColumnId, to: ColumnId) {
22        self.columns.insert(from, to);
23    }
24
25    pub fn add_index_hint(&mut self, from: IndexId, to: IndexId) {
26        self.indices.insert(from, to);
27    }
28
29    pub fn get_table(&self, from: TableId) -> Option<TableId> {
30        self.tables.get(&from).copied()
31    }
32
33    pub fn get_column(&self, from: ColumnId) -> Option<ColumnId> {
34        self.columns.get(&from).copied()
35    }
36
37    pub fn get_index(&self, from: IndexId) -> Option<IndexId> {
38        self.indices.get(&from).copied()
39    }
40}
41
42pub struct DiffContext<'a> {
43    previous: &'a Schema,
44    next: &'a Schema,
45
46    rename_hints: &'a RenameHints,
47}
48
49impl<'a> DiffContext<'a> {
50    pub fn new(previous: &'a Schema, next: &'a Schema, rename_hints: &'a RenameHints) -> Self {
51        Self {
52            previous,
53            next,
54            rename_hints,
55        }
56    }
57
58    pub fn rename_hints(&self) -> &'a RenameHints {
59        self.rename_hints
60    }
61
62    pub fn previous(&self) -> &'a Schema {
63        self.previous
64    }
65
66    pub fn next(&self) -> &'a Schema {
67        self.next
68    }
69}