summaryrefslogtreecommitdiff
path: root/app/models/deployment.rb
diff options
context:
space:
mode:
authorTimothy Andrew <mail@timothyandrew.net>2016-09-20 15:06:54 +0530
committerTimothy Andrew <mail@timothyandrew.net>2016-09-20 16:05:25 +0530
commit8957293d9bd0d711db3af26182205c2fe4125194 (patch)
treedcdd25393e2d6248971fddbd6c9eac30fe27ff6e /app/models/deployment.rb
parentfa890604aaf15b9e4f0199e6a4cff24c29955a37 (diff)
downloadgitlab-ce-8957293d9bd0d711db3af26182205c2fe4125194.tar.gz
Implement review comments from @yorickpeterse
1. Change multiple updates to a single `update_all` 2. Use cascading deletes 3. Extract an average function for the database median. 4. Move database median to `lib/gitlab/database` 5. Use `delete_all` instead of `destroy_all` 6. Minor refactoring
Diffstat (limited to 'app/models/deployment.rb')
-rw-r--r--app/models/deployment.rb31
1 files changed, 31 insertions, 0 deletions
diff --git a/app/models/deployment.rb b/app/models/deployment.rb
index 1e338889714..183d538a867 100644
--- a/app/models/deployment.rb
+++ b/app/models/deployment.rb
@@ -42,4 +42,35 @@ class Deployment < ActiveRecord::Base
project.repository.is_ancestor?(commit.id, sha)
end
+
+
+ def update_merge_request_metrics
+ if environment.update_merge_request_metrics?
+ query = project.merge_requests.
+ joins(:metrics).
+ where(target_branch: self.ref, merge_request_metrics: { first_deployed_to_production_at: nil })
+
+ merge_requests =
+ if previous_deployment
+ query.where("merge_request_metrics.merged_at <= ? AND merge_request_metrics.merged_at >= ?",
+ self.created_at,
+ previous_deployment.created_at)
+ else
+ query.where("merge_request_metrics.merged_at <= ?", self.created_at)
+ end
+
+ # Need to use `map` instead of `select` because MySQL doesn't allow `SELECT`ing from the same table
+ # that we're updating.
+ MergeRequest::Metrics.where(merge_request_id: merge_requests.map(&:id), first_deployed_to_production_at: nil).
+ update_all(first_deployed_to_production_at: self.created_at)
+ end
+ end
+
+ def previous_deployment
+ @previous_deployment ||=
+ project.deployments.joins(:environment).
+ where(environments: { name: self.environment.name }, ref: self.ref).
+ where.not(id: self.id).
+ take
+ end
end