deduplicar arduinos
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
All checks were successful
ci/woodpecker/push/woodpecker Pipeline was successful
This commit is contained in:
parent
e085d5345d
commit
a7809d13e8
1 changed files with 21 additions and 0 deletions
21
db/migrate/20220514213623_deduplicate_arduinos.rb
Normal file
21
db/migrate/20220514213623_deduplicate_arduinos.rb
Normal 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
|
Loading…
Reference in a new issue