Module: Parse::Schema

Defined in:
lib/parse/schema.rb,
lib/parse/schema/index_migrator.rb,
lib/parse/schema/search_index_migrator.rb

Overview

Schema introspection and migration tools for Parse Server. Provides utilities to compare local Ruby models with server schema, generate migration scripts, and manage schema changes.

Examples:

Inspecting server schema

schema = Parse::Schema.fetch("Song")
puts schema.fields  # => { "title" => "String", "duration" => "Number" }

Comparing local model to server

diff = Parse::Schema.diff(Song)
puts diff.missing_on_server  # Fields in model but not on server
puts diff.missing_locally    # Fields on server but not in model

Generating migration

migration = Parse::Schema.migration(Song)
migration.apply!  # Apply changes to server

Defined Under Namespace

Classes: IndexMigrator, Migration, SchemaDiff, SchemaInfo, SearchIndexMigrator

Constant Summary collapse

TYPE_MAP =

Parse field type mappings to Ruby types

{
  "String" => :string,
  "Number" => :integer,
  "Boolean" => :boolean,
  "Date" => :date,
  "File" => :file,
  "GeoPoint" => :geopoint,
  "Polygon" => :polygon,
  "Array" => :array,
  "Object" => :object,
  "Pointer" => :pointer,
  "Relation" => :relation,
  "Bytes" => :bytes,
}.freeze
REVERSE_TYPE_MAP =

Reverse mapping from Ruby types to Parse types

{
  string: "String",
  integer: "Number",
  float: "Number",
  boolean: "Boolean",
  date: "Date",
  file: "File",
  geopoint: "GeoPoint",
  geo_point: "GeoPoint",
  polygon: "Polygon",
  array: "Array",
  object: "Object",
  pointer: "Pointer",
  relation: "Relation",
  bytes: "Bytes",
  acl: "ACL",
}.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_class_level_permissionsObject

Returns the value of attribute default_class_level_permissions.



50
51
52
# File 'lib/parse/schema.rb', line 50

def default_class_level_permissions
  @default_class_level_permissions
end

Class Method Details

.all(client: nil) ⇒ Array<SchemaInfo>

Fetch all schemas from the Parse Server.

Parameters:

  • client (Parse::Client) (defaults to: nil)

    optional client to use

Returns:



92
93
94
95
96
97
98
99
# File 'lib/parse/schema.rb', line 92

def all(client: nil)
  client ||= Parse.client
  response = client.schemas
  return [] unless response.success?

  results = response.result.is_a?(Hash) ? response.result["results"] : response.result
  (results || []).map { |data| SchemaInfo.new(data) }
end

.class_names(client: nil) ⇒ Array<String>

Get all class names from the server.

Parameters:

  • client (Parse::Client) (defaults to: nil)

    optional client to use

Returns:



144
145
146
# File 'lib/parse/schema.rb', line 144

def class_names(client: nil)
  all(client: client).map(&:class_name)
end

.diff(model_class, client: nil) ⇒ SchemaDiff

Compare a local Parse::Object model with its server schema.

Parameters:

  • model_class (Class)

    a Parse::Object subclass

  • client (Parse::Client) (defaults to: nil)

    optional client to use

Returns:

  • (SchemaDiff)

    the differences between local and server schema

Raises:

  • (ArgumentError)


117
118
119
120
121
122
# File 'lib/parse/schema.rb', line 117

def diff(model_class, client: nil)
  raise ArgumentError, "Expected a Parse::Object subclass" unless model_class < Parse::Object

  server_schema = fetch(model_class.parse_class, client: client)
  SchemaDiff.new(model_class, server_schema)
end

.exists?(class_name, client: nil) ⇒ Boolean

Check if a class exists on the server.

Parameters:

  • class_name (String, Class)

    the Parse class name or model class

  • client (Parse::Client) (defaults to: nil)

    optional client to use

Returns:

  • (Boolean)

    true if the class exists



137
138
139
# File 'lib/parse/schema.rb', line 137

def exists?(class_name, client: nil)
  !fetch(class_name, client: client).nil?
end

.fetch(class_name, client: nil) ⇒ SchemaInfo?

Fetch schema for a specific class.

Parameters:

  • class_name (String, Class)

    the Parse class name or model class

  • client (Parse::Client) (defaults to: nil)

    optional client to use

Returns:

  • (SchemaInfo, nil)

    the schema info or nil if not found



105
106
107
108
109
110
111
# File 'lib/parse/schema.rb', line 105

def fetch(class_name, client: nil)
  class_name = class_name.parse_class if class_name.respond_to?(:parse_class)
  client ||= Parse.client
  response = client.schema(class_name)
  return nil unless response.success?
  SchemaInfo.new(response.result)
end

.migration(model_class, client: nil) ⇒ Migration

Generate a migration for a model class.

Parameters:

  • model_class (Class)

    a Parse::Object subclass

  • client (Parse::Client) (defaults to: nil)

    optional client to use

Returns:



128
129
130
131
# File 'lib/parse/schema.rb', line 128

def migration(model_class, client: nil)
  diff_result = diff(model_class, client: client)
  Migration.new(model_class, diff_result, client: client)
end