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.

54 lines
1.0KB

  1. defmodule Pleroma.Repo.Migrations.FixMissingFollowingCount do
  2. use Ecto.Migration
  3. def up do
  4. """
  5. UPDATE
  6. users
  7. SET
  8. following_count = sub.count
  9. FROM
  10. (
  11. SELECT
  12. users.id AS sub_id
  13. ,COUNT (following_relationships.id)
  14. FROM
  15. following_relationships
  16. ,users
  17. WHERE
  18. users.id = following_relationships.follower_id
  19. AND following_relationships.state = 'accept'
  20. GROUP BY
  21. users.id
  22. ) AS sub
  23. WHERE
  24. users.id = sub.sub_id
  25. AND users.local = TRUE
  26. ;
  27. """
  28. |> execute()
  29. """
  30. UPDATE
  31. users
  32. SET
  33. following_count = 0
  34. WHERE
  35. following_count IS NULL
  36. """
  37. |> execute()
  38. execute("ALTER TABLE users
  39. ALTER COLUMN following_count SET DEFAULT 0,
  40. ALTER COLUMN following_count SET NOT NULL
  41. ")
  42. end
  43. def down do
  44. execute("ALTER TABLE users
  45. ALTER COLUMN following_count DROP DEFAULT,
  46. ALTER COLUMN following_count DROP NOT NULL
  47. ")
  48. end
  49. end