提交 210631ac 编写于 作者: S Shinya Maeda

Add action to deployment

上级 9bd4b2e8
......@@ -21,7 +21,8 @@ module Deployable
sha: sha,
user: user,
deployable: self,
on_stop: on_stop).tap do |_|
on_stop: on_stop,
action: environment_action).tap do |_|
self.reload # Reload relationships
end
end
......
......@@ -4,6 +4,7 @@ class Deployment < ActiveRecord::Base
include AtomicInternalId
include IidRoutes
include AfterCommitQueue
include EnumWithNil
belongs_to :project, required: true
belongs_to :environment, required: true
......@@ -55,6 +56,12 @@ class Deployment < ActiveRecord::Base
canceled: 4
}
enum_with_nil action: {
unknown_action: nil,
start: 1,
stop: 2
}
def self.last_for_environment(environment)
ids = self
.for_environment(environment)
......@@ -149,6 +156,12 @@ class Deployment < ActiveRecord::Base
finished_at
end
def action
return 'start' if deployment.unknown_action?
super
end
def formatted_deployment_time
deployed_at&.to_time&.in_time_zone&.to_s(:medium)
end
......
......@@ -20,7 +20,7 @@ class UpdateDeploymentService
environment.external_url = expanded_environment_url if
expanded_environment_url
environment.fire_state_event(action)
environment.fire_state_event(deployment.action)
break unless environment.save
break if environment.stopped?
......@@ -46,8 +46,4 @@ class UpdateDeploymentService
def environment_url
environment_options[:url]
end
def action
environment_options[:action] || 'start'
end
end
# frozen_string_literal: true
class AddActionToDeployments < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
def up
add_column :deployments, :action, :integer, limit: 2
end
def down
remove_column :deployments, :action, :integer, limit: 2
end
end
# frozen_string_literal: true
class AddIndexOnActionToDeployments < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_index :deployments, [:environment_id, :action]
add_concurrent_index :deployments, [:project_id, :action]
end
def down
remove_concurrent_index :deployments, [:environment_id, :action]
remove_concurrent_index :deployments, [:project_id, :action]
end
end
# frozen_string_literal: true
class FillEmptyActionInDeployments < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
DEPLOYMENT_ACTION_START = 1
DEPLOYMENT_ACTION_STOP = 2
def up
ActiveRecord::Base.connection.execute <<~SQL
UPDATE
"deployments"
SET
"action" = (CASE WHEN (SELECT true
FROM ci_builds
WHERE ci_builds.id = deployments.deployable_id
AND ci_builds.options LIKE '%action: stop%') THEN #{DEPLOYMENT_ACTION_STOP}
ELSE #{DEPLOYMENT_ACTION_START}
END)
WHERE
"deployments"."action" IS NULL
SQL
end
def down
# no-op
end
end
......@@ -11,7 +11,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20181030135124) do
ActiveRecord::Schema.define(version: 20181031190559) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
......@@ -826,13 +826,16 @@ ActiveRecord::Schema.define(version: 20181030135124) do
t.string "on_stop"
t.integer "status", limit: 2, default: 2, null: false
t.datetime_with_timezone "finished_at"
t.integer "action", limit: 2
end
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", ["environment_id", "action"], name: "index_deployments_on_environment_id_and_action", 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", "status"], name: "index_deployments_on_environment_id_and_status", using: :btree
add_index "deployments", ["project_id", "action"], name: "index_deployments_on_project_id_and_action", 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
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册