summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-04-24 14:14:33 +0200
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2018-04-24 14:15:26 +0200
commitd7e8bfac48e5b142348cd503f39387e9d88a3b85 (patch)
tree642bc7ec9fe27a5db097643e217557645597557a
parent9f2350644b5416e482d519a2e469a180fb603eb4 (diff)
downloadgitlab-ce-d7e8bfac48e5b142348cd503f39387e9d88a3b85.tar.gz
Improve stages index migration code readability
-rw-r--r--lib/gitlab/background_migration/migrate_stage_index.rb20
1 files changed, 10 insertions, 10 deletions
diff --git a/lib/gitlab/background_migration/migrate_stage_index.rb b/lib/gitlab/background_migration/migrate_stage_index.rb
index 6439ab3e42a..9ba3d7eb455 100644
--- a/lib/gitlab/background_migration/migrate_stage_index.rb
+++ b/lib/gitlab/background_migration/migrate_stage_index.rb
@@ -4,18 +4,20 @@
module Gitlab
module BackgroundMigration
class MigrateStageIndex
- module Migratable
- class Stage < ActiveRecord::Base
- self.table_name = 'ci_stages'
+ def perform(start_id, stop_id)
+ migrate_stage_index_sql(start_id.to_i, stop_id.to_i).tap do |sql|
+ ActiveRecord::Base.connection.execute(sql)
end
end
- def perform(start_id, stop_id)
+ private
+
+ def migrate_stage_index_sql(start_id, stop_id)
if Gitlab::Database.postgresql?
- sql = <<~SQL
+ <<~SQL
WITH freqs AS (
SELECT stage_id, stage_idx, COUNT(*) AS freq FROM ci_builds
- WHERE stage_id BETWEEN #{start_id.to_i} AND #{stop_id.to_i}
+ WHERE stage_id BETWEEN #{start_id} AND #{stop_id}
AND stage_idx IS NOT NULL
GROUP BY stage_id, stage_idx
), indexes AS (
@@ -29,18 +31,16 @@ module Gitlab
AND ci_stages.index IS NULL;
SQL
else
- sql = <<~SQL
+ <<~SQL
UPDATE ci_stages
SET index =
(SELECT stage_idx FROM ci_builds
WHERE ci_builds.stage_id = ci_stages.id
GROUP BY ci_builds.stage_idx ORDER BY COUNT(*) DESC LIMIT 1)
- WHERE ci_stages.id BETWEEN #{start_id.to_i} AND #{stop_id.to_i}
+ WHERE ci_stages.id BETWEEN #{start_id} AND #{stop_id}
AND ci_stages.index IS NULL
SQL
end
-
- ActiveRecord::Base.connection.execute(sql)
end
end
end