deduplicar arduinos
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful

This commit is contained in:
f 2022-05-14 18:59:22 -03:00
parent e085d5345d
commit a7809d13e8

View file

@ -0,0 +1,21 @@
# frozen_string_literal: true
# Elimina arduinos multiplicados
class DeduplicateArduinos < ActiveRecord::Migration[6.1]
def up
Raspberry.find_each do |raspberry|
unique_local_ids = raspberry.arduinos.distinct(:local_id).pluck(:local_id)
unique_local_ids.each do |unique_local_id|
arduinos = raspberry.arduinos.where(local_id: unique_local_id)
unique_arduino = arduinos.first
duplicated_arduinos = arduinos.where.not(id: unique_arduino.id)
result = Sensor.where(arduino_id: duplicated_arduinos.pluck(:id)).update_all(arduino_id: unique_arduino.id)
duplicated_arduinos.destroy_all if result
end
end
end
def down; end
end