From 0ae18d45b83548280cb01f49713933cf0879112f Mon Sep 17 00:00:00 2001
From: Bartek Wrona <wrona@syncad.com>
Date: Sat, 31 Oct 2020 22:13:09 +0100
Subject: [PATCH] Fixed fatal bugs (syntactical errors in UPDATE queries) in
 the follow implementation Improved __post_children table management in the
 update_all_hive_posts_children_count, to avoid an error during subsequent
 call.

---
 .../update_hive_posts_children_count.sql      | 15 ++++++------
 hive/indexer/follow.py                        | 24 +++++++++----------
 2 files changed, 20 insertions(+), 19 deletions(-)

diff --git a/hive/db/sql_scripts/update_hive_posts_children_count.sql b/hive/db/sql_scripts/update_hive_posts_children_count.sql
index 83ea5c5b3..1a91e44bf 100644
--- a/hive/db/sql_scripts/update_hive_posts_children_count.sql
+++ b/hive/db/sql_scripts/update_hive_posts_children_count.sql
@@ -67,17 +67,18 @@ CREATE OR REPLACE FUNCTION public.update_all_hive_posts_children_count()
   LANGUAGE 'plpgsql'
   VOLATILE
 AS $BODY$
-declare __depth int;
+declare __depth INT;
 BEGIN
   SELECT MAX(hp.depth) into __depth FROM hive_posts hp ;
 
-  DROP TABLE if exists __post_children;
-  create unlogged table __post_children
+  CREATE UNLOGGED TABLE IF NOT EXISTS __post_children
   (
-    id int not null,
-    child_count int not null,
-    CONSTRAINT __post_children2_pkey PRIMARY KEY (id)
+    id INT NOT NULL,
+    child_count INT NOT NULL,
+    CONSTRAINT __post_children_pkey PRIMARY KEY (id)
   );
+
+  TRUNCATE TABLE __post_children;
   
   WHILE __depth >= 0 LOOP
     INSERT INTO __post_children
@@ -92,7 +93,7 @@ BEGIN
       WHERE (h1.parent_id != 0 OR __depth = 0) AND h1.counter_deleted = 0 AND h1.id != 0 AND h1.depth = __depth
       GROUP BY h1.parent_id
 
-    ON CONFLICT ON CONSTRAINT __post_children2_pkey DO UPDATE
+    ON CONFLICT ON CONSTRAINT __post_children_pkey DO UPDATE
       SET child_count = __post_children.child_count + excluded.child_count
     ;
 
diff --git a/hive/indexer/follow.py b/hive/indexer/follow.py
index a4d2930cb..131f07dcd 100644
--- a/hive/indexer/follow.py
+++ b/hive/indexer/follow.py
@@ -277,7 +277,7 @@ class Follow(DbAdapterHolder):
                             UPDATE
                                 hive_follows hf
                             SET
-                                hf.blacklisted = false
+                                blacklisted = false
                             FROM
                             (
                                 SELECT
@@ -298,7 +298,7 @@ class Follow(DbAdapterHolder):
                             UPDATE
                                 hive_follows hf
                             SET
-                                hf.state = 0
+                                state = 0
                             FROM
                             (
                                 SELECT
@@ -320,7 +320,7 @@ class Follow(DbAdapterHolder):
                             UPDATE
                                 hive_follows hf
                             SET
-                                hf.state = 0
+                                state = 0
                             FROM
                             (
                                 SELECT
@@ -342,7 +342,7 @@ class Follow(DbAdapterHolder):
                             UPDATE
                                 hive_follows hf
                             SET
-                                hf.follow_blacklists = false
+                                follow_blacklists = false
                             FROM
                             (
                                 SELECT
@@ -383,7 +383,7 @@ class Follow(DbAdapterHolder):
                             UPDATE
                                 hive_follows hf
                             SET
-                                hf.follow_muted = false
+                                follow_muted = false
                             FROM
                             (
                                 SELECT
@@ -401,7 +401,7 @@ class Follow(DbAdapterHolder):
                             UPDATE
                                 hive_follows hf
                             SET
-                                hf.follow_muted = true
+                                follow_muted = true
                             FROM
                             (
                                 SELECT
@@ -423,10 +423,10 @@ class Follow(DbAdapterHolder):
                             UPDATE
                                 hive_follows hf
                             SET
-                                hf.blacklisted = false,
-                                hf.follow_blacklists = false,
-                                hf.follow_muted = false,
-                                hf.state = 0
+                                blacklisted = false,
+                                follow_blacklists = false,
+                                follow_muted = false,
+                                state = 0
                             FROM
                             (
                                 SELECT
@@ -444,8 +444,8 @@ class Follow(DbAdapterHolder):
                             UPDATE
                                 hive_follows hf
                             SET
-                                hf.follow_blacklists = true,
-                                hf.follow_muted = true
+                                follow_blacklists = true,
+                                follow_muted = true
                             FROM
                             (
                                 SELECT
-- 
GitLab