Skip to content
Snippets Groups Projects
Commit 0051ca86 authored by Dariusz Kędzierski's avatar Dariusz Kędzierski Committed by Bartek Wrona
Browse files

Database api fixes

parent ab277b83
No related branches found
No related tags found
4 merge requests!456Release candidate v1 24,!230Setup monitoring with pghero,!138Small typos fixed,!135Enable postgres monitoring on CI server
...@@ -774,21 +774,31 @@ def setup(db): ...@@ -774,21 +774,31 @@ def setup(db):
db.query_no_return(sql) db.query_no_return(sql)
sql = """ sql = """
DROP FUNCTION IF EXISTS find_comment_id(character varying, character varying) DROP FUNCTION IF EXISTS find_comment_id(character varying, character varying, boolean)
; ;
CREATE OR REPLACE FUNCTION find_comment_id( CREATE OR REPLACE FUNCTION find_comment_id(
in _author hive_accounts.name%TYPE, in _author hive_accounts.name%TYPE,
in _permlink hive_permlink_data.permlink%TYPE) in _permlink hive_permlink_data.permlink%TYPE,
RETURNS INT AS in _check boolean)
RETURNS INT
LANGUAGE 'plpgsql'
AS
$function$ $function$
SELECT COALESCE( (SELECT hp.id DECLARE
FROM hive_posts hp post_id INT;
JOIN hive_accounts ha ON ha.id = hp.author_id BEGIN
JOIN hive_permlink_data hpd ON hpd.id = hp.permlink_id SELECT INTO post_id COALESCE( (SELECT hp.id
WHERE ha.name = _author AND hpd.permlink = _permlink AND hp.counter_deleted = 0 FROM hive_posts hp
), 0 ); JOIN hive_accounts ha ON ha.id = hp.author_id
JOIN hive_permlink_data hpd ON hpd.id = hp.permlink_id
WHERE ha.name = _author AND hpd.permlink = _permlink AND hp.counter_deleted = 0
), 0 );
IF _check AND (_author <> '' OR _permlink <> '') AND post_id = 0 THEN
RAISE EXCEPTION 'Post/comment %/% does not exist', _author, _permlink;
END IF;
RETURN post_id;
END
$function$ $function$
LANGUAGE sql
; ;
""" """
db.query_no_return(sql) db.query_no_return(sql)
...@@ -852,7 +862,7 @@ def setup(db): ...@@ -852,7 +862,7 @@ def setup(db):
DECLARE DECLARE
__post_id INT; __post_id INT;
BEGIN BEGIN
__post_id = find_comment_id(_author,_permlink); __post_id = find_comment_id(_author,_permlink, False);
RETURN QUERY RETURN QUERY
SELECT SELECT
hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body, hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body,
...@@ -932,8 +942,8 @@ def setup(db): ...@@ -932,8 +942,8 @@ def setup(db):
__root_id INT; __root_id INT;
__post_id INT; __post_id INT;
BEGIN BEGIN
__root_id = find_comment_id(_root_author,_root_permlink); __root_id = find_comment_id(_root_author, _root_permlink, True);
__post_id = find_comment_id(_start_post_author,_start_post_permlink); __post_id = find_comment_id(_start_post_author, _start_post_permlink, True);
RETURN QUERY RETURN QUERY
SELECT SELECT
hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body, hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body,
...@@ -972,11 +982,10 @@ def setup(db): ...@@ -972,11 +982,10 @@ def setup(db):
in _limit INT) in _limit INT)
RETURNS SETOF database_api_post RETURNS SETOF database_api_post
LANGUAGE sql LANGUAGE sql
COST 100 COST 100
STABLE STABLE
ROWS 1000 ROWS 1000
AS $BODY$ AS $function$
SELECT SELECT
hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body, hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body,
hp.category, hp.depth, hp.promoted, hp.payout, hp.last_payout_at, hp.cashout_time, hp.is_paidout, hp.category, hp.depth, hp.promoted, hp.payout, hp.last_payout_at, hp.cashout_time, hp.is_paidout,
...@@ -995,7 +1004,7 @@ def setup(db): ...@@ -995,7 +1004,7 @@ def setup(db):
WHERE WHERE
h.parent_author > _parent_author OR h.parent_author > _parent_author OR
h.parent_author = _parent_author AND ( h.parent_permlink_or_category > _parent_permlink OR h.parent_author = _parent_author AND ( h.parent_permlink_or_category > _parent_permlink OR
h.parent_permlink_or_category = _parent_permlink AND h.id >= find_comment_id(_start_post_author,_start_post_permlink) ) h.parent_permlink_or_category = _parent_permlink AND h.id >= find_comment_id(_start_post_author, _start_post_permlink, True) )
ORDER BY ORDER BY
h.parent_author ASC, h.parent_author ASC,
h.parent_permlink_or_category ASC, h.parent_permlink_or_category ASC,
...@@ -1006,8 +1015,8 @@ def setup(db): ...@@ -1006,8 +1015,8 @@ def setup(db):
WHERE WHERE
NOT hp.is_muted NOT hp.is_muted
; ;
$BODY$; $function$
; ;
DROP FUNCTION IF EXISTS list_comments_by_last_update(character varying, timestamp, character varying, character varying, int) DROP FUNCTION IF EXISTS list_comments_by_last_update(character varying, timestamp, character varying, character varying, int)
; ;
...@@ -1023,7 +1032,7 @@ def setup(db): ...@@ -1023,7 +1032,7 @@ def setup(db):
DECLARE DECLARE
__post_id INT; __post_id INT;
BEGIN BEGIN
__post_id = find_comment_id(_start_post_author,_start_post_permlink); __post_id = find_comment_id(_start_post_author, _start_post_permlink, True);
RETURN QUERY RETURN QUERY
SELECT SELECT
hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body, hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body,
...@@ -1067,7 +1076,7 @@ def setup(db): ...@@ -1067,7 +1076,7 @@ def setup(db):
DECLARE DECLARE
__post_id INT; __post_id INT;
BEGIN BEGIN
__post_id = find_comment_id(_start_post_author,_start_post_permlink); __post_id = find_comment_id(_start_post_author, _start_post_permlink, True);
RETURN QUERY RETURN QUERY
SELECT SELECT
hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body, hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body,
......
...@@ -115,3 +115,23 @@ def valid_follow_type(follow_type: str): ...@@ -115,3 +115,23 @@ def valid_follow_type(follow_type: str):
"""Ensure follow type is valid steemd type.""" """Ensure follow type is valid steemd type."""
assert follow_type in ['blog', 'ignore'], 'invalid follow_type `%s`' % follow_type assert follow_type in ['blog', 'ignore'], 'invalid follow_type `%s`' % follow_type
return follow_type return follow_type
def valid_date(date, allow_empty=False):
""" Ensure that date is in correct format """
if not date:
assert allow_empty, 'Date is blank'
check_date = False
# check format "%Y-%m-%d %H:%M:%S"
try:
check_date = (date == datetime.datetime.strptime(date, "%Y-%m-%d %H:%M:%S").strftime('%Y-%m-%d %H:%M%S'))
except ValueError:
check_date = False
# if check failed for format above try another format
# check format "%Y-%m-%dT%H:%M:%S"
if not check_date:
try:
check_date = (date == datetime.datetime.strptime(date, "%Y-%m-%dT%H:%M:%S").strftime('%Y-%m-%dT%H:%M:%S'))
except ValueError:
pass
assert check_date, "Date should be in format Y-m-d H:M:S or Y-m-dTH:M:S"
# pylint: disable=too-many-arguments,line-too-long,too-many-lines # pylint: disable=too-many-arguments,line-too-long,too-many-lines
from enum import Enum from enum import Enum
from hive.server.common.helpers import return_error_info, valid_limit, valid_account, valid_permlink from hive.server.common.helpers import return_error_info, valid_limit, valid_account, valid_permlink, valid_date
from hive.server.database_api.objects import database_post_object from hive.server.database_api.objects import database_post_object
from hive.utils.normalize import rep_to_raw, number_to_json_value, time_string_with_t from hive.utils.normalize import rep_to_raw, number_to_json_value, time_string_with_t
...@@ -20,48 +20,69 @@ async def list_comments(context, start: list, limit: int, order: str): ...@@ -20,48 +20,69 @@ async def list_comments(context, start: list, limit: int, order: str):
if order == 'by_cashout_time': if order == 'by_cashout_time':
assert len(start) == 3, "Expecting three arguments" assert len(start) == 3, "Expecting three arguments"
cashout_time = start[0] cashout_time = start[0]
valid_date(cashout_time)
if cashout_time[0:4] == '1969': if cashout_time[0:4] == '1969':
cashout_time = "infinity" cashout_time = "infinity"
author = start[1] author = start[1]
valid_account(author, allow_empty=True)
permlink = start[2] permlink = start[2]
valid_permlink(permlink, allow_empty=True)
sql = "SELECT * FROM list_comments_by_cashout_time(:cashout_time, :author, :permlink, :limit)" sql = "SELECT * FROM list_comments_by_cashout_time(:cashout_time, :author, :permlink, :limit)"
result = await db.query_all(sql, cashout_time=cashout_time, author=author, permlink=permlink, limit=limit) result = await db.query_all(sql, cashout_time=cashout_time, author=author, permlink=permlink, limit=limit)
elif order == 'by_permlink': elif order == 'by_permlink':
assert len(start) == 2, "Expecting two arguments" assert len(start) == 2, "Expecting two arguments"
author = start[0] author = start[0]
valid_account(author, allow_empty=True)
permlink = start[1] permlink = start[1]
valid_permlink(permlink, allow_empty=True)
sql = "SELECT * FROM list_comments_by_permlink(:author, :permlink, :limit)" sql = "SELECT * FROM list_comments_by_permlink(:author, :permlink, :limit)"
result = await db.query_all(sql, author=author, permlink=permlink, limit=limit) result = await db.query_all(sql, author=author, permlink=permlink, limit=limit)
elif order == 'by_root': elif order == 'by_root':
assert len(start) == 4, "Expecting 4 arguments" assert len(start) == 4, "Expecting 4 arguments"
root_author = start[0] root_author = start[0]
valid_account(root_author, allow_empty=True)
root_permlink = start[1] root_permlink = start[1]
valid_permlink(root_permlink, allow_empty=True)
start_post_author = start[2] start_post_author = start[2]
valid_account(start_post_author, allow_empty=True)
start_post_permlink = start[3] start_post_permlink = start[3]
valid_permlink(start_post_permlink, allow_empty=True)
sql = "SELECT * FROM list_comments_by_root(:root_author, :root_permlink, :start_post_author, :start_post_permlink, :limit)" sql = "SELECT * FROM list_comments_by_root(:root_author, :root_permlink, :start_post_author, :start_post_permlink, :limit)"
result = await db.query_all(sql, root_author=root_author, root_permlink=root_permlink, start_post_author=start_post_author, start_post_permlink=start_post_permlink, limit=limit) result = await db.query_all(sql, root_author=root_author, root_permlink=root_permlink, start_post_author=start_post_author, start_post_permlink=start_post_permlink, limit=limit)
elif order == 'by_parent': elif order == 'by_parent':
assert len(start) == 4, "Expecting 4 arguments" assert len(start) == 4, "Expecting 4 arguments"
parent_author = start[0] parent_author = start[0]
valid_account(parent_author, allow_empty=True)
parent_permlink = start[1] parent_permlink = start[1]
valid_permlink(parent_permlink, allow_empty=True)
start_post_author = start[2] start_post_author = start[2]
valid_account(start_post_author, allow_empty=True)
start_post_permlink = start[3] start_post_permlink = start[3]
valid_permlink(start_post_permlink, allow_empty=True)
sql = "SELECT * FROM list_comments_by_parent(:parent_author, :parent_permlink, :start_post_author, :start_post_permlink, :limit)" sql = "SELECT * FROM list_comments_by_parent(:parent_author, :parent_permlink, :start_post_author, :start_post_permlink, :limit)"
result = await db.query_all(sql, parent_author=parent_author, parent_permlink=parent_permlink, start_post_author=start_post_author, start_post_permlink=start_post_permlink, limit=limit) result = await db.query_all(sql, parent_author=parent_author, parent_permlink=parent_permlink, start_post_author=start_post_author, start_post_permlink=start_post_permlink, limit=limit)
elif order == 'by_last_update': elif order == 'by_last_update':
assert len(start) == 4, "Expecting 4 arguments" assert len(start) == 4, "Expecting 4 arguments"
parent_author = start[0] parent_author = start[0]
valid_account(parent_author, allow_empty=True)
updated_at = start[1] updated_at = start[1]
valid_date(updated_at)
start_post_author = start[2] start_post_author = start[2]
valid_account(start_post_author, allow_empty=True)
start_post_permlink = start[3] start_post_permlink = start[3]
valid_permlink(start_post_permlink, allow_empty=True)
sql = "SELECT * FROM list_comments_by_last_update(:parent_author, :updated_at, :start_post_author, :start_post_permlink, :limit)" sql = "SELECT * FROM list_comments_by_last_update(:parent_author, :updated_at, :start_post_author, :start_post_permlink, :limit)"
result = await db.query_all(sql, parent_author=parent_author, updated_at=updated_at, start_post_author=start_post_author, start_post_permlink=start_post_permlink, limit=limit) result = await db.query_all(sql, parent_author=parent_author, updated_at=updated_at, start_post_author=start_post_author, start_post_permlink=start_post_permlink, limit=limit)
elif order == 'by_author_last_update': elif order == 'by_author_last_update':
assert len(start) == 4, "Expecting 4 arguments" assert len(start) == 4, "Expecting 4 arguments"
author = start[0] author = start[0]
valid_account(author, allow_empty=True)
updated_at = start[1] updated_at = start[1]
valid_date(updated_at)
start_post_author = start[2] start_post_author = start[2]
valid_account(start_post_author, allow_empty=True)
start_post_permlink = start[3] start_post_permlink = start[3]
valid_permlink(start_post_permlink, allow_empty=True)
sql = "SELECT * FROM list_comments_by_author_last_update(:author, :updated_at, :start_post_author, :start_post_permlink, :limit)" sql = "SELECT * FROM list_comments_by_author_last_update(:author, :updated_at, :start_post_author, :start_post_permlink, :limit)"
result = await db.query_all(sql, author=author, updated_at=updated_at, start_post_author=start_post_author, start_post_permlink=start_post_permlink, limit=limit) result = await db.query_all(sql, author=author, updated_at=updated_at, start_post_author=start_post_author, start_post_permlink=start_post_permlink, limit=limit)
......
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