Skip to content
Snippets Groups Projects
Commit 32f46ecc authored by Bartek Wrona's avatar Bartek Wrona
Browse files

Merge branch 'post_children_count_update_fix' into 'develop'

Fixed query counting children for posts collected during sync.

See merge request !45
parents df59611d aa6bda69
No related branches found
No related tags found
4 merge requests!456Release candidate v1 24,!230Setup monitoring with pghero,!135Enable postgres monitoring on CI server,!45Fixed query counting children for posts collected during sync.
...@@ -3,6 +3,8 @@ ...@@ -3,6 +3,8 @@
#pylint: disable=too-many-lines #pylint: disable=too-many-lines
import time import time
from time import perf_counter
import logging import logging
import sqlalchemy import sqlalchemy
...@@ -174,12 +176,17 @@ class DbState: ...@@ -174,12 +176,17 @@ class DbState:
index.create(engine) index.create(engine)
time_start = perf_counter()
# Update count of all child posts (what was hold during initial sync) # Update count of all child posts (what was hold during initial sync)
sql = """ sql = """
select update_hive_posts_children_count() select update_hive_posts_children_count()
""" """
row = DbState.db().query_row(sql) row = DbState.db().query_row(sql)
time_end = perf_counter()
log.info("[INIT] update_hive_posts_children_count executed in %fs", time_end - time_start)
# TODO: #111 # TODO: #111
#for key in cls._all_foreign_keys(): #for key in cls._all_foreign_keys():
# log.info("Create fk %s", key.name) # log.info("Create fk %s", key.name)
......
...@@ -653,40 +653,38 @@ def setup(db): ...@@ -653,40 +653,38 @@ def setup(db):
db.query_no_return(sql) db.query_no_return(sql)
sql = """ sql = """
DROP FUNCTION if exists update_hive_posts_children_count() DROP FUNCTION IF EXISTS public.update_hive_posts_children_count();
;
CREATE OR REPLACE FUNCTION update_hive_posts_children_count() CREATE OR REPLACE FUNCTION public.update_hive_posts_children_count()
RETURNS VOID RETURNS void
LANGUAGE plpgsql LANGUAGE 'plpgsql'
AS VOLATILE
$function$ AS $BODY$
BEGIN BEGIN
update hive_posts uhp update hive_posts uhp
set children = data_source.childrencount set children = data_source.children_count
from from
( (
WITH RECURSIVE ChildrenCTE AS ( WITH recursive tblChild AS
SELECT ID as RootId, ID (
FROM hive_posts hp SELECT s.queried_parent, s.id
where hp.parent_id != 0 from
UNION ALL (SELECT h1.Parent_Id as queried_parent, h1.id
SELECT cte.RootID, d.ID FROM hive_posts h1 WHERE h1.depth > 0
FROM ChildrenCTE cte order by h1.depth desc
INNER JOIN hive_posts d ON d.parent_id = cte.ID ) s
) UNION ALL
SELECT d.ID, d.parent_id, cnt.ChildrenCount SELECT tblChild.queried_parent, p.id FROM hive_posts p JOIN tblChild ON p.Parent_Id = tblChild.Id
FROM hive_posts d )
INNER JOIN ( SELECT queried_parent, cast(count(1) as smallint) as children_count
SELECT RootID as ID, COUNT(*) - 1 as ChildrenCount FROM tblChild
FROM ChildrenCTE group by queried_parent
GROUP BY RootID ) data_source
) cnt ON cnt.ID = d.ID where uhp.id = data_source.queried_parent
) as data_source
where uhp.id = data_source.id
; ;
END END
$function$ $BODY$;
""" """
db.query_no_return(sql) db.query_no_return(sql)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment