westonganger / active_snapshot

Simplified snapshots and restoration for ActiveRecord models and associations with a transparent white-box implementation
MIT License
103 stars 16 forks source link

Using JSON Column instead of YAML encoded object column #15

Closed westonganger closed 1 year ago

westonganger commented 2 years ago

If you want to use a JSON column instead of YAML encoded solution.

active_record-import and MySQL were not playing nice with JSON columns so I switch to a YAML encoded solution. It should be relatively straight forward to switch to json instead of the text column.The following patch should work for this case.

if SnapshotItem.connection.adapter_name == "Mysql2" && SnapshotItem.columns_hash["object"].type.start_with?("json")
  snapshot_items.each do |attrs|
    SnapshotItem.create!(attrs)
  end
else
  SnapshotItem.import(snapshot_items, validate: true)
end
westonganger commented 2 years ago

One reason to switch to JSON columns would be that we could remove the user_id and user_type columns from snapshots table as all info could be stored in metadata column. For custom solutions even the identifier column could be removed and stored in metadata column too.

westonganger commented 2 years ago

See issue on pay gem where they discuss JSON problems with MySQL/MariaDB

https://github.com/pay-rails/pay/issues/425

Maybe the solution there can be used to mitigate whatever issues were occuring

westonganger commented 1 year ago

This is now supported see the readme, https://github.com/westonganger/active_snapshot#using-a-different-storage-format