Class: Parse::Schema::Migration
- Inherits:
-
Object
- Object
- Parse::Schema::Migration
- Defined in:
- lib/parse/schema.rb
Overview
Represents a schema migration to be applied.
Instance Attribute Summary collapse
-
#client ⇒ Object
readonly
Returns the value of attribute client.
-
#diff ⇒ Object
readonly
Returns the value of attribute diff.
-
#model_class ⇒ Object
readonly
Returns the value of attribute model_class.
Instance Method Summary collapse
-
#apply!(dry_run: false) ⇒ Hash
Apply the migration to the server.
-
#initialize(model_class, diff, client: nil) ⇒ Migration
constructor
A new instance of Migration.
-
#needed? ⇒ Boolean
Check if migration is needed.
-
#operations ⇒ Array<Hash>
Get the operations that would be performed.
-
#preview ⇒ String
Preview the migration without applying.
Constructor Details
Instance Attribute Details
#client ⇒ Object (readonly)
Returns the value of attribute client.
444 445 446 |
# File 'lib/parse/schema.rb', line 444 def client @client end |
#diff ⇒ Object (readonly)
Returns the value of attribute diff.
444 445 446 |
# File 'lib/parse/schema.rb', line 444 def diff @diff end |
#model_class ⇒ Object (readonly)
Returns the value of attribute model_class.
444 445 446 |
# File 'lib/parse/schema.rb', line 444 def model_class @model_class end |
Instance Method Details
#apply!(dry_run: false) ⇒ Hash
Apply the migration to the server.
507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 |
# File 'lib/parse/schema.rb', line 507 def apply!(dry_run: false) return { status: :skipped, message: "No migration needed" } unless needed? if dry_run return { status: :preview, operations: operations, preview: preview } end results = { status: :success, applied: [], errors: [] } # Create class if needed unless @diff.server_exists? schema = build_schema response = @client.create_schema(@model_class.parse_class, schema) if response.success? results[:applied] << { action: :create_class, class_name: @model_class.parse_class } else results[:errors] << { action: :create_class, error: response.error } results[:status] = :partial end return results end # Add missing fields @diff.missing_on_server.each do |name, type| field_name = @model_class.field_map[name].to_s definition = Parse::Schema.field_definition_for(@model_class, name, field_name) field_schema = { "fields" => { field_name => definition } } response = @client.update_schema(@model_class.parse_class, field_schema) if response.success? results[:applied] << { action: :add_field, field: field_name, type: type } else results[:errors] << { action: :add_field, field: field_name, error: response.error } results[:status] = :partial end end results[:status] = :failed if results[:applied].empty? && results[:errors].any? results end |
#needed? ⇒ Boolean
Check if migration is needed.
A migration is needed when the class does not yet exist on the server, or when the server does not already cover every locally-defined field. Defined in terms of the one-way SchemaDiff#server_covers_local? rather than the strict bidirectional SchemaDiff#in_sync? so that a server which is a strict superset of the local model (extra server-only columns the migrator would never add) does not report a "needed" migration with zero operations.
462 463 464 |
# File 'lib/parse/schema.rb', line 462 def needed? !@diff.server_exists? || !@diff.server_covers_local? end |
#operations ⇒ Array<Hash>
Get the operations that would be performed.
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 |
# File 'lib/parse/schema.rb', line 468 def operations ops = [] unless @diff.server_exists? ops << { action: :create_class, class_name: @model_class.parse_class } end @diff.missing_on_server.each do |name, type| wire = @model_class.field_map[name] definition = Parse::Schema.field_definition_for(@model_class, name, wire) op = { action: :add_field, field: wire.to_s, type: definition["type"] } op[:target_class] = definition["targetClass"] if definition["targetClass"] ops << op end ops end |
#preview ⇒ String
Preview the migration without applying.
488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 |
# File 'lib/parse/schema.rb', line 488 def preview return "No migration needed" unless needed? lines = ["Migration for #{@model_class.parse_class}:"] operations.each do |op| case op[:action] when :create_class lines << " CREATE CLASS #{op[:class_name]}" when :add_field target = op[:target_class] ? " -> #{op[:target_class]}" : "" lines << " ADD FIELD #{op[:field]} (#{op[:type]}#{target})" end end lines.join("\n") end |