Last active
June 1, 2017 12:41
-
-
Save VvanGemert/5664bf4d8519762a5361300bd03ff7a9 to your computer and use it in GitHub Desktop.
Example Rails migration to identify problematic database column names
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
class RemoveColumns < ActiveRecord::Migration[5.1] | |
def change | |
working = false | |
columns = Answer.columns.map &:name | |
rm_cols = columns - %w[cols to keep] | |
rm_target = '' | |
until working || rm_cols.empty? | |
working = true | |
begin | |
puts "attempting to run query with #{rm_cols.first} column present" | |
Question.last.reload.replies.reload.count | |
rescue => e | |
rm_target = rm_cols.shift.to_s.downcase.to_sym | |
puts "had to rescue #{rm_target} column #{e}" | |
remove_column :answer, rm_target | |
working = false | |
end | |
end | |
puts "works after removing #{rm_target}" if working | |
# Stop the migration from passing, as we | |
# need to rerun this script multiple times | |
exit 1 | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment