summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorTimothy Andrew <mail@timothyandrew.net>2017-05-22 11:59:10 +0000
committerTimothy Andrew <mail@timothyandrew.net>2017-05-22 12:01:34 +0000
commit539d9a0044ec82b5526031eed2e749476022e7e8 (patch)
tree4a3ef5b8794544d5720b0f30a58ffca7ffc3145b
parentadd9abadbdfac7e4c03b06b47d3afc296e00ac97 (diff)
downloadgitlab-ce-9-2-stable-fix-conflicts-for-mr-11593.tar.gz
Merge branch 'fix-retried-for-mysql' into 'master'9-2-stable-fix-conflicts-for-mr-11593
Fixes broken MySQL migration for retried Closes #32647 See merge request !11593 Conflicts: db/post_migrate/20170503004427_upate_retried_for_ci_build.rb
-rw-r--r--db/post_migrate/20170503004427_upate_retried_for_ci_build.rb57
-rw-r--r--spec/migrations/upate_retried_for_ci_builds_spec.rb17
2 files changed, 74 insertions, 0 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 80215d662e4..ab43dd923d2 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,7 @@ class UpateRetriedForCiBuild < ActiveRecord::Migration
def up
disable_statement_timeout
+<<<<<<< HEAD
latest_id = <<-SQL.strip_heredoc
SELECT MAX(ci_builds2.id)
FROM ci_builds ci_builds2
@@ -25,5 +26,61 @@ class UpateRetriedForCiBuild < ActiveRecord::Migration
end
def down
+=======
+ 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)
+ FROM ci_builds ci_builds2
+ WHERE ci_builds.commit_id=ci_builds2.commit_id
+ AND ci_builds.name=ci_builds2.name
+ SQL
+
+ # This is slow update as it does single-row query
+ # This is designed to be run as idle, or a post deployment migration
+ is_retried = Arel.sql("((#{latest_id}) != ci_builds.id)")
+
+ update_column_in_batches(:ci_builds, :retried, is_retried) do |table, query|
+ query.where(table[:retried].eq(nil))
+ end
+ end
+ 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;'
+ end
+
+ yield
+
+ if Gitlab::Database.postgresql?
+ execute 'DROP INDEX CONCURRENTLY IF EXISTS index_for_ci_builds_retried_migration'
+ end
+>>>>>>> 7f31768... Merge branch 'fix-retried-for-mysql' into 'master'
end
end
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