Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

does not work when environment was started with empty database #226

Open
grosser opened this issue Aug 1, 2016 · 7 comments
Open

does not work when environment was started with empty database #226

grosser opened this issue Aug 1, 2016 · 7 comments

Comments

@grosser
Copy link
Contributor

grosser commented Aug 1, 2016

When an rails app is started with eager_load but not database, then attr_encrypted initializes with attr_accessors ... which is bad ... basically trying to be too smart ... maybe it should ask for what kind of setup you want and then verify/blow up when that does not match your columns ... see #212

In my test I now do this to make it behave normally ... seems kind of bad :D

    # hack to make attr_encrypted always behave the same even when loaded without a database being
    # present ... `CI=1 RAILS_ENV=test rake db:drop db:create default`
    # since on load it checks if the column exists and then defined attr_accessors if they do not
    # 
    if ENV['CI'] && SecretStorage::DbBackend::Secret.instance_methods.include?(:encrypted_value_iv)
      [:encrypted_value_iv, :encrypted_value_iv=, :encrypted_value, :encrypted_value=].each do |m|
        SecretStorage::DbBackend::Secret.send(:undef_method, m)
      end
    end

see zendesk/samson#1176

/fyi @saghaulor

@lihanli
Copy link

lihanli commented Sep 9, 2016

i ran into the same issue when starting with an empty database and loading the schema with ActiveRecord::Migration.maintain_test_schema!, encrypted fields wouldn't save to the db

@grosser
Copy link
Contributor Author

grosser commented Oct 13, 2016

found a better hack ...

    # A hack to make attr_encrypted always behave the same even when loaded without a database being present.
    # On load it checks if the column exists and then defined attr_accessors if they do not.
    # Reproduce with:
    # CI=1 RAILS_ENV=test TEST=test/some_test.rb rake db:drop db:create default
    #
    # https://github.com/attr-encrypted/attr_encrypted/issues/226
    def attr_encrypted(column, *)
      super
      bad = [
        :"encrypted_#{column}_iv",
        :"encrypted_#{column}_iv=",
        :"encrypted_#{column}",
        :"encrypted_#{column}="
      ]
      (instance_methods & bad).each { |m| undef_method m }
    end

@grosser
Copy link
Contributor Author

grosser commented Nov 28, 2016

FYI hacky migration to make this work when moving data from an unencrypted column to an encrypted column.

grosser/orgdeps@702b89c

@ryansch
Copy link

ryansch commented Mar 31, 2017

I ran into this with rake db:reset and was able to fix it by splitting out the seed command like so:

rake db:schema:load_if_ruby db:structure:load_if_sql
rake environment db:seed

@saghaulor
Copy link
Contributor

I believe that #294 should fix this. Can someone please confirm?

@oehlschl
Copy link

oehlschl commented Apr 9, 2018

Just found this thread after some long hours debugging unrelated changes to our CI tooling that triggered a similar case; attr_encrypted properties were written to the models but not persisted to the DB.

Upgrading from 3.0.3 to 3.1 resolved the issue for me. Thanks!

@adelatuduce
Copy link

Same as @oehlschl
Thank you for the information. I almost went mad trying to understand why my specs were failing on Circle CI, but when logging into the instance, all of them passed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

6 participants