Skip to content

Commit

Permalink
On restore bypass assignment for snapshot object data where the assoc…
Browse files Browse the repository at this point in the history
…iated column no longer exists (#66)

Ensure `SnapshotItem#restore_item!` and `Snapshot#fetch_reified_items` bypass assignment for snapshot object data where the associated column no longer exists.

Solves #58
  • Loading branch information
westonganger authored Nov 12, 2024
1 parent 16305bb commit b0b6871
Show file tree
Hide file tree
Showing 6 changed files with 73 additions and 4 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ CHANGELOG

- **Unreleased**
* [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.5.0...master)
* [#66](https://github.com/westonganger/active_snapshot/pull/66) - Ensure `SnapshotItem#restore_item!` and `Snapshot#fetch_reified_items` bypass assignment for snapshot object data where the associated column no longer exists.
* [#63](https://github.com/westonganger/active_snapshot/pull/63) - Fix bug when enum value is nil

- **v0.5.0** - Nov 8, 2024
Expand Down
32 changes: 30 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -131,9 +131,11 @@ end

Now when you run `create_snapshot!` the associations will be tracked accordingly

# Reifying Snapshot Items
# Reifying Snapshots

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.
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.

```ruby
reified_parent, reified_children_hash = snapshot.fetch_reified_items
Expand Down Expand Up @@ -166,6 +168,32 @@ attrs_not_changed = old_attrs.to_a.intersection(new_attrs.to_a).to_h
attrs_changed = new_attrs.to_a - attrs_not_changed.to_a
```

# Important Data Considerations / Warnings

### Dropping columns

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:

```ruby
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
end
```

# Key Models Provided & Additional Customizations

A 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.
Expand Down
10 changes: 9 additions & 1 deletion lib/active_snapshot/models/snapshot.rb
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,15 @@ def fetch_reified_items(readonly: true)
reified_parent = nil

snapshot_items.each do |si|
reified_item = si.item_type.constantize.new(si.object)
reified_item = si.item_type.constantize.new

si.object.each do |k,v|
if reified_item.respond_to?("#{k}=")
reified_item[k] = v
else
# database column was likely dropped since the snapshot was created
end
end

if readonly
reified_item.readonly!
Expand Down
8 changes: 7 additions & 1 deletion lib/active_snapshot/models/snapshot_item.rb
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,13 @@ def restore_item!
self.item = item_klass.new
end

item.assign_attributes(object)
object.each do |k,v|
if item.respond_to?("#{k}=")
item[k] = v
else
# database column was likely dropped since the snapshot was created
end
end

item.save!(validate: false, touch: false)
end
Expand Down
13 changes: 13 additions & 0 deletions test/models/snapshot_item_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -67,4 +67,17 @@ def test_restore_item!
@snapshot_item.restore_item!
end

def test_restore_item_handles_dropped_columns!
snapshot = @snapshot_klass.includes(:snapshot_items).first

snapshot_item = snapshot.snapshot_items.first

attrs = snapshot_item.object
attrs["foo"] = "bar"

snapshot_item.update!(object: attrs)

snapshot_item.restore_item!
end

end
13 changes: 13 additions & 0 deletions test/models/snapshot_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -192,6 +192,19 @@ def test_fetch_reified_items_with_sti_class
assert_equal comment_content, reified_items.second[:comments].first.content
end

def test_fetch_reified_items_handles_dropped_columns!
snapshot = @snapshot_klass.first

snapshot_item = snapshot.snapshot_items.first

attrs = snapshot_item.object
attrs["foo"] = "bar"

snapshot_item.update!(object: attrs)

reified_items = snapshot.fetch_reified_items(readonly: false)
end

def test_single_model_snapshots_without_children
instance = ParentWithoutChildren.create!({a: 1, b: 2})

Expand Down

0 comments on commit b0b6871

Please sign in to comment.