Add cluster_id to deployments table as an FK

We nullify when cluster is deleted as we want to keep the deployment
record around.

Add cluster as an optional association

We will have only cluster for deployments where the build deploys to a
kubernetes cluster
This commit is contained in:
Thong Kuah 2019-06-24 09:48:16 +12:00
parent 871d06993e
commit 525edec78b
5 changed files with 36 additions and 1 deletions

View File

@ -7,6 +7,7 @@ class Deployment < ApplicationRecord
belongs_to :project, required: true
belongs_to :environment, required: true
belongs_to :cluster, class_name: 'Clusters::Cluster', optional: true
belongs_to :user
belongs_to :deployable, polymorphic: true # rubocop:disable Cop/PolymorphicAssociations

View File

@ -0,0 +1,9 @@
# frozen_string_literal: true
class AddClusterIdToDeployments < ActiveRecord::Migration[5.1]
DOWNTIME = false
def change
add_column :deployments, :cluster_id, :integer
end
end

View File

@ -0,0 +1,21 @@
# frozen_string_literal: true
class AddClusterIdIndexFkToDeployments < ActiveRecord::Migration[5.1]
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
add_concurrent_index :deployments, :cluster_id
add_concurrent_foreign_key :deployments, :clusters, column: :cluster_id, on_delete: :nullify
end
def down
remove_foreign_key :deployments, :clusters
remove_concurrent_index :deployments, :cluster_id
end
end

View File

@ -10,7 +10,7 @@
#
# It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20190625184066) do
ActiveRecord::Schema.define(version: 20190627051902) do
# These are extensions that must be enabled in order to support this database
enable_extension "plpgsql"
@ -1066,6 +1066,8 @@ ActiveRecord::Schema.define(version: 20190625184066) do
t.string "on_stop"
t.integer "status", limit: 2, null: false
t.datetime_with_timezone "finished_at"
t.integer "cluster_id"
t.index ["cluster_id"], name: "index_deployments_on_cluster_id", using: :btree
t.index ["created_at"], name: "index_deployments_on_created_at", using: :btree
t.index ["deployable_type", "deployable_id"], name: "index_deployments_on_deployable_type_and_deployable_id", using: :btree
t.index ["environment_id", "id"], name: "index_deployments_on_environment_id_and_id", using: :btree
@ -3650,6 +3652,7 @@ ActiveRecord::Schema.define(version: 20190625184066) do
add_foreign_key "dependency_proxy_blobs", "namespaces", column: "group_id", name: "fk_db58bbc5d7", on_delete: :cascade
add_foreign_key "dependency_proxy_group_settings", "namespaces", column: "group_id", name: "fk_616ddd680a", on_delete: :cascade
add_foreign_key "deploy_keys_projects", "projects", name: "fk_58a901ca7e", on_delete: :cascade
add_foreign_key "deployments", "clusters", name: "fk_289bba3222", on_delete: :nullify
add_foreign_key "deployments", "projects", name: "fk_b9a3851b82", on_delete: :cascade
add_foreign_key "design_management_designs", "issues", on_delete: :cascade
add_foreign_key "design_management_designs", "projects", on_delete: :cascade

View File

@ -7,6 +7,7 @@ describe Deployment do
it { is_expected.to belong_to(:project).required }
it { is_expected.to belong_to(:environment).required }
it { is_expected.to belong_to(:cluster).class_name('Clusters::Cluster') }
it { is_expected.to belong_to(:user) }
it { is_expected.to belong_to(:deployable) }