From 22276a25c82bb8202381681876584de607171acd Mon Sep 17 00:00:00 2001 From: "Stanko K.R." Date: Thu, 12 Feb 2026 11:36:35 +0100 Subject: [PATCH] Index notifications on updated_at As we now update notifications with unread counts we sort them by the time they were updated instead of when they were created. To keep that performant we also have to change the previous indices that used created_at to use updated_at. --- .../20260212102026_fix_notifications_ordered_index.rb | 8 ++++++++ db/schema.rb | 4 ++-- db/schema_sqlite.rb | 4 ++-- 3 files changed, 12 insertions(+), 4 deletions(-) create mode 100644 db/migrate/20260212102026_fix_notifications_ordered_index.rb diff --git a/db/migrate/20260212102026_fix_notifications_ordered_index.rb b/db/migrate/20260212102026_fix_notifications_ordered_index.rb new file mode 100644 index 000000000..f7e3cfcac --- /dev/null +++ b/db/migrate/20260212102026_fix_notifications_ordered_index.rb @@ -0,0 +1,8 @@ +class FixNotificationsOrderedIndex < ActiveRecord::Migration[8.2] + def change + add_index :notifications, [ :user_id, :read_at, :updated_at ], + order: { read_at: :desc, updated_at: :desc }, + name: "index_notifications_on_user_id_and_read_at_and_updated_at" + remove_index :notifications, name: "index_notifications_on_user_id_and_read_at_and_created_at" + end +end diff --git a/db/schema.rb b/db/schema.rb index db1a2283d..a46444460 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.2].define(version: 2026_02_11_122517) do +ActiveRecord::Schema[8.2].define(version: 2026_02_12_102026) do create_table "accesses", id: :uuid, charset: "utf8mb4", collation: "utf8mb4_0900_ai_ci", force: :cascade do |t| t.datetime "accessed_at" t.uuid "account_id", null: false @@ -398,7 +398,7 @@ ActiveRecord::Schema[8.2].define(version: 2026_02_11_122517) do t.index ["creator_id"], name: "index_notifications_on_creator_id" t.index ["source_type", "source_id"], name: "index_notifications_on_source" t.index ["user_id", "card_id"], name: "index_notifications_on_user_id_and_card_id", unique: true - t.index ["user_id", "read_at", "created_at"], name: "index_notifications_on_user_id_and_read_at_and_created_at", order: { read_at: :desc, created_at: :desc } + t.index ["user_id", "read_at", "updated_at"], name: "index_notifications_on_user_id_and_read_at_and_updated_at", order: { read_at: :desc, updated_at: :desc } t.index ["user_id"], name: "index_notifications_on_user_id" end diff --git a/db/schema_sqlite.rb b/db/schema_sqlite.rb index 3486ca9d1..4254c30f1 100644 --- a/db/schema_sqlite.rb +++ b/db/schema_sqlite.rb @@ -10,7 +10,7 @@ # # It's strongly recommended that you check this file into your version control system. -ActiveRecord::Schema[8.2].define(version: 2026_02_11_122517) do +ActiveRecord::Schema[8.2].define(version: 2026_02_12_102026) do create_table "accesses", id: :uuid, force: :cascade do |t| t.datetime "accessed_at" t.uuid "account_id", null: false @@ -398,7 +398,7 @@ ActiveRecord::Schema[8.2].define(version: 2026_02_11_122517) do t.index ["creator_id"], name: "index_notifications_on_creator_id" t.index ["source_type", "source_id"], name: "index_notifications_on_source" t.index ["user_id", "card_id"], name: "index_notifications_on_user_id_and_card_id", unique: true - t.index ["user_id", "read_at", "created_at"], name: "index_notifications_on_user_id_and_read_at_and_created_at", order: { read_at: :desc, created_at: :desc } + t.index ["user_id", "read_at", "updated_at"], name: "index_notifications_on_user_id_and_read_at_and_updated_at", order: { read_at: :desc, updated_at: :desc } t.index ["user_id"], name: "index_notifications_on_user_id" end