summaryrefslogtreecommitdiff
path: root/db/post_migrate
diff options
context:
space:
mode:
authorGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-05-26 14:46:45 +0200
committerGrzegorz Bizon <grzesiek.bizon@gmail.com>2017-05-26 14:46:45 +0200
commit918ababba6fce694c61d54bb2ff6983a886f696a (patch)
tree1d502368d29711fb4c2fae814b4fb99bb8ca4bfe /db/post_migrate
parent0f9fbae78a51f7bf4df50d96060087e1cf903b05 (diff)
downloadgitlab-ce-918ababba6fce694c61d54bb2ff6983a886f696a.tar.gz
Add pipeline stages post deployment migration
Diffstat (limited to 'db/post_migrate')
-rw-r--r--db/post_migrate/20170526101042_migrate_pipeline_stages.rb43
1 files changed, 43 insertions, 0 deletions
diff --git a/db/post_migrate/20170526101042_migrate_pipeline_stages.rb b/db/post_migrate/20170526101042_migrate_pipeline_stages.rb
new file mode 100644
index 00000000000..12ad0db3a60
--- /dev/null
+++ b/db/post_migrate/20170526101042_migrate_pipeline_stages.rb
@@ -0,0 +1,43 @@
+class MigratePipelineStages < ActiveRecord::Migration
+ include Gitlab::Database::MigrationHelpers
+
+ DOWNTIME = false
+
+ disable_ddl_transaction!
+
+ def up
+ disable_statement_timeout
+
+ execute <<-SQL.strip_heredoc
+ INSERT INTO ci_stages (project_id, pipeline_id, name)
+ SELECT project_id, commit_id, stage FROM ci_builds
+ WHERE stage IS NOT NULL
+ GROUP BY project_id, commit_id, stage, stage_idx
+ ORDER BY stage_idx
+ SQL
+
+ add_concurrent_index(:ci_stages, [:pipeline_id, :name])
+
+ add_column(:ci_builds, :stage_id, :integer)
+
+ stage_id = Arel.sql('(SELECT id FROM ci_stages ' \
+ 'WHERE ci_stages.pipeline_id = ci_builds.commit_id ' \
+ 'AND ci_stages.name = ci_builds.stage)')
+ update_column_in_batches(:ci_builds, :stage_id, stage_id)
+
+ # add_concurrent_foreign_key :ci_stages, :projects, column: :project_id, on_delete: :cascade
+ # add_concurrent_foreign_key :ci_builds, :ci_stages, column: :stage_id, on_delete: :cascade
+ end
+
+ def down
+ execute('TRUNCATE TABLE ci_stages')
+
+ if column_exists?(:ci_builds, :stage_id)
+ remove_column(:ci_builds, :stage_id)
+ end
+
+ if index_exists?(:ci_stages, [:pipeline_id, :name])
+ remove_index(:ci_stages, [:pipeline_id, :name])
+ end
+ end
+end