-
-
Save 5minpause/46bbd7a9da2feb943ced91e75279e785 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
add_index :attachments, :parent_id | |
add_index :attachments, :asset_id | |
add_index :domain_names, :user_id | |
add_index :domain_names, :event_id | |
add_index :event_memberships, :user_id | |
add_index :event_memberships, :event_id | |
remove_index :attachments, :parent_id | |
remove_index :attachments, :asset_id | |
remove_index:domain_names, :user_id | |
remove_index:domain_names, :event_id | |
remove_index:event_memberships, :user_id | |
remove_index:event_memberships, :event_id |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
missing = {} | |
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 | |
missing[t] = unindexed unless unindexed.empty? | |
end | |
missing.each do |table, columns| | |
columns.each do |col| | |
puts " add_index :#{table}, :#{col}" | |
end | |
end | |
missing.each do |table, columns| | |
columns.each do |col| | |
puts " remove_index :#{table}, :#{col}" | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Found the article back in the days with archive.org. It only contains the script. https://web.archive.org/web/20090928080237/http://penguincoder.org/pages/A_Slightly_Better_Way_To_Find_Missing_Foreign_Key_Indexes
A further article is linked there:
https://tomafro.net/2009/09/quickly-list-missing-foreign-key-indexes