CrossDatabaseModificationAcrossUnsupportedTablesError false positives in Capybara

Everyone can contribute. Help move this issue forward while earning points, leveling up and collecting rewards.

Related to #498053 (comment 2153761215) we found that the middleware at https://gitlab.com/gitlab-org/gitlab/-/blob/b53433e22070dedf30f93ad915cac5f1192f9928/lib/gitlab/database/query_analyzers/prevent_cross_database_modification.rb#L127 might have some edge cases where it is not cleared in between requests and we are misattributing cross-DB transactions to queries that are not in a transaction.

One proposal in that thread from @tkuah is to clear the transaction stack in rack middleware (at least for feature specs).

It's likely this particular bug could be causing many different flaky specs especially in feature specs.

Action items

  • In lib/gitlab/database/query_analyzers/prevent_cross_database_modification.rb, likely can clear ApplicationRecord.gitlab_transactions_stack.clear in the self.begin! method. As we call begin! as part of the QueryAnalyzer middleware.
Edited by 🤖 GitLab Bot 🤖