byroot / activerecord-typedstore

ActiveRecord::Store but with type definition
MIT License
437 stars 57 forks source link

Change coder after data have persisted #106

Closed Tashows closed 3 weeks ago

Tashows commented 6 months ago

I was late to realize I would need to run jsonb boolean queries on my typed_store :settings field. I didn't set a coder initially, which seems to result in falling back to a YAML coder. Now if I change coder, data seems empty even if it's not really. Is there a defined way to migrate data in such cases in order to move to the desired coder? If not, could you provide external resources or suggest ways to move about this issue that might help me and others that land here?

Tashows commented 3 weeks ago

I ended up adding a second jsonb column to my model with the coder I wanted and the exact same typed_store setup. I then added a data_migration that did model.data_column_2 = model.data_column for each record.

For anyone trying this, you might want to make sure you are not doing a lot of stuff on model callbacks, since having a duplicated typed_store setup will cause the original accessors to be overridden by the second setup. Or run some validation scripts according to your case.