1
0
mirror of https://github.com/LaCasemate/fab-manager.git synced 2024-12-10 21:24:20 +01:00
fab-manager/db/migrate/20220705125232_insert_missing_slots.rb
2022-07-18 17:18:01 +02:00

32 lines
1.1 KiB
Ruby

# 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
Availability.where(available_type: %w[machines space]).each do |availability|
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
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
end
def down
Slot.where.not(id: SlotsReservation.all.map(&:slot_id)).each(&:destroy)
end
end