logo

pleroma

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

20220711182322_add_associated_object_id_function.exs (979B)


  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.AddAssociatedObjectIdFunction do
  5. use Ecto.Migration
  6. def up do
  7. statement = """
  8. CREATE OR REPLACE FUNCTION associated_object_id(data jsonb) RETURNS varchar AS $$
  9. DECLARE
  10. object_data jsonb;
  11. BEGIN
  12. IF jsonb_typeof(data->'object') = 'array' THEN
  13. object_data := data->'object'->0;
  14. ELSE
  15. object_data := data->'object';
  16. END IF;
  17. IF jsonb_typeof(object_data->'id') = 'string' THEN
  18. RETURN object_data->>'id';
  19. ELSIF jsonb_typeof(object_data) = 'string' THEN
  20. RETURN object_data#>>'{}';
  21. ELSE
  22. RETURN NULL;
  23. END IF;
  24. END;
  25. $$ LANGUAGE plpgsql IMMUTABLE;
  26. """
  27. execute(statement)
  28. end
  29. def down do
  30. execute("DROP FUNCTION IF EXISTS associated_object_id(data jsonb)")
  31. end
  32. end