Created
January 9, 2019 04:04
-
-
Save Xenofex/6cfdbe08a8f485a6aad36c2ed1b93a4e 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
class AddColumnsForAttrEncrypted < ActiveRecord::Migration[5.2] | |
def change | |
tasks = { | |
alipays: [:private_key], | |
crawlers: [:username, :password], | |
delivery_platforms: [:key, :secret, :brand_key, :sid], | |
meituans: [:api_key], | |
wechat_service_accounts: [:wechat_pay_api_key, :wechat_pay_certificate] | |
} | |
tasks.each_pair do |table_name, columns| | |
klass = table_name.to_s.classify.constantize | |
columns.each do |column| | |
column_type = klass.type_for_attribute(:"encrypted_#{column}").type | |
add_column table_name, column, column_type | |
end | |
end | |
reversible do |direction| | |
direction.up do | |
tasks.each_pair do |table_name, columns| | |
klass = table_name.to_s.classify.constantize | |
columns.each do |column| | |
klass.all.each do |record| | |
begin | |
value = record.send(column) | |
record.update_column(column, value) | |
rescue => ex | |
logger.error { ex.inspect } | |
end | |
end | |
end | |
end | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment