Skip to content

Commit a94c7dc

Browse files
committed
Add index to created_at on attachment and pictures tables
We have the recent scope on the attachment and pictures models. Also the picture archive now defaults to sorting by recent images. On large projects it should be a good idea to have an index on these columns. It will add the index concurrently on Postgres.
1 parent 7e196d6 commit a94c7dc

3 files changed

Lines changed: 32 additions & 1 deletion

File tree

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
class AddCreatedAtIndexToPicturesAndAttachments < ActiveRecord::Migration[7.1]
2+
disable_ddl_transaction! if connection.adapter_name.match?(/postgres/i)
3+
4+
def change
5+
add_index :alchemy_pictures, :created_at, if_not_exists: true, algorithm: algorithm
6+
add_index :alchemy_attachments, :created_at, if_not_exists: true, algorithm: algorithm
7+
end
8+
9+
private
10+
11+
def algorithm
12+
connection.adapter_name.match?(/postgres/i) ? :concurrently : nil
13+
end
14+
end
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# This migration comes from alchemy (originally 20250905140323)
2+
class AddCreatedAtIndexToPicturesAndAttachments < ActiveRecord::Migration[7.1]
3+
disable_ddl_transaction! if connection.adapter_name.match?(/postgres/i)
4+
5+
def change
6+
add_index :alchemy_pictures, :created_at, if_not_exists: true, algorithm: algorithm
7+
add_index :alchemy_attachments, :created_at, if_not_exists: true, algorithm: algorithm
8+
end
9+
10+
private
11+
12+
def algorithm
13+
connection.adapter_name.match?(/postgres/i) ? :concurrently : nil
14+
end
15+
end

spec/dummy/db/schema.rb

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010
#
1111
# It's strongly recommended that you check this file into your version control system.
1212

13-
ActiveRecord::Schema[8.0].define(version: 2025_06_26_160417) do
13+
ActiveRecord::Schema[8.0].define(version: 2025_09_05_140502) do
1414
create_table "active_storage_attachments", force: :cascade do |t|
1515
t.string "name", null: false
1616
t.string "record_type", null: false
@@ -49,6 +49,7 @@
4949
t.datetime "created_at", null: false
5050
t.datetime "updated_at", null: false
5151
t.string "file_uid"
52+
t.index ["created_at"], name: "index_alchemy_attachments_on_created_at"
5253
t.index ["creator_id"], name: "index_alchemy_attachments_on_creator_id"
5354
t.index ["file_uid"], name: "index_alchemy_attachments_on_file_uid"
5455
t.index ["updater_id"], name: "index_alchemy_attachments_on_updater_id"
@@ -246,6 +247,7 @@
246247
t.string "image_file_uid"
247248
t.integer "image_file_size"
248249
t.string "image_file_format"
250+
t.index ["created_at"], name: "index_alchemy_pictures_on_created_at"
249251
t.index ["creator_id"], name: "index_alchemy_pictures_on_creator_id"
250252
t.index ["image_file_name"], name: "index_alchemy_pictures_on_image_file_name"
251253
t.index ["name"], name: "index_alchemy_pictures_on_name"

0 commit comments

Comments
 (0)