lib/tasks/statesman.rake in statesman-3.2.0 vs lib/tasks/statesman.rake in statesman-3.3.0
- old
+ new
@@ -6,24 +6,29 @@
abort("Parent model name must be specified") unless parent_model_name
parent_class = parent_model_name.constantize
transition_class = parent_class.transition_class
parent_fk = "#{parent_model_name.demodulize.underscore}_id"
+ updated_at = if ActiveRecord::Base.default_timezone == :utc
+ Time.now.utc
+ else
+ Time.now
+ end
total_models = parent_class.count
done_models = 0
batch_size = 500
parent_class.find_in_batches(batch_size: batch_size) do |models|
ActiveRecord::Base.transaction do
if Statesman::Adapters::ActiveRecord.database_supports_partial_indexes?
# Set all transitions' most_recent to FALSE
transition_class.where(parent_fk => models.map(&:id)).
- update_all(most_recent: false)
+ update_all(most_recent: false, updated_at: updated_at)
else
transition_class.where(parent_fk => models.map(&:id)).
- update_all(most_recent: nil)
+ update_all(most_recent: nil, updated_at: updated_at)
end
# Set current transition's most_recent to TRUE
initial_t = transition_class.arel_table
subsequent_t = initial_t.alias
@@ -42,10 +47,11 @@
project(initial_t[:id]).to_sql
latest_ids = transition_class.find_by_sql(latest_ids_query).
to_a.collect(&:id)
- transition_class.where(id: latest_ids).update_all(most_recent: true)
+ transition_class.where(id: latest_ids).
+ update_all(most_recent: true, updated_at: updated_at)
end
done_models += batch_size
puts "Updated #{transition_class.name.pluralize} for "\
"#{[done_models, total_models].min}/#{total_models} "\