"Cross-database data modification" spec failures with eager loading disabled

Problem

Spec fail when eager loading is disabled with:

Failures:

  1) Issues::BuildService for a single discussion #execute references the noteable title in the issue title
     Failure/Error:
       raise Database::PreventCrossDatabaseModification::CrossDatabaseModificationAcrossUnsupportedTablesError,
         "Cross-database data modification of '#{schemas.to_a.join(", ")}' were detected within " \
         "a transaction modifying the '#{all_tables.to_a.join(", ")}' tables." \
         "Please refer to https://docs.gitlab.com/ee/development/database/multiple_databases.html#removing-cross-database-transactions for details on how to resolve this exception."

     Database::PreventCrossDatabaseModification::CrossDatabaseModificationAcrossUnsupportedTablesError:
       Cross-database data modification of 'undefined, gitlab_main' were detected within a transaction modifying the 'internal_ids, merge_requests' tables.Please refer to https://docs.gitlab.com/ee/development/database/multiple_databases.html#removing-cross-database-transactions for details on how to resolve this exception.
     # ./spec/support/database/prevent_cross_database_modification.rb:95:in `prevent_cross_database_modification!'
     # ./spec/support/database/prevent_cross_database_modification.rb:24:in `block in with_cross_database_modification_prevented'
     # ./lib/gitlab/database/load_balancing/connection_proxy.rb:119:in `block in write_using_load_balancer'
     # ./lib/gitlab/database/load_balancing/load_balancer.rb:107:in `block in read_write'
     # ./lib/gitlab/database/load_balancing/load_balancer.rb:167:in `retry_with_backoff'
     # ./lib/gitlab/database/load_balancing/load_balancer.rb:105:in `read_write'
     # ./lib/gitlab/database/load_balancing/connection_proxy.rb:118:in `write_using_load_balancer'
     # ./lib/gitlab/database/load_balancing/connection_proxy.rb:60:in `block (2 levels) in <class:ConnectionProxy>'
     # ./lib/gitlab/database/load_balancing/connection_proxy.rb:119:in `block in write_using_load_balancer'
     # ./lib/gitlab/database/load_balancing/load_balancer.rb:107:in `block in read_write'
     # ./lib/gitlab/database/load_balancing/load_balancer.rb:167:in `retry_with_backoff'
     # ./lib/gitlab/database/load_balancing/load_balancer.rb:105:in `read_write'
     # ./lib/gitlab/database/load_balancing/connection_proxy.rb:118:in `write_using_load_balancer'
     # ./lib/gitlab/database/load_balancing/connection_proxy.rb:70:in `transaction'
     # ./spec/services/issues/build_service_spec.rb:25:in `block (4 levels) in <top (required)>'
     # ./spec/services/issues/build_service_spec.rb:28:in `block (4 levels) in <top (required)>'
     # ./spec/services/issues/build_service_spec.rb:31:in `block (4 levels) in <top (required)>'
     # ./spec/spec_helper.rb:408:in `block (3 levels) in <top (required)>'
     # ./spec/support/sidekiq_middleware.rb:9:in `with_sidekiq_server_middleware'
     # ./spec/spec_helper.rb:399:in `block (2 levels) in <top (required)>'
     # ./spec/spec_helper.rb:395:in `block (3 levels) in <top (required)>'
     # ./lib/gitlab/application_context.rb:31:in `with_raw_context'
     # ./spec/spec_helper.rb:395:in `block (2 levels) in <top (required)>'
     # ./spec/support/database/prevent_cross_joins.rb:102:in `block (3 levels) in <main>'
     # ./spec/support/database/prevent_cross_joins.rb:56:in `with_cross_joins_prevented'
     # ./spec/support/database/prevent_cross_joins.rb:102:in `block (2 levels) in <main>'

Finished in 13.61 seconds (files took 14.48 seconds to load)
1 example, 1 failure

Failed examples:

rspec ./spec/services/issues/build_service_spec.rb:30 # Issues::BuildService for a single discussion #execute references the noteable title in the issue title

Reproduction

# Run with eager loading disabled
GITLAB_TEST_EAGER_LOAD=false bin/rspec ./spec/services/issues/build_service_spec.rb:30

# fails with error message above

Counter example

# Run with eager loading _enabled_
GITLAB_TEST_EAGER_LOAD=true bin/rspec ./spec/services/issues/build_service_spec.rb:30

1 example, 0 failures

Why?

gitlab_schema seems to be undefined for some of the models.

Edited by Peter Leitzen