Module: Parse::Schema

Defined in:
lib/parse/schema.rb,
lib/parse/schema/index_migrator.rb,
lib/parse/schema/search_index_migrator.rb,
lib/parse/vector_search/index_definition.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,
  # A Number column holds integral and fractional values; :number keeps
  # integers as Integer and fractional values as Float.
  "Number" => :number,
  "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",
  number: "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",
  # SDK-only property types with no native Parse column type. They are
  # stored in the column type Parse Server actually holds them as.
  vector: "Array",
  timezone: "String",
  time_zone: "String",
  phone: "String",
  email: "String",
}.freeze

Class Attribute Summary collapse

Class Method Summary collapse

Class Attribute Details

.default_class_level_permissions ⇒ Object

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:



138
139
140
141
142
143
144
145
# File 'lib/parse/schema.rb', line 138

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:



190
191
192
# File 'lib/parse/schema.rb', line 190

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)


163
164
165
166
167
168
# File 'lib/parse/schema.rb', line 163

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



183
184
185
# File 'lib/parse/schema.rb', line 183

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



151
152
153
154
155
156
157
# File 'lib/parse/schema.rb', line 151

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

.field_definition_for(model_class, name, wire) ⇒ Hash

Build the server column definition for a model field, including the targetClass a Pointer or Relation column requires.

Parameters:

  • model_class (Class) —

    a Parse::Object subclass

  • name (Symbol) —

    the canonical field name (a field_map key)

  • wire (String, Symbol) —

    the server column name

Returns:

  • (Hash) —

    the column definition, e.g. { "type" => "Pointer", "targetClass" => "Author" }



118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
# File 'lib/parse/schema.rb', line 118

def self.field_definition_for(model_class, name, wire)
  type = local_field_type(model_class, name) || :string
  parse_type = REVERSE_TYPE_MAP[type] || "String"
  definition = { "type" => parse_type }
  case parse_type
  when "Pointer"
    target = model_class.references[wire.to_s] || model_class.references[wire.to_sym] ||
             model_class.references[name]
    definition["targetClass"] = target.to_s if target
  when "Relation"
    target = model_class.relations[name.to_sym] || model_class.relations[wire.to_sym]
    definition["targetClass"] = target.to_s if target
  end
  definition
end

.local_field_type(model_class, name) ⇒ Symbol?

Resolve the local type of a model field for schema comparison and migration. fields has no entry for a has_many through: :relation association, so a field that only appears in relations is reported as :relation.

Parameters:

  • model_class (Class) —

    a Parse::Object subclass

  • name (Symbol) —

    the canonical field name (a field_map key)

Returns:



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

def self.local_field_type(model_class, name)
  type = model_class.fields[name]
  return type.to_sym unless type.nil?
  return :relation if model_class.respond_to?(:relations) && model_class.relations.key?(name.to_sym)
  nil
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:



174
175
176
177
# File 'lib/parse/schema.rb', line 174

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

.vector_index_definition(model_class, field: nil) ⇒ Hash

Generate the Atlas vectorSearch index definition for a model's :vector field. See VectorSearch::IndexDefinition.build.

Returns:



232
233
234
# File 'lib/parse/vector_search/index_definition.rb', line 232

def vector_index_definition(model_class, field: nil)
  Parse::VectorSearch::IndexDefinition.build(model_class, field: field)
end