{"action":"created","issue":{"url":"https://api.github.com/repos/GaggleAMP/cryptonite/issues/7","labels_url":"https://api.github.com/repos/GaggleAMP/cryptonite/issues/7/labels{/name}","comments_url":"https://api.github.com/repos/GaggleAMP/cryptonite/issues/7/comments","events_url":"https://api.github.com/repos/GaggleAMP/cryptonite/issues/7/events","html_url":"https://github.com/GaggleAMP/cryptonite/pull/7","id":54490430,"number":7,"title":"Migration helpers","user":{"login":"ebababi","id":142444,"avatar_url":"https://avatars.githubusercontent.com/u/142444?v=3","gravatar_id":"","url":"https://api.github.com/users/ebababi","html_url":"https://github.com/ebababi","followers_url":"https://api.github.com/users/ebababi/followers","following_url":"https://api.github.com/users/ebababi/following{/other_user}","gists_url":"https://api.github.com/users/ebababi/gists{/gist_id}","starred_url":"https://api.github.com/users/ebababi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ebababi/subscriptions","organizations_url":"https://api.github.com/users/ebababi/orgs","repos_url":"https://api.github.com/users/ebababi/repos","events_url":"https://api.github.com/users/ebababi/events{/privacy}","received_events_url":"https://api.github.com/users/ebababi/received_events","type":"User","site_admin":false},"labels":[{"url":"https://api.github.com/repos/GaggleAMP/cryptonite/labels/enhancement","name":"enhancement","color":"84b6eb"}],"state":"open","locked":false,"assignee":{"login":"ebababi","id":142444,"avatar_url":"https://avatars.githubusercontent.com/u/142444?v=3","gravatar_id":"","url":"https://api.github.com/users/ebababi","html_url":"https://github.com/ebababi","followers_url":"https://api.github.com/users/ebababi/followers","following_url":"https://api.github.com/users/ebababi/following{/other_user}","gists_url":"https://api.github.com/users/ebababi/gists{/gist_id}","starred_url":"https://api.github.com/users/ebababi/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/ebababi/subscriptions","organizations_url":"https://api.github.com/users/ebababi/orgs","repos_url":"https://api.github.com/users/ebababi/repos","events_url":"https://api.github.com/users/ebababi/events{/privacy}","received_events_url":"https://api.github.com/users/ebababi/received_events","type":"User","site_admin":false},"milestone":null,"comments":1,"created_at":"2015-01-15T19:10:12Z","updated_at":"2015-01-18T14:40:47Z","closed_at":null,"pull_request":{"url":"https://api.github.com/repos/GaggleAMP/cryptonite/pulls/7","html_url":"https://github.com/GaggleAMP/cryptonite/pull/7","diff_url":"https://github.com/GaggleAMP/cryptonite/pull/7.diff","patch_url":"https://github.com/GaggleAMP/cryptonite/pull/7.patch"},"body":"This will resolve #1 \r\nMigration helpers added to facilitate the encryption / decryption of data during migration.\r\n\r\nIt's WIP since documentation should be updated."},"comment":{"url":"https://api.github.com/repos/GaggleAMP/cryptonite/issues/comments/70410576","html_url":"https://github.com/GaggleAMP/cryptonite/pull/7#issuecomment-70410576","issue_url":"https://api.github.com/repos/GaggleAMP/cryptonite/issues/7","id":70410576,"user":{"login":"hundredwatt","id":91577,"avatar_url":"https://avatars.githubusercontent.com/u/91577?v=3","gravatar_id":"","url":"https://api.github.com/users/hundredwatt","html_url":"https://github.com/hundredwatt","followers_url":"https://api.github.com/users/hundredwatt/followers","following_url":"https://api.github.com/users/hundredwatt/following{/other_user}","gists_url":"https://api.github.com/users/hundredwatt/gists{/gist_id}","starred_url":"https://api.github.com/users/hundredwatt/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/hundredwatt/subscriptions","organizations_url":"https://api.github.com/users/hundredwatt/orgs","repos_url":"https://api.github.com/users/hundredwatt/repos","events_url":"https://api.github.com/users/hundredwatt/events{/privacy}","received_events_url":"https://api.github.com/users/hundredwatt/received_events","type":"User","site_admin":false},"created_at":"2015-01-18T14:40:47Z","updated_at":"2015-01-18T14:40:47Z","body":"@ebababi This isn't working for me as spec'd. In Amplify, I tried the migration like this:\r\n```\r\nclass ChangeTokenSecretInUsers < ActiveRecord::Migration\r\n  def up\r\n    change_column :users, :token, :text, limit: 4096\r\n    change_column :users, :secret, :text, limit: 4096\r\n\r\n    say_with_time \"encrypt_user_tokens_and_secrets\" do\r\n      Cryptonite.encrypt_model_attributes(User, :token, keypair: private_key)\r\n    end\r\n  end\r\n\r\n  def down\r\n\r\n    say_with_time \"decrypt_user_tokens_and_secrets\" do\r\n      Cryptonite.decrypt_model_attributes(User, :token, keypair: private_key)\r\n    end\r\n\r\n    change_column :users, :secret, :string\r\n    change_column :users, :token, :string\r\n  end\r\n\r\n  private\r\n\r\n  def public_key\r\n    OpenSSL::PKey::RSA.new(File.read(Rails.root.join(ENV['PUBLIC_KEY_FILE'])))\r\n  end\r\n\r\n  def private_key\r\n    OpenSSL::PKey::RSA.new(File.read(Rails.root.join(ENV['PRIVATE_KEY_FILE'])), ENV['PRIVATE_KEY_PASSWORD'])\r\n  end\r\nend\r\n```\r\n\r\nand it raised an ArgumentError. \r\n\r\nIf I specify a private key and a public key, it works:\r\n```\r\nclass ChangeTokenSecretInUsers < ActiveRecord::Migration\r\n  def up\r\n    change_column :users, :token, :text, limit: 4096\r\n    change_column :users, :secret, :text, limit: 4096\r\n\r\n    say_with_time \"encrypt_user_tokens_and_secrets\" do\r\n      Cryptonite.encrypt_model_attributes(User, :token, public_key: public_key, private_key: private_key)\r\n    end\r\n  end\r\n\r\n  def down\r\n\r\n    say_with_time \"decrypt_user_tokens_and_secrets\" do\r\n      Cryptonite.decrypt_model_attributes(User, :token, public_key: public_key, private_key: private_key)\r\n    end\r\n\r\n    change_column :users, :secret, :string\r\n    change_column :users, :token, :string\r\n  end\r\n\r\n  private\r\n\r\n  def public_key\r\n    OpenSSL::PKey::RSA.new(File.read(Rails.root.join(ENV['PUBLIC_KEY_FILE'])))\r\n  end\r\n\r\n  def private_key\r\n    OpenSSL::PKey::RSA.new(File.read(Rails.root.join(ENV['PRIVATE_KEY_FILE'])), ENV['PRIVATE_KEY_PASSWORD'])\r\n  end\r\nend\r\n```\r\n\r\nAdditionally, it's not working at all when their are multiple attributes:\r\n```\r\nclass ChangeTokenSecretInUsers < ActiveRecord::Migration\r\n  def up\r\n    change_column :users, :token, :text, limit: 4096\r\n    change_column :users, :secret, :text, limit: 4096\r\n\r\n    say_with_time \"encrypt_user_tokens_and_secrets\" do\r\n      Cryptonite.encrypt_model_attributes(User, :token, public_key: public_key, private_key: private_key)\r\n      Cryptonite.encrypt_model_attributes(User, :secret, public_key: public_key, private_key: private_key)\r\n    end\r\n  end\r\n\r\n  def down\r\n\r\n    say_with_time \"decrypt_user_tokens_and_secrets\" do\r\n      Cryptonite.decrypt_model_attributes(User, :token, public_key: public_key, private_key: private_key)\r\n      Cryptonite.decrypt_model_attributes(User, :secret, public_key: public_key, private_key: private_key)\r\n    end\r\n\r\n    change_column :users, :secret, :string\r\n    change_column :users, :token, :string\r\n  end\r\n\r\n  private\r\n\r\n  def public_key\r\n    OpenSSL::PKey::RSA.new(File.read(Rails.root.join(ENV['PUBLIC_KEY_FILE'])))\r\n  end\r\n\r\n  def private_key\r\n    OpenSSL::PKey::RSA.new(File.read(Rails.root.join(ENV['PRIVATE_KEY_FILE'])), ENV['PRIVATE_KEY_PASSWORD'])\r\n  end\r\nend\r\n```\r\nThis causes an exception and leaves the database in a state where the first column has been encrypted, but the second hasn't:\r\nclass ChangeTokenSecretInUsers < ActiveRecord::Migration\r\n  def up\r\n    change_column :users, :token, :text, limit: 4096\r\n    change_column :users, :secret, :text, limit: 4096\r\n\r\n    say_with_time \"encrypt_user_tokens_and_secrets\" do\r\n      Cryptonite.encrypt_model_attributes(User, :token, public_key: public_key, private_key: private_key)\r\n      Cryptonite.encrypt_model_attributes(User, :secret, public_key: public_key, private_key: private_key)\r\n    end\r\n  end\r\n\r\n  def down\r\n\r\n    say_with_time \"decrypt_user_tokens_and_secrets\" do\r\n      Cryptonite.decrypt_model_attributes(User, :token, public_key: public_key, private_key: private_key)\r\n      Cryptonite.decrypt_model_attributes(User, :secret, public_key: public_key, private_key: private_key)\r\n    end\r\n\r\n    change_column :users, :secret, :string\r\n    change_column :users, :token, :string\r\n  end\r\n\r\n  private\r\n\r\n  def public_key\r\n    OpenSSL::PKey::RSA.new(File.read(Rails.root.join(ENV['PUBLIC_KEY_FILE'])))\r\n  end\r\n\r\n  def private_key\r\n    OpenSSL::PKey::RSA.new(File.read(Rails.root.join(ENV['PRIVATE_KEY_FILE'])), ENV['PRIVATE_KEY_PASSWORD'])\r\n  end\r\nend"}}