summaryrefslogtreecommitdiff
path: root/priv
diff options
context:
space:
mode:
authorTusooa Zhu <tusooa@kazv.moe>2022-07-11 14:58:38 -0400
committerTusooa Zhu <tusooa@kazv.moe>2022-08-20 20:43:40 -0400
commit06678fb4ad42fcaecb99eccc2237c3b863a2b9a5 (patch)
tree9c27440d3e12cb15aecd5e42e77396330ce669b5 /priv
parent6ccab516a3f62682fe15f8efec36be82acafc155 (diff)
Add function to calculate associated object id
Diffstat (limited to 'priv')
-rw-r--r--priv/repo/migrations/20220711182322_add_associated_object_id_function.exs37
1 files changed, 37 insertions, 0 deletions
diff --git a/priv/repo/migrations/20220711182322_add_associated_object_id_function.exs b/priv/repo/migrations/20220711182322_add_associated_object_id_function.exs
new file mode 100644
index 000000000..76348f31a
--- /dev/null
+++ b/priv/repo/migrations/20220711182322_add_associated_object_id_function.exs
@@ -0,0 +1,37 @@
+# Pleroma: A lightweight social networking server
+# Copyright © 2017-2022 Pleroma Authors <https://pleroma.social/>
+# SPDX-License-Identifier: AGPL-3.0-only
+
+defmodule Pleroma.Repo.Migrations.AddAssociatedObjectIdFunction do
+ use Ecto.Migration
+
+ def up do
+ statement = """
+ CREATE OR REPLACE FUNCTION associated_object_id(data jsonb) RETURNS varchar AS $$
+ DECLARE
+ object_data jsonb;
+ BEGIN
+ IF jsonb_typeof(data->'object') = 'array' THEN
+ object_data := data->'object'->0;
+ ELSE
+ object_data := data->'object';
+ END IF;
+
+ IF jsonb_typeof(object_data->'id') = 'string' THEN
+ RETURN object_data->>'id';
+ ELSIF jsonb_typeof(object_data) = 'string' THEN
+ RETURN object_data#>>'{}';
+ ELSE
+ RETURN NULL;
+ END IF;
+ END;
+ $$ LANGUAGE plpgsql IMMUTABLE;
+ """
+
+ execute(statement)
+ end
+
+ def down do
+ execute("DROP FUNCTION IF EXISTS associated_object_id(data jsonb)")
+ end
+end