2021-01-13 19:07:38 +00:00
|
|
|
# Pleroma: A lightweight social networking server
|
|
|
|
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
|
|
|
|
# SPDX-License-Identifier: AGPL-3.0-only
|
|
|
|
|
|
|
|
defmodule Pleroma.Migrators.HashtagsTableMigrator do
|
2021-03-12 09:18:11 +00:00
|
|
|
defmodule State do
|
|
|
|
use Pleroma.Migrators.Support.BaseMigratorState
|
2021-01-13 19:07:38 +00:00
|
|
|
|
2021-03-12 09:18:11 +00:00
|
|
|
@impl Pleroma.Migrators.Support.BaseMigratorState
|
|
|
|
defdelegate data_migration(), to: Pleroma.DataMigration, as: :populate_hashtags_table
|
|
|
|
end
|
2021-01-13 19:07:38 +00:00
|
|
|
|
2021-03-12 09:18:11 +00:00
|
|
|
use Pleroma.Migrators.Support.BaseMigrator
|
2021-01-13 19:07:38 +00:00
|
|
|
|
|
|
|
alias Pleroma.Hashtag
|
2021-03-12 09:18:11 +00:00
|
|
|
alias Pleroma.Migrators.Support.BaseMigrator
|
2021-01-13 19:07:38 +00:00
|
|
|
alias Pleroma.Object
|
|
|
|
|
2021-03-12 09:18:11 +00:00
|
|
|
@impl BaseMigrator
|
|
|
|
def feature_config_path, do: [:features, :improved_hashtag_timeline]
|
2021-01-13 19:07:38 +00:00
|
|
|
|
2021-03-12 09:18:11 +00:00
|
|
|
@impl BaseMigrator
|
|
|
|
def fault_rate_allowance, do: Config.get([:populate_hashtags_table, :fault_rate_allowance], 0)
|
2021-01-13 19:07:38 +00:00
|
|
|
|
2021-03-12 09:18:11 +00:00
|
|
|
@impl BaseMigrator
|
|
|
|
def perform do
|
2021-02-18 17:40:10 +00:00
|
|
|
data_migration_id = data_migration_id()
|
2021-02-16 20:14:15 +00:00
|
|
|
max_processed_id = get_stat(:max_processed_id, 0)
|
2021-01-13 19:07:38 +00:00
|
|
|
|
2021-02-16 20:14:15 +00:00
|
|
|
Logger.info("Transferring embedded hashtags to `hashtags` (from oid: #{max_processed_id})...")
|
2021-01-13 19:07:38 +00:00
|
|
|
|
2021-01-19 18:13:32 +00:00
|
|
|
query()
|
|
|
|
|> where([object], object.id > ^max_processed_id)
|
2021-01-13 19:07:38 +00:00
|
|
|
|> Repo.chunk_stream(100, :batches, timeout: :infinity)
|
|
|
|
|> Stream.each(fn objects ->
|
|
|
|
object_ids = Enum.map(objects, & &1.id)
|
|
|
|
|
2021-02-18 17:40:10 +00:00
|
|
|
results = Enum.map(objects, &transfer_object_hashtags(&1))
|
|
|
|
|
2021-01-13 19:07:38 +00:00
|
|
|
failed_ids =
|
2021-02-18 17:40:10 +00:00
|
|
|
results
|
2021-01-13 19:07:38 +00:00
|
|
|
|> Enum.filter(&(elem(&1, 0) == :error))
|
|
|
|
|> Enum.map(&elem(&1, 1))
|
|
|
|
|
2021-03-12 09:18:11 +00:00
|
|
|
# Count of objects with hashtags: `{:noop, id}` is returned for objects having other AS2 tags
|
2021-02-18 17:40:10 +00:00
|
|
|
chunk_affected_count =
|
|
|
|
results
|
|
|
|
|> Enum.filter(&(elem(&1, 0) == :ok))
|
|
|
|
|> length()
|
|
|
|
|
2021-01-13 19:07:38 +00:00
|
|
|
for failed_id <- failed_ids do
|
|
|
|
_ =
|
|
|
|
Repo.query(
|
|
|
|
"INSERT INTO data_migration_failed_ids(data_migration_id, record_id) " <>
|
|
|
|
"VALUES ($1, $2) ON CONFLICT DO NOTHING;",
|
2021-02-16 20:14:15 +00:00
|
|
|
[data_migration_id, failed_id]
|
2021-01-13 19:07:38 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
|
|
|
|
_ =
|
|
|
|
Repo.query(
|
2021-01-21 17:19:09 +00:00
|
|
|
"DELETE FROM data_migration_failed_ids " <>
|
|
|
|
"WHERE data_migration_id = $1 AND record_id = ANY($2)",
|
2021-02-16 20:14:15 +00:00
|
|
|
[data_migration_id, object_ids -- failed_ids]
|
2021-01-13 19:07:38 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
max_object_id = Enum.at(object_ids, -1)
|
|
|
|
|
2021-01-14 19:41:27 +00:00
|
|
|
put_stat(:max_processed_id, max_object_id)
|
2021-02-22 20:26:07 +00:00
|
|
|
increment_stat(:iteration_processed_count, length(object_ids))
|
2021-01-14 19:41:27 +00:00
|
|
|
increment_stat(:processed_count, length(object_ids))
|
|
|
|
increment_stat(:failed_count, length(failed_ids))
|
2021-02-18 17:40:10 +00:00
|
|
|
increment_stat(:affected_count, chunk_affected_count)
|
2021-02-16 20:14:15 +00:00
|
|
|
put_stat(:records_per_second, records_per_second())
|
2021-02-17 06:23:35 +00:00
|
|
|
persist_state()
|
2021-01-13 19:07:38 +00:00
|
|
|
|
|
|
|
# A quick and dirty approach to controlling the load this background migration imposes
|
|
|
|
sleep_interval = Config.get([:populate_hashtags_table, :sleep_interval_ms], 0)
|
|
|
|
Process.sleep(sleep_interval)
|
|
|
|
end)
|
|
|
|
|> Stream.run()
|
2021-02-11 16:30:21 +00:00
|
|
|
end
|
|
|
|
|
2021-03-12 09:18:11 +00:00
|
|
|
@impl BaseMigrator
|
|
|
|
def query do
|
2021-01-19 18:13:32 +00:00
|
|
|
# Note: most objects have Mention-type AS2 tags and no hashtags (but we can't filter them out)
|
2021-02-11 16:30:21 +00:00
|
|
|
# Note: not checking activity type, expecting remove_non_create_objects_hashtags/_ to clean up
|
2021-01-19 18:13:32 +00:00
|
|
|
from(
|
|
|
|
object in Object,
|
|
|
|
where:
|
|
|
|
fragment("(?)->'tag' IS NOT NULL AND (?)->'tag' != '[]'::jsonb", object.data, object.data),
|
|
|
|
select: %{
|
|
|
|
id: object.id,
|
|
|
|
tag: fragment("(?)->'tag'", object.data)
|
|
|
|
}
|
|
|
|
)
|
2021-01-24 20:27:02 +00:00
|
|
|
|> join(:left, [o], hashtags_objects in fragment("SELECT object_id FROM hashtags_objects"),
|
|
|
|
on: hashtags_objects.object_id == o.id
|
|
|
|
)
|
|
|
|
|> where([_o, hashtags_objects], is_nil(hashtags_objects.object_id))
|
2021-01-19 18:13:32 +00:00
|
|
|
end
|
|
|
|
|
2021-02-18 17:40:10 +00:00
|
|
|
@spec transfer_object_hashtags(Map.t()) :: {:noop | :ok | :error, integer()}
|
2021-01-13 19:07:38 +00:00
|
|
|
defp transfer_object_hashtags(object) do
|
2021-01-24 20:27:02 +00:00
|
|
|
embedded_tags = if Map.has_key?(object, :tag), do: object.tag, else: object.data["tag"]
|
2021-01-21 17:19:09 +00:00
|
|
|
hashtags = Object.object_data_hashtags(%{"tag" => embedded_tags})
|
2021-01-13 19:07:38 +00:00
|
|
|
|
2021-01-24 20:27:02 +00:00
|
|
|
if Enum.any?(hashtags) do
|
|
|
|
transfer_object_hashtags(object, hashtags)
|
|
|
|
else
|
2021-02-18 17:40:10 +00:00
|
|
|
{:noop, object.id}
|
2021-01-24 20:27:02 +00:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
defp transfer_object_hashtags(object, hashtags) do
|
2021-01-13 19:07:38 +00:00
|
|
|
Repo.transaction(fn ->
|
|
|
|
with {:ok, hashtag_records} <- Hashtag.get_or_create_by_names(hashtags) do
|
2021-02-11 16:30:21 +00:00
|
|
|
maps = Enum.map(hashtag_records, &%{hashtag_id: &1.id, object_id: object.id})
|
2021-02-18 17:40:10 +00:00
|
|
|
base_error = "ERROR when inserting hashtags_objects for object with id #{object.id}"
|
2021-02-13 19:01:11 +00:00
|
|
|
|
|
|
|
try do
|
2021-02-18 17:40:10 +00:00
|
|
|
with {rows_count, _} when is_integer(rows_count) <-
|
|
|
|
Repo.insert_all("hashtags_objects", maps, on_conflict: :nothing) do
|
2021-02-13 19:01:11 +00:00
|
|
|
object.id
|
|
|
|
else
|
|
|
|
e ->
|
|
|
|
Logger.error("#{base_error}: #{inspect(e)}")
|
|
|
|
Repo.rollback(object.id)
|
|
|
|
end
|
|
|
|
rescue
|
2021-02-11 16:30:21 +00:00
|
|
|
e ->
|
2021-02-13 19:01:11 +00:00
|
|
|
Logger.error("#{base_error}: #{inspect(e)}")
|
2021-02-11 16:30:21 +00:00
|
|
|
Repo.rollback(object.id)
|
2021-01-13 19:07:38 +00:00
|
|
|
end
|
|
|
|
else
|
|
|
|
e ->
|
|
|
|
error = "ERROR: could not create hashtags for object #{object.id}: #{inspect(e)}"
|
|
|
|
Logger.error(error)
|
|
|
|
Repo.rollback(object.id)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
end
|
|
|
|
|
2021-03-12 09:18:11 +00:00
|
|
|
@impl BaseMigrator
|
2021-01-21 17:19:09 +00:00
|
|
|
def retry_failed do
|
2021-02-18 17:40:10 +00:00
|
|
|
data_migration_id = data_migration_id()
|
2021-01-21 17:19:09 +00:00
|
|
|
|
|
|
|
failed_objects_query()
|
|
|
|
|> Repo.chunk_stream(100, :one)
|
|
|
|
|> Stream.each(fn object ->
|
2021-02-18 17:40:10 +00:00
|
|
|
with {res, _} when res != :error <- transfer_object_hashtags(object) do
|
2021-01-21 17:19:09 +00:00
|
|
|
_ =
|
|
|
|
Repo.query(
|
|
|
|
"DELETE FROM data_migration_failed_ids " <>
|
|
|
|
"WHERE data_migration_id = $1 AND record_id = $2",
|
2021-02-18 17:40:10 +00:00
|
|
|
[data_migration_id, object.id]
|
2021-01-21 17:19:09 +00:00
|
|
|
)
|
|
|
|
end
|
|
|
|
end)
|
|
|
|
|> Stream.run()
|
2021-02-18 17:40:10 +00:00
|
|
|
|
|
|
|
put_stat(:failed_count, failures_count())
|
|
|
|
persist_state()
|
|
|
|
|
|
|
|
force_continue()
|
2021-01-21 17:19:09 +00:00
|
|
|
end
|
|
|
|
|
2021-03-12 09:18:11 +00:00
|
|
|
defp failed_objects_query do
|
|
|
|
from(o in Object)
|
|
|
|
|> join(:inner, [o], dmf in fragment("SELECT * FROM data_migration_failed_ids"),
|
|
|
|
on: dmf.record_id == o.id
|
|
|
|
)
|
|
|
|
|> where([_o, dmf], dmf.data_migration_id == ^data_migration_id())
|
|
|
|
|> order_by([o], asc: o.id)
|
2021-01-14 19:41:27 +00:00
|
|
|
end
|
|
|
|
|
2021-03-12 09:18:11 +00:00
|
|
|
@doc """
|
|
|
|
Service func to delete `hashtags_objects` for legacy objects not associated with Create activity.
|
|
|
|
Also deletes unreferenced `hashtags` records (might occur after deletion of `hashtags_objects`).
|
|
|
|
"""
|
|
|
|
def delete_non_create_activities_hashtags do
|
|
|
|
hashtags_objects_cleanup_query = """
|
|
|
|
DELETE FROM hashtags_objects WHERE object_id IN
|
|
|
|
(SELECT DISTINCT objects.id FROM objects
|
|
|
|
JOIN hashtags_objects ON hashtags_objects.object_id = objects.id LEFT JOIN activities
|
|
|
|
ON COALESCE(activities.data->'object'->>'id', activities.data->>'object') =
|
|
|
|
(objects.data->>'id')
|
|
|
|
AND activities.data->>'type' = 'Create'
|
|
|
|
WHERE activities.id IS NULL);
|
|
|
|
"""
|
|
|
|
|
|
|
|
hashtags_cleanup_query = """
|
|
|
|
DELETE FROM hashtags WHERE id IN
|
|
|
|
(SELECT hashtags.id FROM hashtags
|
|
|
|
LEFT OUTER JOIN hashtags_objects
|
|
|
|
ON hashtags_objects.hashtag_id = hashtags.id
|
|
|
|
WHERE hashtags_objects.hashtag_id IS NULL);
|
|
|
|
"""
|
2021-01-16 17:22:14 +00:00
|
|
|
|
2021-03-12 09:18:11 +00:00
|
|
|
{:ok, %{num_rows: hashtags_objects_count}} =
|
|
|
|
Repo.query(hashtags_objects_cleanup_query, [], timeout: :infinity)
|
2021-01-21 17:19:09 +00:00
|
|
|
|
2021-03-12 09:18:11 +00:00
|
|
|
{:ok, %{num_rows: hashtags_count}} =
|
|
|
|
Repo.query(hashtags_cleanup_query, [], timeout: :infinity)
|
|
|
|
|
|
|
|
{:ok, hashtags_objects_count, hashtags_count}
|
2021-01-16 17:22:14 +00:00
|
|
|
end
|
2021-01-13 19:07:38 +00:00
|
|
|
end
|