bin/crypt_keeper in crypt_keeper-0.18.3 vs bin/crypt_keeper in crypt_keeper-0.18.4

- old
+ new

@@ -72,28 +72,36 @@ puts "#{encryptor} is not a known encryptor that can be migrated" exit 1 end class MigrateData < ActiveRecord::Base + self.inheritance_column = :_disable_sti_ + def self.reencrypt_all(encrypted_fields, old_encryptor, new_encryptor) ActiveRecord::Base.transaction do find_each do |record| - new_hash = {} - encrypted_fields.each do |f| - plaintext = old_encryptor.decrypt(record[f]) + new_hash = encrypted_fields.each_with_object({}) do |field, hash| + next unless raw = record[field].presence + + plaintext = old_encryptor.decrypt(raw) ciphertext = new_encryptor.encrypt(plaintext) - new_hash[f] = ciphertext + hash[field] = ciphertext end - record.update_attributes! new_hash - yield record if block_given? + updated = record.update_attributes!(new_hash) unless new_hash.empty? + + yield record, updated if block_given? end end end end ActiveRecord::Base.establish_connection(config.fetch(env)) MigrateData.table_name = table_name -MigrateData.reencrypt_all(columns, old_encryptor, new_encryptor) do |r| - puts "#{r.id} is now re-encrypted" +MigrateData.reencrypt_all(columns, old_encryptor, new_encryptor) do |r, updated| + if updated + puts "#{r.id} is now re-encrypted" + else + puts "#{r.id} was not updated" + end end