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 CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: dcfaf7d239a170431fa82d70ab56b18bfb54a327b46376017357368b38f484b3
4
- data.tar.gz: 833d8fb9ca610983f9a9e510cd6fc94776f043bf3d35ec7e052a2ade87cf1e5a
3
+ metadata.gz: d5fd89751eb3c64739f2a3242edfb4a8f3fd1dfce8047bbd4db51b790039ee39
4
+ data.tar.gz: 5cf804fd3bb81be067153fa1120b45f5045febf0eb93921950ae51bea17c65a8
5
5
  SHA512:
6
- metadata.gz: a412ace11cbeee26528d5e3b58116a1b716f415ab4c1aead7978e6c5f2ccd1db49ee7c08ea0120226d1f66d1d099384cd6cadec5353a3f2fa5aee28d32b825e8
7
- data.tar.gz: 5290672baca7ed50d67e9654acb85851e997f9c601d6fa6aae898cf51ba448686595e30b61c4819eafe6f81608850777a9b75ca5a995c18f9f6b6d8047542bf8
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.0...master)
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 Snapshot Items
134
+ # Reifying Snapshots
135
135
 
136
- 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.
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
- attributes = instance.attributes
50
- attributes.each do |k, v|
51
- if instance.class.defined_enums.key?(k)
52
- attributes[k] = instance.class.defined_enums.fetch(k).fetch(v)
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: attributes,
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(si.object)
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
- item.assign_attributes(object)
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
@@ -1,3 +1,3 @@
1
1
  module ActiveSnapshot
2
- VERSION = "0.5.0".freeze
2
+ VERSION = "0.5.1".freeze
3
3
  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.0
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-08 00:00:00.000000000 Z
11
+ date: 2024-11-12 00:00:00.000000000 Z
12
12
  dependencies:
13
13
  - !ruby/object:Gem::Dependency
14
14
  name: activerecord