diff --git a/app/models/discourse_translator/post_locale.rb b/app/models/discourse_translator/post_locale.rb index 305c7b62..afbaa2ac 100644 --- a/app/models/discourse_translator/post_locale.rb +++ b/app/models/discourse_translator/post_locale.rb @@ -21,3 +21,7 @@ class PostLocale < ActiveRecord::Base # created_at :datetime not null # updated_at :datetime not null # +# Indexes +# +# index_discourse_translator_post_locales_on_post_id (post_id) UNIQUE +# diff --git a/db/migrate/20250630040849_add_index_post_locale_post_id.rb b/db/migrate/20250630040849_add_index_post_locale_post_id.rb new file mode 100644 index 00000000..becff146 --- /dev/null +++ b/db/migrate/20250630040849_add_index_post_locale_post_id.rb @@ -0,0 +1,21 @@ +# frozen_string_literal: true + +class AddIndexPostLocalePostId < ActiveRecord::Migration[7.2] + disable_ddl_transaction! + + def up + # clean up invalid index if index creation timeout + execute <<~SQL + DROP INDEX IF EXISTS index_discourse_translator_post_locales_on_post_id + SQL + + execute <<~SQL + CREATE UNIQUE INDEX CONCURRENTLY index_discourse_translator_post_locales_on_post_id + ON discourse_translator_post_locales (post_id) + SQL + end + + def down + raise ActiveRecord::IrreversibleMigration + end +end diff --git a/plugin.rb b/plugin.rb index 2258e359..f5976b04 100644 --- a/plugin.rb +++ b/plugin.rb @@ -31,6 +31,10 @@ module ::DiscourseTranslator Topic.prepend(DiscourseTranslator::Extensions::TopicExtension) end + TopicView.on_preload do |topic_view| + topic_view.instance_variable_set(:@posts, topic_view.posts.includes(:content_locale)) + end + add_to_serializer :post, :can_translate do scope.can_translate?(object) end