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

How to migrate existing application from paper_trail to active_snapshot #28

Open westonganger opened 1 year ago

westonganger commented 1 year ago

This question was raised in another thread, https://github.com/westonganger/active_snapshot/issues/14#issuecomment-1234237716

Is there any guide on migrating existing application from paper_trail to this gem?

westonganger commented 1 year ago

Seems to me that this should be easy to do. Disclaimer, I just whipped this up quickly, its totally untested.

class MyRecord
  has_snapshot_children do
    ...
  end
end

MyRecord.all.each do |record|
  record.versions.each do |version|

    ### Use whatever options are applicable here
    reified_record = version.reify(
      has_many: true, 
      has_one: true, 
      belongs_to: false,
    )

    snapshot_identifier = "#{version.updated_at}"

    user = User.find(version.whodunnit) if version.whodunnit

    reified_record.create_snapshot(
      identifier: snapshot_identifier, 
      user: user,
      metadata: {
        info: "Migrated from paper_trail-association_tracking"
      }
    )

  end
end