1use schema_model::model::column::Column;
2use schema_model::model::constraint::Constraint;
3use schema_model::model::key::Key;
4use schema_model::model::relation::Relation;
5use schema_model::model::schema::Schema;
6use schema_model::model::view::View;
7
8use crate::change::SchemaChange;
9use crate::change_set::ChangeSet;
10
11pub struct SchemaDiffEngine;
12
13impl SchemaDiffEngine {
14 pub fn diff(old: &Schema, new: &Schema) -> ChangeSet {
15 let mut change_set = ChangeSet::new();
16
17 diff_drop_views(old, new, &mut change_set);
19 diff_drop_relations(old, new, &mut change_set);
20 diff_drop_keys(old, new, &mut change_set);
21 diff_drop_constraints(old, new, &mut change_set);
22 diff_drop_columns(old, new, &mut change_set);
23 diff_drop_tables(old, new, &mut change_set);
24
25 diff_add_tables(old, new, &mut change_set);
27 diff_add_columns(old, new, &mut change_set);
28 diff_modify_columns(old, new, &mut change_set);
29 diff_add_keys(old, new, &mut change_set);
30 diff_add_constraints(old, new, &mut change_set);
31 diff_add_relations(old, new, &mut change_set);
32 diff_add_views(old, new, &mut change_set);
33
34 change_set
35 }
36}
37
38fn diff_drop_tables(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
39 for old_table in old.tables() {
40 if new.get_optional_table(old_table.name()).is_none() {
41 cs.add_change(SchemaChange::DropTable {
42 table_name: old_table.name().to_string(),
43 });
44 }
45 }
46}
47
48fn diff_add_tables(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
49 for new_table in new.tables() {
50 if old.get_optional_table(new_table.name()).is_none() {
51 cs.add_change(SchemaChange::AddTable {
52 table_name: new_table.name().to_string(),
53 });
54 }
55 }
56}
57
58fn diff_drop_columns(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
59 for old_table in old.tables() {
60 if let Some(new_table) = new.get_optional_table(old_table.name()) {
61 for old_col in old_table.columns() {
62 if !new_table.has_column(old_col.name()) {
63 let rename_candidates: Vec<String> = new_table
64 .columns()
65 .iter()
66 .filter(|nc| !old_table.has_column(nc.name()) && nc.column_type() == old_col.column_type())
67 .map(|nc| nc.name().to_string())
68 .collect();
69 cs.add_change(SchemaChange::DropColumn {
70 table_name: old_table.name().to_string(),
71 column_name: old_col.name().to_string(),
72 rename_candidates,
73 });
74 }
75 }
76 }
77 }
78}
79
80fn diff_add_columns(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
81 for new_table in new.tables() {
82 if let Some(old_table) = old.get_optional_table(new_table.name()) {
83 for new_col in new_table.columns() {
84 if !old_table.has_column(new_col.name()) {
85 cs.add_change(SchemaChange::AddColumn {
86 table_name: new_table.name().to_string(),
87 column: new_col.clone(),
88 });
89 }
90 }
91 }
92 }
93}
94
95fn diff_modify_columns(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
96 for new_table in new.tables() {
97 if let Some(old_table) = old.get_optional_table(new_table.name()) {
98 for new_col in new_table.columns() {
99 if old_table.has_column(new_col.name()) {
100 let old_col = old_table.column(new_col.name());
101 if columns_differ(old_col, new_col) {
102 cs.add_change(SchemaChange::ModifyColumn {
103 table_name: new_table.name().to_string(),
104 old_column: old_col.clone(),
105 new_column: new_col.clone(),
106 });
107 }
108 }
109 }
110 }
111 }
112}
113
114fn columns_differ(a: &Column, b: &Column) -> bool {
115 a.column_type() != b.column_type()
116 || a.length() != b.length()
117 || a.scale() != b.scale()
118 || a.required() != b.required()
119 || a.default_constraint() != b.default_constraint()
120 || a.check_constraint() != b.check_constraint()
121}
122
123fn diff_drop_keys(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
124 for old_table in old.tables() {
125 if let Some(new_table) = new.get_optional_table(old_table.name()) {
126 for old_key in old_table.keys().iter().chain(old_table.indexes().iter()) {
127 if !key_exists_in(old_key, new_table.keys()) && !key_exists_in(old_key, new_table.indexes()) {
128 cs.add_change(SchemaChange::DropKey {
129 table_name: old_table.name().to_string(),
130 key: old_key.clone(),
131 });
132 }
133 }
134 }
135 }
136}
137
138fn diff_add_keys(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
139 for new_table in new.tables() {
140 if let Some(old_table) = old.get_optional_table(new_table.name()) {
141 for new_key in new_table.keys().iter().chain(new_table.indexes().iter()) {
142 if !key_exists_in(new_key, old_table.keys()) && !key_exists_in(new_key, old_table.indexes()) {
143 cs.add_change(SchemaChange::AddKey {
144 table_name: new_table.name().to_string(),
145 key: new_key.clone(),
146 });
147 }
148 }
149 }
150 }
151}
152
153fn key_exists_in(key: &Key, keys: &[Key]) -> bool {
154 keys.iter().any(|k| keys_equal(k, key))
155}
156
157fn keys_equal(a: &Key, b: &Key) -> bool {
158 a.key_type() == b.key_type()
159 && a.columns().len() == b.columns().len()
160 && a.columns().iter().zip(b.columns().iter()).all(|(ac, bc)| {
161 ac.name().eq_ignore_ascii_case(bc.name())
162 })
163}
164
165fn diff_drop_constraints(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
166 for old_table in old.tables() {
167 if let Some(new_table) = new.get_optional_table(old_table.name()) {
168 for old_con in old_table.constraints() {
169 if !constraint_exists_in(old_con, new_table.constraints()) {
170 cs.add_change(SchemaChange::DropConstraint {
171 table_name: old_table.name().to_string(),
172 constraint_name: old_con.name().to_string(),
173 });
174 }
175 }
176 }
177 }
178}
179
180fn diff_add_constraints(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
181 for new_table in new.tables() {
182 if let Some(old_table) = old.get_optional_table(new_table.name()) {
183 for new_con in new_table.constraints() {
184 if !constraint_exists_in(new_con, old_table.constraints()) {
185 cs.add_change(SchemaChange::AddConstraint {
186 table_name: new_table.name().to_string(),
187 constraint: new_con.clone(),
188 });
189 }
190 }
191 }
192 }
193}
194
195fn constraint_exists_in(con: &Constraint, constraints: &[Constraint]) -> bool {
196 constraints
197 .iter()
198 .any(|c| c.name().eq_ignore_ascii_case(con.name()))
199}
200
201fn diff_drop_relations(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
202 for old_table in old.tables() {
203 if let Some(new_table) = new.get_optional_table(old_table.name()) {
204 for old_rel in old_table.relations() {
205 if !relation_exists_in(old_rel, new_table.relations()) {
206 cs.add_change(SchemaChange::DropRelation {
207 relation: old_rel.clone(),
208 });
209 }
210 }
211 }
212 }
213}
214
215fn diff_add_relations(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
216 for new_table in new.tables() {
217 if let Some(old_table) = old.get_optional_table(new_table.name()) {
218 for new_rel in new_table.relations() {
219 if !relation_exists_in(new_rel, old_table.relations()) {
220 cs.add_change(SchemaChange::AddRelation {
221 relation: new_rel.clone(),
222 });
223 }
224 }
225 }
226 }
227}
228
229fn relation_exists_in(rel: &Relation, relations: &[Relation]) -> bool {
230 relations.iter().any(|r| relations_equal(r, rel))
231}
232
233fn relations_equal(a: &Relation, b: &Relation) -> bool {
234 a.from_table_name().eq_ignore_ascii_case(b.from_table_name())
235 && a.from_column_name().eq_ignore_ascii_case(b.from_column_name())
236 && a.to_table_name().eq_ignore_ascii_case(b.to_table_name())
237 && a.to_column_name().eq_ignore_ascii_case(b.to_column_name())
238}
239
240fn diff_drop_views(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
241 for old_view in old.all_views() {
242 if !view_exists_in(old_view, new.all_views()) {
243 cs.add_change(SchemaChange::DropView {
244 view_name: old_view.name().to_string(),
245 });
246 }
247 }
248}
249
250fn diff_add_views(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
251 for new_view in new.all_views() {
252 if !view_exists_in(new_view, old.all_views()) {
253 cs.add_change(SchemaChange::AddView {
254 view: new_view.clone(),
255 });
256 }
257 }
258}
259
260fn view_exists_in(view: &View, views: &[View]) -> bool {
261 views
262 .iter()
263 .any(|v| v.name().eq_ignore_ascii_case(view.name()))
264}