toasty_core/schema/diff/schema.rs
1use super::{Context, RenameHints, Table, Type};
2use crate::schema::db;
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, diff};
13///
14/// let previous = db::Schema::default();
15/// let next = db::Schema::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 db::Schema,
22 next: &'a db::Schema,
23 tables: Vec<Table<'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 db::Schema, to: &'a db::Schema, rename_hints: &'a RenameHints) -> Self {
29 let cx = Context::new(from, to, rename_hints);
30 Self {
31 previous: from,
32 next: to,
33 tables: Table::diff(&cx, &from.tables, &to.tables),
34 }
35 }
36
37 /// Computes the enum type diff between the two schemas.
38 pub fn types(&self) -> Vec<Type<'a>> {
39 Type::diff(self.previous, self.next)
40 }
41
42 /// Returns the table-level diff.
43 pub fn tables(&self) -> &[Table<'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 db::Schema {
54 self.previous
55 }
56
57 /// Returns the schema after the change.
58 pub fn next(&self) -> &'a db::Schema {
59 self.next
60 }
61}