Class: Lyra::Schema::Diff
- Inherits:
-
Object
- Object
- Lyra::Schema::Diff
- Defined in:
- lib/lyra/schema/diff.rb
Overview
Compares schemas and produces human-readable difference reports
Constant Summary collapse
- SEVERITIES =
Severity levels for different types of changes
{ model_added: :info, model_removed: :breaking, column_added: :info, column_removed: :breaking, column_type_changed: :breaking, column_nullable_changed: :warning, column_limit_changed: :warning, pii_field_added: :warning, pii_field_removed: :info, event_name_changed: :breaking, event_prefix_changed: :warning, config_changed: :info }.freeze
Class Method Summary collapse
Instance Method Summary collapse
- #compare(old_schema, new_schema) ⇒ Object
- #compare_column(model_name, col_name, old_col, new_col) ⇒ Object
- #compare_columns(model_name, old_columns, new_columns) ⇒ Object
- #compare_configuration(old_config, new_config) ⇒ Object
- #compare_events(model_name, old_events, new_events) ⇒ Object
- #compare_model(model_name, old_model, new_model) ⇒ Object
- #format_report(differences) ⇒ Object
Class Method Details
.compare(old_schema, new_schema) ⇒ Object
24 25 26 |
# File 'lib/lyra/schema/diff.rb', line 24 def compare(old_schema, new_schema) new.compare(old_schema, new_schema) end |
.format_report(differences) ⇒ Object
28 29 30 |
# File 'lib/lyra/schema/diff.rb', line 28 def format_report(differences) new.format_report(differences) end |
Instance Method Details
#compare(old_schema, new_schema) ⇒ Object
33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 |
# File 'lib/lyra/schema/diff.rb', line 33 def compare(old_schema, new_schema) differences = [] # Compare models (normalize keys to strings for comparison) old_models = normalize_keys(old_schema[:models] || {}) new_models = normalize_keys(new_schema[:models] || {}) # Check for removed models (old_models.keys - new_models.keys).each do |model_name| differences << { type: :model_removed, severity: SEVERITIES[:model_removed], model: model_name, message: "Model '#{model_name}' was removed from monitoring" } end # Check for added models (new_models.keys - old_models.keys).each do |model_name| differences << { type: :model_added, severity: SEVERITIES[:model_added], model: model_name, message: "Model '#{model_name}' was added to monitoring" } end # Compare existing models (old_models.keys & new_models.keys).each do |model_name| differences += compare_model( model_name, old_models[model_name], new_models[model_name] ) end # Compare configuration changes differences += compare_configuration( old_schema[:configuration] || {}, new_schema[:configuration] || {} ) differences end |
#compare_column(model_name, col_name, old_col, new_col) ⇒ Object
144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 |
# File 'lib/lyra/schema/diff.rb', line 144 def compare_column(model_name, col_name, old_col, new_col) differences = [] # Use string keys (columns are already normalized) old_type = old_col["type"] new_type = new_col["type"] old_nullable = old_col["nullable"] new_nullable = new_col["nullable"] old_limit = old_col["limit"] new_limit = new_col["limit"] old_pii = old_col["pii"] new_pii = new_col["pii"] # Type change if old_type != new_type differences << { type: :column_type_changed, severity: SEVERITIES[:column_type_changed], model: model_name, column: col_name, old_value: old_type, new_value: new_type, message: "#{model_name}.#{col_name} type changed: #{old_type} -> #{new_type}" } end # Nullable change if old_nullable != new_nullable differences << { type: :column_nullable_changed, severity: SEVERITIES[:column_nullable_changed], model: model_name, column: col_name, old_value: old_nullable, new_value: new_nullable, message: "#{model_name}.#{col_name} nullable changed: #{old_nullable} -> #{new_nullable}" } end # Limit change if old_limit != new_limit differences << { type: :column_limit_changed, severity: SEVERITIES[:column_limit_changed], model: model_name, column: col_name, old_value: old_limit, new_value: new_limit, message: "#{model_name}.#{col_name} limit changed: #{old_limit} -> #{new_limit}" } end # PII detection change if old_pii != new_pii type = new_pii ? :pii_field_added : :pii_field_removed differences << { type: type, severity: SEVERITIES[type], model: model_name, column: col_name, message: "#{model_name}.#{col_name} PII status changed: #{old_pii} -> #{new_pii}" } end differences end |
#compare_columns(model_name, old_columns, new_columns) ⇒ Object
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 |
# File 'lib/lyra/schema/diff.rb', line 106 def compare_columns(model_name, old_columns, new_columns) differences = [] # Removed columns (old_columns.keys - new_columns.keys).each do |col_name| differences << { type: :column_removed, severity: SEVERITIES[:column_removed], model: model_name, column: col_name, message: "Column '#{col_name}' removed from #{model_name}" } end # Added columns (new_columns.keys - old_columns.keys).each do |col_name| col_info = new_columns[col_name] msg = "Column '#{col_name}' added to #{model_name}" msg += " (PII: #{col_info["pii_type"]})" if col_info["pii"] differences << { type: :column_added, severity: SEVERITIES[:column_added], model: model_name, column: col_name, pii: col_info["pii"], message: msg } end # Changed columns (old_columns.keys & new_columns.keys).each do |col_name| differences += compare_column(model_name, col_name, old_columns[col_name], new_columns[col_name]) end differences end |
#compare_configuration(old_config, new_config) ⇒ Object
243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 |
# File 'lib/lyra/schema/diff.rb', line 243 def compare_configuration(old_config, new_config) differences = [] # Normalize to string keys for comparison old_cfg = normalize_keys(old_config) new_cfg = normalize_keys(new_config) %w[mode strict_schema projection_mode].each do |key| old_val = old_cfg[key] new_val = new_cfg[key] # Normalize values to strings for comparison old_val_str = old_val.to_s if old_val new_val_str = new_val.to_s if new_val if old_val_str != new_val_str differences << { type: :config_changed, severity: SEVERITIES[:config_changed], key: key, old_value: old_val, new_value: new_val, message: "Configuration '#{key}' changed: #{old_val} -> #{new_val}" } end end differences end |
#compare_events(model_name, old_events, new_events) ⇒ Object
211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 |
# File 'lib/lyra/schema/diff.rb', line 211 def compare_events(model_name, old_events, new_events) differences = [] # Check for event name changes (removed + added = renamed) removed_events = old_events.keys - new_events.keys added_events = new_events.keys - old_events.keys # Detect renames by matching operations removed_events.each do |old_name| old_op = old_events[old_name]["operation"] matching_new = added_events.find do |new_name| new_events[new_name]["operation"] == old_op end if matching_new differences << { type: :event_name_changed, severity: SEVERITIES[:event_name_changed], model: model_name, old_value: old_name, new_value: matching_new, operation: old_op, message: "#{model_name} event renamed: #{old_name} -> #{matching_new}" } added_events.delete(matching_new) end end differences end |
#compare_model(model_name, old_model, new_model) ⇒ Object
78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 |
# File 'lib/lyra/schema/diff.rb', line 78 def compare_model(model_name, old_model, new_model) differences = [] # Access with string keys (models are already normalized) old_prefix = old_model["event_prefix"] new_prefix = new_model["event_prefix"] # Compare event prefix if old_prefix != new_prefix differences << { type: :event_prefix_changed, severity: SEVERITIES[:event_prefix_changed], model: model_name, old_value: old_prefix, new_value: new_prefix, message: "#{model_name} event_prefix changed: '#{old_prefix}' -> '#{new_prefix}'" } end # Compare columns differences += compare_columns(model_name, old_model["columns"] || {}, new_model["columns"] || {}) # Compare events differences += compare_events(model_name, old_model["events"] || {}, new_model["events"] || {}) differences end |
#format_report(differences) ⇒ Object
273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 |
# File 'lib/lyra/schema/diff.rb', line 273 def format_report(differences) return "No schema changes detected." if differences.empty? lines = ["Schema Changes Detected:"] lines << "=" * 60 grouped = differences.group_by { |d| d[:severity] } [:breaking, :warning, :info].each do |severity| next unless grouped[severity]&.any? lines << "" icon = severity_icon(severity) lines << "#{icon} #{severity.to_s.upcase} (#{grouped[severity].size}):" lines << "-" * 40 grouped[severity].each do |diff| lines << " - #{diff[:message]}" end end lines << "" lines << "=" * 60 # Add action suggestions if grouped[:breaking]&.any? lines << "" lines << "ACTION REQUIRED: Breaking changes detected." lines << "Run 'rake lyra:schema:update' to create a new schema version." end lines.join("\n") end |