20240416163429_calendar_public_holiday_cleanup.rb 751 B

12345678910111213141516171819202122
  1. # Copyright (C) 2012-2024 Zammad Foundation, https://zammad-foundation.org/
  2. class CalendarPublicHolidayCleanup < ActiveRecord::Migration[7.0]
  3. def change
  4. # return if it's a new setup
  5. return if !Setting.exists?(name: 'system_init_done')
  6. Calendar.find_each do |calendar|
  7. next if calendar.public_holidays.blank? && calendar.ical_url.present?
  8. cache_key = "CalendarIcal::#{calendar.id}"
  9. Rails.cache.delete(cache_key) if Rails.cache.exist?(cache_key)
  10. checksum = Digest::MD5.hexdigest(calendar.ical_url.to_s)
  11. public_holidays_to_keep = calendar.public_holidays.reject { |_, info| info['feed'] == checksum }
  12. calendar.update!(public_holidays: public_holidays_to_keep)
  13. calendar.sync
  14. end
  15. end
  16. end