docbrown/db/migrate/20210609103939_create_trigger_articles_insert_update1.rb
rhymes 4bc56b8ac0
Search results: prioritize articles titles (#13918)
* Add HairTrigger spec

* Add weights to columns tsvectors

* Add correct sorting for search results

* Remove Timecop.travel

* Changed order of columns in trigger based on weights
2021-06-09 14:28:19 +02:00

69 lines
2.7 KiB
Ruby

# This migration was auto-generated via `rake db:generate_trigger_migration'.
# While you can edit this file, any changes you make to the definitions here
# will be undone by the next auto-generated trigger migration.
class CreateTriggerArticlesInsertUpdate1 < ActiveRecord::Migration[6.1]
def up
drop_trigger("update_reading_list_document", "articles", :generated => true)
create_trigger("update_reading_list_document", :generated => true, :compatibility => 1).
on("articles").
name("update_reading_list_document").
before(:insert, :update).
for_each(:row).
declare("l_org_vector tsvector; l_user_vector tsvector") do
<<-SQL_ACTIONS
NEW.reading_list_document :=
setweight(to_tsvector('simple'::regconfig, unaccent(coalesce(NEW.title, ''))), 'A') ||
setweight(to_tsvector('simple'::regconfig, unaccent(coalesce(NEW.cached_tag_list, ''))), 'B') ||
setweight(to_tsvector('simple'::regconfig, unaccent(coalesce(NEW.body_markdown, ''))), 'C') ||
setweight(to_tsvector('simple'::regconfig, unaccent(coalesce(NEW.cached_user_name, ''))), 'D') ||
setweight(to_tsvector('simple'::regconfig, unaccent(coalesce(NEW.cached_user_username, ''))), 'D') ||
setweight(to_tsvector('simple'::regconfig,
unaccent(
coalesce(
array_to_string(
-- cached_organization is serialized to the DB as a YAML string, we extract only the name attribute
regexp_match(NEW.cached_organization, 'name: (.*)$', 'n'),
' '
),
''
)
)
), 'D');
SQL_ACTIONS
end
end
def down
drop_trigger("update_reading_list_document", "articles", :generated => true)
create_trigger("update_reading_list_document", :generated => true, :compatibility => 1).
on("articles").
name("update_reading_list_document").
before(:insert, :update).
for_each(:row).
declare("l_org_vector tsvector; l_user_vector tsvector") do
<<-SQL_ACTIONS
NEW.reading_list_document :=
to_tsvector('simple'::regconfig, unaccent(coalesce(NEW.body_markdown, ''))) ||
to_tsvector('simple'::regconfig, unaccent(coalesce(NEW.cached_tag_list, ''))) ||
to_tsvector('simple'::regconfig, unaccent(coalesce(NEW.cached_user_name, ''))) ||
to_tsvector('simple'::regconfig, unaccent(coalesce(NEW.cached_user_username, ''))) ||
to_tsvector('simple'::regconfig, unaccent(coalesce(NEW.title, ''))) ||
to_tsvector('simple'::regconfig,
unaccent(
coalesce(
array_to_string(
-- cached_organization is serialized to the DB as a YAML string, we extract only the name attribute
regexp_match(NEW.cached_organization, 'name: (.*)$', 'n'),
' '
),
''
)
)
);
SQL_ACTIONS
end
end
end