-
-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #328 from fablabbcn/bugfix-duplicate-components-ta…
…ke-2 Bugfix duplicate components take 2
- Loading branch information
Showing
12 changed files
with
49 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,40 @@ | ||
namespace :components do | ||
task :remove_duplicates => :environment do | ||
puts "WARNING: This task is potentially destructive. Before continuing please ensure that you have made a database backup. Press 'y' to continue or any other key to exit." | ||
ch = STDIN.getch | ||
unless ch == "y" | ||
abort | ||
end | ||
|
||
pre_migrate_sensor_counts = ActiveRecord::Base.connection.execute( | ||
"SELECT device_id, count(distinct sensor_id) FROM components GROUP BY device_id" | ||
).to_a | ||
|
||
devices_and_sensors_with_dupes = ActiveRecord::Base.connection.execute( | ||
"SELECT device_id, sensor_id FROM components GROUP BY device_id, sensor_id HAVING count(id) > 1" | ||
) | ||
|
||
removed = 0 | ||
|
||
devices_and_sensors_with_dupes.each do |record| | ||
device_id = record["device_id"] | ||
sensor_id = record["sensor_id"] | ||
last_updated_component = Component.where(device_id: device_id, sensor_id: sensor_id).order("last_reading_at DESC").first | ||
all_components = Component.where(device_id: device_id, sensor_id: sensor_id, key: last_updated_component.key).all | ||
components_to_remove = all_components - [last_updated_component] | ||
removed += components_to_remove.length | ||
components_to_remove.each(&:destroy!) | ||
end | ||
|
||
post_migrate_sensor_counts = ActiveRecord::Base.connection.execute( | ||
"SELECT device_id, count(distinct sensor_id) FROM components GROUP BY device_id" | ||
).to_a | ||
|
||
if pre_migrate_sensor_counts == post_migrate_sensor_counts | ||
puts "components deduplicated ok, #{removed} components deleted for #{devices_and_sensors_with_dupes.to_a.length} device/sensor pairs." | ||
else | ||
raise "Number of sensors per device pre deduplication does not match number post duplication. Please revert to the db backup and inspect." | ||
end | ||
end | ||
end | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters