Merge branch 'fix/gb/improve-build-stage-reference-migration' into 'master'
Improve build stages reference migration Closes #33866 See merge request !12337
This commit is contained in:
commit
043f8c260f
|
@ -3,19 +3,11 @@ class AddStageIdToCiBuilds < ActiveRecord::Migration
|
|||
|
||||
DOWNTIME = false
|
||||
|
||||
disable_ddl_transaction!
|
||||
|
||||
def up
|
||||
add_column :ci_builds, :stage_id, :integer
|
||||
|
||||
add_concurrent_foreign_key :ci_builds, :ci_stages, column: :stage_id, on_delete: :cascade
|
||||
add_concurrent_index :ci_builds, :stage_id
|
||||
end
|
||||
|
||||
def down
|
||||
remove_foreign_key :ci_builds, column: :stage_id
|
||||
remove_concurrent_index :ci_builds, :stage_id
|
||||
|
||||
remove_column :ci_builds, :stage_id, :integer
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,18 @@
|
|||
class RemoveStageIdIndexFromBuilds < ActiveRecord::Migration
|
||||
include Gitlab::Database::MigrationHelpers
|
||||
|
||||
DOWNTIME = false
|
||||
|
||||
disable_ddl_transaction!
|
||||
|
||||
def up
|
||||
if index_exists?(:ci_builds, :stage_id)
|
||||
remove_foreign_key(:ci_builds, column: :stage_id)
|
||||
remove_concurrent_index(:ci_builds, :stage_id)
|
||||
end
|
||||
end
|
||||
|
||||
def down
|
||||
# noop
|
||||
end
|
||||
end
|
|
@ -3,23 +3,17 @@ class MigrateBuildStageReference < ActiveRecord::Migration
|
|||
|
||||
DOWNTIME = false
|
||||
|
||||
##
|
||||
# This is an empty migration, content has been moved to a new one:
|
||||
# post migrate 20170526190000 MigrateBuildStageReferenceAgain
|
||||
#
|
||||
# See gitlab-org/gitlab-ce!12337 for more details.
|
||||
|
||||
def up
|
||||
disable_statement_timeout
|
||||
|
||||
stage_id = Arel.sql <<-SQL.strip_heredoc
|
||||
(SELECT id FROM ci_stages
|
||||
WHERE ci_stages.pipeline_id = ci_builds.commit_id
|
||||
AND ci_stages.name = ci_builds.stage)
|
||||
SQL
|
||||
|
||||
update_column_in_batches(:ci_builds, :stage_id, stage_id) do |table, query|
|
||||
query.where(table[:stage_id].eq(nil))
|
||||
end
|
||||
# noop
|
||||
end
|
||||
|
||||
def down
|
||||
disable_statement_timeout
|
||||
|
||||
update_column_in_batches(:ci_builds, :stage_id, nil)
|
||||
# noop
|
||||
end
|
||||
end
|
||||
|
|
|
@ -0,0 +1,27 @@
|
|||
class MigrateBuildStageReferenceAgain < ActiveRecord::Migration
|
||||
include Gitlab::Database::MigrationHelpers
|
||||
|
||||
DOWNTIME = false
|
||||
|
||||
disable_ddl_transaction!
|
||||
|
||||
def up
|
||||
disable_statement_timeout
|
||||
|
||||
stage_id = Arel.sql <<-SQL.strip_heredoc
|
||||
(SELECT id FROM ci_stages
|
||||
WHERE ci_stages.pipeline_id = ci_builds.commit_id
|
||||
AND ci_stages.name = ci_builds.stage)
|
||||
SQL
|
||||
|
||||
update_column_in_batches(:ci_builds, :stage_id, stage_id) do |table, query|
|
||||
query.where(table[:stage_id].eq(nil))
|
||||
end
|
||||
end
|
||||
|
||||
def down
|
||||
disable_statement_timeout
|
||||
|
||||
update_column_in_batches(:ci_builds, :stage_id, nil)
|
||||
end
|
||||
end
|
|
@ -0,0 +1,21 @@
|
|||
class AddStageIdIndexToBuilds < ActiveRecord::Migration
|
||||
include Gitlab::Database::MigrationHelpers
|
||||
|
||||
DOWNTIME = false
|
||||
|
||||
disable_ddl_transaction!
|
||||
|
||||
def up
|
||||
unless index_exists?(:ci_builds, :stage_id)
|
||||
add_concurrent_foreign_key(:ci_builds, :ci_stages, column: :stage_id, on_delete: :cascade)
|
||||
add_concurrent_index(:ci_builds, :stage_id)
|
||||
end
|
||||
end
|
||||
|
||||
def down
|
||||
if index_exists?(:ci_builds, :stage_id)
|
||||
remove_foreign_key(:ci_builds, column: :stage_id)
|
||||
remove_concurrent_index(:ci_builds, :stage_id)
|
||||
end
|
||||
end
|
||||
end
|
|
@ -11,7 +11,7 @@
|
|||
#
|
||||
# It's strongly recommended that you check this file into your version control system.
|
||||
|
||||
ActiveRecord::Schema.define(version: 20170619144837) do
|
||||
ActiveRecord::Schema.define(version: 20170621102400) do
|
||||
|
||||
# These are extensions that must be enabled in order to support this database
|
||||
enable_extension "plpgsql"
|
||||
|
|
|
@ -1,7 +1,7 @@
|
|||
require 'spec_helper'
|
||||
require Rails.root.join('db', 'post_migrate', '20170526185921_migrate_build_stage_reference.rb')
|
||||
require Rails.root.join('db', 'post_migrate', '20170526190000_migrate_build_stage_reference_again.rb')
|
||||
|
||||
describe MigrateBuildStageReference, :migration do
|
||||
describe MigrateBuildStageReferenceAgain, :migration do
|
||||
##
|
||||
# Create test data - pipeline and CI/CD jobs.
|
||||
#
|
Loading…
Reference in New Issue