Class: Parse::Schema::IndexMigrator

Inherits:
Object
  • Object
show all
Defined in:
lib/parse/schema/index_migrator.rb

Overview

Reconciliation engine for ‘Parse::Core::Indexing` declarations vs. the actual MongoDB index state. Reads existing indexes via the reader connection (so `plan` works in dry-run mode without writer config); applies via the writer connection through MongoDB.create_index / MongoDB.drop_index (which re-check the triple gate and run their own per-call idempotency check on the writer-side index list).

Multi-collection. A single model can declare indexes against both its own collection (via ‘mongo_index`) and one or more `_Join:<field>:<ParentClass>` collections (via `mongo_relation_index`). `plan` returns a Hash keyed by collection name with one entry per unique target collection across the declaration list. `apply!` returns a similarly-keyed result Hash.

Parse-managed indexes (the ones Parse Server auto-creates on collections like ‘_User`, `_Session`, `_Role`) are never proposed for drop, regardless of whether they appear in the declaration list. The list is conservative — any name matching PARSE_MANAGED_INDEX_PATTERNS is treated as off-limits to the migrator, full stop.

Constant Summary collapse

PARSE_MANAGED_INDEX_PATTERNS =

Names / patterns of indexes Parse Server creates and owns. The migrator excludes these from both ‘to_drop` (so a missing declaration never proposes their removal) and from `in_sync` (so they don’t visually clutter operator review). They appear in ‘parse_managed:` for transparency.

**Coverage is not forward-compatible.** This list reflects the indexes Parse Server auto-creates as of Parse Server 7.x. Any future Parse Server release that adds a new managed index will cause that index to be classified as an orphan and be eligible for drop under ‘DROP=true`. Operators upgrading Parse Server should re-review this list before re-running `parse:mongo:indexes:apply` with the drop flag.

Any non-declared, non-managed index — including DBA-created diagnostic indexes, indexes created by other Parse SDKs, and MongoDB Atlas index recommendations — is also classified as an orphan. If you need to preserve such an index, declare it via Core::Indexing#mongo_index on the model.

[
  /\A_id_\z/,
  /\A_username_unique\z/,
  /\A_email_unique\z/,
  /\Aemail_1\z/,
  /\Ausername_1\z/,
  /\A_session_token_/,
  /\A_email_verify_token_/,
  /\A_perishable_token_/,
  /\A_account_lockout_/,
  /\Acase_insensitive_/,
].freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(model_class) ⇒ IndexMigrator

Returns a new instance of IndexMigrator.



62
63
64
65
66
67
# File 'lib/parse/schema/index_migrator.rb', line 62

def initialize(model_class)
  unless model_class.is_a?(Class) && model_class < Parse::Object
    raise ArgumentError, "IndexMigrator expects a Parse::Object subclass; got #{model_class.inspect}"
  end
  @model_class = model_class
end

Instance Attribute Details

#model_classObject (readonly)

Returns the value of attribute model_class.



60
61
62
# File 'lib/parse/schema/index_migrator.rb', line 60

def model_class
  @model_class
end

Instance Method Details

#apply!(drop: false) ⇒ Hash{String => Hash}

Apply the plan across all target collections. Additive by default; ‘drop: true` opts into orphan removal on every target. Each drop carries its own confirmation envelope through `Parse::MongoDB.drop_index`.

Returns:

  • (Hash{String => Hash})

    keyed by collection name. Each value Hash mirrors the legacy single-collection apply shape: ‘{ created:, skipped_exists:, dropped:, conflicts:, capacity_blocked: }`.



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

def apply!(drop: false)
  target_collections.each_with_object({}) do |coll, h|
    h[coll] = apply_for!(coll, drop: drop)
  end
end

#apply_for!(collection, drop: false) ⇒ Object

Per-collection apply. Honors the same triple-gate / idempotency rules as the cross-collection ‘apply!`. When `drop: true` the method runs orphan drops BEFORE create_index so freed index slots are available to satisfy `to_create` — required when the collection is at or near the 64-index cap. Capacity is checked against the post-drop count, matching the actual mid-apply state.



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
210
# File 'lib/parse/schema/index_migrator.rb', line 164

def apply_for!(collection, drop: false)
  p = plan_for(collection)
  capacity_ok = drop ? p[:capacity_ok_with_drop] : p[:capacity_ok]
  return { created: [], skipped_exists: [], dropped: [], conflicts: p[:conflicts],
           capacity_blocked: true } unless capacity_ok

  created = []
  # Pre-seed skipped_exists with the declarations the plan already
  # classified as in_sync — they don't go through create_index, but
  # callers expect the result to reflect EVERY declaration's fate.
  skipped = p[:in_sync].dup
  dropped = []

  # Drops run BEFORE creates so a full collection with one orphan
  # and one new declaration doesn't hit "too many indexes" before
  # the drop frees a slot.
  if drop
    p[:orphans].each do |name|
      confirm = "drop:#{collection}:#{name}"
      res = Parse::MongoDB.drop_index(collection, name, confirm: confirm,
                                      allow_system_classes: collection.start_with?("_Join:"))
      dropped << name if res == :dropped
    end
  end

  p[:to_create].each do |decl|
    result = Parse::MongoDB.create_index(
      collection,
      decl[:keys],
      name:                decl[:options][:name],
      unique:              decl[:options][:unique] == true,
      sparse:              decl[:options][:sparse] == true,
      partial_filter:      decl[:options][:partial_filter],
      expire_after:        decl[:options][:expire_after],
      allow_system_classes: collection.start_with?("_Join:"),
    )
    (result == :exists ? skipped : created) << decl
  end

  {
    created:        created,
    skipped_exists: skipped,
    dropped:        dropped,
    conflicts:      p[:conflicts],
    capacity_blocked: false,
  }
end

#collection_nameString

Returns the model’s primary collection name (parse_class).

Returns:

  • (String)

    the model’s primary collection name (parse_class).



70
71
72
# File 'lib/parse/schema/index_migrator.rb', line 70

def collection_name
  @model_class.parse_class
end

#planHash{String => Hash}

Compute the plan: what would change if ‘apply!` ran now.

Returns:

  • (Hash{String => Hash})

    keyed by collection name. Each value Hash carries the per-collection result (see #plan_for).



86
87
88
89
90
# File 'lib/parse/schema/index_migrator.rb', line 86

def plan
  target_collections.each_with_object({}) do |coll, h|
    h[coll] = plan_for(coll)
  end
end

#plan_for(collection) ⇒ Hash

Per-collection plan. Filters declarations to those targeting ‘collection`, then runs the diff against the actual MongoDB state for that collection.

Capacity accounting reports two scenarios so callers can reason about both apply modes from a single plan:

- `:capacity_after`, `:capacity_ok` — additive-only mode
  (no drops). Equal to `used + to_create.size`.
- `:capacity_after_with_drop`, `:capacity_ok_with_drop` —
  additive + orphan removal. Equal to `used + to_create.size
  - orphans.size`. Use these when planning an `apply!(drop:
  true)` call.

Parameters:

  • collection (String)

    target collection name

Returns:

  • (Hash)

    per-collection plan



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
# File 'lib/parse/schema/index_migrator.rb', line 107

def plan_for(collection)
  existing = fetch_existing_indexes(collection)
  declared = declarations_for(collection)
  managed, ours = partition_parse_managed(existing)
  to_create, in_sync, conflicts = diff_declarations(declared, ours)
  declared_names = declared.map { |d| d[:options][:name] }.compact.to_set
  declared_sigs  = declared.map { |d| key_sig(d[:keys]) }.to_set
  orphans = ours.reject do |idx|
    declared_sigs.include?(key_sig(idx["key"] || idx[:key])) ||
      declared_names.include?(idx["name"] || idx[:name])
  end

  max = Parse::Core::Indexing::MAX_INDEXES_PER_COLLECTION
  used = existing.size
  after_no_drop = used + to_create.size
  after_with_drop = after_no_drop - orphans.size

  {
    collection:               collection,
    declared:                 declared,
    existing:                 existing,
    parse_managed:            managed.map { |i| i["name"] || i[:name] },
    to_create:                to_create,
    in_sync:                  in_sync,
    conflicts:                conflicts,
    orphans:                  orphans.map { |i| i["name"] || i[:name] }.compact,
    capacity_used:            used,
    capacity_after:           after_no_drop,
    capacity_remaining:       max - after_no_drop,
    capacity_ok:              after_no_drop <= max,
    capacity_after_with_drop: after_with_drop,
    capacity_remaining_with_drop: max - after_with_drop,
    capacity_ok_with_drop:    after_with_drop <= max,
  }
end

#target_collectionsArray<String>

Returns unique target collections across all declarations. Includes the parent collection only when at least one declaration targets it (i.e. a non-relation index).

Returns:

  • (Array<String>)

    unique target collections across all declarations. Includes the parent collection only when at least one declaration targets it (i.e. a non-relation index).



77
78
79
# File 'lib/parse/schema/index_migrator.rb', line 77

def target_collections
  @model_class.mongo_index_declarations.map { |d| d[:collection] || collection_name }.uniq
end