2
0
Fork 0
mirror of https://github.com/discourse/discourse.git synced 2025-10-03 17:21:20 +08:00

DEV: Rerun AddIndexToPostStatComposerColumns migration (#31765)

The previous version of the migration is not idempotent and can result
in indexes created but marked as "invalid" by Postgres.

Per postgres documentation:

If a problem arises while scanning the table, such as a deadlock or a
uniqueness violation in a unique index,
the CREATE INDEX command will fail but leave behind an "invalid" index.
This index will be ignored for querying
purposes because it might be incomplete; however it will still consume
update overhead. The recommended recovery
  method in such cases is to drop the index and try again to perform
  CREATE INDEX CONCURRENTLY.
This commit is contained in:
Alan Guo Xiang Tan 2025-03-12 11:49:51 +08:00 committed by GitHub
parent cf4d80d0b3
commit 5ddae1c574
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 16 additions and 19 deletions

View file

@ -1,19 +0,0 @@
# frozen_string_literal: true

class AddIndexToPostStatComposerColumns < ActiveRecord::Migration[7.2]
disable_ddl_transaction!

def up
if !index_exists?(:post_stats, :composer_version)
add_index :post_stats, :composer_version, algorithm: :concurrently
end
if !index_exists?(:post_stats, :writing_device)
add_index :post_stats, :writing_device, algorithm: :concurrently
end
end

def down
remove_index :post_stats, :composer_version if index_exists?(:post_stats, :composer_version)
remove_index :post_stats, :writing_device if index_exists?(:post_stats, :writing_device)
end
end

View file

@ -0,0 +1,16 @@
# frozen_string_literal: true

class AddIndexToPostStatComposerColumns < ActiveRecord::Migration[7.2]
disable_ddl_transaction!

def up
remove_index :post_stats, :composer_version, algorithm: :concurrently, if_exists: true
remove_index :post_stats, :writing_device, algorithm: :concurrently, if_exists: true
add_index :post_stats, :composer_version, algorithm: :concurrently
add_index :post_stats, :writing_device, algorithm: :concurrently
end

def down
raise ActiveRecord::IrreversibleMigration
end
end