Commit 54957d69 authored by Dmitriy Zaporozhets's avatar Dmitriy Zaporozhets

Merge branch 'rs-fix-migrations' into 'master'

Use raw SQL instead of Rails models in migrations where appropriate

Closes #14694.

See merge request !3429
parents f4238634 47a539df
class ConvertClosedToStateInIssue < ActiveRecord::Migration class ConvertClosedToStateInIssue < ActiveRecord::Migration
include Gitlab::Database
def up def up
Issue.transaction do execute "UPDATE #{table_name} SET state = 'closed' WHERE closed = #{true_value}"
Issue.where(closed: true).update_all(state: :closed) execute "UPDATE #{table_name} SET state = 'opened' WHERE closed = #{false_value}"
Issue.where(closed: false).update_all(state: :opened)
end
end end
def down def down
Issue.transaction do execute "UPDATE #{table_name} SET closed = #{true_value} WHERE state = 'closed'"
Issue.where(state: :closed).update_all(closed: true)
end end
private
def table_name
Issue.table_name
end end
end end
class ConvertClosedToStateInMergeRequest < ActiveRecord::Migration class ConvertClosedToStateInMergeRequest < ActiveRecord::Migration
include Gitlab::Database
def up def up
MergeRequest.transaction do execute "UPDATE #{table_name} SET state = 'merged' WHERE closed = #{true_value} AND merged = #{true_value}"
MergeRequest.where(closed: true, merged: true).update_all(state: :merged) execute "UPDATE #{table_name} SET state = 'closed' WHERE closed = #{true_value} AND merged = #{false_value}"
MergeRequest.where(closed: true, merged: false).update_all(state: :closed) execute "UPDATE #{table_name} SET state = 'opened' WHERE closed = #{false_value}"
MergeRequest.where(closed: false).update_all(state: :opened)
end
end end
def down def down
MergeRequest.transaction do execute "UPDATE #{table_name} SET closed = #{true_value} WHERE state = 'closed'"
MergeRequest.where(state: :closed).update_all(closed: true) execute "UPDATE #{table_name} SET closed = #{true_value}, merged = #{true_value} WHERE state = 'merged'"
MergeRequest.where(state: :merged).update_all(closed: true, merged: true)
end end
private
def table_name
MergeRequest.table_name
end end
end end
class ConvertClosedToStateInMilestone < ActiveRecord::Migration class ConvertClosedToStateInMilestone < ActiveRecord::Migration
include Gitlab::Database
def up def up
Milestone.transaction do execute "UPDATE #{table_name} SET state = 'closed' WHERE closed = #{true_value}"
Milestone.where(closed: true).update_all(state: :closed) execute "UPDATE #{table_name} SET state = 'active' WHERE closed = #{false_value}"
Milestone.where(closed: false).update_all(state: :active)
end
end end
def down def down
Milestone.transaction do execute "UPDATE #{table_name} SET closed = #{true_value} WHERE state = 'cloesd'"
Milestone.where(state: :closed).update_all(closed: true)
end end
private
def table_name
Milestone.table_name
end end
end end
class ConvertMergeStatusInMergeRequest < ActiveRecord::Migration class ConvertMergeStatusInMergeRequest < ActiveRecord::Migration
def up def up
MergeRequest.transaction do execute "UPDATE #{table_name} SET new_merge_status = 'unchecked' WHERE merge_status = 1"
MergeRequest.where(merge_status: 1).update_all("new_merge_status = 'unchecked'") execute "UPDATE #{table_name} SET new_merge_status = 'can_be_merged' WHERE merge_status = 2"
MergeRequest.where(merge_status: 2).update_all("new_merge_status = 'can_be_merged'") execute "UPDATE #{table_name} SET new_merge_status = 'cannot_be_merged' WHERE merge_status = 3"
MergeRequest.where(merge_status: 3).update_all("new_merge_status = 'cannot_be_merged'")
end
end end
def down def down
MergeRequest.transaction do execute "UPDATE #{table_name} SET merge_status = 1 WHERE new_merge_status = 'unchecked'"
MergeRequest.where(new_merge_status: :unchecked).update_all("merge_status = 1") execute "UPDATE #{table_name} SET merge_status = 2 WHERE new_merge_status = 'can_be_merged'"
MergeRequest.where(new_merge_status: :can_be_merged).update_all("merge_status = 2") execute "UPDATE #{table_name} SET merge_status = 3 WHERE new_merge_status = 'cannot_be_merged'"
MergeRequest.where(new_merge_status: :cannot_be_merged).update_all("merge_status = 3")
end end
private
def table_name
MergeRequest.table_name
end end
end end
class AllowMergesForForks < ActiveRecord::Migration class AllowMergesForForks < ActiveRecord::Migration
def self.up def self.up
add_column :merge_requests, :target_project_id, :integer, :null => true add_column :merge_requests, :target_project_id, :integer, :null => true
MergeRequest.update_all("target_project_id = project_id") execute "UPDATE #{table_name} SET target_project_id = project_id"
change_column :merge_requests, :target_project_id, :integer, :null => false change_column :merge_requests, :target_project_id, :integer, :null => false
rename_column :merge_requests, :project_id, :source_project_id rename_column :merge_requests, :project_id, :source_project_id
end end
...@@ -10,4 +10,10 @@ class AllowMergesForForks < ActiveRecord::Migration ...@@ -10,4 +10,10 @@ class AllowMergesForForks < ActiveRecord::Migration
remove_column :merge_requests, :target_project_id remove_column :merge_requests, :target_project_id
rename_column :merge_requests, :source_project_id,:project_id rename_column :merge_requests, :source_project_id,:project_id
end end
private
def table_name
MergeRequest.table_name
end
end end
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment