Skip to content

Ensure that no audit-related queries run when auditing is disabled #755

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

rafaelsales
Copy link

@rafaelsales rafaelsales commented Mar 10, 2025

Problem

Calling Models::ActiveRecord::User.without_auditing { user.touch } generates the query below:

SELECT "audits".* FROM "audits" WHERE "audits"."auditable_id" = ? AND "audits"."auditable_type" = ? ORDER BY "audits"."version" DESC LIMIT ?

Culprit:

def audit_touch
unless (changes = audited_changes(for_touch: true, exclude_readonly_attrs: true)).empty?

Why this is a big deal

In an endpoint that requires high performance, I need to perform record.touch(:connected_at) along with some other audited record updates and make sure we're not running any extra queries.

Solution

The current code checks for auditing_enabled too deep in the callback chain. Instead, we should use guards to return in each callback as early as possible.

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.

1 participant