diff options
Diffstat (limited to 'priv/repo')
-rw-r--r-- | priv/repo/migrations/20210120161014_move_fts_index.exs | 54 |
1 files changed, 54 insertions, 0 deletions
diff --git a/priv/repo/migrations/20210120161014_move_fts_index.exs b/priv/repo/migrations/20210120161014_move_fts_index.exs new file mode 100644 index 000000000..d32b56214 --- /dev/null +++ b/priv/repo/migrations/20210120161014_move_fts_index.exs @@ -0,0 +1,54 @@ +defmodule Pleroma.Repo.Migrations.MoveFtsIndex do + use Ecto.Migration + + def up do + # Drop the old index + drop_if_exists( + index(:objects, ["(to_tsvector('english', data->>'content'))"], + using: :gin, + name: :objects_fts + ) + ) + + alter table(:activities) do + add(:fts_content, :tsvector) + end + + execute("CREATE FUNCTION activities_fts_update() RETURNS trigger AS $$ + declare + content text := ''; + begin + if new.data->>'type' = 'Create' then + + select objects.data->>'content' + from objects + into content + where objects.data->>'id' = new.data->>'object'; + + new.fts_content := to_tsvector('english', content); + + end if; + + return new; + end + $$ LANGUAGE plpgsql") + + create_if_not_exists(index(:activities, ["fts_content"], using: :gin, name: :activities_fts)) + execute("CREATE TRIGGER ts_vector_activities_update BEFORE INSERT ON activities + FOR EACH ROW EXECUTE PROCEDURE activities_fts_update() + ") + end + + def down do + execute("drop function if exists actitivies_fts_update()") + execute("drop trigger if existis ts_vector_activities_update") + drop_if_exists(index(:activities, ["fts_content"], using: :gin, name: :activities_fts)) + + create_if_not_exists( + index(:objects, ["(to_tsvector('english', data->>'content'))"], + using: :gin, + name: :objects_fts + ) + ) + end +end |