2017-06-30 16:23:28 +08:00
|
|
|
class AddCounterCacheToSamples < ActiveRecord::Migration[4.2]
|
2016-02-12 23:52:43 +08:00
|
|
|
def up
|
2019-01-07 20:29:17 +08:00
|
|
|
add_column :samples, :nr_of_modules_assigned_to, :integer, default: 0
|
|
|
|
add_column :my_modules, :nr_of_assigned_samples, :integer, default: 0
|
2016-02-12 23:52:43 +08:00
|
|
|
|
|
|
|
# Okay, now initialize the values
|
2019-01-07 20:29:17 +08:00
|
|
|
if defined?(Sample)
|
|
|
|
Sample.find_each do |sample|
|
|
|
|
Sample.reset_counters(sample.id, :sample_my_modules)
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
|
2019-01-07 20:29:17 +08:00
|
|
|
MyModule.find_each do |my_module|
|
|
|
|
MyModule.reset_counters(my_module.id, :sample_my_modules)
|
|
|
|
end
|
2016-02-12 23:52:43 +08:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
remove_column :samples, :nr_of_modules_assigned_to
|
|
|
|
remove_column :my_modules, :nr_of_assigned_samples
|
|
|
|
end
|
|
|
|
end
|