Track and apply changes using Changesets
mainNote: Changeset support requires the extralite-bundle version because the SQLite session extension is disabled by default in many distributions.
Changesets allow you to track data modifications within a block, apply those changes to other databases, or undo them.
- Use
db.track_changes(table_names)to start tracking. Passnilto track all tables. - Use
changeset.apply(target_db)to apply changes to a different database. - Use
changeset.invert.apply(db)to undo the changes. - Use
changeset.to_blobto serialize a changeset andExtralite::Changeset.new.load(blob)to restore it.
# track changes to specific tables
changeset = db.track_changes(:foo, :bar) do
insert_a_bunch_of_records(db)
end
# track changes to all tables
changeset = db.track_changes(nil) do
insert_a_bunch_of_records(db)
end
# apply to another database
changeset.apply(some_other_db)
# undo changes
changeset.invert.apply(db)
# save and load
IO.write('my.changes', changeset.to_blob)
changeset = Extralite::Changeset.new
changeset.load(IO.read('my.changes'))