summaryrefslogtreecommitdiff
path: root/lib/gitlab/background_migration/migrate_build_stage.rb
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-02-15 10:13:20 +0100
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-02-15 10:13:20 +0100
commit6cb5b7c8729151c95d1610f0b2f7255fdac2bdec (patch)
tree860bf81531053a5a97ddc01b183531cfebf2c48f /lib/gitlab/background_migration/migrate_build_stage.rb
parent378b2bad11b893fabebc083d000295c1ec499b23 (diff)
downloadgitlab-ce-6cb5b7c8729151c95d1610f0b2f7255fdac2bdec.tar.gz
Recover from unique constraint violation in stages migration
Diffstat (limited to 'lib/gitlab/background_migration/migrate_build_stage.rb')
-rw-r--r--lib/gitlab/background_migration/migrate_build_stage.rb18
1 files changed, 9 insertions, 9 deletions
diff --git a/lib/gitlab/background_migration/migrate_build_stage.rb b/lib/gitlab/background_migration/migrate_build_stage.rb
index 3e297a90b19..adca16751af 100644
--- a/lib/gitlab/background_migration/migrate_build_stage.rb
+++ b/lib/gitlab/background_migration/migrate_build_stage.rb
@@ -13,19 +13,20 @@ module Gitlab
class Build < ActiveRecord::Base
self.table_name = 'ci_builds'
- def ensure_stage!
- find || create!
+ def ensure_stage!(attempts: 2)
+ find_stage || create_stage!
rescue ActiveRecord::RecordNotUnique
- # TODO
+ retry if (attempts -= 1) > 0
+ raise
end
- def find
- Stage.find_by(name: self.stage,
+ def find_stage
+ Stage.find_by(name: self.stage || 'test',
pipeline_id: self.commit_id,
project_id: self.project_id)
end
- def create!
+ def create_stage!
Stage.create!(name: self.stage || 'test',
pipeline_id: self.commit_id,
project_id: self.project_id)
@@ -34,11 +35,10 @@ module Gitlab
end
def perform(start_id, stop_id)
- # TODO, should we disable_statement_timeout?
- # TODO, use plain SQL query?
+ # TODO, statement timeout?
stages = Migratable::Build.where('stage_id IS NULL')
- .where("id BETWEEN #{start_id.to_i} AND #{stop_id.to_i}")
+ .where('id BETWEEN ? AND ?', start_id, stop_id)
.map { |build| build.ensure_stage! }
.compact.map(&:id)