针对 ActiveRecord 模型和关联的简化快照和恢复,采用透明的白盒实现
Simplified snapshots and restoration for ActiveRecord models and associations with a transparent white-box implementation.
Key Features:
Why This Library:
Model Versioning and Restoration require conscious thought, design, and understanding. You should understand your versioning and restoration process completely. This gem's small API and fully understandable design fully supports this.
I do not recommend using paper_trail-association_tracking because it is mostly a blackbox solution which encourages you to set it up and then assume its Just WorkingTM. This makes for major data problems later. Dont fall into this trap. Instead read this gems brief source code completely before use OR copy the code straight into your codebase. Once you know it, then you are free.
gem 'active_snapshot'Then generate and run the necessary migrations to setup the snapshots and snapshot_items tables.
rails generate active_snapshot:install
rake db:migrateThen add include ActiveSnapshot to your ApplicationRecord or individual models.
class ApplicationRecord (item){ item.release! }
}
}
end
endNow when you run create_snapshot! the associations will be tracked accordingly
A reified record refers to an ActiveRecord instance where the local objects data is set to match the snaphotted data, but the database remains changed.
You can view all of the "reified" snapshot items by calling the following method. Its completely up to you on how to use this data.
reified_parent, reified_children_hash = snapshot.fetch_reified_itemsAs a safety these records have the readonly attribute set on them.
If you want to perform any write actions on the returned instances you will have to set the readonly attribute to false
reified_parent, reified_children_hash = snapshot.fetch_reified_items(readonly: false)
# or
reified_parent, reified_children_hash = snapshot.fetch_reified_items
reified_children_hash.first.instance_variable_set("@readonly", false)You can obtain the diff between two snapshots like this:
from = post.snapshots.first
to = post.snapshots.second
ActiveSnapshot::Snapshot.diff(from, to)
# [
# {action: :update, item_type: "Post", item_id: 1, changes: {name: ["Old Name", "New Name"]}},
# {action: :destroy, item_type: "Comment", item_id: 1, changes: {id: [1, nil], content: ["Some Content", nil]}},
# {action: :create, item_type: "Comment", item_id: 2, changes: {id: [nil, 1], content: [nil, "New Content"]}}
# ]You can also obtain the diff between a snapshot and the current record:
from = post.snapshots.last
ActiveSnapshot::Snapshot.diff(from, post)If you plan to use the snapshot restore capabilities please be aware:
Whenever you drop a database column and there already exists snapshots of that model then you are kind of silently breaking your restore mechanism. Because now the application will not be able to assign data to columns that dont exist on the model. We work around this by bypassing the attribute assignment for snapshot item object entries that does not correlate to a current database column.
I recommend that you add an entry to this in your applications safe-migrations guidelines.
If you would like to detect if this situation has already ocurred you can use the following script:
SnapshotItem.all.each do |snapshot_item|
snapshot_item.object.keys.each do |key|
klass = Class.const_get(snapshot_item.item_type)
if !klass.column_names.include?(key)
invalid_data = snapshot_item.object.slice(*klass.column_names)
raise "invalid data found - #{invalid_data}"
end
end
endA key aspect of this library is its simplicity and small API. For major functionality customizations we encourage you to first delete this gem and then copy this gems code directly into your repository.
I strongly encourage you to read the code for this library to understand how it works within your project so that you are capable of customizing the functionality later.
snapshots and snapshot_items has_many associationscreate_snapshot! and has_snapshot_children methodsidentifier column (optional, but available for custom identification purposes)has_many :item_snapshotsobject column which contains an encoded database rowbelongs_to :snapshotCreated & Maintained by Weston Ganger - @westonganger
暂无开放 Issues,或尚未同步最近议题。