2019-05-11 09:26:46 +00:00
defmodule Pleroma.Repo.Migrations.AddFtsIndexToObjectsTwo do
use Ecto.Migration
def up do
2020-06-22 14:27:49 +00:00
execute ( " create extension if not exists rum " )
2021-01-10 08:25:36 +00:00
drop_if_exists (
index ( :objects , [ " (to_tsvector('english', data->>'content')) " ] ,
using : :gin ,
name : :objects_fts
)
)
2020-06-22 14:27:49 +00:00
alter table ( :objects ) do
add ( :fts_content , :tsvector )
end
2019-05-11 09:26:46 +00:00
2020-06-22 14:27:49 +00:00
execute ( " CREATE FUNCTION objects_fts_update() RETURNS trigger AS $$
begin
2021-02-06 09:42:17 +00:00
new . fts_content := to_tsvector ( new . data -> > ' content ' ) ;
2020-06-22 14:27:49 +00:00
return new ;
end
$ $ LANGUAGE plpgsql " )
2021-01-10 08:25:36 +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
2020-06-22 14:27:49 +00:00
execute ( " CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE ON objects
FOR EACH ROW EXECUTE PROCEDURE objects_fts_update ( ) " )
2019-05-11 09:26:46 +00:00
2020-06-22 14:27:49 +00:00
execute ( " UPDATE objects SET updated_at = NOW() " )
2019-05-11 09:26:46 +00:00
end
def down do
2021-01-10 08:25:36 +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
2021-01-10 08:25:36 +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