logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

20190510135645_add_fts_index_to_objects_two.exs (1539B)


  1. # Pleroma: A lightweight social networking server
  2. # Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
  3. # SPDX-License-Identifier: AGPL-3.0-only
  4. defmodule Pleroma.Repo.Migrations.AddFtsIndexToObjectsTwo do
  5. use Ecto.Migration
  6. def up do
  7. execute("create extension if not exists rum")
  8. drop_if_exists(
  9. index(:objects, ["(to_tsvector('english', data->>'content'))"],
  10. using: :gin,
  11. name: :objects_fts
  12. )
  13. )
  14. alter table(:objects) do
  15. add(:fts_content, :tsvector)
  16. end
  17. execute("CREATE FUNCTION objects_fts_update() RETURNS trigger AS $$
  18. begin
  19. new.fts_content := to_tsvector(new.data->>'content');
  20. return new;
  21. end
  22. $$ LANGUAGE plpgsql")
  23. execute(
  24. "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');"
  25. )
  26. execute("CREATE TRIGGER tsvectorupdate BEFORE INSERT OR UPDATE ON objects
  27. FOR EACH ROW EXECUTE PROCEDURE objects_fts_update()")
  28. execute("UPDATE objects SET updated_at = NOW()")
  29. end
  30. def down do
  31. execute("drop index if exists objects_fts")
  32. execute("drop trigger if exists tsvectorupdate on objects")
  33. execute("drop function if exists objects_fts_update()")
  34. alter table(:objects) do
  35. remove(:fts_content, :tsvector)
  36. end
  37. create_if_not_exists(
  38. index(:objects, ["(to_tsvector('english', data->>'content'))"],
  39. using: :gin,
  40. name: :objects_fts
  41. )
  42. )
  43. end
  44. end