socialforge/db/migrate/20170220065632_homework_ban...

46 lines
2.2 KiB
Ruby
Raw Normal View History

2017-02-24 10:32:48 +08:00
class HomeworkBankMigration < ActiveRecord::Migration
def up
add_column :homework_commons, :homework_bank_id, :integer
add_column :homework_commons, :is_update, :boolean, :default => false
count =Course.all.count / 30 + 2
transaction do
for i in 1 ... count do i
Course.page(i).per(30).each do |course|
if course.is_delete == 0
course.homework_commons.each do |homework|
unless homework.user.homework_banks.select("name").to_s.include?(homework.name)
homework_bank = HomeworkBank.create(:user_id => homework.user_id, :name => homework.name, :description => homework.description, :homework_type => homework.homework_type,
:quotes => 1, :is_public => course.is_public, :applicable_syllabus => course.syllabus.title, :homework_common_id => homework.id, :created_at => homework.created_at)
2017-02-24 10:32:48 +08:00
if homework.homework_type == 2 && homework.homework_detail_programing
homework_bank.update_attributes(:language => homework.homework_detail_programing.language, :standard_code => homework.homework_detail_programing.standard_code)
homework.homework_tests.each do |test|
homework_bank.homework_bank_tests << HomeworkBankTest.create(
input: test.input,
output: test.output,
test_type: true
)
end
homework.homework_samples.each do |test|
homework_bank.homework_bank_tests << HomeworkBankTest.create(
input: test.input,
output: test.output,
test_type: false
)
end
elsif homework.homework_type == 3 && homework.homework_detail_group
homework_bank.update_attributes(:min_num => homework.homework_detail_group.min_num, :max_num => homework.homework_detail_group.max_num, :base_on_project => homework.homework_detail_group.base_on_project)
end
homework.update_column('homework_bank_id', homework_bank.id)
end
end
end
end
end
end
end
def down
end
end