# Spreads demo record timestamps over the last two weeks (working hours), so the activity page, # "updated" columns and history show different days and times. Deterministic (based on ids), # safe to run again. Called by seed.rb; can also run alone: # docker compose exec redmine bin/rails runner /seed/spread_activity.rb now = Time.current today = Date.current DAYS = 14 # Day `days_ago` at a working-hour time derived from `n` at = lambda do |days_ago, n| (today - days_ago).in_time_zone.change(hour: 8 + (n * 7) % 10, min: (n * 13) % 60) end not_future = ->(t, n) { t < now ? t : now - (n % 90 + 5).minutes } ActiveRecord::Base.transaction do Issue.order(:id).each_with_index do |issue, i| created = at.(DAYS - 1 - (i % DAYS), i) last = created issue.journals.order(:id).each do |journal| t = not_future.(created + (1 + journal.id % 3).days + (journal.id * 17 % 300).minutes, journal.id) journal.update_columns(created_on: t, updated_on: t) last = [last, t].max end closed = issue.closed? ? [last, created + 1.day].max : nil issue.update_columns(created_on: created, updated_on: last, closed_on: closed && not_future.(closed, issue.id)) end TimeEntry.find_each do |entry| t = not_future.(entry.spent_on.in_time_zone.change(hour: 17, min: entry.id * 7 % 60), entry.id) entry.update_columns(created_on: t, updated_on: t) end Attachment.find_each { |a| a.update_columns(created_on: not_future.(a.container.try(:created_on).to_time + 2.hours, a.id)) if a.container } WikiContentVersion.order(:id).each_with_index do |version, i| t = at.(9 - i * 2, version.id) version.update_columns(updated_on: t) version.page.content.update_columns(updated_on: t) if version.version == version.page.content.version end WikiPage.find_each { |page| page.update_columns(created_on: page.content.versions.minimum(:updated_on) || page.created_on) } News.find_each do |news| news.update_columns(created_on: at.(6, news.id)) news.comments.each_with_index { |c, i| c.update_columns(created_on: at.(5 - i, c.id), updated_on: at.(5 - i, c.id)) } end Message.where(parent_id: nil).find_each do |topic| topic.update_columns(created_on: at.(4, topic.id), updated_on: at.(4, topic.id)) topic.children.each_with_index do |reply, i| t = not_future.(at.(3 - i, reply.id), reply.id) reply.update_columns(created_on: t, updated_on: t) topic.update_columns(updated_on: t) end end Document.find_each { |d| d.update_columns(created_on: at.(8, d.id)) } end puts "Spread activity over the last #{DAYS} days."