Fork of Pleroma with site-specific changes and feature branches https://git.pleroma.social/pleroma/pleroma
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

90 lines
2.6KB

  1. defmodule Pleroma.Repo.Migrations.MigrateFollowingRelationships do
  2. use Ecto.Migration
  3. def change do
  4. execute(import_following_from_users(), "")
  5. execute(import_following_from_activities(), restore_following_column())
  6. end
  7. defp import_following_from_users do
  8. """
  9. INSERT INTO following_relationships (follower_id, following_id, state, inserted_at, updated_at)
  10. SELECT
  11. relations.follower_id,
  12. following.id,
  13. 'accept',
  14. now(),
  15. now()
  16. FROM (
  17. SELECT
  18. users.id AS follower_id,
  19. unnest(users.following) AS following_ap_id
  20. FROM
  21. users
  22. WHERE
  23. users.following != '{}'
  24. AND users.local = false OR users.local = true AND users.email IS NOT NULL -- Exclude `internal/fetch` and `relay`
  25. ) AS relations
  26. JOIN users AS "following" ON "following".follower_address = relations.following_ap_id
  27. WHERE relations.follower_id != following.id
  28. ON CONFLICT DO NOTHING
  29. """
  30. end
  31. defp import_following_from_activities do
  32. """
  33. INSERT INTO
  34. following_relationships (
  35. follower_id,
  36. following_id,
  37. state,
  38. inserted_at,
  39. updated_at
  40. )
  41. SELECT
  42. followers.id,
  43. following.id,
  44. activities.data ->> 'state',
  45. (activities.data ->> 'published') :: timestamp,
  46. now()
  47. FROM
  48. activities
  49. JOIN users AS followers ON (activities.actor = followers.ap_id)
  50. JOIN users AS following ON (activities.data ->> 'object' = following.ap_id)
  51. WHERE
  52. activities.data ->> 'type' = 'Follow'
  53. AND activities.data ->> 'state' IN ('accept', 'pending', 'reject')
  54. ORDER BY activities.updated_at DESC
  55. ON CONFLICT DO NOTHING
  56. """
  57. end
  58. defp restore_following_column do
  59. """
  60. UPDATE
  61. users
  62. SET
  63. following = following_query.following_array,
  64. updated_at = now()
  65. FROM (
  66. SELECT
  67. follower.id AS follower_id,
  68. CASE follower.local
  69. WHEN TRUE THEN
  70. array_prepend(follower.follower_address, array_agg(following.follower_address))
  71. ELSE
  72. array_agg(following.follower_address)
  73. END AS following_array
  74. FROM
  75. following_relationships
  76. JOIN users AS follower ON follower.id = following_relationships.follower_id
  77. JOIN users AS following ON following.id = following_relationships.following_id
  78. GROUP BY
  79. follower.id) AS following_query
  80. WHERE
  81. following_query.follower_id = users.id
  82. """
  83. end
  84. end