Giter VIP home page Giter VIP logo

Comments (6)

ankane avatar ankane commented on May 30, 2024 1

Yeah, that's correct (email_migrating is migrated_email). The extra attribute is an implementation detail that users shouldn't need to worry about, so I don't think it makes sense in the readme.

from lockbox.

mzanini avatar mzanini commented on May 30, 2024 1

For who is interested, in the Migrator class I changed record.send("#{v[:attribute]}=", record.send(k)) unless record.send(v[:encrypted_attribute]) to decrypted_value = record.send(k).decrypt Rails.application.secrets.token_encryption_password record.send("#{v[:attribute]}=", decrypted_value) unless record.send(v[:encrypted_attribute]).

In the Model class, I changed the code:

define_method "#{original_name}=" do |value|
  result = super(value)
  send("#{name}=", send(original_name))
  result
end

to

define_method "#{original_name}=" do |value|
  result = super(value)
  send("#{name}=", value)
  lock_for(original_name).content value
  result
end

This will allow to migrate existing data and keep the attribute and migrated_attribute in sync, allowing to run Strongbox and Lockbox side by side. The change is not totally complete though because strongbox has a different syntax for setting an attribute to a nil value. Therefore for a successful migration we have to be careful in setting migrated_attribute to nil whenever a one of the original attributes is nullified.

from lockbox.

ankane avatar ankane commented on May 30, 2024

Hey @mzanini, it's probably easiest to fork the gem, replace the send calls, and then switch back after migrating. The two places you'll want to change off the top of my head are the migrator class you mentioned and the model module (may be able to replace send(original_name) with value).

if options[:migrating]
# TODO reuse module
m = Module.new do
define_method "#{original_name}=" do |value|
result = super(value)
send("#{name}=", send(original_name))
result
end

I haven't tried strongbox, so there may be more work involved.

Edit: Basically, migrating consists of two parts: keeping the column in sync with new updates (the model module, which allows for zero downtime) and backfilling existing data (the migrator class)

from lockbox.

mzanini avatar mzanini commented on May 30, 2024

Thank you for your answer! To clarify how the migration process works right now (I think this could be added to the readme): let's say I want to migrate the email attribute on the User model.

  1. Adding encrypts :email, migrating: true will add an attribute email_migrating to the model.
  2. Assigning a value to email will cause email_migrating to be assigned that value too.
  3. Whenever the value of the email_migrating attribute is set, lockbox will encrypt the value and save it to the DB column email_ciphertext.
  4. Running Lockbox.migrate(User) will set email_migrating = email for every record
  5. Removing the migrating: true option will map the email attribute to the email_ciphertext column

Is that right or are thing working in a different way?

from lockbox.

mzanini avatar mzanini commented on May 30, 2024

Perfect, thanks for clarifying!

from lockbox.

ankane avatar ankane commented on May 30, 2024

Awesome, thanks for sharing @mzanini! This should be really helpful for others in the same situation.

from lockbox.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.