Skip to content

Instantly share code, notes, and snippets.

@vbguard
Forked from exAspArk/diff_mongoid_indexes.rb
Created October 4, 2019 21:47
Show Gist options
  • Save vbguard/cf7da5934efb48b908fa752bcb91b754 to your computer and use it in GitHub Desktop.
Save vbguard/cf7da5934efb48b908fa752bcb91b754 to your computer and use it in GitHub Desktop.
defined_indexes = Model.index_specifications.map { |s| s.fields.map(&:to_s) };
existing_indexes = Model.collection.indexes.map { |i| i['key'].keys };
missing_indexes = defined_indexes - existing_indexes
# => []
extra_indexes = existing_indexes - defined_indexes - [['_id']]
# => []
#################################################################################
# if the arrays are correct (hopefully the indexes have {background: true} flag),
# then you're good to run
Model.create_indexes # or Model.remove_indexes
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment