Commit 0937eff1 authored by Kamil Trzciński's avatar Kamil Trzciński

Merge branch 'fix/gb/improve-stage-id-foreign-key-migration' into 'master'

Improve stage_id foreign key migration

Closes #34495

See merge request !12617
parents ef0ac7c2 f5641dcf
class AddStageIdForeignKeyToBuilds < ActiveRecord::Migration
include Gitlab::Database::MigrationHelpers
DOWNTIME = false
disable_ddl_transaction!
def up
unless index_exists?(:ci_builds, :stage_id)
add_concurrent_index(:ci_builds, :stage_id)
end
unless foreign_key_exists?(:ci_builds, :stage_id)
add_concurrent_foreign_key(:ci_builds, :ci_stages, column: :stage_id, on_delete: :cascade)
end
end
def down
if foreign_key_exists?(:ci_builds, :stage_id)
remove_foreign_key(:ci_builds, column: :stage_id)
end
if index_exists?(:ci_builds, :stage_id)
remove_concurrent_index(:ci_builds, :stage_id)
end
end
private
def foreign_key_exists?(table, column)
foreign_keys(:ci_builds).any? do |key|
key.options[:column] == column.to_s
end
end
end
...@@ -3,19 +3,15 @@ class AddStageIdIndexToBuilds < ActiveRecord::Migration ...@@ -3,19 +3,15 @@ class AddStageIdIndexToBuilds < ActiveRecord::Migration
DOWNTIME = false DOWNTIME = false
disable_ddl_transaction! ##
# Improved in 20170703102400_add_stage_id_foreign_key_to_builds.rb
#
def up def up
unless index_exists?(:ci_builds, :stage_id) # noop
add_concurrent_foreign_key(:ci_builds, :ci_stages, column: :stage_id, on_delete: :cascade)
add_concurrent_index(:ci_builds, :stage_id)
end
end end
def down def down
if index_exists?(:ci_builds, :stage_id) # noop
remove_foreign_key(:ci_builds, column: :stage_id)
remove_concurrent_index(:ci_builds, :stage_id)
end
end end
end end
...@@ -11,7 +11,7 @@ ...@@ -11,7 +11,7 @@
# #
# It's strongly recommended that you check this file into your version control system. # It's strongly recommended that you check this file into your version control system.
ActiveRecord::Schema.define(version: 20170623080805) do ActiveRecord::Schema.define(version: 20170703102400) do
# These are extensions that must be enabled in order to support this database # These are extensions that must be enabled in order to support this database
enable_extension "plpgsql" enable_extension "plpgsql"
......
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