1use schema_model::model::column::Column;
2use schema_model::model::constraint::Constraint;
3use schema_model::model::enum_type::EnumType;
4use schema_model::model::function::Function;
5use schema_model::model::initial_data::InitialData;
6use schema_model::model::key::Key;
7use schema_model::model::other_sql::OtherSql;
8use schema_model::model::procedure::Procedure;
9use schema_model::model::relation::Relation;
10use schema_model::model::schema::Schema;
11use schema_model::model::trigger::Trigger;
12use schema_model::model::types::KeyType;
13use schema_model::model::view::View;
14
15use crate::change::SchemaChange;
16use crate::change_set::ChangeSet;
17
18pub struct SchemaDiffEngine;
19
20impl SchemaDiffEngine {
21 pub fn diff(old: &Schema, new: &Schema) -> ChangeSet {
22 let mut change_set = ChangeSet::new();
23
24 diff_drop_views(old, new, &mut change_set);
26 diff_drop_relations(old, new, &mut change_set);
27 diff_drop_keys(old, new, &mut change_set);
28 diff_drop_constraints(old, new, &mut change_set);
29 diff_drop_columns(old, new, &mut change_set);
30 diff_drop_tables(old, new, &mut change_set);
31 diff_drop_triggers(old, new, &mut change_set);
32 diff_drop_functions(old, new, &mut change_set);
33 diff_drop_procedures(old, new, &mut change_set);
34 diff_drop_other_sql(old, new, &mut change_set);
35 diff_drop_initial_data(old, new, &mut change_set);
36 diff_drop_enum_types(old, new, &mut change_set);
38
39 diff_add_enum_types(old, new, &mut change_set);
43 diff_add_functions(old, new, &mut change_set);
44 diff_add_procedures(old, new, &mut change_set);
45 diff_add_other_sql(old, new, &mut change_set);
46 diff_add_tables(old, new, &mut change_set);
47 diff_add_columns(old, new, &mut change_set);
48 diff_modify_columns(old, new, &mut change_set);
49 diff_add_keys(old, new, &mut change_set);
50 diff_add_constraints(old, new, &mut change_set);
51 diff_add_relations(old, new, &mut change_set);
52 diff_add_views(old, new, &mut change_set);
53 diff_add_triggers(old, new, &mut change_set);
54 diff_add_initial_data(old, new, &mut change_set);
56
57 change_set
58 }
59}
60
61fn diff_drop_tables(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
62 for old_table in old.tables() {
63 if new.get_optional_table(old_table.name()).is_none() {
64 cs.add_change(SchemaChange::DropTable {
65 table_name: old_table.name().to_string(),
66 });
67 }
68 }
69}
70
71fn diff_add_tables(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
72 for new_table in new.tables() {
73 if old.get_optional_table(new_table.name()).is_none() {
74 cs.add_change(SchemaChange::AddTable {
75 table_name: new_table.name().to_string(),
76 });
77 }
78 }
79}
80
81fn diff_drop_columns(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
82 for old_table in old.tables() {
83 if let Some(new_table) = new.get_optional_table(old_table.name()) {
84 for old_col in old_table.columns() {
85 if !new_table.has_column(old_col.name()) {
86 let rename_candidates: Vec<String> = new_table
87 .columns()
88 .iter()
89 .filter(|nc| !old_table.has_column(nc.name()) && nc.column_type() == old_col.column_type())
90 .map(|nc| nc.name().to_string())
91 .collect();
92 cs.add_change(SchemaChange::DropColumn {
93 table_name: old_table.name().to_string(),
94 column_name: old_col.name().to_string(),
95 rename_candidates,
96 });
97 }
98 }
99 }
100 }
101}
102
103fn diff_add_columns(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
104 for new_table in new.tables() {
105 if let Some(old_table) = old.get_optional_table(new_table.name()) {
106 for new_col in new_table.columns() {
107 if !old_table.has_column(new_col.name()) {
108 cs.add_change(SchemaChange::AddColumn {
109 table_name: new_table.name().to_string(),
110 column: new_col.clone(),
111 });
112 }
113 }
114 }
115 }
116}
117
118fn diff_modify_columns(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
119 for new_table in new.tables() {
120 if let Some(old_table) = old.get_optional_table(new_table.name()) {
121 for new_col in new_table.columns() {
122 if old_table.has_column(new_col.name()) {
123 let old_col = old_table.column(new_col.name());
124 if columns_differ(old_col, new_col) {
125 cs.add_change(SchemaChange::ModifyColumn {
126 table_name: new_table.name().to_string(),
127 old_column: old_col.clone(),
128 new_column: new_col.clone(),
129 });
130 }
131 }
132 }
133 }
134 }
135}
136
137fn columns_differ(a: &Column, b: &Column) -> bool {
138 a.column_type() != b.column_type()
139 || a.length() != b.length()
140 || a.scale() != b.scale()
141 || a.required() != b.required()
142 || a.default_constraint() != b.default_constraint()
143 || a.check_constraint() != b.check_constraint()
144 || a.enum_type() != b.enum_type()
145 || a.element_type() != b.element_type()
146 || a.generated() != b.generated()
147 || a.min_value() != b.min_value()
148 || a.max_value() != b.max_value()
149}
150
151fn diff_drop_keys(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
152 for old_table in old.tables() {
153 if let Some(new_table) = new.get_optional_table(old_table.name()) {
154 let mut unique_ordinal = 0;
159 for old_key in old_table.keys() {
160 if old_key.key_type() == KeyType::Unique {
161 unique_ordinal += 1;
162 }
163 if !key_exists_in(old_key, new_table.keys()) && !key_exists_in(old_key, new_table.indexes()) {
164 cs.add_change(SchemaChange::DropKey {
165 table_name: old_table.name().to_string(),
166 key: old_key.clone(),
167 ordinal: unique_ordinal,
168 });
169 }
170 }
171
172 let mut index_ordinal = 0;
173 for old_key in old_table.indexes() {
174 if old_key.is_index() {
175 index_ordinal += 1;
176 }
177 if !key_exists_in(old_key, new_table.keys()) && !key_exists_in(old_key, new_table.indexes()) {
178 cs.add_change(SchemaChange::DropKey {
179 table_name: old_table.name().to_string(),
180 key: old_key.clone(),
181 ordinal: index_ordinal,
182 });
183 }
184 }
185 }
186 }
187}
188
189fn diff_add_keys(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
190 for new_table in new.tables() {
191 if let Some(old_table) = old.get_optional_table(new_table.name()) {
192 let mut unique_ordinal = 0;
195 for new_key in new_table.keys() {
196 if new_key.key_type() == KeyType::Unique {
197 unique_ordinal += 1;
198 }
199 if !key_exists_in(new_key, old_table.keys()) && !key_exists_in(new_key, old_table.indexes()) {
200 cs.add_change(SchemaChange::AddKey {
201 table_name: new_table.name().to_string(),
202 key: new_key.clone(),
203 ordinal: unique_ordinal,
204 });
205 }
206 }
207
208 let mut index_ordinal = 0;
209 for new_key in new_table.indexes() {
210 if new_key.is_index() {
211 index_ordinal += 1;
212 }
213 if !key_exists_in(new_key, old_table.keys()) && !key_exists_in(new_key, old_table.indexes()) {
214 cs.add_change(SchemaChange::AddKey {
215 table_name: new_table.name().to_string(),
216 key: new_key.clone(),
217 ordinal: index_ordinal,
218 });
219 }
220 }
221 }
222 }
223}
224
225fn key_exists_in(key: &Key, keys: &[Key]) -> bool {
226 keys.iter().any(|k| keys_equal(k, key))
227}
228
229fn keys_equal(a: &Key, b: &Key) -> bool {
230 a.key_type() == b.key_type()
231 && a.is_unique() == b.is_unique()
232 && a.is_cluster() == b.is_cluster()
233 && a.include() == b.include()
234 && a.columns().len() == b.columns().len()
235 && a.columns().iter().zip(b.columns().iter()).all(|(ac, bc)| {
236 ac.name().eq_ignore_ascii_case(bc.name())
237 })
238}
239
240fn diff_drop_constraints(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
241 for old_table in old.tables() {
242 if let Some(new_table) = new.get_optional_table(old_table.name()) {
243 for old_con in old_table.constraints() {
244 if !constraint_exists_in(old_con, new_table.constraints()) {
245 cs.add_change(SchemaChange::DropConstraint {
246 table_name: old_table.name().to_string(),
247 constraint_name: old_con.name().to_string(),
248 });
249 }
250 }
251 }
252 }
253}
254
255fn diff_add_constraints(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
256 for new_table in new.tables() {
257 if let Some(old_table) = old.get_optional_table(new_table.name()) {
258 for new_con in new_table.constraints() {
259 if !constraint_exists_in(new_con, old_table.constraints()) {
260 cs.add_change(SchemaChange::AddConstraint {
261 table_name: new_table.name().to_string(),
262 constraint: new_con.clone(),
263 });
264 }
265 }
266 }
267 }
268}
269
270fn constraint_exists_in(con: &Constraint, constraints: &[Constraint]) -> bool {
271 constraints.iter().any(|c| constraints_equal(c, con))
272}
273
274fn constraints_equal(a: &Constraint, b: &Constraint) -> bool {
275 a.name().eq_ignore_ascii_case(b.name()) && a.sql().trim() == b.sql().trim()
276}
277
278fn diff_drop_relations(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
279 for old_table in old.tables() {
280 if let Some(new_table) = new.get_optional_table(old_table.name()) {
281 for (index, old_rel) in old_table.relations().iter().enumerate() {
285 if !relation_exists_in(old_rel, new_table.relations()) {
286 cs.add_change(SchemaChange::DropRelation {
287 relation: old_rel.clone(),
288 ordinal: index + 1,
289 });
290 }
291 }
292 }
293 }
294}
295
296fn diff_add_relations(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
297 for new_table in new.tables() {
298 if let Some(old_table) = old.get_optional_table(new_table.name()) {
299 for (index, new_rel) in new_table.relations().iter().enumerate() {
302 if !relation_exists_in(new_rel, old_table.relations()) {
303 cs.add_change(SchemaChange::AddRelation {
304 relation: new_rel.clone(),
305 ordinal: index + 1,
306 });
307 }
308 }
309 }
310 }
311}
312
313fn relation_exists_in(rel: &Relation, relations: &[Relation]) -> bool {
314 relations.iter().any(|r| relations_equal(r, rel))
315}
316
317fn relations_equal(a: &Relation, b: &Relation) -> bool {
318 a.from_table_name().eq_ignore_ascii_case(b.from_table_name())
319 && a.from_column_name().eq_ignore_ascii_case(b.from_column_name())
320 && a.to_table_name().eq_ignore_ascii_case(b.to_table_name())
321 && a.to_column_name().eq_ignore_ascii_case(b.to_column_name())
322 && a.relation_type() == b.relation_type()
323}
324
325fn diff_drop_views(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
326 for old_view in old.all_views() {
327 if !view_exists_in(old_view, new.all_views()) {
328 cs.add_change(SchemaChange::DropView {
329 view_name: old_view.name().to_string(),
330 });
331 }
332 }
333}
334
335fn diff_add_views(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
336 for new_view in new.all_views() {
337 if !view_exists_in(new_view, old.all_views()) {
338 cs.add_change(SchemaChange::AddView {
339 view: new_view.clone(),
340 });
341 }
342 }
343}
344
345fn view_exists_in(view: &View, views: &[View]) -> bool {
346 views.iter().any(|v| views_equal(v, view))
347}
348
349fn views_equal(a: &View, b: &View) -> bool {
350 a.name().eq_ignore_ascii_case(b.name()) && a.sql().trim() == b.sql().trim()
351}
352
353fn diff_add_enum_types(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
354 for new_enum in new.enum_types() {
355 match find_enum_type(old, new_enum.name()) {
356 None => cs.add_change(SchemaChange::AddEnumType { enum_type: new_enum.clone() }),
357 Some(old_enum) if !enum_types_equal(old_enum, new_enum) => {
358 cs.add_change(SchemaChange::ModifyEnumType {
359 old_enum_type: old_enum.clone(),
360 new_enum_type: new_enum.clone(),
361 });
362 }
363 Some(_) => {}
364 }
365 }
366}
367
368fn diff_drop_enum_types(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
369 for old_enum in old.enum_types() {
370 if find_enum_type(new, old_enum.name()).is_none() {
371 cs.add_change(SchemaChange::DropEnumType {
372 enum_type_name: old_enum.name().to_string(),
373 });
374 }
375 }
376}
377
378fn find_enum_type<'a>(schema: &'a Schema, name: &str) -> Option<&'a EnumType> {
379 schema.enum_types().find(|e| e.name().eq_ignore_ascii_case(name))
380}
381
382fn enum_types_equal(a: &EnumType, b: &EnumType) -> bool {
383 a.name().eq_ignore_ascii_case(b.name())
384 && a.values().len() == b.values().len()
385 && a.values()
386 .iter()
387 .zip(b.values().iter())
388 .all(|(av, bv)| av.name() == bv.name() && av.code() == bv.code())
389}
390
391fn diff_add_functions(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
392 for new_fn in new.functions() {
393 if !functions_contains(old.functions(), new_fn) {
394 cs.add_change(SchemaChange::AddFunction { function: new_fn.clone() });
395 }
396 }
397}
398
399fn diff_drop_functions(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
400 for old_fn in old.functions() {
401 if !functions_contains(new.functions(), old_fn) {
402 cs.add_change(SchemaChange::DropFunction {
403 function_name: old_fn.name().to_string(),
404 database_type: old_fn.database_type(),
405 });
406 }
407 }
408}
409
410fn functions_contains(functions: &[Function], target: &Function) -> bool {
411 functions.iter().any(|f| functions_equal(f, target))
412}
413
414fn functions_equal(a: &Function, b: &Function) -> bool {
415 a.name().eq_ignore_ascii_case(b.name())
416 && a.database_type() == b.database_type()
417 && a.sql().trim() == b.sql().trim()
418}
419
420fn diff_add_procedures(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
421 for new_proc in new.procedures() {
422 if !procedures_contains(old.procedures(), new_proc) {
423 cs.add_change(SchemaChange::AddProcedure { procedure: new_proc.clone() });
424 }
425 }
426}
427
428fn diff_drop_procedures(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
429 for old_proc in old.procedures() {
430 if !procedures_contains(new.procedures(), old_proc) {
431 cs.add_change(SchemaChange::DropProcedure {
432 procedure_name: old_proc.name().to_string(),
433 database_type: old_proc.database_type(),
434 });
435 }
436 }
437}
438
439fn procedures_contains(procedures: &[Procedure], target: &Procedure) -> bool {
440 procedures.iter().any(|p| procedures_equal(p, target))
441}
442
443fn procedures_equal(a: &Procedure, b: &Procedure) -> bool {
444 a.name().eq_ignore_ascii_case(b.name())
445 && a.database_type() == b.database_type()
446 && a.sql().trim() == b.sql().trim()
447}
448
449fn diff_add_other_sql(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
450 for new_sql in new.other_sql() {
451 if !other_sql_contains(old.other_sql(), new_sql) {
452 cs.add_change(SchemaChange::AddOtherSql { other_sql: new_sql.clone() });
453 }
454 }
455}
456
457fn diff_drop_other_sql(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
458 for old_sql in old.other_sql() {
459 if !other_sql_contains(new.other_sql(), old_sql) {
460 cs.add_change(SchemaChange::DropOtherSql { other_sql: old_sql.clone() });
461 }
462 }
463}
464
465fn other_sql_contains(entries: &[OtherSql], target: &OtherSql) -> bool {
466 entries.iter().any(|o| other_sql_equal(o, target))
467}
468
469fn other_sql_equal(a: &OtherSql, b: &OtherSql) -> bool {
470 a.database_type() == b.database_type() && a.order() == b.order() && a.sql().trim() == b.sql().trim()
471}
472
473fn diff_add_triggers(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
478 for new_table in new.tables() {
479 if let Some(old_table) = old.get_optional_table(new_table.name()) {
480 for new_trigger in new_table.triggers() {
481 if !triggers_contains(old_table.triggers(), new_trigger) {
482 cs.add_change(SchemaChange::AddTrigger {
483 table_name: new_table.name().to_string(),
484 trigger: new_trigger.clone(),
485 });
486 }
487 }
488 }
489 }
490}
491
492fn diff_drop_triggers(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
493 for old_table in old.tables() {
494 if let Some(new_table) = new.get_optional_table(old_table.name()) {
495 for old_trigger in old_table.triggers() {
496 if !triggers_contains(new_table.triggers(), old_trigger) {
497 cs.add_change(SchemaChange::DropTrigger {
498 table_name: old_table.name().to_string(),
499 trigger: old_trigger.clone(),
500 });
501 }
502 }
503 }
504 }
505}
506
507fn triggers_contains(triggers: &[Trigger], target: &Trigger) -> bool {
508 triggers.iter().any(|t| triggers_equal(t, target))
509}
510
511fn triggers_equal(a: &Trigger, b: &Trigger) -> bool {
512 a.trigger_text().trim() == b.trigger_text().trim()
513 && a.trigger_type() == b.trigger_type()
514 && a.database_type() == b.database_type()
515}
516
517fn diff_add_initial_data(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
518 for new_table in new.tables() {
519 if let Some(old_table) = old.get_optional_table(new_table.name()) {
520 for new_data in new_table.initial_data() {
521 if !initial_data_contains(old_table.initial_data(), new_data) {
522 cs.add_change(SchemaChange::AddInitialData {
523 table_name: new_table.name().to_string(),
524 initial_data: new_data.clone(),
525 });
526 }
527 }
528 }
529 }
530}
531
532fn diff_drop_initial_data(old: &Schema, new: &Schema, cs: &mut ChangeSet) {
533 for old_table in old.tables() {
534 if let Some(new_table) = new.get_optional_table(old_table.name()) {
535 for old_data in old_table.initial_data() {
536 if !initial_data_contains(new_table.initial_data(), old_data) {
537 cs.add_change(SchemaChange::DropInitialData {
538 table_name: old_table.name().to_string(),
539 initial_data: old_data.clone(),
540 });
541 }
542 }
543 }
544 }
545}
546
547fn initial_data_contains(entries: &[InitialData], target: &InitialData) -> bool {
548 entries.iter().any(|d| initial_data_equal(d, target))
549}
550
551fn initial_data_equal(a: &InitialData, b: &InitialData) -> bool {
552 a.sql().trim() == b.sql().trim() && a.database_type() == b.database_type()
553}