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::types::KeyType;
7use schema_model::model::view::View;
8
9use crate::change::SchemaChange;
10use crate::change_set::ChangeSet;
11
12pub struct SchemaDiffEngine;
13
14impl SchemaDiffEngine {
15 pub fn diff(old: &Schema, new: &Schema) -> ChangeSet {
16 let mut change_set = ChangeSet::new();
17
18 diff_drop_views(old, new, &mut change_set);
20 diff_drop_relations(old, new, &mut change_set);
21 diff_drop_keys(old, new, &mut change_set);
22 diff_drop_constraints(old, new, &mut change_set);
23 diff_drop_columns(old, new, &mut change_set);
24 diff_drop_tables(old, new, &mut change_set);
25
26 diff_add_tables(old, new, &mut change_set);
28 diff_add_columns(old, new, &mut change_set);
29 diff_modify_columns(old, new, &mut change_set);
30 diff_add_keys(old, new, &mut change_set);
31 diff_add_constraints(old, new, &mut change_set);
32 diff_add_relations(old, new, &mut change_set);
33 diff_add_views(old, new, &mut change_set);
34
35 change_set
36 }
37}
38
39fn diff_drop_tables(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
40 for old_table in old.tables() {
41 if new.get_optional_table(old_table.name()).is_none() {
42 cs.add_change(SchemaChange::DropTable {
43 table_name: old_table.name().to_string(),
44 });
45 }
46 }
47}
48
49fn diff_add_tables(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
50 for new_table in new.tables() {
51 if old.get_optional_table(new_table.name()).is_none() {
52 cs.add_change(SchemaChange::AddTable {
53 table_name: new_table.name().to_string(),
54 });
55 }
56 }
57}
58
59fn diff_drop_columns(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
60 for old_table in old.tables() {
61 if let Some(new_table) = new.get_optional_table(old_table.name()) {
62 for old_col in old_table.columns() {
63 if !new_table.has_column(old_col.name()) {
64 let rename_candidates: Vec<String> = new_table
65 .columns()
66 .iter()
67 .filter(|nc| !old_table.has_column(nc.name()) && nc.column_type() == old_col.column_type())
68 .map(|nc| nc.name().to_string())
69 .collect();
70 cs.add_change(SchemaChange::DropColumn {
71 table_name: old_table.name().to_string(),
72 column_name: old_col.name().to_string(),
73 rename_candidates,
74 });
75 }
76 }
77 }
78 }
79}
80
81fn diff_add_columns(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
82 for new_table in new.tables() {
83 if let Some(old_table) = old.get_optional_table(new_table.name()) {
84 for new_col in new_table.columns() {
85 if !old_table.has_column(new_col.name()) {
86 cs.add_change(SchemaChange::AddColumn {
87 table_name: new_table.name().to_string(),
88 column: new_col.clone(),
89 });
90 }
91 }
92 }
93 }
94}
95
96fn diff_modify_columns(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
97 for new_table in new.tables() {
98 if let Some(old_table) = old.get_optional_table(new_table.name()) {
99 for new_col in new_table.columns() {
100 if old_table.has_column(new_col.name()) {
101 let old_col = old_table.column(new_col.name());
102 if columns_differ(old_col, new_col) {
103 cs.add_change(SchemaChange::ModifyColumn {
104 table_name: new_table.name().to_string(),
105 old_column: old_col.clone(),
106 new_column: new_col.clone(),
107 });
108 }
109 }
110 }
111 }
112 }
113}
114
115fn columns_differ(a: &Column, b: &Column) -> bool {
116 a.column_type() != b.column_type()
117 || a.length() != b.length()
118 || a.scale() != b.scale()
119 || a.required() != b.required()
120 || a.default_constraint() != b.default_constraint()
121 || a.check_constraint() != b.check_constraint()
122 || a.enum_type() != b.enum_type()
123 || a.element_type() != b.element_type()
124 || a.generated() != b.generated()
125 || a.min_value() != b.min_value()
126 || a.max_value() != b.max_value()
127}
128
129fn diff_drop_keys(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
130 for old_table in old.tables() {
131 if let Some(new_table) = new.get_optional_table(old_table.name()) {
132 let mut unique_ordinal = 0;
137 for old_key in old_table.keys() {
138 if old_key.key_type() == KeyType::Unique {
139 unique_ordinal += 1;
140 }
141 if !key_exists_in(old_key, new_table.keys()) && !key_exists_in(old_key, new_table.indexes()) {
142 cs.add_change(SchemaChange::DropKey {
143 table_name: old_table.name().to_string(),
144 key: old_key.clone(),
145 ordinal: unique_ordinal,
146 });
147 }
148 }
149
150 let mut index_ordinal = 0;
151 for old_key in old_table.indexes() {
152 if old_key.is_index() {
153 index_ordinal += 1;
154 }
155 if !key_exists_in(old_key, new_table.keys()) && !key_exists_in(old_key, new_table.indexes()) {
156 cs.add_change(SchemaChange::DropKey {
157 table_name: old_table.name().to_string(),
158 key: old_key.clone(),
159 ordinal: index_ordinal,
160 });
161 }
162 }
163 }
164 }
165}
166
167fn diff_add_keys(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
168 for new_table in new.tables() {
169 if let Some(old_table) = old.get_optional_table(new_table.name()) {
170 let mut unique_ordinal = 0;
173 for new_key in new_table.keys() {
174 if new_key.key_type() == KeyType::Unique {
175 unique_ordinal += 1;
176 }
177 if !key_exists_in(new_key, old_table.keys()) && !key_exists_in(new_key, old_table.indexes()) {
178 cs.add_change(SchemaChange::AddKey {
179 table_name: new_table.name().to_string(),
180 key: new_key.clone(),
181 ordinal: unique_ordinal,
182 });
183 }
184 }
185
186 let mut index_ordinal = 0;
187 for new_key in new_table.indexes() {
188 if new_key.is_index() {
189 index_ordinal += 1;
190 }
191 if !key_exists_in(new_key, old_table.keys()) && !key_exists_in(new_key, old_table.indexes()) {
192 cs.add_change(SchemaChange::AddKey {
193 table_name: new_table.name().to_string(),
194 key: new_key.clone(),
195 ordinal: index_ordinal,
196 });
197 }
198 }
199 }
200 }
201}
202
203fn key_exists_in(key: &Key, keys: &[Key]) -> bool {
204 keys.iter().any(|k| keys_equal(k, key))
205}
206
207fn keys_equal(a: &Key, b: &Key) -> bool {
208 a.key_type() == b.key_type()
209 && a.is_unique() == b.is_unique()
210 && a.is_cluster() == b.is_cluster()
211 && a.include() == b.include()
212 && a.columns().len() == b.columns().len()
213 && a.columns().iter().zip(b.columns().iter()).all(|(ac, bc)| {
214 ac.name().eq_ignore_ascii_case(bc.name())
215 })
216}
217
218fn diff_drop_constraints(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
219 for old_table in old.tables() {
220 if let Some(new_table) = new.get_optional_table(old_table.name()) {
221 for old_con in old_table.constraints() {
222 if !constraint_exists_in(old_con, new_table.constraints()) {
223 cs.add_change(SchemaChange::DropConstraint {
224 table_name: old_table.name().to_string(),
225 constraint_name: old_con.name().to_string(),
226 });
227 }
228 }
229 }
230 }
231}
232
233fn diff_add_constraints(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
234 for new_table in new.tables() {
235 if let Some(old_table) = old.get_optional_table(new_table.name()) {
236 for new_con in new_table.constraints() {
237 if !constraint_exists_in(new_con, old_table.constraints()) {
238 cs.add_change(SchemaChange::AddConstraint {
239 table_name: new_table.name().to_string(),
240 constraint: new_con.clone(),
241 });
242 }
243 }
244 }
245 }
246}
247
248fn constraint_exists_in(con: &Constraint, constraints: &[Constraint]) -> bool {
249 constraints.iter().any(|c| constraints_equal(c, con))
250}
251
252fn constraints_equal(a: &Constraint, b: &Constraint) -> bool {
253 a.name().eq_ignore_ascii_case(b.name()) && a.sql().trim() == b.sql().trim()
254}
255
256fn diff_drop_relations(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
257 for old_table in old.tables() {
258 if let Some(new_table) = new.get_optional_table(old_table.name()) {
259 for (index, old_rel) in old_table.relations().iter().enumerate() {
263 if !relation_exists_in(old_rel, new_table.relations()) {
264 cs.add_change(SchemaChange::DropRelation {
265 relation: old_rel.clone(),
266 ordinal: index + 1,
267 });
268 }
269 }
270 }
271 }
272}
273
274fn diff_add_relations(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
275 for new_table in new.tables() {
276 if let Some(old_table) = old.get_optional_table(new_table.name()) {
277 for (index, new_rel) in new_table.relations().iter().enumerate() {
280 if !relation_exists_in(new_rel, old_table.relations()) {
281 cs.add_change(SchemaChange::AddRelation {
282 relation: new_rel.clone(),
283 ordinal: index + 1,
284 });
285 }
286 }
287 }
288 }
289}
290
291fn relation_exists_in(rel: &Relation, relations: &[Relation]) -> bool {
292 relations.iter().any(|r| relations_equal(r, rel))
293}
294
295fn relations_equal(a: &Relation, b: &Relation) -> bool {
296 a.from_table_name().eq_ignore_ascii_case(b.from_table_name())
297 && a.from_column_name().eq_ignore_ascii_case(b.from_column_name())
298 && a.to_table_name().eq_ignore_ascii_case(b.to_table_name())
299 && a.to_column_name().eq_ignore_ascii_case(b.to_column_name())
300 && a.relation_type() == b.relation_type()
301}
302
303fn diff_drop_views(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
304 for old_view in old.all_views() {
305 if !view_exists_in(old_view, new.all_views()) {
306 cs.add_change(SchemaChange::DropView {
307 view_name: old_view.name().to_string(),
308 });
309 }
310 }
311}
312
313fn diff_add_views(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
314 for new_view in new.all_views() {
315 if !view_exists_in(new_view, old.all_views()) {
316 cs.add_change(SchemaChange::AddView {
317 view: new_view.clone(),
318 });
319 }
320 }
321}
322
323fn view_exists_in(view: &View, views: &[View]) -> bool {
324 views.iter().any(|v| views_equal(v, view))
325}
326
327fn views_equal(a: &View, b: &View) -> bool {
328 a.name().eq_ignore_ascii_case(b.name()) && a.sql().trim() == b.sql().trim()
329}