Skip to content

Instantly share code, notes, and snippets.

@magpieuk
Last active December 30, 2015 11:19
Show Gist options
  • Save magpieuk/7821859 to your computer and use it in GitHub Desktop.
Save magpieuk/7821859 to your computer and use it in GitHub Desktop.
Get list of foreign keys without indexes: source http://tomafro.net/2009/09/quickly-list-missing-foreign-key-indexes
c = ActiveRecord::Base.connection
c.tables.collect do |t|
columns = c.columns(t).collect(&:name).select {|x| x.ends_with?("_id" || x.ends_with("_type"))}
indexed_columns = c.indexes(t).collect(&:columns).flatten.uniq
unindexed = columns - indexed_columns
unless unindexed.empty?
unindexed.each do |ui|
puts "add_index :#{t}, :#{ui}"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment