rtiss_acts_as_versioned 0.10.3 → 0.11.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/README.md +89 -14
- data/lib/rtiss_acts_as_versioned/act_methods.rb +108 -38
- data/lib/rtiss_acts_as_versioned/version.rb +1 -1
- data/lib/rtiss_acts_as_versioned.rb +50 -15
- data/rtiss_acts_as_versioned.gemspec +15 -8
- data/test/callbacks_test.rb +87 -0
- data/test/failed_version_save_test.rb +54 -0
- data/test/fixtures/audited_thing.rb +43 -0
- data/test/revert_test.rb +61 -0
- data/test/schema.rb +21 -0
- data/test/sql_injection_test.rb +69 -0
- data/test/tiss_test.rb +123 -0
- data/test/versioned_test.rb +9 -9
- data/test/without_revision_and_locking_test.rb +86 -0
- metadata +18 -9
checksums.yaml
CHANGED
|
@@ -1,7 +1,7 @@
|
|
|
1
1
|
---
|
|
2
2
|
SHA256:
|
|
3
|
-
metadata.gz:
|
|
4
|
-
data.tar.gz:
|
|
3
|
+
metadata.gz: fb313d4b1afee0141352ed1efde3e40e39311839d1710d8698f802608ee5fe0d
|
|
4
|
+
data.tar.gz: 7982aca770e8f595b04e3c3127370497c08d15c5ef5ab49627a1bfb33ce799cb
|
|
5
5
|
SHA512:
|
|
6
|
-
metadata.gz:
|
|
7
|
-
data.tar.gz:
|
|
6
|
+
metadata.gz: c3f609e7348a1152eae8ee3c6760d5328e6565765f20b8ab74ef79a0df279e392445b565487845620dc11104b2e6fa33fe2f5c7be918125fb3eec8a2d3b2ed5e
|
|
7
|
+
data.tar.gz: 22eead504e93fc6cdaf30db1f82cc67f3770fd0d5009c8a631a0f47f3a09d93599963ff337485ad8eb4c839d814096bc76097c428c988fa773c008d061120e93
|
data/README.md
CHANGED
|
@@ -1,10 +1,17 @@
|
|
|
1
1
|
# rtiss_acts_as_versioned
|
|
2
2
|
|
|
3
|
-
Simple versioning for ActiveRecord models. Every change to a record
|
|
4
|
-
|
|
3
|
+
Simple versioning for ActiveRecord models. Every change to a record saves a **full copy of the
|
|
4
|
+
record** in a separate history table (`Model::Version`). The data is stored many times over, which
|
|
5
|
+
has advantages and disadvantages:
|
|
6
|
+
|
|
7
|
+
* **Pro – traceability:** you can see exactly what a record looked like at any point in time.
|
|
8
|
+
* **Pro – undo:** any earlier version can be restored. Deletions are versioned too, so destroyed
|
|
9
|
+
records can be restored (with the same `id`).
|
|
10
|
+
* **Con – space:** each version is a complete row, so the history table grows with every change.
|
|
11
|
+
Use `:limit`, `:if` / `:if_changed` or `:non_versioned_columns` to keep it smaller.
|
|
5
12
|
|
|
6
13
|
This is a fork of [technoweenie/acts_as_versioned](https://github.com/technoweenie/acts_as_versioned)
|
|
7
|
-
(Rick Olson, 2005), used by [
|
|
14
|
+
(Rick Olson, 2005), used by the TISS-Team at [TU Wien](https://www.tuwien.at/). It adds a few changes on top
|
|
8
15
|
of the original. The most important one: **destroyed records stay in the history table and can be
|
|
9
16
|
restored.**
|
|
10
17
|
|
|
@@ -16,13 +23,13 @@ restored.**
|
|
|
16
23
|
| Gem version | Rails | Ruby |
|
|
17
24
|
|-------------|----------------|-----------|
|
|
18
25
|
| 0.9.x | 4.x – 6.x | 2.x |
|
|
19
|
-
| 0.10
|
|
26
|
+
| >= 0.10 | >= 7.1 (activerecord >= 7.1.6) | >= 3.1.6 |
|
|
20
27
|
|
|
21
28
|
## Installation
|
|
22
29
|
|
|
23
30
|
```ruby
|
|
24
31
|
# Gemfile
|
|
25
|
-
gem 'rtiss_acts_as_versioned', '~> 0.
|
|
32
|
+
gem 'rtiss_acts_as_versioned', '~> 0.11'
|
|
26
33
|
```
|
|
27
34
|
|
|
28
35
|
---
|
|
@@ -54,6 +61,33 @@ In this fork:
|
|
|
54
61
|
|
|
55
62
|
Note: `delete` / `delete_all` skip callbacks, so they do **not** create a deletion version.
|
|
56
63
|
|
|
64
|
+
The deletion version is built from the **in-memory state** of the record when `after_destroy` runs.
|
|
65
|
+
Attributes you assign in a `before_destroy` callback are therefore stored in the deletion version,
|
|
66
|
+
without an extra `UPDATE` of the record. Use this e.g. to record who deleted it:
|
|
67
|
+
|
|
68
|
+
```ruby
|
|
69
|
+
before_destroy { self.changed_by = Current.user_id } # ends up in the deletion version only
|
|
70
|
+
```
|
|
71
|
+
|
|
72
|
+
Alternatively, without touching the original record, hook into the version itself:
|
|
73
|
+
`Version#deletion_version?` is `true` for the deletion version (also inside its own callbacks).
|
|
74
|
+
|
|
75
|
+
```ruby
|
|
76
|
+
Page::Version.before_create { self.changed_by = Current.user_id if deletion_version? }
|
|
77
|
+
```
|
|
78
|
+
|
|
79
|
+
Rails runs the destroy callbacks even if nothing is deleted. In that case **no** deletion version is
|
|
80
|
+
written:
|
|
81
|
+
|
|
82
|
+
* `destroy` is called a second time on an already destroyed instance (e.g. after a `has_one`
|
|
83
|
+
`dependent: :destroy` already removed it);
|
|
84
|
+
* the `DELETE` affects no row because another process deleted the record in the meantime
|
|
85
|
+
(on Oracle the second process waits for the row lock, then deletes nothing).
|
|
86
|
+
|
|
87
|
+
Only an explicit "nothing deleted" suppresses the version. The row count comes from overriding
|
|
88
|
+
ActiveRecord's private `destroy_row`; if a future Rails version stops calling it, deletions are
|
|
89
|
+
versioned as before (fail-open), and `test/tiss_test.rb` detects it.
|
|
90
|
+
|
|
57
91
|
### 3. Restoring destroyed records
|
|
58
92
|
|
|
59
93
|
New methods let you bring a destroyed record back from its history:
|
|
@@ -66,13 +100,26 @@ New methods let you bring a destroyed record back from its history:
|
|
|
66
100
|
A restore always creates a **new** version. That version stores, in the `record_restored`
|
|
67
101
|
column, which version the record was restored from.
|
|
68
102
|
|
|
103
|
+
The restored record is created with all attributes copied from the version. While `restore` saves
|
|
104
|
+
it (and its new version), `restoring?` returns `true`, so your callbacks can tell a restore apart
|
|
105
|
+
from a normal create, e.g. to record the restoring user instead of the copied value:
|
|
106
|
+
|
|
107
|
+
```ruby
|
|
108
|
+
before_create { self.changed_by = Current.user_id if changed_by.nil? || restoring? }
|
|
109
|
+
```
|
|
110
|
+
|
|
111
|
+
Outside of `restore`, `restoring?` is `false`, also after a failed restore.
|
|
112
|
+
|
|
69
113
|
### 4. `record_restored` column
|
|
70
114
|
|
|
71
115
|
The column holds the version number the record was restored from (`restore`) or reverted to
|
|
72
116
|
(`revert_to!`). It is `nil` for normal changes.
|
|
73
117
|
|
|
74
|
-
* `Version#record_restored?`
|
|
75
|
-
* `Version#record_restored_from_version` → `Integer` or `nil`
|
|
118
|
+
* `Version#record_restored?` → `true`/`false`
|
|
119
|
+
* `Version#record_restored_from_version` / `Version#record_restored` (the column) → `Integer` or `nil`
|
|
120
|
+
|
|
121
|
+
In queries compare with `nil`, not with `false`: `where(record_restored: nil)` (normal changes) or
|
|
122
|
+
`where.not(record_restored: nil)` (restores and reverts).
|
|
76
123
|
|
|
77
124
|
### 5. Version numbers only ever go up
|
|
78
125
|
|
|
@@ -80,7 +127,9 @@ In the original, `revert_to!(3)` set the record back to version number 3 and did
|
|
|
80
127
|
new version. In the fork:
|
|
81
128
|
|
|
82
129
|
* `revert_to!(n)` copies the data from version `n` and saves it as a **new version**
|
|
83
|
-
(`max + 1`), with `record_restored = n
|
|
130
|
+
(`max + 1`), with `record_restored = n`. This also applies when `:if` / `:if_changed` would
|
|
131
|
+
not create a version for a normal save. If the record can't be saved (validation), `revert_to!`
|
|
132
|
+
returns `false` and no version is written;
|
|
84
133
|
* `restore` also always gives the record a new, higher version number;
|
|
85
134
|
* the version column is **always separate from `lock_version`**. Do not set
|
|
86
135
|
`:version_column => 'lock_version'`. Optimistic locking and versioning work side by side.
|
|
@@ -92,6 +141,11 @@ The history therefore has no gaps and is append-only (an audit trail).
|
|
|
92
141
|
* `find_version(n)` – returns version `n`. Raises a `RuntimeError` if it does not exist.
|
|
93
142
|
* `find_newest_version` – returns the newest version, including a deletion version.
|
|
94
143
|
* `highest_version` – the highest version number, or `-1` if there is none or the record is unsaved.
|
|
144
|
+
* `Version.ordered` – scope that sorts by original record (foreign key), then version, e.g.
|
|
145
|
+
`Page::Version.ordered` or `page.versions.ordered`.
|
|
146
|
+
* `Version.currently_deleted` – scope with the deletion versions of all records that are deleted and
|
|
147
|
+
not restored, one per record (the newest version of the record is a deletion).
|
|
148
|
+
* `Version#deletion_version?` – `true` for the version written by `destroy`.
|
|
95
149
|
|
|
96
150
|
### 7. Other changes
|
|
97
151
|
|
|
@@ -212,7 +266,7 @@ id = page.id
|
|
|
212
266
|
page.destroy
|
|
213
267
|
Page.exists?(id) # => false
|
|
214
268
|
|
|
215
|
-
v = Page::Version.where(page_id: id).
|
|
269
|
+
v = Page::Version.where(page_id: id).ordered.last
|
|
216
270
|
v.version # => 4
|
|
217
271
|
v.deleted_in_original_table # => true
|
|
218
272
|
v.original_record_exists? # => false
|
|
@@ -223,10 +277,28 @@ Page.restore_deleted(id) # restore from the newest version
|
|
|
223
277
|
|
|
224
278
|
page = Page.find(id) # same id as before
|
|
225
279
|
page.version # => 5
|
|
226
|
-
page.find_version(5).record_restored
|
|
280
|
+
page.find_version(5).record_restored? # => true
|
|
281
|
+
page.find_version(5).record_restored_from_version # => 4
|
|
227
282
|
```
|
|
228
283
|
|
|
284
|
+
Finding deleted records that have not been restored:
|
|
285
|
+
|
|
286
|
+
```ruby
|
|
287
|
+
Page::Version.currently_deleted # all deleted pages (one deletion version each)
|
|
288
|
+
Page::Version.currently_deleted.ordered # sorted by page_id
|
|
289
|
+
Page::Version.currently_deleted.where(page_id: id).first # deletion version of one page, or nil
|
|
290
|
+
Page::Version.currently_deleted.find_each(&:restore) # restore all of them
|
|
291
|
+
```
|
|
292
|
+
|
|
293
|
+
`where(deleted_in_original_table: true)` alone is not enough: the deletion version keeps the flag
|
|
294
|
+
after a restore, so it also returns restored records, and records deleted more than once several times.
|
|
295
|
+
|
|
229
296
|
`restore` raises a `RuntimeError` if the original record still exists.
|
|
297
|
+
`restore_deleted` / `restore_deleted_version` raise `ActiveRecord::RecordNotFound` if there is no matching version.
|
|
298
|
+
|
|
299
|
+
If a version record can't be saved (e.g. a validation on the versioned class fails), `save`, `save!`,
|
|
300
|
+
`destroy` and `restore` raise `ActiveRecord::RecordNotSaved` (with the version as `error.record`)
|
|
301
|
+
and the change to the original record is rolled back.
|
|
230
302
|
|
|
231
303
|
### 7. Saving without a new version
|
|
232
304
|
|
|
@@ -240,10 +312,11 @@ Page.without_revision do
|
|
|
240
312
|
end
|
|
241
313
|
```
|
|
242
314
|
|
|
243
|
-
|
|
244
|
-
|
|
245
|
-
|
|
246
|
-
|
|
315
|
+
`without_revision` disables versioning for the class and its subclasses. `without_locking` disables
|
|
316
|
+
optimistic locking for all models. Both only affect the current thread (or fiber, depending on
|
|
317
|
+
`config.active_support.isolation_level`) and may be nested. `Page.without_revision?` tells you
|
|
318
|
+
whether you are inside such a block. `ActiveRecord::Base.lock_optimistically` itself is not
|
|
319
|
+
changed by `without_locking`.
|
|
247
320
|
|
|
248
321
|
### 8. Options
|
|
249
322
|
|
|
@@ -284,6 +357,8 @@ The tests run against SQLite (`test/database.yml`).
|
|
|
284
357
|
* `test/versioned_test.rb` – the original acts_as_versioned tests, adapted to the fork's behaviour
|
|
285
358
|
* `test/tiss_test.rb` – the fork features (destroy, restore, `record_restored`, `find_version`, …),
|
|
286
359
|
using the test model `GoodThing` (`name`, `why_good`), e.g. `GoodThing.create!(name: 'Sun', why_good: 'Keeps us warm')`
|
|
360
|
+
* `test/callbacks_test.rb` – contracts for application callbacks: `before_destroy` attributes in the
|
|
361
|
+
deletion version, `restoring?` during `restore`, using the test model `AuditedThing` (`name`, `changed_by`)
|
|
287
362
|
* `test/migration_test.rb` – `create_versioned_table`
|
|
288
363
|
|
|
289
364
|
## Release
|
|
@@ -2,12 +2,26 @@ module ActiveRecord
|
|
|
2
2
|
module Acts
|
|
3
3
|
module Versioned
|
|
4
4
|
module ActMethods
|
|
5
|
+
WITHOUT_REVISION_KEY = :rtiss_acts_as_versioned_without_revision
|
|
6
|
+
WITHOUT_LOCKING_KEY = :rtiss_acts_as_versioned_without_locking
|
|
7
|
+
|
|
8
|
+
# Prepended to ActiveRecord::Base.singleton_class: disables optimistic locking inside without_locking blocks
|
|
9
|
+
module LockingOverride
|
|
10
|
+
def locking_enabled?
|
|
11
|
+
return false if ActiveSupport::IsolatedExecutionState[WITHOUT_LOCKING_KEY]
|
|
12
|
+
|
|
13
|
+
super
|
|
14
|
+
end
|
|
15
|
+
end
|
|
16
|
+
|
|
5
17
|
def self.included(base) # :nodoc:
|
|
6
18
|
base.extend ClassMethods
|
|
7
19
|
end
|
|
8
20
|
|
|
9
21
|
# Saves a version of the model in the versioned table. This is called in the after_save callback by default
|
|
10
22
|
def save_version(save_this=false, deleted_flag=false, restored_from_version=nil)
|
|
23
|
+
return if self.class.without_revision?
|
|
24
|
+
|
|
11
25
|
if @saving_version || save_this
|
|
12
26
|
@saving_version = nil
|
|
13
27
|
rev = self.class.versioned_class.new
|
|
@@ -19,12 +33,23 @@ module ActiveRecord
|
|
|
19
33
|
if rev.respond_to? :updated_at=
|
|
20
34
|
rev.updated_at = Time.now
|
|
21
35
|
end
|
|
22
|
-
rev
|
|
36
|
+
save_version_record!(rev)
|
|
23
37
|
end
|
|
24
38
|
end
|
|
25
39
|
|
|
40
|
+
# Writes the deletion version (after_destroy callback).
|
|
41
|
+
# The version is built from the in-memory state of the record at this point, so attributes assigned in a
|
|
42
|
+
# before_destroy callback (e.g. who deleted the record) are stored in the deletion version.
|
|
43
|
+
#
|
|
44
|
+
# Rails runs the destroy callbacks even if nothing was deleted. No deletion version is written then:
|
|
45
|
+
# * the record was already destroyed (destroy called twice on the same instance), or
|
|
46
|
+
# * the DELETE affected no row, because another process deleted the record in the meantime.
|
|
47
|
+
# Fail-open: only an explicit "nothing deleted" suppresses the version, so a deletion is never lost
|
|
48
|
+
# if the information is missing.
|
|
26
49
|
def set_deleted_flag
|
|
27
50
|
return if self.id.nil?
|
|
51
|
+
return if @destroying_persisted_record == false
|
|
52
|
+
return if @destroyed_rows == 0
|
|
28
53
|
|
|
29
54
|
rev = self.class.versioned_class.new
|
|
30
55
|
clone_versioned_model(self, rev)
|
|
@@ -35,7 +60,7 @@ module ActiveRecord
|
|
|
35
60
|
if rev.respond_to? :updated_at=
|
|
36
61
|
rev.updated_at = Time.now
|
|
37
62
|
end
|
|
38
|
-
rev
|
|
63
|
+
save_version_record!(rev)
|
|
39
64
|
end
|
|
40
65
|
|
|
41
66
|
# Clears old revisions if a limit is set with the :limit option in <tt>acts_as_versioned</tt>.
|
|
@@ -44,7 +69,7 @@ module ActiveRecord
|
|
|
44
69
|
return if self.class.max_version_limit == 0
|
|
45
70
|
excess_baggage = send(self.class.version_column).to_i - self.class.max_version_limit
|
|
46
71
|
if excess_baggage > 0
|
|
47
|
-
self.class.versioned_class.where(
|
|
72
|
+
self.class.versioned_class.where(self.class.versioned_foreign_key => id, self.class.version_column => ..excess_baggage).delete_all
|
|
48
73
|
end
|
|
49
74
|
end
|
|
50
75
|
|
|
@@ -65,13 +90,16 @@ module ActiveRecord
|
|
|
65
90
|
|
|
66
91
|
# Reverts a model to a given version and saves the model.
|
|
67
92
|
# Takes either a version number or an instance of the versioned model
|
|
93
|
+
# A revert always creates a new version with the next version number.
|
|
94
|
+
# Returns false if the version is not found or the record can't be saved.
|
|
68
95
|
def revert_to!(version)
|
|
69
|
-
|
|
70
|
-
|
|
71
|
-
|
|
72
|
-
|
|
73
|
-
|
|
74
|
-
false
|
|
96
|
+
return false unless revert_to(version)
|
|
97
|
+
|
|
98
|
+
self.class.transaction do
|
|
99
|
+
send("#{self.class.version_column}=", next_version)
|
|
100
|
+
saved = save_without_revision
|
|
101
|
+
save_version(true, false, @reverted_from) if saved
|
|
102
|
+
saved
|
|
75
103
|
end
|
|
76
104
|
end
|
|
77
105
|
|
|
@@ -94,6 +122,22 @@ module ActiveRecord
|
|
|
94
122
|
end
|
|
95
123
|
end
|
|
96
124
|
|
|
125
|
+
# True while Version#restore saves this record, false otherwise.
|
|
126
|
+
# Lets the application tell a restore apart from a normal create in its own callbacks, e.g. to record
|
|
127
|
+
# the restoring user instead of the value copied from the version.
|
|
128
|
+
def restoring?
|
|
129
|
+
!!@restoring
|
|
130
|
+
end
|
|
131
|
+
|
|
132
|
+
# Marks the record as being restored for the duration of the block. Used by Version#restore.
|
|
133
|
+
def while_restoring # :nodoc:
|
|
134
|
+
previous = @restoring
|
|
135
|
+
@restoring = true
|
|
136
|
+
yield
|
|
137
|
+
ensure
|
|
138
|
+
@restoring = previous
|
|
139
|
+
end
|
|
140
|
+
|
|
97
141
|
def altered?
|
|
98
142
|
track_altered_attributes ? (version_if_changed - changed).length < version_if_changed.length : changed?
|
|
99
143
|
end
|
|
@@ -117,6 +161,8 @@ module ActiveRecord
|
|
|
117
161
|
|
|
118
162
|
# Checks whether a new version shall be saved or not. Calls <tt>version_condition_met?</tt> and <tt>changed?</tt>.
|
|
119
163
|
def save_version?
|
|
164
|
+
return false if self.class.without_revision?
|
|
165
|
+
|
|
120
166
|
version_condition_met? && altered?
|
|
121
167
|
end
|
|
122
168
|
|
|
@@ -153,12 +199,10 @@ module ActiveRecord
|
|
|
153
199
|
self.class.without_locking(&block)
|
|
154
200
|
end
|
|
155
201
|
|
|
156
|
-
def empty_callback() end #:nodoc:
|
|
157
|
-
|
|
158
202
|
def find_newest_version
|
|
159
203
|
return nil if self.id.nil?
|
|
160
204
|
|
|
161
|
-
self.class.versioned_class.where(
|
|
205
|
+
self.class.versioned_class.where(self.class.versioned_foreign_key => id).order(self.class.version_column => :desc).first
|
|
162
206
|
end
|
|
163
207
|
|
|
164
208
|
def highest_version
|
|
@@ -168,7 +212,7 @@ module ActiveRecord
|
|
|
168
212
|
def find_version(version)
|
|
169
213
|
return nil if self.id.nil?
|
|
170
214
|
|
|
171
|
-
ret = self.class.versioned_class.
|
|
215
|
+
ret = self.class.versioned_class.find_by(self.class.versioned_foreign_key => id, self.class.version_column => version)
|
|
172
216
|
raise "find_version: version #{version} not found in database" unless ret
|
|
173
217
|
ret
|
|
174
218
|
end
|
|
@@ -176,15 +220,43 @@ module ActiveRecord
|
|
|
176
220
|
protected
|
|
177
221
|
# sets the new version before saving
|
|
178
222
|
def set_new_version
|
|
223
|
+
return if self.class.without_revision?
|
|
224
|
+
|
|
179
225
|
@saving_version = new_record? || save_version?
|
|
180
226
|
self.send("#{self.class.version_column}=", next_version) if new_record? || save_version?
|
|
181
227
|
end
|
|
182
228
|
|
|
229
|
+
# Saves the version record. Raises ActiveRecord::RecordNotSaved if it can't be saved (e.g. a validation on the
|
|
230
|
+
# versioned class fails), so the surrounding save/destroy of the original record is rolled back.
|
|
231
|
+
def save_version_record!(rev)
|
|
232
|
+
return true if rev.save
|
|
233
|
+
|
|
234
|
+
raise ActiveRecord::RecordNotSaved.new(
|
|
235
|
+
"Couldn't save version #{rev.send(self.class.version_column)} of #{self.class.name} id = #{id}: " \
|
|
236
|
+
"#{rev.errors.full_messages.join(', ')}", rev)
|
|
237
|
+
end
|
|
238
|
+
|
|
183
239
|
# Gets the next available version for the current record, or 1 for a new record
|
|
184
240
|
def next_version
|
|
185
241
|
(new_record? ? 0 : versions.calculate(:maximum, version_column).to_i) + 1
|
|
186
242
|
end
|
|
187
243
|
|
|
244
|
+
# before_destroy callback, see set_deleted_flag. A destroyed record is no longer persisted, so a second
|
|
245
|
+
# destroy of the same instance deletes nothing.
|
|
246
|
+
def remember_destroying_persisted_record
|
|
247
|
+
@destroying_persisted_record = persisted?
|
|
248
|
+
@destroyed_rows = nil
|
|
249
|
+
end
|
|
250
|
+
|
|
251
|
+
private
|
|
252
|
+
|
|
253
|
+
# Overrides ActiveRecord::Persistence#destroy_row (private Rails API, also overridden by
|
|
254
|
+
# ActiveRecord::Locking::Optimistic) to remember how many rows the DELETE affected, see set_deleted_flag.
|
|
255
|
+
# If Rails ever stops calling this method, @destroyed_rows stays nil and deletions are versioned as before.
|
|
256
|
+
def destroy_row
|
|
257
|
+
@destroyed_rows = super
|
|
258
|
+
end
|
|
259
|
+
|
|
188
260
|
module ClassMethods
|
|
189
261
|
# Returns an array of columns that are versioned. See non_versioned_columns
|
|
190
262
|
def versioned_columns
|
|
@@ -197,51 +269,49 @@ module ActiveRecord
|
|
|
197
269
|
end
|
|
198
270
|
|
|
199
271
|
def restore_deleted(id)
|
|
200
|
-
version_record = versioned_class.where(
|
|
272
|
+
version_record = versioned_class.where(versioned_foreign_key => id).order(version_column => :desc).first!
|
|
201
273
|
version_record.restore
|
|
202
274
|
end
|
|
203
275
|
|
|
204
276
|
def restore_deleted_version(id, version)
|
|
205
|
-
version_record = versioned_class.
|
|
277
|
+
version_record = versioned_class.find_by!(versioned_foreign_key => id, version_column => version)
|
|
206
278
|
version_record.restore
|
|
207
279
|
end
|
|
208
280
|
|
|
209
|
-
# Executes the block with
|
|
281
|
+
# Executes the block with versioning disabled for this class (and its subclasses).
|
|
282
|
+
# Only affects the current thread (or fiber, see config.active_support.isolation_level); may be nested.
|
|
210
283
|
#
|
|
211
284
|
# Foo.without_revision do
|
|
212
285
|
# @foo.save
|
|
213
286
|
# end
|
|
214
287
|
#
|
|
215
|
-
def without_revision
|
|
216
|
-
|
|
217
|
-
|
|
218
|
-
|
|
219
|
-
alias_method attr_name, :empty_callback
|
|
220
|
-
end
|
|
221
|
-
end
|
|
222
|
-
block.call
|
|
288
|
+
def without_revision
|
|
289
|
+
previous = ActiveSupport::IsolatedExecutionState[WITHOUT_REVISION_KEY]
|
|
290
|
+
ActiveSupport::IsolatedExecutionState[WITHOUT_REVISION_KEY] = [*previous, self]
|
|
291
|
+
yield
|
|
223
292
|
ensure
|
|
224
|
-
|
|
225
|
-
CALLBACKS.each do |attr_name|
|
|
226
|
-
alias_method attr_name, "orig_#{attr_name}".to_sym
|
|
227
|
-
end
|
|
228
|
-
end
|
|
293
|
+
ActiveSupport::IsolatedExecutionState[WITHOUT_REVISION_KEY] = previous
|
|
229
294
|
end
|
|
230
295
|
|
|
231
|
-
#
|
|
296
|
+
# True while inside a without_revision block for this class (or a superclass) in the current thread.
|
|
297
|
+
def without_revision?
|
|
298
|
+
classes = ActiveSupport::IsolatedExecutionState[WITHOUT_REVISION_KEY]
|
|
299
|
+
!classes.nil? && classes.any? { |klass| self <= klass }
|
|
300
|
+
end
|
|
301
|
+
|
|
302
|
+
# Turns off optimistic locking for all models for the duration of the block.
|
|
303
|
+
# Only affects the current thread (or fiber, see config.active_support.isolation_level); may be nested.
|
|
232
304
|
#
|
|
233
305
|
# Foo.without_locking do
|
|
234
306
|
# @foo.save
|
|
235
307
|
# end
|
|
236
308
|
#
|
|
237
|
-
def without_locking
|
|
238
|
-
|
|
239
|
-
|
|
240
|
-
|
|
241
|
-
|
|
242
|
-
|
|
243
|
-
ActiveRecord::Base.lock_optimistically = true if current
|
|
244
|
-
end
|
|
309
|
+
def without_locking
|
|
310
|
+
previous = ActiveSupport::IsolatedExecutionState[WITHOUT_LOCKING_KEY]
|
|
311
|
+
ActiveSupport::IsolatedExecutionState[WITHOUT_LOCKING_KEY] = true
|
|
312
|
+
yield
|
|
313
|
+
ensure
|
|
314
|
+
ActiveSupport::IsolatedExecutionState[WITHOUT_LOCKING_KEY] = previous
|
|
245
315
|
end
|
|
246
316
|
|
|
247
317
|
# Rake migration task to create the versioned table using options passed to acts_as_versioned
|
|
@@ -70,7 +70,6 @@ module ActiveRecord #:nodoc:
|
|
|
70
70
|
#
|
|
71
71
|
# See ActiveRecord::Acts::Versioned::ClassMethods#acts_as_versioned for configuration options
|
|
72
72
|
module Versioned
|
|
73
|
-
CALLBACKS = [:set_new_version, :save_version, :save_version?]
|
|
74
73
|
def self.included(base) # :nodoc:
|
|
75
74
|
base.extend ClassMethods
|
|
76
75
|
end
|
|
@@ -206,17 +205,18 @@ module ActiveRecord #:nodoc:
|
|
|
206
205
|
has_many :versions, **version_association_options do
|
|
207
206
|
# finds earliest version of this record
|
|
208
207
|
def earliest
|
|
209
|
-
@earliest ||= order(
|
|
208
|
+
@earliest ||= order(#{version_column.to_sym.inspect} => :asc).first
|
|
210
209
|
end
|
|
211
210
|
|
|
212
211
|
# find latest version of this record
|
|
213
212
|
def latest
|
|
214
|
-
@latest ||= order(
|
|
213
|
+
@latest ||= order(#{version_column.to_sym.inspect} => :desc).first
|
|
215
214
|
end
|
|
216
215
|
end
|
|
217
216
|
before_save :set_new_version
|
|
218
217
|
after_save :save_version
|
|
219
218
|
after_save :clear_old_versions
|
|
219
|
+
before_destroy :remember_destroying_persisted_record
|
|
220
220
|
after_destroy :set_deleted_flag
|
|
221
221
|
|
|
222
222
|
unless options[:if_changed].nil?
|
|
@@ -233,18 +233,39 @@ module ActiveRecord #:nodoc:
|
|
|
233
233
|
def self.reloadable? ; false ; end
|
|
234
234
|
# find first version before the given version
|
|
235
235
|
def self.before(version)
|
|
236
|
-
where(
|
|
237
|
-
version.send(original_class.
|
|
238
|
-
.order(
|
|
236
|
+
where(original_class.versioned_foreign_key => version.send(original_class.versioned_foreign_key),
|
|
237
|
+
original_class.version_column => ...version.send(original_class.version_column))
|
|
238
|
+
.order(original_class.version_column => :desc).first
|
|
239
239
|
end
|
|
240
240
|
|
|
241
241
|
# find first version after the given version.
|
|
242
242
|
def self.after(version)
|
|
243
|
-
where(
|
|
244
|
-
|
|
245
|
-
.order(original_class.version_column).first
|
|
243
|
+
where(original_class.versioned_foreign_key => version.send(original_class.versioned_foreign_key))
|
|
244
|
+
.where(arel_table[original_class.version_column].gt(version.send(original_class.version_column)))
|
|
245
|
+
.order(original_class.version_column => :asc).first
|
|
246
246
|
end
|
|
247
247
|
|
|
248
|
+
# sorts by original record, then version
|
|
249
|
+
# Page::Version.ordered
|
|
250
|
+
# page.versions.ordered
|
|
251
|
+
scope :ordered, -> { order(original_class.versioned_foreign_key => :asc, original_class.version_column => :asc) }
|
|
252
|
+
|
|
253
|
+
# deletion versions of records that are deleted and not restored (the newest version is a deletion),
|
|
254
|
+
# one per record
|
|
255
|
+
# Page::Version.currently_deleted
|
|
256
|
+
# Page::Version.currently_deleted.where(page_id: id).first&.restore
|
|
257
|
+
# SQL: ... WHERE deleted_in_original_table = TRUE AND NOT EXISTS (newer version of the same record)
|
|
258
|
+
scope :currently_deleted, -> {
|
|
259
|
+
fk = original_class.versioned_foreign_key
|
|
260
|
+
vc = original_class.version_column
|
|
261
|
+
newer = arel_table.alias('newer_versions')
|
|
262
|
+
newer_version_exists = Arel::SelectManager.new(newer)
|
|
263
|
+
.project(Arel.sql('1'))
|
|
264
|
+
.where(newer[fk].eq(arel_table[fk]).and(newer[vc].gt(arel_table[vc])))
|
|
265
|
+
.exists
|
|
266
|
+
where(original_class.deleted_in_original_table_flag => true).where(newer_version_exists.not)
|
|
267
|
+
}
|
|
268
|
+
|
|
248
269
|
def previous
|
|
249
270
|
self.class.before(self)
|
|
250
271
|
end
|
|
@@ -275,23 +296,36 @@ module ActiveRecord #:nodoc:
|
|
|
275
296
|
# executed.
|
|
276
297
|
# Some ActiveRecord-Methods (for example #ActiveRecord::Base::AutosaveAssociation#save_collection_association) try to use ActiveRecord methods
|
|
277
298
|
# with these Relations, and if these Relations have been converted to Arrays, these calls fail with an Exception
|
|
278
|
-
new_version_number = self.class.where(self.original_class.versioned_foreign_key => id)
|
|
299
|
+
new_version_number = self.class.where(self.original_class.versioned_foreign_key => id)
|
|
300
|
+
.maximum(self.original_class.version_column).to_i + 1
|
|
279
301
|
restored_record.send("#{restored_record.version_column}=", new_version_number)
|
|
280
|
-
|
|
281
|
-
|
|
302
|
+
# one transaction, so the record is not restored if its version can't be saved
|
|
303
|
+
self.class.transaction do
|
|
304
|
+
# restoring? is true for the callbacks of the restored record while it and its version are saved
|
|
305
|
+
restored_record.while_restoring do
|
|
306
|
+
unless restored_record.save_without_revision(perform_validation)
|
|
307
|
+
raise RuntimeError.new("Couldn't restore the record, id = #{id} class = #{self.class.name}")
|
|
308
|
+
end
|
|
309
|
+
restored_record.save_version(true, false, self.send(self.original_class.version_column))
|
|
310
|
+
end
|
|
282
311
|
end
|
|
283
|
-
restored_record.save_version(true, false, self.send(self.original_class.version_column))
|
|
284
312
|
end
|
|
285
313
|
|
|
286
314
|
def record_restored?
|
|
287
315
|
self.read_attribute(self.original_class.record_restored_column) != nil
|
|
288
316
|
end
|
|
289
|
-
alias :record_restored :record_restored?
|
|
290
317
|
|
|
291
318
|
def record_restored_from_version
|
|
292
319
|
self.read_attribute(self.original_class.record_restored_column)
|
|
293
320
|
end
|
|
294
321
|
|
|
322
|
+
# True for the version written when the original record was destroyed.
|
|
323
|
+
# Lets the application tell a deletion version apart in its own callbacks (e.g. record the deleting
|
|
324
|
+
# user instead of the value copied from the record), without knowing the flag column name.
|
|
325
|
+
def deletion_version?
|
|
326
|
+
self.read_attribute(self.original_class.deleted_in_original_table_flag) == true
|
|
327
|
+
end
|
|
328
|
+
|
|
295
329
|
def original_record_exists?
|
|
296
330
|
original_class.exists?(self.send original_class.versioned_foreign_key)
|
|
297
331
|
end
|
|
@@ -312,4 +346,5 @@ module ActiveRecord #:nodoc:
|
|
|
312
346
|
end
|
|
313
347
|
end
|
|
314
348
|
|
|
315
|
-
ActiveRecord::Base.send :include, ActiveRecord::Acts::Versioned
|
|
349
|
+
ActiveRecord::Base.send :include, ActiveRecord::Acts::Versioned
|
|
350
|
+
ActiveRecord::Base.singleton_class.prepend ActiveRecord::Acts::Versioned::ActMethods::LockingOverride
|
|
@@ -12,20 +12,21 @@ Gem::Specification.new do |s|
|
|
|
12
12
|
## If your rubyforge_project name is different, then edit it and comment out
|
|
13
13
|
## the sub! line in the Rakefile
|
|
14
14
|
s.name = 'rtiss_acts_as_versioned'
|
|
15
|
-
s.version = '0.
|
|
16
|
-
s.date = '2026-
|
|
15
|
+
s.version = '0.11.0'
|
|
16
|
+
s.date = '2026-12-02'
|
|
17
17
|
s.rubyforge_project = 'rtiss_acts_as_versioned'
|
|
18
18
|
s.summary = "Add simple versioning to ActiveRecord models (TISS version)."
|
|
19
19
|
s.description = "Add simple versioning to ActiveRecord models (TISS version).
|
|
20
20
|
|
|
21
|
-
|
|
22
|
-
(
|
|
23
|
-
|
|
24
|
-
|
|
25
|
-
|
|
21
|
+
Every change to a record saves a full copy of the record in a separate history
|
|
22
|
+
table (Model::Version). Destroys are versioned too (deletes are not), so destroyed
|
|
23
|
+
records can be restored with the same id.
|
|
24
|
+
|
|
25
|
+
This fork is used by the TISS-Team at TU Wien and substantially differs from the
|
|
26
|
+
original. If you need a general-purpose versioning gem, consider technoweenie's
|
|
27
|
+
acts_as_versioned (on GitHub) or one of the actively maintained alternatives."
|
|
26
28
|
|
|
27
29
|
s.authors = ["Rick Olson", "Johannes Thoma", "Igor Jancev"]
|
|
28
|
-
s.email = 'igor.jancev@tuwien.ac.at'
|
|
29
30
|
s.homepage = 'http://github.com/rtiss/rtiss_acts_as_versioned'
|
|
30
31
|
s.require_paths = ["lib"]
|
|
31
32
|
s.rdoc_options = ["--charset=UTF-8"]
|
|
@@ -53,7 +54,10 @@ to use technoweenie's version (can be found also on github)"
|
|
|
53
54
|
mise.toml.example
|
|
54
55
|
rtiss_acts_as_versioned.gemspec
|
|
55
56
|
test/abstract_unit.rb
|
|
57
|
+
test/callbacks_test.rb
|
|
56
58
|
test/database.yml
|
|
59
|
+
test/failed_version_save_test.rb
|
|
60
|
+
test/fixtures/audited_thing.rb
|
|
57
61
|
test/fixtures/authors.yml
|
|
58
62
|
test/fixtures/good_thing.rb
|
|
59
63
|
test/fixtures/landmark.rb
|
|
@@ -67,9 +71,12 @@ to use technoweenie's version (can be found also on github)"
|
|
|
67
71
|
test/fixtures/pages_h.yml
|
|
68
72
|
test/fixtures/widget.rb
|
|
69
73
|
test/migration_test.rb
|
|
74
|
+
test/revert_test.rb
|
|
70
75
|
test/schema.rb
|
|
76
|
+
test/sql_injection_test.rb
|
|
71
77
|
test/tiss_test.rb
|
|
72
78
|
test/versioned_test.rb
|
|
79
|
+
test/without_revision_and_locking_test.rb
|
|
73
80
|
]
|
|
74
81
|
# = MANIFEST =
|
|
75
82
|
|