Class: Parse::Schema::SchemaDiff
- Inherits:
-
Object
- Object
- Parse::Schema::SchemaDiff
- Defined in:
- lib/parse/schema.rb
Overview
Represents the difference between local model and server schema.
Instance Attribute Summary collapse
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
-
#server_schema ⇒ Object
readonly
Returns the value of attribute server_schema.
Instance Method Summary collapse
-
#in_sync? ⇒ Boolean
Check if schemas are in sync.
-
#initialize(model_class, server_schema) ⇒ SchemaDiff
constructor
A new instance of SchemaDiff.
-
#missing_locally ⇒ Hash
Fields on server but not defined locally.
-
#missing_on_server ⇒ Hash
Fields defined locally but missing on server.
-
#server_exists? ⇒ Boolean
Check if server schema exists.
-
#summary ⇒ String
Generate a human-readable summary.
-
#type_mismatches ⇒ Hash
Fields with type mismatches.
Constructor Details
#initialize(model_class, server_schema) ⇒ SchemaDiff
Returns a new instance of SchemaDiff.
222 223 224 225 |
# File 'lib/parse/schema.rb', line 222 def initialize(model_class, server_schema) @model_class = model_class @server_schema = server_schema end |
Instance Attribute Details
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
220 221 222 |
# File 'lib/parse/schema.rb', line 220 def model_class @model_class end |
#server_schema ⇒ Object (readonly)
Returns the value of attribute server_schema.
220 221 222 |
# File 'lib/parse/schema.rb', line 220 def server_schema @server_schema end |
Instance Method Details
#in_sync? ⇒ Boolean
Check if schemas are in sync.
289 290 291 |
# File 'lib/parse/schema.rb', line 289 def in_sync? missing_on_server.empty? && missing_locally.empty? && type_mismatches.empty? end |
#missing_locally ⇒ Hash
Fields on server but not defined locally.
250 251 252 253 254 255 256 257 258 259 260 261 262 |
# File 'lib/parse/schema.rb', line 250 def missing_locally return {} unless server_exists? server = @server_schema.fields local = local_field_names missing = {} server.each do |name, info| # Skip core fields next if %w[objectId createdAt updatedAt ACL].include?(name) missing[name] = info[:type] unless local.include?(name) || local.include?(name.underscore.to_sym) end missing end |
#missing_on_server ⇒ Hash
Fields defined locally but missing on server.
235 236 237 238 239 240 241 242 243 244 245 246 |
# File 'lib/parse/schema.rb', line 235 def missing_on_server return local_fields unless server_exists? local = local_fields server = server_field_names missing = {} local.each do |name, type| name_str = name.to_s.camelize(:lower) missing[name] = type unless server.include?(name_str) || core_field?(name) end missing end |
#server_exists? ⇒ Boolean
Check if server schema exists.
229 230 231 |
# File 'lib/parse/schema.rb', line 229 def server_exists? !@server_schema.nil? end |
#summary ⇒ String
Generate a human-readable summary.
295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 |
# File 'lib/parse/schema.rb', line 295 def summary lines = ["Schema diff for #{@model_class.parse_class}:"] if !server_exists? lines << " - Class does not exist on server" elsif in_sync? lines << " - Schemas are in sync" else unless missing_on_server.empty? lines << " Missing on server:" missing_on_server.each { |n, t| lines << " + #{n}: #{t}" } end unless missing_locally.empty? lines << " Missing locally:" missing_locally.each { |n, t| lines << " - #{n}: #{t}" } end unless type_mismatches.empty? lines << " Type mismatches:" type_mismatches.each { |n, m| lines << " ~ #{n}: local=#{m[:local]}, server=#{m[:server]}" } end end lines.join("\n") end |
#type_mismatches ⇒ Hash
Fields with type mismatches.
266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 |
# File 'lib/parse/schema.rb', line 266 def type_mismatches return {} unless server_exists? mismatches = {} local_fields.each do |name, local_type| next if core_field?(name) name_str = name.to_s.camelize(:lower) server_type = @server_schema.field_type(name_str) next unless server_type # Normalize types for comparison normalized_local = normalize_type(local_type) normalized_server = normalize_type(server_type) if normalized_local != normalized_server mismatches[name] = { local: local_type, server: server_type } end end mismatches end |