20250211122953_change_token_expiration_handling.rb 1.0 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. # Copyright (C) 2012-2025 Zammad Foundation, https://zammad-foundation.org/
  2. class ChangeTokenExpirationHandling < 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. migrate_table_column
  7. migrate_scheduler_job
  8. end
  9. def migrate_table_column
  10. change_table :tokens do |t|
  11. t.change :expires_at, :datetime, null: true, limit: 3
  12. end
  13. Token.reset_column_information
  14. Token.where.not(expires_at: nil).in_batches.each_record do |token|
  15. date = token.expires_at.to_date
  16. time = Time.use_zone(Setting.get('timezone_default')) { date.beginning_of_day }
  17. token.update! expires_at: time
  18. end
  19. end
  20. def migrate_scheduler_job
  21. Scheduler.create_or_update(
  22. name: 'Delete old token entries.',
  23. method: 'Token.cleanup',
  24. period: 1.day,
  25. prio: 2,
  26. active: true,
  27. last_run: Time.zone.now,
  28. updated_by_id: 1,
  29. created_by_id: 1,
  30. )
  31. end
  32. end