Skip to content

Bring async versions to parity with paper_trail 17 - #1

Merged
filippoliverani merged 5 commits into
mainfrom
paper-trail-17-parity
Nov 25, 2026
Merged

filippoliverani merged 5 commits into
mainfrom
paper-trail-17-parity

Conversation

@filippoliverani

@filippoliverani filippoliverani commented Nov 25, 2026 •

Copy link
Copy Markdown

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::RecordTrail does in sync mode, and two of them were broken: paper_trail.update_columns raised NameError on 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 same created_at and the same metadata. On main they didn't, and the CI workflow still triggered on the old core branch, so none of this was being checked on pushes or pull requests.

I replaced ar_after_transaction with ActiveRecord.after_all_transactions_commit in trigger_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_write sends item_id and item_type instead of the record, so the version is still written when the record is deleted before the job runs. item_type comes from polymorphic_name, as in sync versions.
  • record_update takes created_at from the record's updated_at, following RecordTrail#build_version_on_update. Destroy and update_columns versions take the enqueue time instead of the job's run time.
  • record_update merges data_for_update instead of data_for_update_columns.
  • record_update_columns no longer references the undefined force. The spec double defined force, so the specs never hit the NameError.

I routed all four record_* overrides through a single private async? check. An explicit async: on has_paper_trail wins, otherwise a model is async when opt_in is unset. Before this, create and destroy only checked async:, while update also looked at opt_in.

with_versioning in the RSpec helpers drops expected_open_transactions: and the ActiveRecord::Base.normally_open_transactions setup it fed, since the native hook doesn't need it.

  • lib/paper_trail/background.rb: the overrides, async? and trigger_write described above.
  • lib/paper_trail/background/rspec_helpers.rb and lib/paper_trail-background/rspec.rb: with_versioning without 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 which data_for_* hook each update path merges, and the removed after_transaction stubs.
  • Gemspec and Gemfile: paper_trail >= 17.0, < 18, ar_after_transaction removed, activejob added for the specs.
  • CI: the workflow runs on main with a Ruby 3.4/4.0 matrix, actions/checkout@v5 and rake spec. The local Ruby is pinned to 4.0.10, and the spec helper no longer requires pry, which isn't in the Gemfile.
  • Release: version 2.0.0, a CHANGELOG entry and a README note on async: taking precedence over opt_in. I also dropped the stale @return comments from the overrides, since they enqueue a job and never returned a version.

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.
@filippoliverani filippoliverani self-assigned this Nov 25, 2026
@filippoliverani
filippoliverani requested an adaptive review from Copilot November 25, 2026 10:07
@filippoliverani
filippoliverani marked this pull request as ready for review November 25, 2026 10:08

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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)

Comment thread lib/paper_trail/background.rb Resolved
Comment thread lib/paper_trail/background.rb Resolved
Comment thread lib/paper_trail/background.rb Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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)

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

The opt_in-off test covers only creation, leaving destroy-version queuing and writing after job execution untested and vulnerable to regression.

1 open issue
1 resolved since last review

Review effort: Balanced (adaptive)

@filippoliverani
filippoliverani requested an adaptive review from Copilot November 25, 2026 12:56
@filippoliverani filippoliverani changed the title Bring async versions to parity with paper_trail 17 (2.0.0) Bring async versions to parity with paper_trail 17 Nov 25, 2026

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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)

Comment thread lib/paper_trail/background.rb Resolved
Comment thread lib/paper_trail/background.rb Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Asynchronous version jobs can reorder record history, recreate versions contrary to deletion policies, and omit required scoped association attributes.

3 open issues
2 resolved since last review

Review effort: Max (adaptive)

Comment thread lib/paper_trail/background.rb Resolved
Comment thread lib/paper_trail/background.rb Resolved
Comment thread lib/paper_trail/background.rb Resolved

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 Changes recommended

Legacy queued payloads can crash before version creation, and the non-atomic existence check can recreate history after a committed purge.

4 open issues
3 resolved since last review

Review effort: Max (adaptive)

Comment thread lib/paper_trail/background/job.rb Resolved
Comment thread lib/paper_trail/background/job.rb Resolved
Comment thread lib/paper_trail/background/job.rb Resolved
Comment thread lib/paper_trail/background/job.rb Resolved
@filippoliverani
filippoliverani requested an adaptive review from Copilot November 25, 2026 14:37

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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)

Comment thread lib/paper_trail/background/job.rb Resolved
Comment thread lib/paper_trail/background/job.rb Resolved
Comment thread lib/paper_trail/background/job.rb Resolved
Comment thread lib/paper_trail/background.rb Resolved
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.

Copilot AI left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟡 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)

Comment thread lib/paper_trail/background.rb Resolved
@filippoliverani
filippoliverani merged commit bb55b86 into main Nov 25, 2026
3 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants