diff --git a/db/migrate/20250507183451_add_partial_unique_index_to_heartbeat_fields_hash.rb b/db/migrate/20250507183451_add_partial_unique_index_to_heartbeat_fields_hash.rb new file mode 100644 index 0000000..ac9653f --- /dev/null +++ b/db/migrate/20250507183451_add_partial_unique_index_to_heartbeat_fields_hash.rb @@ -0,0 +1,9 @@ +class AddPartialUniqueIndexToHeartbeatFieldsHash < ActiveRecord::Migration[8.0] + def change + # Add a partial index that only applies to non-deleted records + add_index :heartbeats, :fields_hash, + unique: true, + where: "deleted_at IS NULL", + name: "index_heartbeats_on_fields_hash_when_not_deleted" + end +end diff --git a/db/schema.rb b/db/schema.rb index 7ee6544..b331ca6 100644 --- a/db/schema.rb +++ b/db/schema.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.0].define(version: 2025_05_07_182617) do +ActiveRecord::Schema[8.0].define(version: 2025_05_07_183451) do # These are extensions that must be enabled in order to support this database enable_extension "pg_catalog.plpgsql" @@ -210,6 +210,7 @@ ActiveRecord::Schema[8.0].define(version: 2025_05_07_182617) do t.datetime "deleted_at" t.index ["category", "time"], name: "index_heartbeats_on_category_and_time" t.index ["fields_hash"], name: "index_heartbeats_on_fields_hash", unique: true + t.index ["fields_hash"], name: "index_heartbeats_on_fields_hash_when_not_deleted", unique: true, where: "(deleted_at IS NULL)" t.index ["user_id"], name: "index_heartbeats_on_user_id" end