2019-05-11 09:26:46 +00:00
defmodule Pleroma.Repo.Migrations.AddFtsIndexToObjectsTwo do
use Ecto.Migration
def up do
2019-05-15 13:28:01 +00:00
execute ( " create extension if not exists rum " )
2019-05-11 09:26:46 +00:00
drop_if_exists index ( :objects , [ " (to_tsvector('english', data->>'content')) " ] , using : :gin , name : :objects_fts )
alter table ( :objects ) do
add ( :fts_content , :tsvector )
end
execute ( " CREATE FUNCTION objects_fts_update() RETURNS trigger AS $$
begin
new . fts_content := to_tsvector ( ' english ' , new . data -> > ' content ' ) ;
return new ;
end
$ $ LANGUAGE plpgsql " )
2019-07-01 01:08:07 +00:00
execute ( " create index if not exists objects_fts on objects using RUM (fts_content rum_tsvector_addon_ops, inserted_at) with (attach = 'inserted_at', to = 'fts_content'); " )
2019-05-11 09:26:46 +00:00
execute ( " CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE ON objects
FOR EACH ROW EXECUTE PROCEDURE objects_fts_update ( ) " )
execute ( " UPDATE objects SET updated_at = NOW() " )
end
def down do
2019-07-01 01:08:07 +00:00
execute " drop index if exists objects_fts "
execute " drop trigger if exists tsvectorupdate on objects "
execute " drop function if exists objects_fts_update() "
2019-05-11 09:26:46 +00:00
alter table ( :objects ) do
remove ( :fts_content , :tsvector )
end
2019-07-01 01:08:07 +00:00
create_if_not_exists index ( :objects , [ " (to_tsvector('english', data->>'content')) " ] , using : :gin , name : :objects_fts )
2019-05-11 09:26:46 +00:00
end
end