1 defmodule Pleroma.Repo.Migrations.MigrateFollowingRelationships do
5 execute(import_following_from_users(), "")
6 execute(import_following_from_activities(), restore_following_column())
9 defp import_following_from_users do
11 INSERT INTO following_relationships (follower_id, following_id, state, inserted_at, updated_at)
13 relations.follower_id,
20 users.id AS follower_id,
21 unnest(users.following) AS following_ap_id
25 users.following != '{}'
26 AND users.local = false OR users.local = true AND users.email IS NOT NULL -- Exclude `internal/fetch` and `relay`
28 JOIN users AS "following" ON "following".follower_address = relations.following_ap_id
30 WHERE relations.follower_id != following.id
31 ON CONFLICT DO NOTHING
35 defp import_following_from_activities do
38 following_relationships (
48 activities.data ->> 'state',
49 (activities.data ->> 'published') :: timestamp,
53 JOIN users AS followers ON (activities.actor = followers.ap_id)
54 JOIN users AS following ON (activities.data ->> 'object' = following.ap_id)
56 activities.data ->> 'type' = 'Follow'
57 AND activities.data ->> 'state' IN ('accept', 'pending', 'reject')
58 ORDER BY activities.updated_at DESC
59 ON CONFLICT DO NOTHING
63 defp restore_following_column do
68 following = following_query.following_array,
72 follower.id AS follower_id,
75 array_prepend(follower.follower_address, array_agg(following.follower_address))
77 array_agg(following.follower_address)
78 END AS following_array
80 following_relationships
81 JOIN users AS follower ON follower.id = following_relationships.follower_id
82 JOIN users AS following ON following.id = following_relationships.following_id
84 follower.id) AS following_query
86 following_query.follower_id = users.id