Skip to main content

toasty_core/schema/diff/
schema.rs

1use super::{Context, RenameHints, Tables, Types};
2use crate::schema::db::Schema as DbSchema;
3
4/// The top-level diff between two database schemas.
5///
6/// Contains a [`Tables`] describing created, dropped, and altered tables.
7/// Constructed via [`Schema::from`].
8///
9/// # Examples
10///
11/// ```ignore
12/// use toasty_core::schema::{db::Schema as DbSchema, diff};
13///
14/// let previous = DbSchema::default();
15/// let next = DbSchema::default();
16/// let hints = diff::RenameHints::new();
17/// let d = diff::Schema::from(&previous, &next, &hints);
18/// assert!(d.is_empty());
19/// ```
20pub struct Schema<'a> {
21    previous: &'a DbSchema,
22    next: &'a DbSchema,
23    tables: Tables<'a>,
24}
25
26impl<'a> Schema<'a> {
27    /// Computes the diff between two schemas, using the provided rename hints.
28    pub fn from(from: &'a DbSchema, to: &'a DbSchema, rename_hints: &'a RenameHints) -> Self {
29        let cx = &Context::new(from, to, rename_hints);
30        Self {
31            previous: from,
32            next: to,
33            tables: Tables::from(cx, &from.tables, &to.tables),
34        }
35    }
36
37    /// Computes the enum type diff between the two schemas.
38    pub fn types(&self) -> Types<'a> {
39        Types::from(self.previous, self.next)
40    }
41
42    /// Returns the table-level diff.
43    pub fn tables(&self) -> &Tables<'a> {
44        &self.tables
45    }
46
47    /// Returns `true` if no tables were created, dropped, or altered.
48    pub fn is_empty(&self) -> bool {
49        self.tables.is_empty()
50    }
51
52    /// Returns the schema before the change.
53    pub fn previous(&self) -> &'a DbSchema {
54        self.previous
55    }
56
57    /// Returns the schema after the change.
58    pub fn next(&self) -> &'a DbSchema {
59        self.next
60    }
61}