Commit 9c811566 authored by Shinya Maeda's avatar Shinya Maeda

Revert add action column changes

parent 4eeb51dc
...@@ -6,7 +6,7 @@ class Projects::DeploymentsController < Projects::ApplicationController ...@@ -6,7 +6,7 @@ class Projects::DeploymentsController < Projects::ApplicationController
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def index def index
deployments = environment.deployments.deployed.reorder(created_at: :desc) deployments = environment.deployments.reorder(created_at: :desc)
deployments = deployments.where('created_at > ?', params[:after].to_time) if params[:after]&.to_time deployments = deployments.where('created_at > ?', params[:after].to_time) if params[:after]&.to_time
render json: { deployments: DeploymentSerializer.new(project: project) render json: { deployments: DeploymentSerializer.new(project: project)
...@@ -47,7 +47,7 @@ class Projects::DeploymentsController < Projects::ApplicationController ...@@ -47,7 +47,7 @@ class Projects::DeploymentsController < Projects::ApplicationController
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def deployment def deployment
@deployment ||= environment.deployments.deployed.find_by(iid: params[:id]) @deployment ||= environment.deployments.find_by(iid: params[:id])
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
......
...@@ -58,7 +58,7 @@ class Projects::EnvironmentsController < Projects::ApplicationController ...@@ -58,7 +58,7 @@ class Projects::EnvironmentsController < Projects::ApplicationController
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def show def show
@deployments = environment.deployments.deployed.order(id: :desc).page(params[:page]) @deployments = environment.deployments.order(id: :desc).page(params[:page])
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
......
...@@ -9,7 +9,7 @@ class EnvironmentsFinder ...@@ -9,7 +9,7 @@ class EnvironmentsFinder
# rubocop: disable CodeReuse/ActiveRecord # rubocop: disable CodeReuse/ActiveRecord
def execute def execute
deployments = project.deployments.deployed deployments = project.deployments.success
deployments = deployments =
if ref if ref
deployments_query = params[:with_tags] ? 'ref = :ref OR tag IS TRUE' : 'ref = :ref' deployments_query = params[:with_tags] ? 'ref = :ref OR tag IS TRUE' : 'ref = :ref'
......
...@@ -7,7 +7,7 @@ module Deployable ...@@ -7,7 +7,7 @@ module Deployable
after_create :create_deployment after_create :create_deployment
def create_deployment def create_deployment
return unless has_environment? && !has_deployment? return unless starts_environment? && !has_deployment?
environment = project.environments.find_or_create_by( environment = project.environments.find_or_create_by(
name: expanded_environment_name name: expanded_environment_name
...@@ -21,8 +21,7 @@ module Deployable ...@@ -21,8 +21,7 @@ module Deployable
sha: sha, sha: sha,
user: user, user: user,
deployable: self, deployable: self,
on_stop: on_stop, on_stop: on_stop).tap do |_|
action: environment_action).tap do |_|
self.reload # Reload relationships self.reload # Reload relationships
end end
end end
......
...@@ -10,7 +10,9 @@ class Deployment < ActiveRecord::Base ...@@ -10,7 +10,9 @@ class Deployment < ActiveRecord::Base
belongs_to :user belongs_to :user
belongs_to :deployable, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations belongs_to :deployable, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations
has_internal_id :iid, scope: :project, init: ->(s) { s&.project&.deployments&.maximum(:iid) } has_internal_id :iid, scope: :project, init: ->(s) do
Deployment.where(project: s.project).maximum(:iid) if s&.project
end
validates :sha, presence: true validates :sha, presence: true
validates :ref, presence: true validates :ref, presence: true
...@@ -18,8 +20,6 @@ class Deployment < ActiveRecord::Base ...@@ -18,8 +20,6 @@ class Deployment < ActiveRecord::Base
delegate :name, to: :environment, prefix: true delegate :name, to: :environment, prefix: true
scope :for_environment, -> (environment) { where(environment_id: environment) } scope :for_environment, -> (environment) { where(environment_id: environment) }
scope :deployed, -> { success.start }
scope :stopped, -> { success.stop }
state_machine :status, initial: :created do state_machine :status, initial: :created do
event :run do event :run do
...@@ -57,11 +57,6 @@ class Deployment < ActiveRecord::Base ...@@ -57,11 +57,6 @@ class Deployment < ActiveRecord::Base
canceled: 4 canceled: 4
} }
enum action: {
start: 1,
stop: 2
}
def self.last_for_environment(environment) def self.last_for_environment(environment)
ids = self ids = self
.for_environment(environment) .for_environment(environment)
...@@ -137,7 +132,7 @@ class Deployment < ActiveRecord::Base ...@@ -137,7 +132,7 @@ class Deployment < ActiveRecord::Base
def previous_deployment def previous_deployment
@previous_deployment ||= @previous_deployment ||=
project.deployments.deployed.joins(:environment) project.deployments.success.joins(:environment)
.where(environments: { name: self.environment.name }, ref: self.ref) .where(environments: { name: self.environment.name }, ref: self.ref)
.where.not(id: self.id) .where.not(id: self.id)
.take .take
...@@ -182,14 +177,6 @@ class Deployment < ActiveRecord::Base ...@@ -182,14 +177,6 @@ class Deployment < ActiveRecord::Base
metrics&.merge(deployment_time: finished_at.to_i) || {} metrics&.merge(deployment_time: finished_at.to_i) || {}
end end
def deployed?
success? && start?
end
def stopped?
success? && stop?
end
private private
def prometheus_adapter def prometheus_adapter
......
...@@ -8,9 +8,9 @@ class Environment < ActiveRecord::Base ...@@ -8,9 +8,9 @@ class Environment < ActiveRecord::Base
belongs_to :project, required: true belongs_to :project, required: true
has_many :deployments, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent has_many :deployments, -> { success }, dependent: :destroy # rubocop:disable Cop/ActiveRecordDependent
has_one :last_deployment, -> { deployed.order('deployments.id DESC') }, class_name: 'Deployment' has_one :last_deployment, -> { success.order('deployments.id DESC') }, class_name: 'Deployment'
before_validation :nullify_external_url before_validation :nullify_external_url
before_validation :generate_slug, if: ->(env) { env.slug.blank? } before_validation :generate_slug, if: ->(env) { env.slug.blank? }
...@@ -50,7 +50,7 @@ class Environment < ActiveRecord::Base ...@@ -50,7 +50,7 @@ class Environment < ActiveRecord::Base
scope :in_review_folder, -> { where(environment_type: "review") } scope :in_review_folder, -> { where(environment_type: "review") }
scope :for_name, -> (name) { where(name: name) } scope :for_name, -> (name) { where(name: name) }
scope :for_project, -> (project) { where(project_id: project) } scope :for_project, -> (project) { where(project_id: project) }
scope :with_deployment, -> (sha) { where('EXISTS (?)', Deployment.select(1).start.where('deployments.environment_id = environments.id').where(sha: sha)) } scope :with_deployment, -> (sha) { where('EXISTS (?)', Deployment.select(1).where('deployments.environment_id = environments.id').where(sha: sha)) }
state_machine :state, initial: :available do state_machine :state, initial: :available do
event :start do event :start do
...@@ -103,6 +103,15 @@ class Environment < ActiveRecord::Base ...@@ -103,6 +103,15 @@ class Environment < ActiveRecord::Base
folder_name == "production" folder_name == "production"
end end
def first_deployment_for(commit_sha)
ref = project.repository.ref_name_for_sha(ref_path, commit_sha)
return nil unless ref
deployment_iid = ref.split('/').last
deployments.find_by(iid: deployment_iid)
end
def ref_path def ref_path
"refs/#{Repository::REF_ENVIRONMENTS}/#{slug}" "refs/#{Repository::REF_ENVIRONMENTS}/#{slug}"
end end
......
...@@ -28,7 +28,7 @@ class EnvironmentStatus ...@@ -28,7 +28,7 @@ class EnvironmentStatus
def deployment def deployment
strong_memoize(:deployment) do strong_memoize(:deployment) do
environment.deployments.start.find_by_sha(sha) Deployment.where(environment: environment).find_by_sha(sha)
end end
end end
......
...@@ -254,7 +254,7 @@ class Project < ActiveRecord::Base ...@@ -254,7 +254,7 @@ class Project < ActiveRecord::Base
has_many :variables, class_name: 'Ci::Variable' has_many :variables, class_name: 'Ci::Variable'
has_many :triggers, class_name: 'Ci::Trigger' has_many :triggers, class_name: 'Ci::Trigger'
has_many :environments has_many :environments
has_many :deployments has_many :deployments, -> { success }
has_many :pipeline_schedules, class_name: 'Ci::PipelineSchedule' has_many :pipeline_schedules, class_name: 'Ci::PipelineSchedule'
has_many :project_deploy_tokens has_many :project_deploy_tokens
has_many :deploy_tokens, through: :project_deploy_tokens has_many :deploy_tokens, through: :project_deploy_tokens
......
# frozen_string_literal: true
class StopEnvironmentService
attr_reader :deployment
delegate :environment, to: :deployment
def initialize(deployment)
@deployment = deployment
end
def execute
return unless deployment.stopped?
environment.fire_state_event(:stop)
environment.expire_etag_cache
end
end
# frozen_string_literal: true # frozen_string_literal: true
class StartEnvironmentService class UpdateDeploymentService
attr_reader :deployment attr_reader :deployment
attr_reader :deployable attr_reader :deployable
...@@ -13,8 +13,6 @@ class StartEnvironmentService ...@@ -13,8 +13,6 @@ class StartEnvironmentService
end end
def execute def execute
return unless deployment.deployed?
deployment.create_ref deployment.create_ref
deployment.invalidate_cache deployment.invalidate_cache
...@@ -22,9 +20,10 @@ class StartEnvironmentService ...@@ -22,9 +20,10 @@ class StartEnvironmentService
environment.external_url = expanded_environment_url if environment.external_url = expanded_environment_url if
expanded_environment_url expanded_environment_url
environment.fire_state_event(:start) environment.fire_state_event(action)
break unless environment.save break unless environment.save
break if environment.stopped?
deployment.tap(&:update_merge_request_metrics!) deployment.tap(&:update_merge_request_metrics!)
end end
...@@ -47,4 +46,8 @@ class StartEnvironmentService ...@@ -47,4 +46,8 @@ class StartEnvironmentService
def environment_url def environment_url
environment_options[:url] environment_options[:url]
end end
def action
environment_options[:action] || 'start'
end
end end
...@@ -10,6 +10,7 @@ class BuildSuccessWorker ...@@ -10,6 +10,7 @@ class BuildSuccessWorker
def perform(build_id) def perform(build_id)
Ci::Build.find_by(id: build_id).try do |build| Ci::Build.find_by(id: build_id).try do |build|
create_deployment(build) if build.has_environment? create_deployment(build) if build.has_environment?
stop_environment(build) if build.stops_environment?
end end
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
...@@ -26,4 +27,10 @@ class BuildSuccessWorker ...@@ -26,4 +27,10 @@ class BuildSuccessWorker
deployment.succeed deployment.succeed
end end
end end
##
# TODO: This should be processed in DeploymentSuccessWorker once we started storing `action` value in `deployments` records
def stop_environment(build)
build.persisted_environment.fire_state_event(:stop)
end
end end
...@@ -8,11 +8,9 @@ module Deployments ...@@ -8,11 +8,9 @@ module Deployments
def perform(deployment_id) def perform(deployment_id)
Deployment.find_by_id(deployment_id).try do |deployment| Deployment.find_by_id(deployment_id).try do |deployment|
if deployment.deployed? break unless deployment.success?
StartEnvironmentService.new(deployment).execute
elsif deployment.stopped? UpdateDeploymentService.new(deployment).execute
StopEnvironmentService.new(deployment).execute
end
end end
end end
end end
......
...@@ -43,14 +43,12 @@ class Gitlab::Seeder::Pipelines ...@@ -43,14 +43,12 @@ class Gitlab::Seeder::Pipelines
# deploy stage # deploy stage
{ name: 'staging', stage: 'deploy', environment: 'staging', status_event: :success, { name: 'staging', stage: 'deploy', environment: 'staging', status_event: :success,
options: { environment: { name: 'staging', action: 'start', on_stop: 'stop staging' } }, options: { environment: { action: 'start', on_stop: 'stop staging' } },
queued_at: 7.hour.ago, started_at: 6.hour.ago, finished_at: 4.hour.ago }, queued_at: 7.hour.ago, started_at: 6.hour.ago, finished_at: 4.hour.ago },
{ name: 'stop staging', stage: 'deploy', environment: 'staging', { name: 'stop staging', stage: 'deploy', environment: 'staging',
when: 'manual', status: :skipped, when: 'manual', status: :skipped },
options: { environment: { name: 'staging', action: 'stop' } } },
{ name: 'production', stage: 'deploy', environment: 'production', { name: 'production', stage: 'deploy', environment: 'production',
when: 'manual', status: :skipped, when: 'manual', status: :skipped },
options: { environment: { name: 'production' } } },
# notify stage # notify stage
{ name: 'slack', stage: 'notify', when: 'manual', status: :success }, { name: 'slack', stage: 'notify', when: 'manual', status: :success },
......
...@@ -45,18 +45,14 @@ class Gitlab::Seeder::Environments ...@@ -45,18 +45,14 @@ class Gitlab::Seeder::Environments
end end
def create_deployment!(project, name, ref, sha) def create_deployment!(project, name, ref, sha)
find_deployable(project, name).try do |deployable|
environment = find_or_create_environment!(project, name) environment = find_or_create_environment!(project, name)
environment.deployments.create!( environment.deployments.create!(
project: project, project: project,
ref: ref, ref: ref,
sha: sha, sha: sha,
tag: false, tag: false,
deployable: deployable deployable: find_deployable(project, name)
).tap do |deployment| )
deployment.succeed!
end
end
end end
def find_or_create_environment!(project, name) def find_or_create_environment!(project, name)
......
# frozen_string_literal: true
class AddActionToDeployments < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DEPLOYMENT_ACTION_START = 1 # Equivalent to Deployment.actions['start']
DOWNTIME = false
disable_ddl_transaction!
def up
add_column_with_default(:deployments,
:action,
:integer,
limit: 2,
default: DEPLOYMENT_ACTION_START,
allow_null: false)
end
def down
remove_column(:deployments, :action)
end
end
...@@ -4,27 +4,26 @@ class AddIndexToDeployments < ActiveRecord::Migration ...@@ -4,27 +4,26 @@ class AddIndexToDeployments < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers include Gitlab::Database::MigrationHelpers
DOWNTIME = false DOWNTIME = false
SHORT_INDEX_NAME_FOR_ENVIRONMENT_ID = 'index_deployments_on_env_deployed_for_id'
SHORT_INDEX_NAME_FOR_ENVIRONMENT_IID = 'index_deployments_on_env_deployed_for_iid'
SHORT_INDEX_NAME_FOR_PROJECT_FINISHED_AT = 'index_deployments_on_prj_deployed_for_finished_at'
disable_ddl_transaction! disable_ddl_transaction!
def up def up
add_concurrent_index :deployments, [:project_id, :action, :status, :id] remove_concurrent_index :deployments, [:project_id, :status]
add_concurrent_index :deployments, [:project_id, :action, :status, :iid] remove_concurrent_index :deployments, [:environment_id, :status]
add_concurrent_index :deployments, [:project_id, :action, :status, :finished_at], name: SHORT_INDEX_NAME_FOR_PROJECT_FINISHED_AT add_concurrent_index :deployments, [:project_id, :status, :id]
add_concurrent_index :deployments, [:environment_id, :action, :status, :id], name: SHORT_INDEX_NAME_FOR_ENVIRONMENT_ID add_concurrent_index :deployments, [:project_id, :status, :iid]
add_concurrent_index :deployments, [:environment_id, :action, :status, :iid], name: SHORT_INDEX_NAME_FOR_ENVIRONMENT_IID add_concurrent_index :deployments, [:environment_id, :status, :id]
add_concurrent_index :deployments, [:environment_id, :action, :sha] add_concurrent_index :deployments, [:environment_id, :status, :iid]
add_concurrent_index :deployments, [:environment_id, :sha]
end end
def down def down
remove_concurrent_index :deployments, [:project_id, :action, :status, :id] add_concurrent_index :deployments, [:project_id, :status]
remove_concurrent_index :deployments, [:project_id, :action, :status, :iid] add_concurrent_index :deployments, [:environment_id, :status]
remove_concurrent_index_by_name(:deployments, SHORT_INDEX_NAME_FOR_PROJECT_FINISHED_AT) remove_concurrent_index :deployments, [:project_id, :status, :id]
remove_concurrent_index_by_name(:deployments, SHORT_INDEX_NAME_FOR_ENVIRONMENT_ID) remove_concurrent_index :deployments, [:project_id, :status, :iid]
remove_concurrent_index_by_name(:deployments, SHORT_INDEX_NAME_FOR_ENVIRONMENT_IID) remove_concurrent_index :deployments, [:environment_id, :status, :id]
remove_concurrent_index :deployments, [:environment_id, :action, :sha] remove_concurrent_index :deployments, [:environment_id, :status, :iid]
remove_concurrent_index :deployments, [:environment_id, :sha]
end end
end end
...@@ -827,23 +827,19 @@ ActiveRecord::Schema.define(version: 20181106135939) do ...@@ -827,23 +827,19 @@ ActiveRecord::Schema.define(version: 20181106135939) do
t.string "on_stop" t.string "on_stop"
t.integer "status", limit: 2, default: 2, null: false t.integer "status", limit: 2, default: 2, null: false
t.datetime_with_timezone "finished_at" t.datetime_with_timezone "finished_at"
t.integer "action", limit: 2, default: 1, null: false
end end
add_index "deployments", ["created_at"], name: "index_deployments_on_created_at", using: :btree add_index "deployments", ["created_at"], name: "index_deployments_on_created_at", using: :btree
add_index "deployments", ["deployable_type", "deployable_id"], name: "index_deployments_on_deployable_type_and_deployable_id", using: :btree add_index "deployments", ["deployable_type", "deployable_id"], name: "index_deployments_on_deployable_type_and_deployable_id", using: :btree
add_index "deployments", ["environment_id", "action", "sha"], name: "index_deployments_on_environment_id_and_action_and_sha", using: :btree
add_index "deployments", ["environment_id", "action", "status", "id"], name: "index_deployments_on_env_deployed_for_id", using: :btree
add_index "deployments", ["environment_id", "action", "status", "iid"], name: "index_deployments_on_env_deployed_for_iid", using: :btree
add_index "deployments", ["environment_id", "id"], name: "index_deployments_on_environment_id_and_id", using: :btree add_index "deployments", ["environment_id", "id"], name: "index_deployments_on_environment_id_and_id", using: :btree
add_index "deployments", ["environment_id", "iid", "project_id"], name: "index_deployments_on_environment_id_and_iid_and_project_id", using: :btree add_index "deployments", ["environment_id", "iid", "project_id"], name: "index_deployments_on_environment_id_and_iid_and_project_id", using: :btree
add_index "deployments", ["environment_id", "status"], name: "index_deployments_on_environment_id_and_status", using: :btree add_index "deployments", ["environment_id", "sha"], name: "index_deployments_on_environment_id_and_sha", using: :btree
add_index "deployments", ["environment_id", "status", "id"], name: "index_deployments_on_environment_id_and_status_and_id", using: :btree
add_index "deployments", ["environment_id", "status", "iid"], name: "index_deployments_on_environment_id_and_status_and_iid", using: :btree
add_index "deployments", ["id"], name: "partial_index_deployments_for_legacy_successful_deployments", where: "((finished_at IS NULL) AND (status = 2))", using: :btree add_index "deployments", ["id"], name: "partial_index_deployments_for_legacy_successful_deployments", where: "((finished_at IS NULL) AND (status = 2))", using: :btree
add_index "deployments", ["project_id", "action", "status", "finished_at"], name: "index_deployments_on_prj_deployed_for_finished_at", using: :btree
add_index "deployments", ["project_id", "action", "status", "id"], name: "index_deployments_on_project_id_and_action_and_status_and_id", using: :btree
add_index "deployments", ["project_id", "action", "status", "iid"], name: "index_deployments_on_project_id_and_action_and_status_and_iid", using: :btree
add_index "deployments", ["project_id", "iid"], name: "index_deployments_on_project_id_and_iid", unique: true, using: :btree add_index "deployments", ["project_id", "iid"], name: "index_deployments_on_project_id_and_iid", unique: true, using: :btree
add_index "deployments", ["project_id", "status"], name: "index_deployments_on_project_id_and_status", using: :btree add_index "deployments", ["project_id", "status", "id"], name: "index_deployments_on_project_id_and_status_and_id", using: :btree
add_index "deployments", ["project_id", "status", "iid"], name: "index_deployments_on_project_id_and_status_and_iid", using: :btree
create_table "emails", force: :cascade do |t| create_table "emails", force: :cascade do |t|
t.integer "user_id", null: false t.integer "user_id", null: false
...@@ -931,35 +927,6 @@ ActiveRecord::Schema.define(version: 20181106135939) do ...@@ -931,35 +927,6 @@ ActiveRecord::Schema.define(version: 20181106135939) do
add_index "forked_project_links", ["forked_to_project_id"], name: "index_forked_project_links_on_forked_to_project_id", unique: true, using: :btree add_index "forked_project_links", ["forked_to_project_id"], name: "index_forked_project_links_on_forked_to_project_id", unique: true, using: :btree
create_table "gcp_clusters", force: :cascade do |t|
t.integer "project_id", null: false
t.integer "user_id"
t.integer "service_id"
t.integer "status"
t.integer "gcp_cluster_size", null: false
t.datetime_with_timezone "created_at", null: false
t.datetime_with_timezone "updated_at", null: false
t.boolean "enabled", default: true
t.text "status_reason"
t.string "project_namespace"
t.string "endpoint"
t.text "ca_cert"
t.text "encrypted_kubernetes_token"
t.string "encrypted_kubernetes_token_iv"
t.string "username"
t.text "encrypted_password"
t.string "encrypted_password_iv"
t.string "gcp_project_id", null: false
t.string "gcp_cluster_zone", null: false
t.string "gcp_cluster_name", null: false
t.string "gcp_machine_type"
t.string "gcp_operation_id"
t.text "encrypted_gcp_token"
t.string "encrypted_gcp_token_iv"
end
add_index "gcp_clusters", ["project_id"], name: "index_gcp_clusters_on_project_id", unique: true, using: :btree
create_table "gpg_key_subkeys", force: :cascade do |t| create_table "gpg_key_subkeys", force: :cascade do |t|
t.integer "gpg_key_id", null: false t.integer "gpg_key_id", null: false
t.binary "keyid" t.binary "keyid"
...@@ -1860,6 +1827,7 @@ ActiveRecord::Schema.define(version: 20181106135939) do ...@@ -1860,6 +1827,7 @@ ActiveRecord::Schema.define(version: 20181106135939) do
end end
add_index "redirect_routes", ["path"], name: "index_redirect_routes_on_path", unique: true, using: :btree add_index "redirect_routes", ["path"], name: "index_redirect_routes_on_path", unique: true, using: :btree
add_index "redirect_routes", ["path"], name: "index_redirect_routes_on_path_text_pattern_ops", using: :btree, opclasses: {"path"=>"varchar_pattern_ops"}
add_index "redirect_routes", ["source_type", "source_id"], name: "index_redirect_routes_on_source_type_and_source_id", using: :btree add_index "redirect_routes", ["source_type", "source_id"], name: "index_redirect_routes_on_source_type_and_source_id", using: :btree
create_table "releases", force: :cascade do |t| create_table "releases", force: :cascade do |t|
...@@ -2443,9 +2411,6 @@ ActiveRecord::Schema.define(version: 20181106135939) do ...@@ -2443,9 +2411,6 @@ ActiveRecord::Schema.define(version: 20181106135939) do
add_foreign_key "fork_network_members", "projects", on_delete: :cascade add_foreign_key "fork_network_members", "projects", on_delete: :cascade
add_foreign_key "fork_networks", "projects", column: "root_project_id", name: "fk_e7b436b2b5", on_delete: :nullify add_foreign_key "fork_networks", "projects", column: "root_project_id", name: "fk_e7b436b2b5", on_delete: :nullify
add_foreign_key "forked_project_links", "projects", column: "forked_to_project_id", name: "fk_434510edb0", on_delete: :cascade add_foreign_key "forked_project_links", "projects", column: "forked_to_project_id", name: "fk_434510edb0", on_delete: :cascade
add_foreign_key "gcp_clusters", "projects", on_delete: :cascade
add_foreign_key "gcp_clusters", "services", on_delete: :nullify
add_foreign_key "gcp_clusters", "users", on_delete: :nullify
add_foreign_key "gpg_key_subkeys", "gpg_keys", on_delete: :cascade add_foreign_key "gpg_key_subkeys", "gpg_keys", on_delete: :cascade
add_foreign_key "gpg_keys", "users", on_delete: :cascade add_foreign_key "gpg_keys", "users", on_delete: :cascade
add_foreign_key "gpg_signatures", "gpg_key_subkeys", on_delete: :nullify add_foreign_key "gpg_signatures", "gpg_key_subkeys", on_delete: :nullify
......
...@@ -24,7 +24,7 @@ module API ...@@ -24,7 +24,7 @@ module API
get ':id/deployments' do get ':id/deployments' do
authorize! :read_deployment, user_project authorize! :read_deployment, user_project
present paginate(user_project.deployments.deployed.order(params[:order_by] => params[:sort])), with: Entities::Deployment present paginate(user_project.deployments.order(params[:order_by] => params[:sort])), with: Entities::Deployment
end end
# rubocop: enable CodeReuse/ActiveRecord # rubocop: enable CodeReuse/ActiveRecord
...@@ -38,7 +38,7 @@ module API ...@@ -38,7 +38,7 @@ module API
get ':id/deployments/:deployment_id' do get ':id/deployments/:deployment_id' do
authorize! :read_deployment, user_project authorize! :read_deployment, user_project
deployment = user_project.deployments.deployed.find(params[:deployment_id]) deployment = user_project.deployments.find(params[:deployment_id])
present deployment, with: Entities::Deployment present deployment, with: Entities::Deployment
end end
......
...@@ -7,7 +7,7 @@ module Gitlab ...@@ -7,7 +7,7 @@ module Gitlab
end end
def value def value
@value ||= @project.deployments.deployed.where("finished_at > ?", @from).count @value ||= @project.deployments.success.where("created_at > ?", @from).count
end end
end end
end end
......
...@@ -47,13 +47,5 @@ FactoryBot.define do ...@@ -47,13 +47,5 @@ FactoryBot.define do
deployment.succeed! deployment.succeed!
end end
end end
trait :start do
action :start
end
trait :stop do
action :stop
end
end end
end end
...@@ -18,7 +18,6 @@ describe 'Merge request > User sees deployment widget', :js do ...@@ -18,7 +18,6 @@ describe 'Merge request > User sees deployment widget', :js do
sign_in(user) sign_in(user)
end end
context 'when deployment is to start an environment' do
context 'when deployment succeeded' do context 'when deployment succeeded' do
let(:build) { create(:ci_build, :success, pipeline: pipeline) } let(:build) { create(:ci_build, :success, pipeline: pipeline) }
let!(:deployment) { create(:deployment, :succeed, environment: environment, sha: sha, ref: ref, deployable: build) } let!(:deployment) { create(:deployment, :succeed, environment: environment, sha: sha, ref: ref, deployable: build) }
...@@ -70,57 +69,6 @@ describe 'Merge request > User sees deployment widget', :js do ...@@ -70,57 +69,6 @@ describe 'Merge request > User sees deployment widget', :js do
expect(page).not_to have_css('.js-deploy-time') expect(page).not_to have_css('.js-deploy-time')
end end
end end
end
context 'when deployment is to stop an environment' do
context 'when the stop action succeeded' do
let(:build) { create(:ci_build, :success, pipeline: pipeline) }
let!(:deployment) { create(:deployment, :succeed, :stop, environment: environment, sha: sha, ref: ref, deployable: build) }
it 'does not display deployment info' do
visit project_merge_request_path(project, merge_request)
wait_for_requests
expect(page).not_to have_css('.deployment-info')
end
end
context 'when the stop action failed' do
let(:build) { create(:ci_build, :failed, pipeline: pipeline) }
let!(:deployment) { create(:deployment, :failed, :stop, environment: environment, sha: sha, ref: ref, deployable: build) }
it 'does not display deployment info' do
visit project_merge_request_path(project, merge_request)
wait_for_requests
expect(page).not_to have_css('.deployment-info')
end
end
context 'when the stop action is running' do
let(:build) { create(:ci_build, :running, pipeline: pipeline) }
let!(:deployment) { create(:deployment, :running, :stop, environment: environment, sha: sha, ref: ref, deployable: build) }
it 'does not display deployment info' do
visit project_merge_request_path(project, merge_request)
wait_for_requests
expect(page).not_to have_css('.deployment-info')
end
end
context 'when the stop action will happen' do
let(:build) { create(:ci_build, :created, pipeline: pipeline) }
let!(:deployment) { create(:deployment, :stop, environment: environment, sha: sha, ref: ref, deployable: build) }
it 'does not display deployment info' do
visit project_merge_request_path(project, merge_request)
wait_for_requests
expect(page).not_to have_css('.deployment-info')
end
end
end
context 'with stop action' do context 'with stop action' do
let(:build) { create(:ci_build, :success, pipeline: pipeline) } let(:build) { create(:ci_build, :success, pipeline: pipeline) }
......
...@@ -82,19 +82,6 @@ describe 'Environment' do ...@@ -82,19 +82,6 @@ describe 'Environment' do
end end
end end
context 'when there is a successful stop action' do
let(:pipeline) { create(:ci_pipeline, project: project) }
let(:build) { create(:ci_build, :success, pipeline: pipeline) }
let(:deployment) do
create(:deployment, :success, :stop, environment: environment, deployable: build)
end
it 'does show no deployments' do
expect(page).to have_content('You don\'t have any deployments right now.')
end
end
context 'with related deployable present' do context 'with related deployable present' do
let(:pipeline) { create(:ci_pipeline, project: project) } let(:pipeline) { create(:ci_pipeline, project: project) }
let(:build) { create(:ci_build, pipeline: pipeline) } let(:build) { create(:ci_build, pipeline: pipeline) }
......
...@@ -344,23 +344,6 @@ describe 'Environments page', :js do ...@@ -344,23 +344,6 @@ describe 'Environments page', :js do
expect(page).to have_content('No deployments yet') expect(page).to have_content('No deployments yet')
end end
end end
context 'when there is a successful stop action' do
let(:project) { create(:project, :repository) }
let!(:deployment) do
create(:deployment, :success,
:stop,
environment: environment,
sha: project.commit.id)
end
it 'does not show deployments' do
visit_environments(project)
expect(page).to have_content('No deployments yet')
end
end
end end
it 'does have a new environment button' do it 'does have a new environment button' do
......
...@@ -22,17 +22,13 @@ describe Deployable do ...@@ -22,17 +22,13 @@ describe Deployable do
expect(deployment.on_stop).to eq('stop_review_app') expect(deployment.on_stop).to eq('stop_review_app')
expect(environment.name).to eq('review/master') expect(environment.name).to eq('review/master')
end end
it 'updates action column to start' do
expect(deployment).to be_start
end
end end
context 'when the deployable object will stop the review app' do context 'when the deployable object will stop an environment' do
let!(:job) { create(:ci_build, :stop_review_app) } let!(:job) { create(:ci_build, :stop_review_app) }
it 'updates action column to stop' do it 'does not create a deployment record' do
expect(deployment).to be_stop expect(deployment).to be_nil
end end
end end
......
...@@ -191,150 +191,6 @@ describe Deployment do ...@@ -191,150 +191,6 @@ describe Deployment do
end end
end end
describe '.deployed' do
subject { described_class.deployed }
context 'when deployment starts an environment' do
context 'when the action was successful' do
let!(:deployment) { create(:deployment, :start, :success) }
it 'returns the deployment' do
is_expected.to eq([deployment])
end
end
context 'when the action failed' do
let!(:deployment) { create(:deployment, :start, :failed) }
it 'returns nothing' do
is_expected.to be_empty
end
end
end
context 'when deployment stops an environment' do
context 'when the action was successful' do
let!(:deployment) { create(:deployment, :stop, :success) }
it 'returns nothing' do
is_expected.to be_empty
end
end
context 'when the action failed' do
let!(:deployment) { create(:deployment, :stop, :failed) }
it 'returns nothing' do
is_expected.to be_empty
end
end
end
end
describe '.stopped' do
subject { described_class.stopped }
context 'when deployment starts an environment' do
context 'when the action was successful' do
let!(:deployment) { create(:deployment, :start, :success) }
it 'returns nothing' do
is_expected.to be_empty
end
end
context 'when the action failed' do
let!(:deployment) { create(:deployment, :start, :failed) }
it 'returns nothing' do
is_expected.to be_empty
end
end
end
context 'when deployment stops an environment' do
context 'when the action was successful' do
let!(:deployment) { create(:deployment, :stop, :success) }
it 'returns the deployment' do
is_expected.to eq([deployment])
end
end
context 'when the action failed' do
let!(:deployment) { create(:deployment, :stop, :failed) }
it 'returns nothing' do
is_expected.to be_empty
end
end
end
end
describe '#deployed?' do
subject { deployment.deployed? }
context 'when deployment starts an environment' do
context 'when the action was successful' do
let!(:deployment) { create(:deployment, :start, :success) }
it { is_expected.to be_truthy }
end
context 'when the action failed' do
let!(:deployment) { create(:deployment, :start, :failed) }
it { is_expected.to be_falsy }
end
end
context 'when deployment stops an environment' do
context 'when the action was successful' do
let!(:deployment) { create(:deployment, :stop, :success) }
it { is_expected.to be_falsy }
end
context 'when the action failed' do
let!(:deployment) { create(:deployment, :stop, :failed) }
it { is_expected.to be_falsy }
end
end
end
describe '#stopped?' do
subject { deployment.stopped? }
context 'when deployment starts an environment' do
context 'when the action was successful' do
let!(:deployment) { create(:deployment, :start, :success) }
it { is_expected.to be_falsy }
end
context 'when the action failed' do
let!(:deployment) { create(:deployment, :start, :failed) }
it { is_expected.to be_falsy }
end
end
context 'when deployment stops an environment' do
context 'when the action was successful' do
let!(:deployment) { create(:deployment, :stop, :success) }
it { is_expected.to be_truthy }
end
context 'when the action failed' do
let!(:deployment) { create(:deployment, :stop, :failed) }
it { is_expected.to be_falsy }
end
end
end
describe '#deployed_at' do describe '#deployed_at' do
subject { deployment.deployed_at } subject { deployment.deployed_at }
......
...@@ -59,22 +59,14 @@ describe Environment do ...@@ -59,22 +59,14 @@ describe Environment do
let(:environment) { create(:environment) } let(:environment) { create(:environment) }
let(:sha) { RepoHelpers.sample_commit.id } let(:sha) { RepoHelpers.sample_commit.id }
context 'when deployment starts environment' do
context 'when deployment has the specified sha' do context 'when deployment has the specified sha' do
let!(:deployment) { create(:deployment, :start, environment: environment, sha: sha) } let!(:deployment) { create(:deployment, environment: environment, sha: sha) }
it { is_expected.to eq([environment]) } it { is_expected.to eq([environment]) }
end end
context 'when deployment does not have the specified sha' do context 'when deployment does not have the specified sha' do
let!(:deployment) { create(:deployment, :start, environment: environment, sha: 'abc') } let!(:deployment) { create(:deployment, environment: environment, sha: 'abc') }
it { is_expected.to be_empty }
end
end
context 'when deployment stops environment' do
let!(:deployment) { create(:deployment, :stop, environment: environment) }
it { is_expected.to be_empty } it { is_expected.to be_empty }
end end
...@@ -85,28 +77,20 @@ describe Environment do ...@@ -85,28 +77,20 @@ describe Environment do
let(:environment) { create(:environment) } let(:environment) { create(:environment) }
context 'when the latest deployment is for starting an environment' do
context 'when the latest deployment is successful' do context 'when the latest deployment is successful' do
let!(:deployment) { create(:deployment, :start, :success, environment: environment) } let!(:deployment) { create(:deployment, :success, environment: environment) }
it { expect(subject).to be_within(1.second).of(deployment.finished_at) } it { expect(subject).to be_within(1.second).of(deployment.finished_at) }
end end
context 'when the latest deployment failed' do context 'when the latest deployment failed' do
let!(:deployment) { create(:deployment, :start, :failed, environment: environment) } let!(:deployment) { create(:deployment, :failed, environment: environment) }
it { is_expected.to be_nil } it { is_expected.to be_nil }
end end
context 'when the latest deployment is running' do context 'when the latest deployment is running' do
let!(:deployment) { create(:deployment, :start, :running, environment: environment) } let!(:deployment) { create(:deployment, :running, environment: environment) }
it { is_expected.to be_nil }
end
end
context 'when the latest deployment is for stopping environment' do
let!(:deployment) { create(:deployment, :stop, :success, environment: environment) }
it { is_expected.to be_nil } it { is_expected.to be_nil }
end end
......
...@@ -3,7 +3,7 @@ require 'spec_helper' ...@@ -3,7 +3,7 @@ require 'spec_helper'
describe EnvironmentStatus do describe EnvironmentStatus do
include ProjectForksHelper include ProjectForksHelper
let(:deployment) { create(:deployment, :succeed, :start, :review_app) } let(:deployment) { create(:deployment, :succeed, :review_app) }
let(:environment) { deployment.environment } let(:environment) { deployment.environment }
let(:project) { deployment.project } let(:project) { deployment.project }
let(:merge_request) { create(:merge_request, :deployed_review_app, deployment: deployment) } let(:merge_request) { create(:merge_request, :deployed_review_app, deployment: deployment) }
......
require 'spec_helper'
describe StopEnvironmentService do
let(:service) { described_class.new(deployment) }
describe '#execute' do
subject { service.execute }
context 'when environment is available' do
let(:environment) { create(:environment, state: :available) }
context 'when deployment was successful' do
let(:deployment) { create(:deployment, :success, :stop, environment: environment) }
it 'stops the environment' do
subject
expect(environment.reload).to be_stopped
end
end
context 'when deployment failed' do
let(:deployment) { create(:deployment, :failed, :stop, environment: environment) }
it 'does not stop the environment' do
subject
expect(environment.reload).to be_available
end
end
end
context 'when environment is stopped' do
let(:deployment) { create(:deployment, :success, :stop, environment: environment) }
let(:environment) { create(:environment, state: :stopped) }
it 'does not raise an error' do
expect { subject }.not_to raise_error
expect(environment.reload).to be_stopped
end
end
end
end
require 'spec_helper' require 'spec_helper'
describe StartEnvironmentService do describe UpdateDeploymentService do
let(:user) { create(:user) } let(:user) { create(:user) }
let(:options) { { name: 'production' } } let(:options) { { name: 'production' } }
...@@ -84,8 +84,8 @@ describe StartEnvironmentService do ...@@ -84,8 +84,8 @@ describe StartEnvironmentService do
it 'updates external url' do it 'updates external url' do
subject subject
expect(environment.reload.name).to eq('review-apps/master') expect(subject.environment.name).to eq('review-apps/master')
expect(environment.reload.external_url).to eq('http://master.review-apps.gitlab.com') expect(subject.environment.external_url).to eq('http://master.review-apps.gitlab.com')
end end
end end
end end
......
...@@ -47,6 +47,19 @@ describe BuildSuccessWorker do ...@@ -47,6 +47,19 @@ describe BuildSuccessWorker do
expect(build.reload).not_to be_has_deployment expect(build.reload).not_to be_has_deployment
end end
end end
context 'when the build will stop an environment' do
let!(:build) { create(:ci_build, :stop_review_app, environment: environment.name, project: environment.project) }
let(:environment) { create(:environment, state: :available) }
it 'stops the environment' do
expect(environment).to be_available
subject
expect(environment.reload).to be_stopped
end
end
end end
context 'when build does not exist' do context 'when build does not exist' do
......
...@@ -3,51 +3,34 @@ require 'spec_helper' ...@@ -3,51 +3,34 @@ require 'spec_helper'
describe Deployments::SuccessWorker do describe Deployments::SuccessWorker do
subject { described_class.new.perform(deployment&.id) } subject { described_class.new.perform(deployment&.id) }
context 'when deployment starts environment' do context 'when successful deployment' do
context 'when deployment was successful' do let(:deployment) { create(:deployment, :success) }
let(:deployment) { create(:deployment, :start, :success) }
it 'executes StartEnvironmentService' do it 'executes UpdateDeploymentService' do
expect(StartEnvironmentService) expect(UpdateDeploymentService)
.to receive(:new).with(deployment).and_call_original .to receive(:new).with(deployment).and_call_original
subject subject
end end
end end
context 'when deployment failed' do context 'when canceled deployment' do
let(:deployment) { create(:deployment, :start, :failed) } let(:deployment) { create(:deployment, :canceled) }
it 'does not execute StartEnvironmentService' do it 'does not execute UpdateDeploymentService' do
expect(StartEnvironmentService) expect(UpdateDeploymentService).not_to receive(:new)
.not_to receive(:new).with(deployment).and_call_original
subject subject
end end
end end
end
context 'when deployment stops environment' do context 'when deploy record does not exist' do
context 'when deployment was successful' do let(:deployment) { nil }
let(:deployment) { create(:deployment, :stop, :success) }
it 'executes StopEnvironmentService' do it 'does not execute UpdateDeploymentService' do
expect(StopEnvironmentService) expect(UpdateDeploymentService).not_to receive(:new)
.to receive(:new).with(deployment).and_call_original
subject subject
end end
end end
context 'when deployment failed' do
let(:deployment) { create(:deployment, :stop, :failed) }
it 'does not execute StopEnvironmentService' do
expect(StopEnvironmentService)
.not_to receive(:new).with(deployment).and_call_original
subject
end
end
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment