From 34dc7493445d00d180fe67369175962d6cb2da20 Mon Sep 17 00:00:00 2001 From: Max Wofford Date: Wed, 5 Mar 2025 02:23:37 -0500 Subject: [PATCH] Just create new hash instead of enforcing unique hash --- .../generate_unique_heartbeat_hashes_job.rb | 20 ++++++++++++++++++ ..._uniqueness_index_to_hash_on_heartbeats.rb | 21 ++----------------- 2 files changed, 22 insertions(+), 19 deletions(-) create mode 100644 app/jobs/one_time/generate_unique_heartbeat_hashes_job.rb diff --git a/app/jobs/one_time/generate_unique_heartbeat_hashes_job.rb b/app/jobs/one_time/generate_unique_heartbeat_hashes_job.rb new file mode 100644 index 0000000..6e61ce0 --- /dev/null +++ b/app/jobs/one_time/generate_unique_heartbeat_hashes_job.rb @@ -0,0 +1,20 @@ +class OneTime::GenerateUniqueHeartbeatHashesJob < ApplicationJob + queue_as :default + + def perform + Heartbeat.find_each do |heartbeat| + heartbeat.send(:set_fields_hash!) + heartbeat.save! + end + + # error if any two heartbeats have the same fields_hash + duplicates = false + + Heartbeat.group(:fields_hash).having("count(*) > 1").count.each do |fields_hash, count| + puts "Duplicate fields_hash: #{fields_hash} (count: #{count})" + duplicates = true + end + + raise "Duplicate in fields_hash" if duplicates + end +end diff --git a/db/migrate/20250305061242_uniqueness_index_to_hash_on_heartbeats.rb b/db/migrate/20250305061242_uniqueness_index_to_hash_on_heartbeats.rb index fd71934..f0ad62a 100644 --- a/db/migrate/20250305061242_uniqueness_index_to_hash_on_heartbeats.rb +++ b/db/migrate/20250305061242_uniqueness_index_to_hash_on_heartbeats.rb @@ -24,28 +24,11 @@ class UniquenessIndexToHashOnHeartbeats < ActiveRecord::Migration[8.0] :is_write ] - add_column :heartbeats, :fields_hash, :text - - Heartbeat.find_each do |heartbeat| - heartbeat.send(:set_fields_hash!) - heartbeat.save! - end - - # error if any two heartbeats have the same fields_hash - duplicates = false - Heartbeat.group(:fields_hash).having("count(*) > 1").count.each do |fields_hash, count| - puts "Duplicate fields_hash: #{fields_hash} (count: #{count})" - duplicates = true - end - - raise "Duplicate in fields_hash" if duplicates - - change_column_null :heartbeats, :fields_hash, false - add_index :heartbeats, :fields_hash, unique: true - # clean up the index from ./20250303180842_create_heartbeats.rb remove_index :heartbeats, attributes, unique: true + + add_column :heartbeats, :fields_hash, :text end end