Skip to content

Refresh authorizations only after commit in factories

Manoj M J requested to merge mmj-after-commit into master

What does this MR do and why?

This problem came to light in !119792 (comment 1515899125)

In project and project_group_link factories, we refresh authroizations of the project after the record is created.

In specs, we do not use the services (Projects::CreateService/ Projects::GroupLinks::CreateService) to create objects, but rather, we just use create(:project) or create(:project_groupLink). Naturally, the authorizations refresh execution we have defined inside the service objects aren't called when we do this, so we have to refresh authorizations within the factory itself, by manually running the authorizations refresh service.

For this, we depend on the after(:create) callback of FactoryBot.

While using this callback in itself is not a problem, the problem comes to light when we try to enqueue a sidekiq job from within this callback, as we are still within a transaction here.

We do not allow Sidekiq jobs to be enqueued from within a transaction, so if we do that within this callback in FactoryBot, it throws the error we see in !119792 (comment 1515899125).

So, before the change in !119792 (merged) can be made, we need to move authorization refreshes within after(:create) callbacks to run after the transaction, using AfterCommitQueue, which is what this MR does.

This is the right thing to do anyway, as in production, we run refreshes only after commit and the same behaviour should ideally reflect in specs too.

Screenshots or screen recordings

Screenshots are required for UI changes, and strongly recommended for all other merge requests.

Before After

How to set up and validate locally

Numbered steps to set up and validate the change are strongly suggested.

MR acceptance checklist

This checklist encourages us to confirm any changes have been analyzed to reduce risks in quality, performance, reliability, security, and maintainability.

Edited by Manoj M J

Merge request reports