active_snapshot 0.5.0 → 0.5.1
Sign up to get free protection for your applications and to get access to all the features.
- checksums.yaml +4 -4
- data/CHANGELOG.md +6 -1
- data/README.md +47 -2
- data/lib/active_snapshot/models/snapshot.rb +17 -6
- data/lib/active_snapshot/models/snapshot_item.rb +7 -1
- data/lib/active_snapshot/version.rb +1 -1
- metadata +2 -2
checksums.yaml
CHANGED
@@ -1,7 +1,7 @@
|
|
1
1
|
---
|
2
2
|
SHA256:
|
3
|
-
metadata.gz:
|
4
|
-
data.tar.gz:
|
3
|
+
metadata.gz: d5fd89751eb3c64739f2a3242edfb4a8f3fd1dfce8047bbd4db51b790039ee39
|
4
|
+
data.tar.gz: 5cf804fd3bb81be067153fa1120b45f5045febf0eb93921950ae51bea17c65a8
|
5
5
|
SHA512:
|
6
|
-
metadata.gz:
|
7
|
-
data.tar.gz:
|
6
|
+
metadata.gz: c079cdc615a35293cecbe0ab3c86df6a0b96e6bc0a1a92beb5b8580735d4a61209aa97623a18d9e06aeffeb109f94bbc742cea119e96421ba725ed9a22aae1eb
|
7
|
+
data.tar.gz: dad4d76b90fd8caa0dbd09a2c20d618bd6b8060de77eacbbad4eeda7d79cfa13be48fed5888f80122c607f8ce69acdf67eb7ce48707f54bd08dc3e8f9922604b
|
data/CHANGELOG.md
CHANGED
@@ -2,9 +2,14 @@ CHANGELOG
|
|
2
2
|
---------
|
3
3
|
|
4
4
|
- **Unreleased**
|
5
|
-
* [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.5.
|
5
|
+
* [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.5.1...master)
|
6
6
|
* Nothing yet
|
7
7
|
|
8
|
+
- **v0.5.1** - Nov 11, 2024
|
9
|
+
* [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.5.0...v0.5.1)
|
10
|
+
* [#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.
|
11
|
+
* [#63](https://github.com/westonganger/active_snapshot/pull/63) - Fix bug when enum value is nil
|
12
|
+
|
8
13
|
- **v0.5.0** - Nov 8, 2024
|
9
14
|
* [View Diff](https://github.com/westonganger/active_snapshot/compare/v0.4.0...v0.5.0)
|
10
15
|
* [#61](https://github.com/westonganger/active_snapshot/pull/61) - Ensure snapshot record returned by `create_snapshot!` is `valid?`
|
data/README.md
CHANGED
@@ -131,9 +131,11 @@ end
|
|
131
131
|
|
132
132
|
Now when you run `create_snapshot!` the associations will be tracked accordingly
|
133
133
|
|
134
|
-
# Reifying
|
134
|
+
# Reifying Snapshots
|
135
135
|
|
136
|
-
|
136
|
+
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.
|
137
|
+
|
138
|
+
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.
|
137
139
|
|
138
140
|
```ruby
|
139
141
|
reified_parent, reified_children_hash = snapshot.fetch_reified_items
|
@@ -149,6 +151,49 @@ reified_parent, reified_children_hash = snapshot.fetch_reified_items
|
|
149
151
|
reified_children_hash.first.instance_variable_set("@readonly", false)
|
150
152
|
```
|
151
153
|
|
154
|
+
# Diffing Versions
|
155
|
+
|
156
|
+
You can use the following example code to generate your own diffs.
|
157
|
+
|
158
|
+
```ruby
|
159
|
+
snapshot = post.snapshots.find_by!(identifier: "some-identifier")
|
160
|
+
|
161
|
+
snapshot_item = snapshot.snapshot_items.find_by!(item_type: "Post")
|
162
|
+
|
163
|
+
old_attrs = snapshot_item.object
|
164
|
+
new_attrs = post.attributes # or could be another snapshot object
|
165
|
+
|
166
|
+
attrs_not_changed = old_attrs.to_a.intersection(new_attrs.to_a).to_h
|
167
|
+
|
168
|
+
attrs_changed = new_attrs.to_a - attrs_not_changed.to_a
|
169
|
+
```
|
170
|
+
|
171
|
+
# Important Data Considerations / Warnings
|
172
|
+
|
173
|
+
### Dropping columns
|
174
|
+
|
175
|
+
If you plan to use the snapshot restore capabilities please be aware:
|
176
|
+
|
177
|
+
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.
|
178
|
+
|
179
|
+
I recommend that you add an entry to this in your applications safe-migrations guidelines.
|
180
|
+
|
181
|
+
If you would like to detect if this situation has already ocurred you can use the following script:
|
182
|
+
|
183
|
+
```ruby
|
184
|
+
SnapshotItem.all.each do |snapshot_item|
|
185
|
+
snapshot_item.object.keys.each do |key|
|
186
|
+
klass = Class.const_get(snapshot_item.item_type)
|
187
|
+
|
188
|
+
if !klass.column_names.include?(key)
|
189
|
+
invalid_data = snapshot_item.object.slice(*klass.column_names)
|
190
|
+
|
191
|
+
raise "invalid data found - #{invalid_data}"
|
192
|
+
end
|
193
|
+
end
|
194
|
+
end
|
195
|
+
```
|
196
|
+
|
152
197
|
# Key Models Provided & Additional Customizations
|
153
198
|
|
154
199
|
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.
|
@@ -46,15 +46,18 @@ module ActiveSnapshot
|
|
46
46
|
end
|
47
47
|
|
48
48
|
def build_snapshot_item(instance, child_group_name: nil)
|
49
|
-
|
50
|
-
|
51
|
-
|
52
|
-
|
49
|
+
attrs = instance.attributes
|
50
|
+
|
51
|
+
if instance.class.defined_enums.any?
|
52
|
+
instance.class.defined_enums.slice(*attrs.keys).each do |enum_col_name, enum_mapping|
|
53
|
+
val = attrs.fetch(enum_col_name)
|
54
|
+
next if val.nil?
|
55
|
+
attrs[enum_col_name] = enum_mapping.fetch(val)
|
53
56
|
end
|
54
57
|
end
|
55
58
|
|
56
59
|
self.snapshot_items.new({
|
57
|
-
object:
|
60
|
+
object: attrs,
|
58
61
|
item_id: instance.id,
|
59
62
|
item_type: instance.class.name,
|
60
63
|
child_group_name: child_group_name,
|
@@ -109,7 +112,15 @@ module ActiveSnapshot
|
|
109
112
|
reified_parent = nil
|
110
113
|
|
111
114
|
snapshot_items.each do |si|
|
112
|
-
reified_item = si.item_type.constantize.new
|
115
|
+
reified_item = si.item_type.constantize.new
|
116
|
+
|
117
|
+
si.object.each do |k,v|
|
118
|
+
if reified_item.respond_to?("#{k}=")
|
119
|
+
reified_item[k] = v
|
120
|
+
else
|
121
|
+
# database column was likely dropped since the snapshot was created
|
122
|
+
end
|
123
|
+
end
|
113
124
|
|
114
125
|
if readonly
|
115
126
|
reified_item.readonly!
|
@@ -51,7 +51,13 @@ module ActiveSnapshot
|
|
51
51
|
self.item = item_klass.new
|
52
52
|
end
|
53
53
|
|
54
|
-
|
54
|
+
object.each do |k,v|
|
55
|
+
if item.respond_to?("#{k}=")
|
56
|
+
item[k] = v
|
57
|
+
else
|
58
|
+
# database column was likely dropped since the snapshot was created
|
59
|
+
end
|
60
|
+
end
|
55
61
|
|
56
62
|
item.save!(validate: false, touch: false)
|
57
63
|
end
|
metadata
CHANGED
@@ -1,14 +1,14 @@
|
|
1
1
|
--- !ruby/object:Gem::Specification
|
2
2
|
name: active_snapshot
|
3
3
|
version: !ruby/object:Gem::Version
|
4
|
-
version: 0.5.
|
4
|
+
version: 0.5.1
|
5
5
|
platform: ruby
|
6
6
|
authors:
|
7
7
|
- Weston Ganger
|
8
8
|
autorequire:
|
9
9
|
bindir: bin
|
10
10
|
cert_chain: []
|
11
|
-
date: 2024-11-
|
11
|
+
date: 2024-11-12 00:00:00.000000000 Z
|
12
12
|
dependencies:
|
13
13
|
- !ruby/object:Gem::Dependency
|
14
14
|
name: activerecord
|