Skip to content
Snippets Groups Projects
Commit e9dc20ca authored by Heinrich Lee Yu's avatar Heinrich Lee Yu Committed by Michael Kozono
Browse files

Update full-text search index when moving issues

When moving issues, we do several updates to the new issue within an
outer transaction. This caused `saved_changes` to return only the
changes of the latest update even if more changes were committed.

This meant `#update_search_data!` wasn't run even though we saved the
title and description of the issue.
parent 97455d6b
No related branches found
No related tags found
1 merge request!85886Update full-text search index when moving issues
......@@ -80,6 +80,15 @@ def pg_full_text_searchable(columns:)
pg_full_text_searchable_columns[column[:name]] = column[:weight]
end
# When multiple updates are done in a transaction, `saved_changes` will only report the latest save
# and we may miss an update to the searchable columns.
# As a workaround, we set a dirty flag here and update the search data in `after_save_commit`.
after_save do
next unless pg_full_text_searchable_columns.keys.any? { |f| saved_changes.has_key?(f) }
@update_pg_full_text_search_data = true
end
# We update this outside the transaction because this could raise an error if the resulting tsvector
# is too long. When that happens, we still persist the create / update but the model will not have a
# search data record. This is fine in most cases because this is a very rare occurrence and only happens
......@@ -87,9 +96,8 @@ def pg_full_text_searchable(columns:)
#
# We also do not want to use a subtransaction here due to: https://gitlab.com/groups/gitlab-org/-/epics/6540
after_save_commit do
next unless pg_full_text_searchable_columns.keys.any? { |f| saved_changes.has_key?(f) }
update_search_data!
update_search_data! if @update_pg_full_text_search_data
@update_pg_full_text_search_data = nil
end
end
......
......@@ -54,12 +54,23 @@ def self.name
end
context 'when specified columns are not changed' do
it 'does not enqueue worker' do
it 'does not call update_search_data!' do
expect(model).not_to receive(:update_search_data!)
model.update!(description: 'A new description')
end
end
context 'when model is updated twice within a transaction' do
it 'calls update_search_data!' do
expect(model).to receive(:update_search_data!)
model.transaction do
model.update!(title: 'A new title')
model.update!(updated_at: Time.current)
end
end
end
end
describe '.pg_full_text_search' do
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment