summaryrefslogtreecommitdiff
path: root/app/models/deployment_merge_request.rb
blob: 64a578e16bf499fab93be4328b6a0c91daa06b1d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
# frozen_string_literal: true

class DeploymentMergeRequest < ApplicationRecord
  belongs_to :deployment, optional: false
  belongs_to :merge_request, optional: false

  def self.join_deployments_for_merge_requests
    joins(deployment: :environment)
      .where('deployment_merge_requests.merge_request_id = merge_requests.id')
  end

  def self.by_deployment_id(id)
    where('deployments.id = ?', id)
  end

  def self.deployed_to(name)
    # We filter by project ID again so the query uses the index on
    # (project_id, name), instead of using the index on
    # (name varchar_pattern_ops). This results in better performance on
    # GitLab.com.
    where('environments.name = ?', name)
      .where('environments.project_id = merge_requests.target_project_id')
  end

  def self.deployed_after(time)
    where('deployments.finished_at > ?', time)
  end

  def self.deployed_before(time)
    where('deployments.finished_at < ?', time)
  end
end