pleroma/priv/repo/migrations/20230811200000_add_published_at_to_activities.exs
marcin mikołajczak 06e2225bf4 add published_at field to activities
Signed-off-by: marcin mikołajczak <git@mkljczk.pl>
2024-03-15 11:09:19 +01:00

43 lines
1.1 KiB
Elixir

# Pleroma: A lightweight social networking server
# Copyright © 2017-2023 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.Repo.Migrations.AddPublishedAtToActivities do
use Ecto.Migration
def up do
alter table(:activities) do
add_if_not_exists(:published_at, :naive_datetime)
end
create_if_not_exists(index(:activities, [:published_at]))
execute("WITH dates as (
select activities.id as id,
(
CASE
WHEN activities.data->>'type' = 'Create' THEN (objects.data->>'published')::timestamptz
ELSE activities.inserted_at
end
) as published_at
FROM activities
INNER JOIN objects ON COALESCE(
activities.data->'object'->>'id',
activities.data->>'object'
) = objects.data->>'id'
WHERE activities.data->>'type' IN ('Create', 'Announce')
)
UPDATE activities
SET published_at = dates.published_at
FROM dates
WHERE activities.id = dates.id")
end
def down do
alter table(:activities) do
remove_if_exists(:published_at, :naive_datetime)
end
drop_if_exists(index(:activities, [:published_at]))
end
end