forked from AkkomaGang/akkoma
Add function to calculate associated object id
This commit is contained in:
parent
6ccab516a3
commit
06678fb4ad
2 changed files with 111 additions and 0 deletions
|
@ -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
|
|
@ -278,4 +278,78 @@ test "add_by_params_query/3" do
|
|||
|
||||
assert Repo.aggregate(Activity, :count, :id) == 2
|
||||
end
|
||||
|
||||
describe "associated_object_id() sql function" do
|
||||
test "with json object" do
|
||||
%{rows: [[object_id]]} =
|
||||
Ecto.Adapters.SQL.query!(
|
||||
Pleroma.Repo,
|
||||
"""
|
||||
select associated_object_id('{"object": {"id":"foobar"}}'::jsonb);
|
||||
"""
|
||||
)
|
||||
|
||||
assert object_id == "foobar"
|
||||
end
|
||||
|
||||
test "with string object" do
|
||||
%{rows: [[object_id]]} =
|
||||
Ecto.Adapters.SQL.query!(
|
||||
Pleroma.Repo,
|
||||
"""
|
||||
select associated_object_id('{"object": "foobar"}'::jsonb);
|
||||
"""
|
||||
)
|
||||
|
||||
assert object_id == "foobar"
|
||||
end
|
||||
|
||||
test "with array object" do
|
||||
%{rows: [[object_id]]} =
|
||||
Ecto.Adapters.SQL.query!(
|
||||
Pleroma.Repo,
|
||||
"""
|
||||
select associated_object_id('{"object": ["foobar", {}]}'::jsonb);
|
||||
"""
|
||||
)
|
||||
|
||||
assert object_id == "foobar"
|
||||
end
|
||||
|
||||
test "invalid" do
|
||||
%{rows: [[object_id]]} =
|
||||
Ecto.Adapters.SQL.query!(
|
||||
Pleroma.Repo,
|
||||
"""
|
||||
select associated_object_id('{"object": {}}'::jsonb);
|
||||
"""
|
||||
)
|
||||
|
||||
assert is_nil(object_id)
|
||||
end
|
||||
|
||||
test "invalid object id" do
|
||||
%{rows: [[object_id]]} =
|
||||
Ecto.Adapters.SQL.query!(
|
||||
Pleroma.Repo,
|
||||
"""
|
||||
select associated_object_id('{"object": {"id": 123}}'::jsonb);
|
||||
"""
|
||||
)
|
||||
|
||||
assert is_nil(object_id)
|
||||
end
|
||||
|
||||
test "no object field" do
|
||||
%{rows: [[object_id]]} =
|
||||
Ecto.Adapters.SQL.query!(
|
||||
Pleroma.Repo,
|
||||
"""
|
||||
select associated_object_id('{}'::jsonb);
|
||||
"""
|
||||
)
|
||||
|
||||
assert is_nil(object_id)
|
||||
end
|
||||
end
|
||||
end
|
||||
|
|
Loading…
Reference in a new issue