2014-06-04 09:19:01 +08:00
|
|
|
class AddCourseidToToMembers < ActiveRecord::Migration
|
|
|
|
def change
|
2014-06-16 11:34:56 +08:00
|
|
|
add_column :members, :course_id, :int,:default => -1
|
2014-06-04 11:59:26 +08:00
|
|
|
change_column_null(:members, :project_id, true)
|
|
|
|
remove_index :members, name: 'index_members_on_user_id_and_project_id'
|
|
|
|
add_index "members", ["user_id", "project_id","course_id"], :name => "index_members_on_user_id_and_project_id", :unique => true
|
|
|
|
|
2014-06-18 11:08:44 +08:00
|
|
|
#couese_id不能直接设置为project_id
|
|
|
|
#Member.where('project_id IN (SELECT id FROM projects WHERE project_type=1)').update_all("course_id = project_id")
|
|
|
|
#Member.where('project_id IN (SELECT id FROM projects WHERE project_type=1)').update_all(project_id: -1)
|
2014-06-04 09:19:01 +08:00
|
|
|
|
|
|
|
# 课程成员数据迁移
|
2014-06-18 11:08:44 +08:00
|
|
|
Member.all.each do |member|
|
|
|
|
project = Project.find_by_id(member.project_id)
|
|
|
|
if project && project.project_type == 1
|
|
|
|
member.course_id = project.course_extra.id
|
|
|
|
member.project_id = -1
|
2014-07-01 15:29:16 +08:00
|
|
|
member.save(:validate => false)
|
2014-06-18 11:08:44 +08:00
|
|
|
end
|
|
|
|
end
|
2014-06-04 09:19:01 +08:00
|
|
|
end
|
|
|
|
end
|