2022-07-05 16:11:45 +02:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
|
|
|
# Previously, only the reserved slots were saved in DB, other slots were created on the fly.
|
|
|
|
# Now we save all slots in DB, so we must re-create slots for the existing availabilities
|
|
|
|
class InsertMissingSlots < ActiveRecord::Migration[5.2]
|
|
|
|
def up
|
2022-07-12 17:46:01 +02:00
|
|
|
Availability.where(available_type: %w[machines space]).each do |availability|
|
2022-07-05 16:11:45 +02:00
|
|
|
slot_duration = availability.slot_duration || Setting.get('slot_duration').to_i
|
|
|
|
|
|
|
|
((availability.end_at - availability.start_at) / slot_duration.minutes).to_i.times do |i|
|
|
|
|
Slot.find_or_create_by(
|
|
|
|
start_at: availability.start_at + (i * slot_duration).minutes,
|
|
|
|
end_at: availability.start_at + (i * slot_duration).minutes + slot_duration.minutes,
|
|
|
|
availability_id: availability.id
|
|
|
|
)
|
|
|
|
end
|
|
|
|
end
|
2022-07-12 17:46:01 +02:00
|
|
|
|
|
|
|
Availability.where(available_type: %w[training event]).each do |availability|
|
|
|
|
Slot.find_or_create_by(
|
|
|
|
start_at: availability.start_at,
|
|
|
|
end_at: availability.end_at,
|
|
|
|
availability_id: availability.id
|
|
|
|
)
|
|
|
|
end
|
2022-07-05 16:11:45 +02:00
|
|
|
end
|
|
|
|
|
|
|
|
def down
|
|
|
|
Slot.where.not(id: SlotsReservation.all.map(&:slot_id)).each(&:destroy)
|
|
|
|
end
|
|
|
|
end
|