skill-assessment-app/db/migrate/20160826200610_encode_candidate_emails.rb

28 lines
778 B
Ruby
Raw Normal View History

# frozen_string_literal: true
class EncodeCandidateEmails < ActiveRecord::Migration[5.0]
def change
sql = "select id, email from candidates;"
candidates = ActiveRecord::Base.connection.execute(sql).to_h
candidates.each do |id, email|
sql = if base64?(email)
# going down - decrypt
"UPDATE candidates set email = '#{CryptSerializer.load email}' WHERE id = #{id};"
else
# going up - encrypt emails
"UPDATE candidates set email = '#{CryptSerializer.dump email}' WHERE id = #{id};"
end
ActiveRecord::Base.connection.execute(sql)
end
end
private
def base64? string
Base64.urlsafe_decode64 string
true
rescue ArgumentError
false
end
end