|
| 1 | +use std::collections::BTreeSet; |
| 2 | + |
| 3 | +use crate::{model::Table, sql::same_option_value}; |
| 4 | + |
| 5 | +pub(super) fn diff_existing_table( |
| 6 | + current: &Table, |
| 7 | + target: &Table, |
| 8 | + allow_drop: bool, |
| 9 | + out: &mut Vec<String>, |
| 10 | +) { |
| 11 | + let mut emitted_for_table = false; |
| 12 | + |
| 13 | + if current.kind != target.kind { |
| 14 | + out.push(format!( |
| 15 | + "-- manual review required: table {} changed kind from {:?} to {:?}", |
| 16 | + target.name_sql, current.kind, target.kind |
| 17 | + )); |
| 18 | + emitted_for_table = true; |
| 19 | + } |
| 20 | + |
| 21 | + let set_options = target |
| 22 | + .options |
| 23 | + .iter() |
| 24 | + .filter_map(|(key, target_value)| match current.options.get(key) { |
| 25 | + Some(current_value) if same_option_value(current_value, target_value) => None, |
| 26 | + _ => Some(format!("{key} = {target_value}")), |
| 27 | + }) |
| 28 | + .collect::<Vec<_>>(); |
| 29 | + |
| 30 | + if !set_options.is_empty() { |
| 31 | + out.push(format!( |
| 32 | + "ALTER TABLE {} SET TBLPROPERTIES ({});", |
| 33 | + target.name_sql, |
| 34 | + set_options.join(", ") |
| 35 | + )); |
| 36 | + emitted_for_table = true; |
| 37 | + } |
| 38 | + |
| 39 | + for removed_option in current.options.keys() { |
| 40 | + if !target.options.contains_key(removed_option) { |
| 41 | + out.push(format!( |
| 42 | + "-- manual review required: option {} was removed from table {}", |
| 43 | + removed_option, target.name_sql |
| 44 | + )); |
| 45 | + out.push(format!( |
| 46 | + "-- recommended grammar to add: ALTER TABLE {} RESET TBLPROPERTIES ({});", |
| 47 | + target.name_sql, removed_option |
| 48 | + )); |
| 49 | + emitted_for_table = true; |
| 50 | + } |
| 51 | + } |
| 52 | + |
| 53 | + let current_constraints = current.constraints.iter().cloned().collect::<BTreeSet<_>>(); |
| 54 | + let target_constraints = target.constraints.iter().cloned().collect::<BTreeSet<_>>(); |
| 55 | + |
| 56 | + if current_constraints != target_constraints { |
| 57 | + out.push(format!( |
| 58 | + "-- manual review required: constraints changed on table {}", |
| 59 | + target.name_sql |
| 60 | + )); |
| 61 | + out.push(format!("-- current constraints: {current_constraints:?}")); |
| 62 | + out.push(format!("-- target constraints: {target_constraints:?}")); |
| 63 | + emitted_for_table = true; |
| 64 | + } |
| 65 | + |
| 66 | + for column_key in &target.column_order { |
| 67 | + let target_column = target.columns.get(column_key).expect("target column exists"); |
| 68 | + |
| 69 | + match current.columns.get(column_key) { |
| 70 | + Some(current_column) => { |
| 71 | + if current_column.semantic_signature() != target_column.semantic_signature() { |
| 72 | + if current_column.primary_key != target_column.primary_key { |
| 73 | + out.push(format!( |
| 74 | + "-- manual review required: primary key changed for {}.{}", |
| 75 | + target.name_sql, target_column.name_sql |
| 76 | + )); |
| 77 | + emitted_for_table = true; |
| 78 | + continue; |
| 79 | + } |
| 80 | + |
| 81 | + out.push(format!( |
| 82 | + "ALTER TABLE {} MODIFY COLUMN {};", |
| 83 | + target.name_sql, |
| 84 | + target_column.modify_fragment() |
| 85 | + )); |
| 86 | + emitted_for_table = true; |
| 87 | + } |
| 88 | + }, |
| 89 | + None => { |
| 90 | + out.push(format!( |
| 91 | + "ALTER TABLE {} ADD COLUMN {};", |
| 92 | + target.name_sql, target_column.create_sql |
| 93 | + )); |
| 94 | + emitted_for_table = true; |
| 95 | + }, |
| 96 | + } |
| 97 | + } |
| 98 | + |
| 99 | + for column_key in ¤t.column_order { |
| 100 | + if !target.columns.contains_key(column_key) { |
| 101 | + let current_column = current.columns.get(column_key).expect("current column exists"); |
| 102 | + |
| 103 | + if allow_drop { |
| 104 | + out.push(format!( |
| 105 | + "ALTER TABLE {} DROP COLUMN {};", |
| 106 | + target.name_sql, current_column.name_sql |
| 107 | + )); |
| 108 | + } else { |
| 109 | + out.push(format!( |
| 110 | + "-- destructive change skipped: column {}.{} exists in current schema but not in target schema", |
| 111 | + target.name_sql, current_column.name_sql |
| 112 | + )); |
| 113 | + out.push(format!( |
| 114 | + "-- rerun with destructive changes enabled to emit: ALTER TABLE {} DROP COLUMN {};", |
| 115 | + target.name_sql, current_column.name_sql |
| 116 | + )); |
| 117 | + } |
| 118 | + |
| 119 | + emitted_for_table = true; |
| 120 | + } |
| 121 | + } |
| 122 | + |
| 123 | + if emitted_for_table { |
| 124 | + out.push(String::new()); |
| 125 | + } |
| 126 | +} |
| 127 | + |
| 128 | +pub(super) fn emit_create_table(table: &Table) -> String { |
| 129 | + let mut parts = Vec::new(); |
| 130 | + |
| 131 | + for column_key in &table.column_order { |
| 132 | + let column = table.columns.get(column_key).expect("column exists"); |
| 133 | + parts.push(column.create_sql.clone()); |
| 134 | + } |
| 135 | + |
| 136 | + for constraint in &table.constraints { |
| 137 | + parts.push(constraint.clone()); |
| 138 | + } |
| 139 | + |
| 140 | + let kind_prefix = table.kind.map(|kind| kind.as_create_prefix()).unwrap_or(""); |
| 141 | + let mut sql = |
| 142 | + format!("CREATE {}TABLE {} (\n {}\n)", kind_prefix, table.name_sql, parts.join(",\n ")); |
| 143 | + |
| 144 | + if !table.options.is_empty() { |
| 145 | + let options = table |
| 146 | + .options |
| 147 | + .iter() |
| 148 | + .map(|(key, value)| format!("{key} = {value}")) |
| 149 | + .collect::<Vec<_>>() |
| 150 | + .join(",\n "); |
| 151 | + |
| 152 | + sql.push_str(&format!("\nWITH (\n {options}\n)")); |
| 153 | + } |
| 154 | + |
| 155 | + sql.push(';'); |
| 156 | + sql |
| 157 | +} |
0 commit comments