Welcome to mirror list, hosted at ThFree Co, Russian Federation.

gitlab.com/gitlab-org/gitlab-foss.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorDouwe Maan <douwe@selenight.nl>2017-01-31 03:26:40 +0300
committerDouwe Maan <douwe@selenight.nl>2017-02-07 01:12:24 +0300
commitc8b63a28afa811881b617546fe94a19378585a04 (patch)
tree2b1bd2e9d52e059de0589a014f518f23e9b71ca3 /app/models/environment.rb
parent3aa1264dc6c0de3625bb1a2d6a0ee90140a2f519 (diff)
Improve performance of finding last deployed environment
Diffstat (limited to 'app/models/environment.rb')
-rw-r--r--app/models/environment.rb20
1 files changed, 7 insertions, 13 deletions
diff --git a/app/models/environment.rb b/app/models/environment.rb
index ed18e6bdea1..14787f79a36 100644
--- a/app/models/environment.rb
+++ b/app/models/environment.rb
@@ -6,7 +6,8 @@ class Environment < ActiveRecord::Base
belongs_to :project, required: true, validate: true
- has_many :deployments
+ has_many :deployments, dependent: :destroy
+ has_one :last_deployment, -> { order('deployments.id DESC') }, class_name: 'Deployment'
before_validation :nullify_external_url
before_validation :generate_slug, if: ->(env) { env.slug.blank? }
@@ -37,6 +38,7 @@ class Environment < ActiveRecord::Base
scope :available, -> { with_state(:available) }
scope :stopped, -> { with_state(:stopped) }
+ scope :order_by_last_deployed_at, -> { order(Gitlab::Database.nulls_first_order('(SELECT MAX(id) FROM deployments WHERE deployments.environment_id = environments.id)', 'ASC')) }
state_machine :state, initial: :available do
event :start do
@@ -51,14 +53,6 @@ class Environment < ActiveRecord::Base
state :stopped
end
- def self.latest_for_commit(environments, commit)
- environments.sort_by do |environment|
- deployment = environment.first_deployment_for(commit)
-
- deployment.try(:created_at) || DateTime.parse('1970-01-01')
- end.last
- end
-
def predefined_variables
[
{ key: 'CI_ENVIRONMENT_NAME', value: name, public: true },
@@ -70,10 +64,6 @@ class Environment < ActiveRecord::Base
ref.to_s == last_deployment.try(:ref)
end
- def last_deployment
- deployments.last
- end
-
def nullify_external_url
self.external_url = nil if self.external_url.blank?
end
@@ -95,6 +85,10 @@ class Environment < ActiveRecord::Base
last_deployment.includes_commit?(commit)
end
+ def last_deployed_at
+ last_deployment.try(:created_at)
+ end
+
def update_merge_request_metrics?
(environment_type || name) == "production"
end