rtiss_acts_as_versioned 0.10.2 → 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 +367 -11
- 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 +51 -15
- data/mise.toml.example +2 -0
- data/rtiss_acts_as_versioned.gemspec +17 -10
- data/test/abstract_unit.rb +6 -7
- 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/fixtures/good_thing.rb +8 -0
- data/test/fixtures/migrations/2_add_versioned_tables.rb +3 -3
- data/test/fixtures/page.rb +4 -4
- data/test/fixtures/widget.rb +2 -4
- data/test/migration_test.rb +23 -21
- data/test/revert_test.rb +61 -0
- data/test/schema.rb +33 -56
- data/test/sql_injection_test.rb +69 -0
- data/test/tiss_test.rb +261 -172
- data/test/versioned_test.rb +23 -23
- data/test/without_revision_and_locking_test.rb +86 -0
- metadata +22 -16
- data/test/fixtures/locked_rolle.rb +0 -8
- data/test/fixtures/rolle.rb +0 -7
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,23 +1,379 @@
|
|
|
1
1
|
# rtiss_acts_as_versioned
|
|
2
2
|
|
|
3
|
-
|
|
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:
|
|
4
6
|
|
|
5
|
-
|
|
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.
|
|
6
12
|
|
|
7
|
-
|
|
8
|
-
|
|
13
|
+
This is a fork of [technoweenie/acts_as_versioned](https://github.com/technoweenie/acts_as_versioned)
|
|
14
|
+
(Rick Olson, 2005), used by the TISS-Team at [TU Wien](https://www.tuwien.at/). It adds a few changes on top
|
|
15
|
+
of the original. The most important one: **destroyed records stay in the history table and can be
|
|
16
|
+
restored.**
|
|
9
17
|
|
|
10
|
-
|
|
18
|
+
> **Note:** These changes were made for the needs of TISS only. The gem is used and tested only with
|
|
19
|
+
> **Oracle** (via `activerecord-oracle_enhanced-adapter`) and **SQLite**. Other databases may work,
|
|
20
|
+
> but we do not support them. If you need a general-purpose versioning gem, consider the original
|
|
21
|
+
> or one of the actively maintained alternatives.
|
|
11
22
|
|
|
12
|
-
|
|
23
|
+
| Gem version | Rails | Ruby |
|
|
24
|
+
|-------------|----------------|-----------|
|
|
25
|
+
| 0.9.x | 4.x – 6.x | 2.x |
|
|
26
|
+
| >= 0.10 | >= 7.1 (activerecord >= 7.1.6) | >= 3.1.6 |
|
|
13
27
|
|
|
14
|
-
|
|
28
|
+
## Installation
|
|
15
29
|
|
|
30
|
+
```ruby
|
|
31
|
+
# Gemfile
|
|
32
|
+
gem 'rtiss_acts_as_versioned', '~> 0.11'
|
|
33
|
+
```
|
|
16
34
|
|
|
17
|
-
|
|
35
|
+
---
|
|
18
36
|
|
|
19
|
-
|
|
37
|
+
## Differences from the original acts_as_versioned
|
|
20
38
|
|
|
21
|
-
|
|
39
|
+
If you know the original gem, these are the changes in the TISS fork.
|
|
22
40
|
|
|
23
|
-
|
|
41
|
+
### 1. History tables use the `_h` suffix
|
|
42
|
+
|
|
43
|
+
| | Original | TISS fork |
|
|
44
|
+
|---|---|---|
|
|
45
|
+
| Default table of the version model | `page_versions` | `pages_h` (`"#{table_name}_h"`) |
|
|
46
|
+
| Version class | `Page::Version` | `Page::Version` (unchanged) |
|
|
47
|
+
|
|
48
|
+
You can still change both with `:table_name` and `:class_name`.
|
|
49
|
+
|
|
50
|
+
### 2. Destroyed records stay in the history (`deleted_in_original_table`)
|
|
51
|
+
|
|
52
|
+
In the original gem, `destroy` also deleted all versions (`:dependent => :delete_all`).
|
|
53
|
+
In this fork:
|
|
54
|
+
|
|
55
|
+
* the `versions` association has **no** `:dependent` option, so versions survive `destroy`;
|
|
56
|
+
* an `after_destroy` callback (`set_deleted_flag`) writes **one more version**
|
|
57
|
+
(`highest_version + 1`) with `deleted_in_original_table = true`. The record's last state
|
|
58
|
+
is therefore always kept;
|
|
59
|
+
* `:association_options => { :dependent => :nullify }` is **forbidden** and raises an error,
|
|
60
|
+
because it would leave orphaned versions behind.
|
|
61
|
+
|
|
62
|
+
Note: `delete` / `delete_all` skip callbacks, so they do **not** create a deletion version.
|
|
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
|
+
|
|
91
|
+
### 3. Restoring destroyed records
|
|
92
|
+
|
|
93
|
+
New methods let you bring a destroyed record back from its history:
|
|
94
|
+
|
|
95
|
+
* `Version#restore` – restores the original record from this version (with the **same `id`**)
|
|
96
|
+
* `Model.restore_deleted(id)` – restores from the newest version
|
|
97
|
+
* `Model.restore_deleted_version(id, version)` – restores from a specific version
|
|
98
|
+
* `Version#original_record_exists?` – checks whether the original record still exists
|
|
99
|
+
|
|
100
|
+
A restore always creates a **new** version. That version stores, in the `record_restored`
|
|
101
|
+
column, which version the record was restored from.
|
|
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
|
+
|
|
113
|
+
### 4. `record_restored` column
|
|
114
|
+
|
|
115
|
+
The column holds the version number the record was restored from (`restore`) or reverted to
|
|
116
|
+
(`revert_to!`). It is `nil` for normal changes.
|
|
117
|
+
|
|
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).
|
|
123
|
+
|
|
124
|
+
### 5. Version numbers only ever go up
|
|
125
|
+
|
|
126
|
+
In the original, `revert_to!(3)` set the record back to version number 3 and did not create a
|
|
127
|
+
new version. In the fork:
|
|
128
|
+
|
|
129
|
+
* `revert_to!(n)` copies the data from version `n` and saves it as a **new version**
|
|
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;
|
|
133
|
+
* `restore` also always gives the record a new, higher version number;
|
|
134
|
+
* the version column is **always separate from `lock_version`**. Do not set
|
|
135
|
+
`:version_column => 'lock_version'`. Optimistic locking and versioning work side by side.
|
|
136
|
+
|
|
137
|
+
The history therefore has no gaps and is append-only (an audit trail).
|
|
138
|
+
|
|
139
|
+
### 6. Extra lookup methods on the instance
|
|
140
|
+
|
|
141
|
+
* `find_version(n)` – returns version `n`. Raises a `RuntimeError` if it does not exist.
|
|
142
|
+
* `find_newest_version` – returns the newest version, including a deletion version.
|
|
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`.
|
|
149
|
+
|
|
150
|
+
### 7. Other changes
|
|
151
|
+
|
|
152
|
+
* `:non_versioned_columns` option to exclude columns directly in the `acts_as_versioned` call.
|
|
153
|
+
* `:deleted_in_original_table_flag` and `:record_restored_column` options to rename the extra columns.
|
|
154
|
+
* `updated_at` on a version is set to `Time.now` when it is saved.
|
|
155
|
+
* `save_without_revision(perform_validation = true)` lets you skip validation.
|
|
156
|
+
* `create_versioned_table` also creates the `deleted_in_original_table` and
|
|
157
|
+
`record_restored` columns.
|
|
158
|
+
* The `belongs_to` from the version to the original model is `optional: true`
|
|
159
|
+
(the original may already be gone).
|
|
160
|
+
|
|
161
|
+
---
|
|
162
|
+
|
|
163
|
+
## Quick start
|
|
164
|
+
|
|
165
|
+
### 1. Migration
|
|
166
|
+
|
|
167
|
+
The main table needs a `version` column (integer). The history table holds the foreign key,
|
|
168
|
+
`version`, the two fork columns and every column you want versioned.
|
|
169
|
+
|
|
170
|
+
Easiest way, with the built-in helper:
|
|
171
|
+
|
|
172
|
+
```ruby
|
|
173
|
+
class AddVersioningToPages < ActiveRecord::Migration[7.1]
|
|
174
|
+
def up
|
|
175
|
+
Page.create_versioned_table # creates pages_h (and pages.version if missing)
|
|
176
|
+
end
|
|
177
|
+
|
|
178
|
+
def down
|
|
179
|
+
Page.drop_versioned_table
|
|
180
|
+
end
|
|
181
|
+
end
|
|
182
|
+
```
|
|
183
|
+
|
|
184
|
+
Or by hand:
|
|
185
|
+
|
|
186
|
+
```ruby
|
|
187
|
+
create_table :pages do |t|
|
|
188
|
+
t.string :title
|
|
189
|
+
t.text :body
|
|
190
|
+
t.integer :version
|
|
191
|
+
t.integer :lock_version, default: 0 # optional, for optimistic locking
|
|
192
|
+
t.timestamps
|
|
193
|
+
end
|
|
194
|
+
|
|
195
|
+
create_table :pages_h do |t|
|
|
196
|
+
t.integer :page_id # foreign key to the original
|
|
197
|
+
t.integer :version
|
|
198
|
+
t.string :title
|
|
199
|
+
t.text :body
|
|
200
|
+
t.timestamps
|
|
201
|
+
t.boolean :deleted_in_original_table, default: false
|
|
202
|
+
t.integer :record_restored # nil, or a version number
|
|
203
|
+
end
|
|
204
|
+
add_index :pages_h, [:page_id, :version], unique: true
|
|
205
|
+
```
|
|
206
|
+
|
|
207
|
+
> `create_versioned_table` reads the columns from the model, so the main table must exist first.
|
|
208
|
+
> If you change the schema later, you have to update the `_h` table yourself, and restart the app:
|
|
209
|
+
> the version class reads its columns only once.
|
|
210
|
+
|
|
211
|
+
### 2. Model
|
|
212
|
+
|
|
213
|
+
```ruby
|
|
214
|
+
class Page < ApplicationRecord
|
|
215
|
+
acts_as_versioned
|
|
216
|
+
end
|
|
217
|
+
```
|
|
218
|
+
|
|
219
|
+
### 3. Creating and changing records
|
|
220
|
+
|
|
221
|
+
```ruby
|
|
222
|
+
page = Page.create!(title: 'Hello')
|
|
223
|
+
page.version # => 1
|
|
224
|
+
page.versions.count # => 1
|
|
225
|
+
|
|
226
|
+
page.update!(title: 'Hello World')
|
|
227
|
+
page.version # => 2
|
|
228
|
+
|
|
229
|
+
page.update!(title: 'Hello World') # nothing changed, so no new version
|
|
230
|
+
page.version # => 2
|
|
231
|
+
```
|
|
232
|
+
|
|
233
|
+
### 4. Reading versions
|
|
234
|
+
|
|
235
|
+
```ruby
|
|
236
|
+
page.versions # all versions (has_many, Page::Version)
|
|
237
|
+
page.versions.earliest # first version
|
|
238
|
+
page.versions.latest # newest version
|
|
239
|
+
page.versions.where(title: 'Hello') # works like any normal relation
|
|
240
|
+
|
|
241
|
+
page.find_version(1).title # => 'Hello'
|
|
242
|
+
page.find_version(99) # => RuntimeError
|
|
243
|
+
page.find_newest_version.version # => 2
|
|
244
|
+
page.highest_version # => 2
|
|
245
|
+
|
|
246
|
+
v = page.find_version(2)
|
|
247
|
+
v.previous # version 1
|
|
248
|
+
v.next # nil
|
|
249
|
+
v.page # the original record (nil if destroyed)
|
|
250
|
+
```
|
|
251
|
+
|
|
252
|
+
### 5. Reverting to an older version
|
|
253
|
+
|
|
254
|
+
```ruby
|
|
255
|
+
page.revert_to(1) # changes attributes in memory only, does not save
|
|
256
|
+
page.revert_to!(1) # saves it as a NEW version
|
|
257
|
+
page.version # => 3
|
|
258
|
+
page.title # => 'Hello'
|
|
259
|
+
page.find_version(3).record_restored_from_version # => 1
|
|
260
|
+
```
|
|
261
|
+
|
|
262
|
+
### 6. Destroying and restoring
|
|
263
|
+
|
|
264
|
+
```ruby
|
|
265
|
+
id = page.id
|
|
266
|
+
page.destroy
|
|
267
|
+
Page.exists?(id) # => false
|
|
268
|
+
|
|
269
|
+
v = Page::Version.where(page_id: id).ordered.last
|
|
270
|
+
v.version # => 4
|
|
271
|
+
v.deleted_in_original_table # => true
|
|
272
|
+
v.original_record_exists? # => false
|
|
273
|
+
|
|
274
|
+
Page.restore_deleted(id) # restore from the newest version
|
|
275
|
+
# or: Page.restore_deleted_version(id, 2) # restore from version 2
|
|
276
|
+
# or: v.restore
|
|
277
|
+
|
|
278
|
+
page = Page.find(id) # same id as before
|
|
279
|
+
page.version # => 5
|
|
280
|
+
page.find_version(5).record_restored? # => true
|
|
281
|
+
page.find_version(5).record_restored_from_version # => 4
|
|
282
|
+
```
|
|
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
|
+
|
|
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.
|
|
302
|
+
|
|
303
|
+
### 7. Saving without a new version
|
|
304
|
+
|
|
305
|
+
```ruby
|
|
306
|
+
page.title = 'Typo fix'
|
|
307
|
+
page.save_without_revision # saves, no new version
|
|
308
|
+
page.save_without_revision(false) # same, but skips validation
|
|
309
|
+
|
|
310
|
+
Page.without_revision do
|
|
311
|
+
Page.find(id).update!(title: 'Silent')
|
|
312
|
+
end
|
|
313
|
+
```
|
|
314
|
+
|
|
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`.
|
|
320
|
+
|
|
321
|
+
### 8. Options
|
|
322
|
+
|
|
323
|
+
```ruby
|
|
324
|
+
class Page < ApplicationRecord
|
|
325
|
+
acts_as_versioned \
|
|
326
|
+
table_name: :page_history, # default: pages_h
|
|
327
|
+
class_name: 'Revision', # default: Version -> Page::Revision
|
|
328
|
+
foreign_key: :page_id, # default: page_id
|
|
329
|
+
version_column: :version, # never use :lock_version!
|
|
330
|
+
non_versioned_columns: [:view_count], # these columns are not versioned
|
|
331
|
+
if_changed: [:title, :body], # new version only if one of these changed
|
|
332
|
+
if: :versionable?, # Symbol or Proc(record) -> true/false
|
|
333
|
+
limit: 50, # keep at most N versions (older ones are deleted!)
|
|
334
|
+
sequence_name: 'page_history_seq', # e.g. for Oracle
|
|
335
|
+
association_options: { order: 'version' },
|
|
336
|
+
extend: PageVersionHelpers # module mixed into Page and Page::Revision
|
|
337
|
+
end
|
|
338
|
+
```
|
|
339
|
+
|
|
340
|
+
Columns that are never versioned: primary key, `type` (STI), the version column,
|
|
341
|
+
`lock_version` and `versioned_type`. For STI models the `type` value is stored in
|
|
342
|
+
`versioned_type` (see `:inheritance_column`).
|
|
343
|
+
|
|
344
|
+
> ⚠️ `:limit` deletes old versions with `delete_all`. That also removes their deletion and
|
|
345
|
+
> restore information. In an audit setting you usually should not use it.
|
|
346
|
+
|
|
347
|
+
---
|
|
348
|
+
|
|
349
|
+
## Tests
|
|
350
|
+
|
|
351
|
+
```sh
|
|
352
|
+
bundle exec rake test
|
|
353
|
+
```
|
|
354
|
+
|
|
355
|
+
The tests run against SQLite (`test/database.yml`).
|
|
356
|
+
|
|
357
|
+
* `test/versioned_test.rb` – the original acts_as_versioned tests, adapted to the fork's behaviour
|
|
358
|
+
* `test/tiss_test.rb` – the fork features (destroy, restore, `record_restored`, `find_version`, …),
|
|
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`)
|
|
362
|
+
* `test/migration_test.rb` – `create_versioned_table`
|
|
363
|
+
|
|
364
|
+
## Release
|
|
365
|
+
|
|
366
|
+
```sh
|
|
367
|
+
rake build # updates the gemspec manifest and builds pkg/*.gem
|
|
368
|
+
rake release # commit, tag, push, gem push
|
|
369
|
+
```
|
|
370
|
+
|
|
371
|
+
## Resources
|
|
372
|
+
|
|
373
|
+
* RubyGems: https://rubygems.org/gems/rtiss_acts_as_versioned
|
|
374
|
+
* GitHub (fork): https://github.com/rtiss/rtiss_acts_as_versioned
|
|
375
|
+
* Original: https://github.com/technoweenie/acts_as_versioned
|
|
376
|
+
|
|
377
|
+
## License
|
|
378
|
+
|
|
379
|
+
MIT, see [MIT-LICENSE](MIT-LICENSE).
|
|
@@ -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.
|
|
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
|