Avoid cross joins in environment model

After removing models/environment_spec.rb from cross-join-allowlist.yml below failures are observed.

This test run was performed on top of the fix for #338006 (closed) as discussed in !68870 (comment 669755102)

Failures:

  1) Environment#cancel_deployment_jobs! cancels an active deployment job
     Failure/Error:
       raise CrossJoinAcrossUnsupportedTablesError,
         "Unsupported cross-join across '#{tables.join(", ")}' modifying '#{schemas.to_a.join(", ")}' discovered " \
         "when executing query '#{sql}'"

     Database::PreventCrossJoins::CrossJoinAcrossUnsupportedTablesError:
       Unsupported cross-join across 'deployments, ci_builds' modifying 'gitlab_main, gitlab_ci' discovered when executing query 'SELECT "deployments"."id", "deployments"."iid", "deployments"."project_id", "deployments"."environment_id", "deployments"."ref", "deployments"."tag", "deployments"."sha", "deployments"."user_id", "deployments"."deployable_type", "deployments"."created_at", "deployments"."updated_at", "deployments"."on_stop", "deployments"."status", "deployments"."finished_at", "deployments"."cluster_id", "deployments"."deployable_id" FROM "deployments" INNER JOIN ci_builds ON ci_builds.id = deployments.deployable_id WHERE "deployments"."environment_id" = 334 AND "deployments"."status" IN (0, 1) /*application:test,correlation_id:9b44b9bff69d006b3066c1033e260547*/'
     # ./spec/support/database/prevent_cross_joins.rb:48:in `validate_cross_joins!'
     # ./spec/support/database/prevent_cross_joins.rb:57:in `block in with_cross_joins_prevented'
     # ./app/models/environment.rb:291:in `cancel_deployment_jobs!'
     # ./spec/models/environment_spec.rb:1598:in `block (3 levels) in <top (required)>'
     # ./spec/models/environment_spec.rb:1607:in `block (3 levels) in <top (required)>'
     # ./spec/spec_helper.rb:392:in `block (3 levels) in <top (required)>'
     # ./spec/support/sidekiq_middleware.rb:9:in `with_sidekiq_server_middleware'
     # ./spec/spec_helper.rb:383:in `block (2 levels) in <top (required)>'
     # ./spec/spec_helper.rb:379:in `block (3 levels) in <top (required)>'
     # ./lib/gitlab/application_context.rb:31:in `with_raw_context'
     # ./spec/spec_helper.rb:379:in `block (2 levels) in <top (required)>'
     # ./spec/support/database/prevent_cross_joins.rb:95:in `block (3 levels) in <main>'
     # ./spec/support/database/prevent_cross_joins.rb:62:in `with_cross_joins_prevented'
     # ./spec/support/database/prevent_cross_joins.rb:95:in `block (2 levels) in <main>'
     # ./spec/support/caching.rb:8:in `block (2 levels) in <main>'

  2) Environment#cancel_deployment_jobs! when deployable does not exist does not raise an error
     Failure/Error: expect { subject }.not_to raise_error

       expected no Exception, got #<Database::PreventCrossJoins::CrossJoinAcrossUnsupportedTablesError: Unsupported cross-join across '...ployments"."status" IN (0, 1) /*application:test,correlation_id:57c32c9489e5727b6a391b32541f0320*/'> with backtrace:
         # ./spec/support/database/prevent_cross_joins.rb:48:in `validate_cross_joins!'
         # ./spec/support/database/prevent_cross_joins.rb:57:in `block in with_cross_joins_prevented'
         # ./app/models/environment.rb:291:in `cancel_deployment_jobs!'
         # ./spec/models/environment_spec.rb:1598:in `block (3 levels) in <top (required)>'
         # ./spec/models/environment_spec.rb:1618:in `block (5 levels) in <top (required)>'
         # ./spec/models/environment_spec.rb:1618:in `block (4 levels) in <top (required)>'
         # ./spec/spec_helper.rb:392:in `block (3 levels) in <top (required)>'
         # ./spec/support/sidekiq_middleware.rb:9:in `with_sidekiq_server_middleware'
         # ./spec/spec_helper.rb:383:in `block (2 levels) in <top (required)>'
         # ./spec/spec_helper.rb:379:in `block (3 levels) in <top (required)>'
         # ./lib/gitlab/application_context.rb:31:in `with_raw_context'
         # ./spec/spec_helper.rb:379:in `block (2 levels) in <top (required)>'
         # ./spec/support/database/prevent_cross_joins.rb:95:in `block (3 levels) in <main>'
         # ./spec/support/database/prevent_cross_joins.rb:62:in `with_cross_joins_prevented'
         # ./spec/support/database/prevent_cross_joins.rb:95:in `block (2 levels) in <main>'
         # ./spec/support/caching.rb:8:in `block (2 levels) in <main>'
     # ./spec/models/environment_spec.rb:1618:in `block (4 levels) in <top (required)>'
     # ./spec/spec_helper.rb:392:in `block (3 levels) in <top (required)>'
     # ./spec/support/sidekiq_middleware.rb:9:in `with_sidekiq_server_middleware'
     # ./spec/spec_helper.rb:383:in `block (2 levels) in <top (required)>'
     # ./spec/spec_helper.rb:379:in `block (3 levels) in <top (required)>'
     # ./lib/gitlab/application_context.rb:31:in `with_raw_context'
     # ./spec/spec_helper.rb:379:in `block (2 levels) in <top (required)>'
     # ./spec/support/database/prevent_cross_joins.rb:95:in `block (3 levels) in <main>'
     # ./spec/support/database/prevent_cross_joins.rb:62:in `with_cross_joins_prevented'
     # ./spec/support/database/prevent_cross_joins.rb:95:in `block (2 levels) in <main>'
     # ./spec/support/caching.rb:8:in `block (2 levels) in <main>'

Finished in 1 minute 34.94 seconds (files took 51.3 seconds to load)
211 examples, 2 failures

Failed examples:

rspec ./spec/models/environment_spec.rb:1606 # Environment#cancel_deployment_jobs! cancels an active deployment job
rspec ./spec/models/environment_spec.rb:1617 # Environment#cancel_deployment_jobs! when deployable does not exist does not raise an error

Pipeline failure link - https://gitlab.com/gitlab-org/gitlab/-/jobs/1566030833

Edited by Bala Kumar