2017-06-30 16:23:28 +08:00
|
|
|
class AddExperimentToReportElements < ActiveRecord::Migration[4.2]
|
2016-08-04 19:19:46 +08:00
|
|
|
def up
|
2016-08-03 18:49:24 +08:00
|
|
|
add_column :report_elements, :experiment_id, :integer
|
|
|
|
add_foreign_key :report_elements, :experiments
|
|
|
|
add_index :report_elements, :experiment_id
|
2016-08-04 19:19:46 +08:00
|
|
|
|
|
|
|
Project.find_each do |project|
|
|
|
|
experiment = project.experiments.first
|
|
|
|
|
|
|
|
next unless experiment && project.reports.count > 0
|
|
|
|
project.reports.each do |report|
|
|
|
|
project_element = report.report_elements.where(type_of: 0).first
|
|
|
|
module_elements = report.report_elements.where(type_of: 1)
|
|
|
|
|
|
|
|
next unless project_element
|
|
|
|
experiment_element = report.report_elements.create(
|
|
|
|
type_of: :experiment,
|
|
|
|
experiment: experiment,
|
|
|
|
position: 0
|
|
|
|
)
|
|
|
|
|
|
|
|
next unless module_elements.count > 0
|
|
|
|
module_elements.each do |element|
|
|
|
|
element.parent_id = experiment_element.id
|
|
|
|
element.save!
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
Project.find_each do |project|
|
|
|
|
next unless project.reports.count > 0
|
|
|
|
project.reports.each do |report|
|
|
|
|
experiment_elements = report.report_elements.where(type_of: 15)
|
|
|
|
module_elements = report.report_elements.where(type_of: 1)
|
|
|
|
|
|
|
|
next unless module_elements.count > 0
|
|
|
|
module_elements.each do |element|
|
|
|
|
element.parent_id = nil
|
|
|
|
element.save!
|
|
|
|
end
|
|
|
|
experiment_elements.destroy_all
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
remove_index :report_elements, :experiment_id
|
|
|
|
remove_foreign_key :report_elements, :experiments
|
|
|
|
remove_column :report_elements, :experiment_id
|
2016-08-03 18:49:24 +08:00
|
|
|
end
|
|
|
|
end
|