logo

pleroma

My custom branche(s) on git.pleroma.social/pleroma/pleroma git clone https://hacktivis.me/git/pleroma.git

20191128153944_fix_missing_following_count.exs (1205B)


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