Page MenuHomePhorge

No OneTemporary

Size
4 KB
Referenced Files
None
Subscribers
None
diff --git a/lib/pleroma/migration_helper/object_id.ex b/lib/pleroma/migration_helper/object_id.ex
index 170c15666..44af99c8d 100644
--- a/lib/pleroma/migration_helper/object_id.ex
+++ b/lib/pleroma/migration_helper/object_id.ex
@@ -1,83 +1,71 @@
# Pleroma: A lightweight social networking server
# Copyright © 2017-2021 Pleroma Authors <https://pleroma.social/>
# SPDX-License-Identifier: AGPL-3.0-only
defmodule Pleroma.MigrationHelper.ObjectId do
@moduledoc """
Functions for migrating Object IDs.
"""
alias Pleroma.Chat.MessageReference
alias Pleroma.DataMigrationFailedId
alias Pleroma.Delivery
alias Pleroma.HashtagObject
alias Pleroma.Object
alias Pleroma.Repo
import Ecto.Changeset
import Ecto.Query
@doc "Change an object's ID including all references."
def change_id(%Object{id: old_id} = object, new_id) do
Repo.transaction(fn ->
- # Temporarily disable triggers (and by consequence, fkey constraints)
- # https://stackoverflow.com/a/18709987
- Repo.query!("SET session_replication_role = replica")
-
- # Update foreign keys
update_object_fk(MessageReference, old_id, new_id)
update_object_fk(Delivery, old_id, new_id)
update_object_fk(HashtagObject, old_id, new_id)
update_object_fk(DataMigrationFailedId, old_id, new_id, :record_id)
- # Update the object
- object = Repo.update!(change(object, id: new_id))
-
- # Re-enable triggers
- Repo.query!("SET session_replication_role = DEFAULT")
-
- # Return the object
- object
+ Repo.update!(change(object, id: new_id))
end)
end
defp update_object_fk(schema, old_id, new_id, field \\ :object_id) do
binding = [{field, old_id}]
schema
|> where(^binding)
|> Repo.update_all(set: [{field, new_id}])
end
@doc "Shift a FlakeId by N places."
def shift_id(flake_id, n) when is_integer(n) do
flake_id
|> FlakeId.from_string()
|> FlakeId.to_integer()
|> Kernel.+(n)
|> FlakeId.from_integer()
|> FlakeId.to_string()
end
@doc "Generate a FlakeId from a datetime."
@spec flake_from_time(NaiveDateTime.t()) :: flake_id :: String.t()
def flake_from_time(%NaiveDateTime{} = dt) do
dt
|> build_worker()
|> FlakeId.Worker.gen_flake()
|> FlakeId.to_string()
end
# Build a one-off FlakeId worker.
defp build_worker(%NaiveDateTime{} = dt) do
%FlakeId.Worker{
node: FlakeId.Worker.worker_id(),
time: get_timestamp(dt, :millisecond)
}
end
# Convert a NaiveDateTime into a Unix timestamp.
@epoch ~N[1970-01-01 00:00:00]
defp get_timestamp(%NaiveDateTime{} = dt, unit) do
NaiveDateTime.diff(dt, @epoch, unit)
end
end
diff --git a/priv/repo/migrations/20211218181640_resolve_activity_object_conflicts.exs b/priv/repo/migrations/20211218181640_resolve_activity_object_conflicts.exs
index 8c1172332..71bca1cbe 100644
--- a/priv/repo/migrations/20211218181640_resolve_activity_object_conflicts.exs
+++ b/priv/repo/migrations/20211218181640_resolve_activity_object_conflicts.exs
@@ -1,41 +1,49 @@
defmodule Pleroma.Repo.Migrations.ResolveActivityObjectConflicts do
@moduledoc """
Find objects with a conflicting activity ID, and update them.
This should only happen on servers that existed before "20181218172826_users_and_activities_flake_id".
"""
use Ecto.Migration
alias Pleroma.Object
alias Pleroma.MigrationHelper.ObjectId
alias Pleroma.Repo
import Ecto.Query
def up do
# Lock relevant tables
execute("LOCK TABLE objects")
execute("LOCK TABLE chat_message_references")
execute("LOCK TABLE deliveries")
execute("LOCK TABLE hashtags_objects")
+ # Temporarily disable triggers (and by consequence, fkey constraints)
+ # https://stackoverflow.com/a/18709987
+ Repo.query!("SET session_replication_role = replica")
+
+ # Update conflicting objects
activity_conflict_query()
|> Repo.stream()
|> Stream.each(&update_object!/1)
|> Stream.run()
+
+ # Re-enable triggers
+ Repo.query!("SET session_replication_role = DEFAULT")
end
# Get only objects with a conflicting activity ID.
defp activity_conflict_query() do
join(Object, :inner, [o], a in "activities", on: a.id == o.id)
end
# Update the object and its relations with a newly-generated ID.
defp update_object!(object) do
new_id = ObjectId.flake_from_time(object.inserted_at)
{:ok, %Object{}} = ObjectId.change_id(object, new_id)
end
def down do
:ok
end
end

File Metadata

Mime Type
text/x-diff
Expires
Fri, Aug 28, 6:43 PM (20 h, 40 m)
Storage Engine
blob
Storage Format
Raw Data
Storage Handle
1736594
Default Alt Text
(4 KB)

Event Timeline