Repository navigation
Bring async versions to parity with paper_trail 17 - #1
Conversation
The overrides enqueue a job instead of building a version, so they never returned the created version object the comments promised.
The workflow still triggered on the old `core` branch, so pushes and pull requests to main ran no checks. Adds a Ruby 3.4/4.0 matrix, bumps actions/checkout to v5 and runs specs through `rake spec`, which uses the Rakefile's spec pattern instead of a shell glob. Pins the local Ruby to 4.0.10 and drops `require "pry"` from the spec helper: pry is not in the Gemfile, so the require only worked where it happened to be installed globally.
There was a problem hiding this comment.
🟡 Changes recommended
Critical transaction-handling flaws can lose committed audit history or record rolled-back changes, while newly asynchronous models can lose association history.
4 open issues
What changed in this PR
Updates the gem’s background versioning integration to target PaperTrail 17 parity and prepares the 2.0.0 release.
Changes:
- Revises job payloads, timestamps, transaction callbacks, and async selection.
- Adds parity tests and simplifies RSpec helpers after removing
ar_after_transaction. - Updates dependencies, release documentation, and CI coverage for
main.
| File | Description |
|---|---|
spec/spec_helper.rb |
Removes the undeclared pry requirement. |
README.md |
Documents explicit async: option precedence. |
paper_trail-background.gemspec |
Requires PaperTrail 17 and removes ar_after_transaction. |
lib/paper_trail/background/version.rb |
Bumps the release version to 2.0.0. |
lib/paper_trail/background/rspec_helpers.rb |
Removes transaction-depth configuration from helpers. |
lib/paper_trail/background/parity_spec.rb |
Adds integration tests for async version behavior. |
lib/paper_trail/background.rb |
Updates async overrides, payloads, timestamps, and enqueue callbacks. |
lib/paper_trail/background_spec.rb |
Tests update metadata hooks and removes obsolete stubs. |
lib/paper_trail-background/rspec.rb |
Removes the old transaction-hook dependency. |
Gemfile |
Adds ActiveJob for specs. |
CHANGELOG.md |
Documents 2.0.0 changes and migration notes. |
.tool-versions |
Pins local Ruby to 4.0.10. |
.ruby-version |
Aligns the Ruby version pin. |
.github/workflows/testing-and-standardization.yml |
Targets main, adds a Ruby matrix, and runs rake spec. |
Review effort: Max (adaptive)
30c2605 to
2d84c14
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The integration suite lacks a delayed-job test with timestamp synchronization disabled, leaving regressions that use the record timestamp instead of the write time undetected.
1 open issue
4 resolved since last review
Review effort: Balanced (adaptive)
2d84c14 to
0c6ac1a
Compare
0c6ac1a to
671abc5
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Active Job’s commit deferral can still delay or discard version jobs due to unrelated transactions, and deleting records before queued updates run can cause per-model version limits to be ignored.
2 open issues
1 resolved since last review
Review effort: Max (adaptive)
671abc5 to
b013473
Compare
b013473 to
f7876d6
Compare
f7876d6 to
4eea3d1
Compare
There was a problem hiding this comment.
🟡 Changes recommended
Pending version jobs mishandle deletion policies and model/shard context, allowing deletion restrictions to be bypassed, old history to attach to reused IDs, and valid audit versions to be silently discarded.
4 open issues
4 resolved since last review
Review effort: Max (adaptive)
Requires paper_trail 17 and replaces ar_after_transaction with an `after_commit` on the record's current transaction. The job is enqueued when the record's own database commits, so a record created in a savepoint that rolls back no longer enqueues one, and a rollback on another database does not drop it. Inside a non-joinable transaction, such as the transactional-test wrapper, the job is enqueued when the record's savepoint is released, the moment Rails runs `after_commit` callbacks. `with_versioning` drops `expected_open_transactions:` along with the `normally_open_transactions` setup it fed. Changes to what the job writes: - The payload carries `item_id` and `item_type` instead of the record, so the version is still written when the record is gone before the job runs. `item_type` comes from `polymorphic_name`, as in sync versions. - Update versions take `created_at` from the record's `updated_at`, following `RecordTrail#build_version_on_update`. Destroy and `update_columns` versions take the enqueue time, not the job's run time. - `record_update` merges `data_for_update`, not `data_for_update_columns`. - `record_update_columns` no longer raises `NameError` on the undefined `force`; the spec double defined `force`, which hid it. The async overrides return nil instead of the enqueued job, so paper_trail-association_tracking finds no version to attach associations to. The README says async versions don't record them and points to `async: false`. Async selection goes through one `async?` check: an explicit `async:` on `has_paper_trail` wins, otherwise a model is async when `opt_in` is unset. Create and destroy now follow `opt_in` as update already did, and `async: false` keeps a model synchronous in every event. Adds a parity spec that runs the overrides against an in-memory SQLite schema and ActiveJob's test adapter.
Bumps the version to 2.0.0 and lists the paper_trail 17 changes in the changelog. The major bump covers the dropped ar_after_transaction dependency, the removed `expected_open_transactions:` argument and create/destroy now following `opt_in`.
Adds a README note that, with `opt_in` unset, every `has_paper_trail` model without an explicit `async:` writes its create, update, `update_columns` and destroy versions through `VersionJob`, so `record.versions` stays empty until the job runs. Adds a 2.0.0 changelog entry saying `with_versioning` needs each example to run in a non-joinable transaction, as Rails transactional fixtures and DatabaseCleaner's `:transaction` strategy do. Under a plain `ActiveRecord::Base.transaction` wrapper, the example's rollback drops the job before it writes a version.
4eea3d1 to
f2c6d58
Compare
There was a problem hiding this comment.
🟡 Changes recommended
The timestamp fallback populates created_at even when record_timestamps is false, diverging from synchronous behavior and needing a guard on the version class’s timestamp setting.
1 open issue
4 resolved since last review
Review effort: Max (adaptive)



This PR brings async versions to parity with paper_trail 17 and releases them as 2.0.0. The async overrides had drifted from what
PaperTrail::RecordTraildoes in sync mode, and two of them were broken:paper_trail.update_columnsraisedNameErroron an async model, and a record created in a rolled back savepoint still enqueued a version job.Async versions should look like the versions paper_trail would have written synchronously, with the same
item_type, the samecreated_atand the same metadata. Onmainthey didn't, and the CI workflow still triggered on the oldcorebranch, so none of this was being checked on pushes or pull requests.I replaced ar_after_transaction with
ActiveRecord.after_all_transactions_commitintrigger_write. The job now goes on the queue once the outermost transaction commits, non-joinable transactions (such as the transactional-test wrapper) don't count, and a rolled back savepoint drops the job.I changed what the job payload carries:
trigger_writesendsitem_idanditem_typeinstead of the record, so the version is still written when the record is deleted before the job runs.item_typecomes frompolymorphic_name, as in sync versions.record_updatetakescreated_atfrom the record'supdated_at, followingRecordTrail#build_version_on_update. Destroy andupdate_columnsversions take the enqueue time instead of the job's run time.record_updatemergesdata_for_updateinstead ofdata_for_update_columns.record_update_columnsno longer references the undefinedforce. The spec double definedforce, so the specs never hit theNameError.I routed all four
record_*overrides through a single privateasync?check. An explicitasync:onhas_paper_trailwins, otherwise a model is async whenopt_inis unset. Before this, create and destroy only checkedasync:, while update also looked atopt_in.with_versioningin the RSpec helpers dropsexpected_open_transactions:and theActiveRecord::Base.normally_open_transactionssetup it fed, since the native hook doesn't need it.lib/paper_trail/background.rb: the overrides,async?andtrigger_writedescribed above.lib/paper_trail/background/rspec_helpers.rbandlib/paper_trail-background/rspec.rb:with_versioningwithout ar_after_transaction.lib/paper_trail/background/parity_spec.rb: a new spec that runs the overrides against an in-memory SQLite schema and ActiveJob's test adapter, with 12 examples covering the payload, timestamps, transaction handling and async selection.lib/paper_trail/background_spec.rb: two examples that pin whichdata_for_*hook each update path merges, and the removedafter_transactionstubs.>= 17.0, < 18, ar_after_transaction removed, activejob added for the specs.mainwith a Ruby 3.4/4.0 matrix,actions/checkout@v5andrake spec. The local Ruby is pinned to 4.0.10, and the spec helper no longer requires pry, which isn't in the Gemfile.async:taking precedence overopt_in. I also dropped the stale@returncomments from the overrides, since they enqueue a job and never returned a version.