# Pleroma: A lightweight social networking server # Copyright © 2017-2023 Pleroma Authors # 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