summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorrinpatch <rinpatch@sdf.org>2019-11-28 19:13:42 +0000
committerrinpatch <rinpatch@sdf.org>2019-11-28 19:13:42 +0000
commitd7b40dd4ba5bf3890be8a8b69d5aff84f5ffc5d2 (patch)
tree329ccb506bc21f99531fc62b50496f6ac065d0c5
parentca1acfa314a57f78c2f5f8e60a4bd1380350e483 (diff)
parentf0bdbe3f61e3f3278f76d31dad447b982e2e2571 (diff)
downloadpleroma-d7b40dd4ba5bf3890be8a8b69d5aff84f5ffc5d2.tar.gz
pleroma-d7b40dd4ba5bf3890be8a8b69d5aff84f5ffc5d2.zip
Merge branch 'bugfix/1447-set-following' into 'develop'
Migrations: Set users.following_count to NOT NULL Closes #1447 See merge request pleroma/pleroma!2021
-rw-r--r--priv/repo/migrations/20191128153944_fix_missing_following_count.exs53
1 files changed, 53 insertions, 0 deletions
diff --git a/priv/repo/migrations/20191128153944_fix_missing_following_count.exs b/priv/repo/migrations/20191128153944_fix_missing_following_count.exs
new file mode 100644
index 000000000..3236de7a4
--- /dev/null
+++ b/priv/repo/migrations/20191128153944_fix_missing_following_count.exs
@@ -0,0 +1,53 @@
+defmodule Pleroma.Repo.Migrations.FixMissingFollowingCount do
+ use Ecto.Migration
+
+ def up do
+ """
+ UPDATE
+ users
+ SET
+ following_count = sub.count
+ FROM
+ (
+ SELECT
+ users.id AS sub_id
+ ,COUNT (following_relationships.id)
+ FROM
+ following_relationships
+ ,users
+ WHERE
+ users.id = following_relationships.follower_id
+ AND following_relationships.state = 'accept'
+ GROUP BY
+ users.id
+ ) AS sub
+ WHERE
+ users.id = sub.sub_id
+ AND users.local = TRUE
+ ;
+ """
+ |> execute()
+
+ """
+ UPDATE
+ users
+ SET
+ following_count = 0
+ WHERE
+ following_count IS NULL
+ """
+ |> execute()
+
+ execute("ALTER TABLE users
+ ALTER COLUMN following_count SET DEFAULT 0,
+ ALTER COLUMN following_count SET NOT NULL
+ ")
+ end
+
+ def down do
+ execute("ALTER TABLE users
+ ALTER COLUMN following_count DROP DEFAULT,
+ ALTER COLUMN following_count DROP NOT NULL
+ ")
+ end
+end