diff options
author | Timothy Andrew <mail@timothyandrew.net> | 2017-05-22 11:59:10 +0000 |
---|---|---|
committer | Timothy Andrew <mail@timothyandrew.net> | 2017-05-22 11:59:10 +0000 |
commit | 7f31768a7f1d311d7c8cedce045f5d918370b8c9 (patch) | |
tree | c1990db8b7e0c1daf0bf3c204c6ae8e6fd1a5fd8 | |
parent | b94398e930e77d3d6d70b2f33316fd342707fe9f (diff) | |
parent | cbafe24a1fef2c13925490453cb97c3831d03169 (diff) | |
download | gitlab-ce-7f31768a7f1d311d7c8cedce045f5d918370b8c9.tar.gz |
Merge branch 'fix-retried-for-mysql' into 'master'
Fixes broken MySQL migration for retried
Closes #32647
See merge request !11593
-rw-r--r-- | db/post_migrate/20170503004427_upate_retried_for_ci_build.rb | 29 | ||||
-rw-r--r-- | spec/migrations/upate_retried_for_ci_builds_spec.rb | 17 |
2 files changed, 43 insertions, 3 deletions
diff --git a/db/post_migrate/20170503004427_upate_retried_for_ci_build.rb b/db/post_migrate/20170503004427_upate_retried_for_ci_build.rb index 9b20edeb4c3..738e46b9207 100644 --- a/db/post_migrate/20170503004427_upate_retried_for_ci_build.rb +++ b/db/post_migrate/20170503004427_upate_retried_for_ci_build.rb @@ -8,6 +8,32 @@ class UpateRetriedForCiBuild < ActiveRecord::Migration def up disable_statement_timeout + if Gitlab::Database.mysql? + up_mysql + else + up_postgres + end + end + + def down + end + + private + + def up_mysql + # This is a trick to overcome MySQL limitation: + # Mysql2::Error: Table 'ci_builds' is specified twice, both as a target for 'UPDATE' and as a separate source for data + # However, this leads to create a temporary table from `max(ci_builds.id)` which is slow and do full database update + execute <<-SQL.strip_heredoc + UPDATE ci_builds SET retried= + (id NOT IN ( + SELECT * FROM (SELECT MAX(ci_builds.id) FROM ci_builds GROUP BY commit_id, name) AS latest_jobs + )) + WHERE retried IS NULL + SQL + end + + def up_postgres with_temporary_partial_index do latest_id = <<-SQL.strip_heredoc SELECT MAX(ci_builds2.id) @@ -26,9 +52,6 @@ class UpateRetriedForCiBuild < ActiveRecord::Migration end end - def down - end - def with_temporary_partial_index if Gitlab::Database.postgresql? execute 'CREATE INDEX CONCURRENTLY IF NOT EXISTS index_for_ci_builds_retried_migration ON ci_builds (id) WHERE retried IS NULL;' diff --git a/spec/migrations/upate_retried_for_ci_builds_spec.rb b/spec/migrations/upate_retried_for_ci_builds_spec.rb new file mode 100644 index 00000000000..5cdb8a3c7da --- /dev/null +++ b/spec/migrations/upate_retried_for_ci_builds_spec.rb @@ -0,0 +1,17 @@ +require 'spec_helper' +require Rails.root.join('db', 'post_migrate', '20170503004427_upate_retried_for_ci_build.rb') + +describe UpateRetriedForCiBuild, truncate: true do + let(:pipeline) { create(:ci_pipeline) } + let!(:build_old) { create(:ci_build, pipeline: pipeline, name: 'test') } + let!(:build_new) { create(:ci_build, pipeline: pipeline, name: 'test') } + + before do + described_class.new.up + end + + it 'updates ci_builds.is_retried' do + expect(build_old.reload).to be_retried + expect(build_new.reload).not_to be_retried + end +end |