Skip to content
Snippets Groups Projects

find_votes and list_votes fixes

Merged Andrzej Lisak requested to merge find_list_votes_fixes into develop
Files
10
+ 1912
1867
"""Db schema definitions and setup routines."""
"""Db schema definitions and setup routines."""
import sqlalchemy as sa
import sqlalchemy as sa
from sqlalchemy.sql import text as sql_text
from sqlalchemy.sql import text as sql_text
from sqlalchemy.types import SMALLINT
from sqlalchemy.types import SMALLINT
from sqlalchemy.types import CHAR
from sqlalchemy.types import CHAR
from sqlalchemy.types import VARCHAR
from sqlalchemy.types import VARCHAR
from sqlalchemy.types import TEXT
from sqlalchemy.types import TEXT
from sqlalchemy.types import BOOLEAN
from sqlalchemy.types import BOOLEAN
import logging
import logging
log = logging.getLogger(__name__)
log = logging.getLogger(__name__)
#pylint: disable=line-too-long, too-many-lines, bad-whitespace
#pylint: disable=line-too-long, too-many-lines, bad-whitespace
# [DK] we changed and removed some tables so i upgraded DB_VERSION to 18
# [DK] we changed and removed some tables so i upgraded DB_VERSION to 18
DB_VERSION = 18
DB_VERSION = 18
def build_metadata():
def build_metadata():
"""Build schema def with SqlAlchemy"""
"""Build schema def with SqlAlchemy"""
metadata = sa.MetaData()
metadata = sa.MetaData()
sa.Table(
sa.Table(
'hive_blocks', metadata,
'hive_blocks', metadata,
sa.Column('num', sa.Integer, primary_key=True, autoincrement=False),
sa.Column('num', sa.Integer, primary_key=True, autoincrement=False),
sa.Column('hash', CHAR(40), nullable=False),
sa.Column('hash', CHAR(40), nullable=False),
sa.Column('prev', CHAR(40)),
sa.Column('prev', CHAR(40)),
sa.Column('txs', SMALLINT, server_default='0', nullable=False),
sa.Column('txs', SMALLINT, server_default='0', nullable=False),
sa.Column('ops', SMALLINT, server_default='0', nullable=False),
sa.Column('ops', SMALLINT, server_default='0', nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.UniqueConstraint('hash', name='hive_blocks_ux1'),
sa.UniqueConstraint('hash', name='hive_blocks_ux1'),
sa.ForeignKeyConstraint(['prev'], ['hive_blocks.hash'], name='hive_blocks_fk1'),
sa.ForeignKeyConstraint(['prev'], ['hive_blocks.hash'], name='hive_blocks_fk1'),
)
)
sa.Table(
sa.Table(
'hive_accounts', metadata,
'hive_accounts', metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('name', VARCHAR(16, collation='C'), nullable=False),
sa.Column('name', VARCHAR(16, collation='C'), nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
#sa.Column('block_num', sa.Integer, nullable=False),
#sa.Column('block_num', sa.Integer, nullable=False),
sa.Column('reputation', sa.Float(precision=6), nullable=False, server_default='25'),
sa.Column('reputation', sa.Float(precision=6), nullable=False, server_default='25'),
sa.Column('display_name', sa.String(20)),
sa.Column('display_name', sa.String(20)),
sa.Column('about', sa.String(160)),
sa.Column('about', sa.String(160)),
sa.Column('location', sa.String(30)),
sa.Column('location', sa.String(30)),
sa.Column('website', sa.String(1024)),
sa.Column('website', sa.String(1024)),
sa.Column('profile_image', sa.String(1024), nullable=False, server_default=''),
sa.Column('profile_image', sa.String(1024), nullable=False, server_default=''),
sa.Column('cover_image', sa.String(1024), nullable=False, server_default=''),
sa.Column('cover_image', sa.String(1024), nullable=False, server_default=''),
sa.Column('followers', sa.Integer, nullable=False, server_default='0'),
sa.Column('followers', sa.Integer, nullable=False, server_default='0'),
sa.Column('following', sa.Integer, nullable=False, server_default='0'),
sa.Column('following', sa.Integer, nullable=False, server_default='0'),
sa.Column('proxy', VARCHAR(16), nullable=False, server_default=''),
sa.Column('proxy', VARCHAR(16), nullable=False, server_default=''),
sa.Column('proxy_weight', sa.Float(precision=6), nullable=False, server_default='0'),
sa.Column('proxy_weight', sa.Float(precision=6), nullable=False, server_default='0'),
sa.Column('kb_used', sa.Integer, nullable=False, server_default='0'), # deprecated
sa.Column('kb_used', sa.Integer, nullable=False, server_default='0'), # deprecated
sa.Column('rank', sa.Integer, nullable=False, server_default='0'),
sa.Column('rank', sa.Integer, nullable=False, server_default='0'),
sa.Column('lastread_at', sa.DateTime, nullable=False, server_default='1970-01-01 00:00:00'),
sa.Column('lastread_at', sa.DateTime, nullable=False, server_default='1970-01-01 00:00:00'),
sa.Column('cached_at', sa.DateTime, nullable=False, server_default='1970-01-01 00:00:00'),
sa.Column('cached_at', sa.DateTime, nullable=False, server_default='1970-01-01 00:00:00'),
sa.Column('raw_json', sa.Text),
sa.Column('raw_json', sa.Text),
sa.UniqueConstraint('name', name='hive_accounts_ux1'),
sa.UniqueConstraint('name', name='hive_accounts_ux1'),
sa.Index('hive_accounts_ix5', 'cached_at'), # core/listen sweep
sa.Index('hive_accounts_ix5', 'cached_at'), # core/listen sweep
sa.Index('hive_accounts_ix6', 'reputation')
sa.Index('hive_accounts_ix6', 'reputation')
)
)
sa.Table(
sa.Table(
'hive_reputation_data', metadata,
'hive_reputation_data', metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('author_id', sa.Integer, nullable=False),
sa.Column('author_id', sa.Integer, nullable=False),
sa.Column('voter_id', sa.Integer, nullable=False),
sa.Column('voter_id', sa.Integer, nullable=False),
sa.Column('permlink', sa.String(255, collation='C'), nullable=False),
sa.Column('permlink', sa.String(255, collation='C'), nullable=False),
sa.Column('rshares', sa.BigInteger, nullable=False),
sa.Column('rshares', sa.BigInteger, nullable=False),
sa.Column('block_num', sa.Integer, nullable=False),
sa.Column('block_num', sa.Integer, nullable=False),
# sa.ForeignKeyConstraint(['voter_id'], ['hive_accounts.id']),
# sa.ForeignKeyConstraint(['voter_id'], ['hive_accounts.id']),
# sa.ForeignKeyConstraint(['author_id'], ['hive_accounts.id']),
# sa.ForeignKeyConstraint(['author_id'], ['hive_accounts.id']),
# sa.ForeignKeyConstraint(['block_num'], ['hive_blocks.num']),
# sa.ForeignKeyConstraint(['block_num'], ['hive_blocks.num']),
sa.UniqueConstraint('author_id', 'permlink', 'voter_id', name='hive_reputation_data_uk')
sa.UniqueConstraint('author_id', 'permlink', 'voter_id', name='hive_reputation_data_uk')
)
)
sa.Table(
sa.Table(
'hive_posts', metadata,
'hive_posts', metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('root_id', sa.Integer, nullable=False), # records having initially set 0 will be updated to their id
sa.Column('root_id', sa.Integer, nullable=False), # records having initially set 0 will be updated to their id
sa.Column('parent_id', sa.Integer, nullable=False),
sa.Column('parent_id', sa.Integer, nullable=False),
sa.Column('author_id', sa.Integer, nullable=False),
sa.Column('author_id', sa.Integer, nullable=False),
sa.Column('permlink_id', sa.Integer, nullable=False),
sa.Column('permlink_id', sa.Integer, nullable=False),
sa.Column('category_id', sa.Integer, nullable=False),
sa.Column('category_id', sa.Integer, nullable=False),
sa.Column('community_id', sa.Integer, nullable=True),
sa.Column('community_id', sa.Integer, nullable=True),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Column('depth', SMALLINT, nullable=False),
sa.Column('depth', SMALLINT, nullable=False),
sa.Column('counter_deleted', sa.Integer, nullable=False, server_default='0'),
sa.Column('counter_deleted', sa.Integer, nullable=False, server_default='0'),
sa.Column('is_pinned', BOOLEAN, nullable=False, server_default='0'),
sa.Column('is_pinned', BOOLEAN, nullable=False, server_default='0'),
sa.Column('is_muted', BOOLEAN, nullable=False, server_default='0'),
sa.Column('is_muted', BOOLEAN, nullable=False, server_default='0'),
sa.Column('is_valid', BOOLEAN, nullable=False, server_default='1'),
sa.Column('is_valid', BOOLEAN, nullable=False, server_default='1'),
sa.Column('promoted', sa.types.DECIMAL(10, 3), nullable=False, server_default='0'),
sa.Column('promoted', sa.types.DECIMAL(10, 3), nullable=False, server_default='0'),
sa.Column('children', sa.Integer, nullable=False, server_default='0'),
sa.Column('children', sa.Integer, nullable=False, server_default='0'),
# core stats/indexes
# core stats/indexes
sa.Column('payout', sa.types.DECIMAL(10, 3), nullable=False, server_default='0'),
sa.Column('payout', sa.types.DECIMAL(10, 3), nullable=False, server_default='0'),
sa.Column('pending_payout', sa.types.DECIMAL(10, 3), nullable=False, server_default='0'),
sa.Column('pending_payout', sa.types.DECIMAL(10, 3), nullable=False, server_default='0'),
sa.Column('payout_at', sa.DateTime, nullable=False, server_default='1970-01-01'),
sa.Column('payout_at', sa.DateTime, nullable=False, server_default='1970-01-01'),
sa.Column('last_payout_at', sa.DateTime, nullable=False, server_default='1970-01-01'),
sa.Column('last_payout_at', sa.DateTime, nullable=False, server_default='1970-01-01'),
sa.Column('updated_at', sa.DateTime, nullable=False, server_default='1970-01-01'),
sa.Column('updated_at', sa.DateTime, nullable=False, server_default='1970-01-01'),
sa.Column('is_paidout', BOOLEAN, nullable=False, server_default='0'),
sa.Column('is_paidout', BOOLEAN, nullable=False, server_default='0'),
# ui flags/filters
# ui flags/filters
sa.Column('is_nsfw', BOOLEAN, nullable=False, server_default='0'),
sa.Column('is_nsfw', BOOLEAN, nullable=False, server_default='0'),
sa.Column('is_declined', BOOLEAN, nullable=False, server_default='0'),
sa.Column('is_declined', BOOLEAN, nullable=False, server_default='0'),
sa.Column('is_full_power', BOOLEAN, nullable=False, server_default='0'),
sa.Column('is_full_power', BOOLEAN, nullable=False, server_default='0'),
sa.Column('is_hidden', BOOLEAN, nullable=False, server_default='0'),
sa.Column('is_hidden', BOOLEAN, nullable=False, server_default='0'),
sa.Column('is_grayed', BOOLEAN, nullable=False, server_default='0'),
sa.Column('is_grayed', BOOLEAN, nullable=False, server_default='0'),
# important indexes
# important indexes
sa.Column('sc_trend', sa.Float(precision=6), nullable=False, server_default='0'),
sa.Column('sc_trend', sa.Float(precision=6), nullable=False, server_default='0'),
sa.Column('sc_hot', sa.Float(precision=6), nullable=False, server_default='0'),
sa.Column('sc_hot', sa.Float(precision=6), nullable=False, server_default='0'),
sa.Column('total_payout_value', sa.String(30), nullable=False, server_default='0.000 HBD'),
sa.Column('total_payout_value', sa.String(30), nullable=False, server_default='0.000 HBD'),
sa.Column('author_rewards', sa.BigInteger, nullable=False, server_default='0'),
sa.Column('author_rewards', sa.BigInteger, nullable=False, server_default='0'),
sa.Column('author_rewards_hive', sa.BigInteger, nullable=False, server_default='0'),
sa.Column('author_rewards_hive', sa.BigInteger, nullable=False, server_default='0'),
sa.Column('author_rewards_hbd', sa.BigInteger, nullable=False, server_default='0'),
sa.Column('author_rewards_hbd', sa.BigInteger, nullable=False, server_default='0'),
sa.Column('author_rewards_vests', sa.BigInteger, nullable=False, server_default='0'),
sa.Column('author_rewards_vests', sa.BigInteger, nullable=False, server_default='0'),
sa.Column('abs_rshares', sa.BigInteger, nullable=False, server_default='0'),
sa.Column('abs_rshares', sa.BigInteger, nullable=False, server_default='0'),
sa.Column('vote_rshares', sa.BigInteger, nullable=False, server_default='0'),
sa.Column('vote_rshares', sa.BigInteger, nullable=False, server_default='0'),
sa.Column('total_vote_weight', sa.Numeric, nullable=False, server_default='0'),
sa.Column('total_vote_weight', sa.Numeric, nullable=False, server_default='0'),
sa.Column('active', sa.DateTime, nullable=False, server_default='1970-01-01 00:00:00'),
sa.Column('active', sa.DateTime, nullable=False, server_default='1970-01-01 00:00:00'),
sa.Column('cashout_time', sa.DateTime, nullable=False, server_default='1970-01-01 00:00:00'),
sa.Column('cashout_time', sa.DateTime, nullable=False, server_default='1970-01-01 00:00:00'),
sa.Column('percent_hbd', sa.Integer, nullable=False, server_default='10000'),
sa.Column('percent_hbd', sa.Integer, nullable=False, server_default='10000'),
sa.Column('curator_payout_value', sa.String(30), nullable=False, server_default='0.000 HBD'),
sa.Column('curator_payout_value', sa.String(30), nullable=False, server_default='0.000 HBD'),
sa.Column('max_accepted_payout', sa.String(30), nullable=False, server_default='1000000.000 HBD'),
sa.Column('max_accepted_payout', sa.String(30), nullable=False, server_default='1000000.000 HBD'),
sa.Column('allow_votes', BOOLEAN, nullable=False, server_default='1'),
sa.Column('allow_votes', BOOLEAN, nullable=False, server_default='1'),
sa.Column('allow_curation_rewards', BOOLEAN, nullable=False, server_default='1'),
sa.Column('allow_curation_rewards', BOOLEAN, nullable=False, server_default='1'),
sa.Column('beneficiaries', sa.JSON, nullable=False, server_default='[]'),
sa.Column('beneficiaries', sa.JSON, nullable=False, server_default='[]'),
sa.Column('block_num', sa.Integer, nullable=False ),
sa.Column('block_num', sa.Integer, nullable=False ),
sa.ForeignKeyConstraint(['author_id'], ['hive_accounts.id'], name='hive_posts_fk1'),
sa.ForeignKeyConstraint(['author_id'], ['hive_accounts.id'], name='hive_posts_fk1'),
sa.ForeignKeyConstraint(['root_id'], ['hive_posts.id'], name='hive_posts_fk2'),
sa.ForeignKeyConstraint(['root_id'], ['hive_posts.id'], name='hive_posts_fk2'),
sa.ForeignKeyConstraint(['parent_id'], ['hive_posts.id'], name='hive_posts_fk3'),
sa.ForeignKeyConstraint(['parent_id'], ['hive_posts.id'], name='hive_posts_fk3'),
sa.UniqueConstraint('author_id', 'permlink_id', 'counter_deleted', name='hive_posts_ux1'),
sa.UniqueConstraint('author_id', 'permlink_id', 'counter_deleted', name='hive_posts_ux1'),
sa.Index('hive_posts_depth_idx', 'depth'),
sa.Index('hive_posts_depth_idx', 'depth'),
sa.Index('hive_posts_root_id_id_idx', 'root_id','id'),
sa.Index('hive_posts_root_id_id_idx', 'root_id','id'),
sa.Index('hive_posts_parent_id_idx', 'parent_id'),
sa.Index('hive_posts_parent_id_idx', 'parent_id'),
sa.Index('hive_posts_community_id_idx', 'community_id'),
sa.Index('hive_posts_community_id_idx', 'community_id'),
sa.Index('hive_posts_category_id_idx', 'category_id'),
sa.Index('hive_posts_category_id_idx', 'category_id'),
sa.Index('hive_posts_payout_at_idx', 'payout_at'),
sa.Index('hive_posts_payout_at_idx', 'payout_at'),
sa.Index('hive_posts_payout_idx', 'payout'),
sa.Index('hive_posts_payout_idx', 'payout'),
sa.Index('hive_posts_promoted_idx', 'promoted'),
sa.Index('hive_posts_promoted_idx', 'promoted'),
sa.Index('hive_posts_sc_trend_idx', 'sc_trend'),
sa.Index('hive_posts_sc_trend_idx', 'sc_trend'),
sa.Index('hive_posts_sc_hot_idx', 'sc_hot'),
sa.Index('hive_posts_sc_hot_idx', 'sc_hot'),
sa.Index('hive_posts_created_at_idx', 'created_at'),
sa.Index('hive_posts_created_at_idx', 'created_at'),
sa.Index('hive_posts_block_num_idx', 'block_num')
sa.Index('hive_posts_block_num_idx', 'block_num')
)
)
sa.Table(
sa.Table(
'hive_post_data', metadata,
'hive_post_data', metadata,
sa.Column('id', sa.Integer, primary_key=True, autoincrement=False),
sa.Column('id', sa.Integer, primary_key=True, autoincrement=False),
sa.Column('title', VARCHAR(512), nullable=False, server_default=''),
sa.Column('title', VARCHAR(512), nullable=False, server_default=''),
sa.Column('preview', VARCHAR(1024), nullable=False, server_default=''), # first 1k of 'body'
sa.Column('preview', VARCHAR(1024), nullable=False, server_default=''), # first 1k of 'body'
sa.Column('img_url', VARCHAR(1024), nullable=False, server_default=''), # first 'image' from 'json'
sa.Column('img_url', VARCHAR(1024), nullable=False, server_default=''), # first 'image' from 'json'
sa.Column('body', TEXT, nullable=False, server_default=''),
sa.Column('body', TEXT, nullable=False, server_default=''),
sa.Column('json', TEXT, nullable=False, server_default='')
sa.Column('json', TEXT, nullable=False, server_default='')
)
)
sa.Table(
sa.Table(
'hive_permlink_data', metadata,
'hive_permlink_data', metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('permlink', sa.String(255, collation='C'), nullable=False),
sa.Column('permlink', sa.String(255, collation='C'), nullable=False),
sa.UniqueConstraint('permlink', name='hive_permlink_data_permlink')
sa.UniqueConstraint('permlink', name='hive_permlink_data_permlink')
)
)
sa.Table(
sa.Table(
'hive_category_data', metadata,
'hive_category_data', metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('category', sa.String(255, collation='C'), nullable=False),
sa.Column('category', sa.String(255, collation='C'), nullable=False),
sa.UniqueConstraint('category', name='hive_category_data_category')
sa.UniqueConstraint('category', name='hive_category_data_category')
)
)
sa.Table(
sa.Table(
'hive_votes', metadata,
'hive_votes', metadata,
sa.Column('id', sa.BigInteger, primary_key=True),
sa.Column('id', sa.BigInteger, primary_key=True),
sa.Column('post_id', sa.Integer, nullable=False),
sa.Column('post_id', sa.Integer, nullable=False),
sa.Column('voter_id', sa.Integer, nullable=False),
sa.Column('voter_id', sa.Integer, nullable=False),
sa.Column('author_id', sa.Integer, nullable=False),
sa.Column('author_id', sa.Integer, nullable=False),
sa.Column('permlink_id', sa.Integer, nullable=False),
sa.Column('permlink_id', sa.Integer, nullable=False),
sa.Column('weight', sa.Numeric, nullable=False, server_default='0'),
sa.Column('weight', sa.Numeric, nullable=False, server_default='0'),
sa.Column('rshares', sa.BigInteger, nullable=False, server_default='0'),
sa.Column('rshares', sa.BigInteger, nullable=False, server_default='0'),
sa.Column('vote_percent', sa.Integer, server_default='0'),
sa.Column('vote_percent', sa.Integer, server_default='0'),
sa.Column('last_update', sa.DateTime, nullable=False, server_default='1970-01-01 00:00:00'),
sa.Column('last_update', sa.DateTime, nullable=False, server_default='1970-01-01 00:00:00'),
sa.Column('num_changes', sa.Integer, server_default='0'),
sa.Column('num_changes', sa.Integer, server_default='0'),
sa.Column('block_num', sa.Integer, nullable=False ),
sa.Column('block_num', sa.Integer, nullable=False ),
sa.Column('is_effective', BOOLEAN, nullable=False, server_default='0'),
sa.Column('is_effective', BOOLEAN, nullable=False, server_default='0'),
sa.UniqueConstraint('voter_id', 'author_id', 'permlink_id', name='hive_votes_ux1'),
sa.UniqueConstraint('voter_id', 'author_id', 'permlink_id', name='hive_votes_ux1'),
sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id'], name='hive_votes_fk1'),
sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id'], name='hive_votes_fk1'),
sa.ForeignKeyConstraint(['voter_id'], ['hive_accounts.id'], name='hive_votes_fk2'),
sa.ForeignKeyConstraint(['voter_id'], ['hive_accounts.id'], name='hive_votes_fk2'),
sa.ForeignKeyConstraint(['author_id'], ['hive_accounts.id'], name='hive_votes_fk3'),
sa.ForeignKeyConstraint(['author_id'], ['hive_accounts.id'], name='hive_votes_fk3'),
sa.ForeignKeyConstraint(['permlink_id'], ['hive_permlink_data.id'], name='hive_votes_fk4'),
sa.ForeignKeyConstraint(['permlink_id'], ['hive_permlink_data.id'], name='hive_votes_fk4'),
sa.ForeignKeyConstraint(['block_num'], ['hive_blocks.num'], name='hive_votes_fk5'),
sa.ForeignKeyConstraint(['block_num'], ['hive_blocks.num'], name='hive_votes_fk5'),
sa.Index('hive_votes_post_id_idx', 'post_id'),
sa.Index('hive_votes_post_id_idx', 'post_id'),
sa.Index('hive_votes_voter_id_idx', 'voter_id'),
sa.Index('hive_votes_voter_id_idx', 'voter_id'),
sa.Index('hive_votes_voter_id_post_id_idx', 'voter_id', 'post_id'),
sa.Index('hive_votes_voter_id_post_id_idx', 'voter_id', 'post_id'),
sa.Index('hive_votes_post_id_voter_id_idx', 'post_id', 'voter_id'),
sa.Index('hive_votes_post_id_voter_id_idx', 'post_id', 'voter_id'),
sa.Index('hive_votes_block_num_idx', 'block_num')
sa.Index('hive_votes_block_num_idx', 'block_num')
)
)
sa.Table(
sa.Table(
'hive_tag_data', metadata,
'hive_tag_data', metadata,
sa.Column('id', sa.Integer, nullable=False, primary_key=True),
sa.Column('id', sa.Integer, nullable=False, primary_key=True),
sa.Column('tag', VARCHAR(64, collation='C'), nullable=False, server_default=''),
sa.Column('tag', VARCHAR(64, collation='C'), nullable=False, server_default=''),
sa.UniqueConstraint('tag', name='hive_tag_data_ux1')
sa.UniqueConstraint('tag', name='hive_tag_data_ux1')
)
)
sa.Table(
sa.Table(
'hive_post_tags', metadata,
'hive_post_tags', metadata,
sa.Column('post_id', sa.Integer, nullable=False),
sa.Column('post_id', sa.Integer, nullable=False),
sa.Column('tag_id', sa.Integer, nullable=False),
sa.Column('tag_id', sa.Integer, nullable=False),
sa.PrimaryKeyConstraint('post_id', 'tag_id', name='hive_post_tags_pk1'),
sa.PrimaryKeyConstraint('post_id', 'tag_id', name='hive_post_tags_pk1'),
sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id'], name='hive_post_tags_fk1'),
sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id'], name='hive_post_tags_fk1'),
sa.ForeignKeyConstraint(['tag_id'], ['hive_tag_data.id'], name='hive_post_tags_fk2'),
sa.ForeignKeyConstraint(['tag_id'], ['hive_tag_data.id'], name='hive_post_tags_fk2'),
sa.Index('hive_post_tags_tag_id_idx', 'tag_id')
sa.Index('hive_post_tags_tag_id_idx', 'tag_id')
)
)
sa.Table(
sa.Table(
'hive_follows', metadata,
'hive_follows', metadata,
sa.Column('id', sa.Integer, primary_key=True ),
sa.Column('id', sa.Integer, primary_key=True ),
sa.Column('follower', sa.Integer, nullable=False),
sa.Column('follower', sa.Integer, nullable=False),
sa.Column('following', sa.Integer, nullable=False),
sa.Column('following', sa.Integer, nullable=False),
sa.Column('state', SMALLINT, nullable=False, server_default='1'),
sa.Column('state', SMALLINT, nullable=False, server_default='1'),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Column('blacklisted', sa.Boolean, nullable=False, server_default='0'),
sa.Column('blacklisted', sa.Boolean, nullable=False, server_default='0'),
sa.Column('follow_blacklists', sa.Boolean, nullable=False, server_default='0'),
sa.Column('follow_blacklists', sa.Boolean, nullable=False, server_default='0'),
sa.Column('block_num', sa.Integer, nullable=False ),
sa.Column('block_num', sa.Integer, nullable=False ),
sa.UniqueConstraint('following', 'follower', name='hive_follows_ux1'), # core
sa.UniqueConstraint('following', 'follower', name='hive_follows_ux1'), # core
sa.ForeignKeyConstraint(['block_num'], ['hive_blocks.num'], name='hive_follows_fk1'),
sa.ForeignKeyConstraint(['block_num'], ['hive_blocks.num'], name='hive_follows_fk1'),
sa.Index('hive_follows_ix5a', 'following', 'state', 'created_at', 'follower'),
sa.Index('hive_follows_ix5a', 'following', 'state', 'created_at', 'follower'),
sa.Index('hive_follows_ix5b', 'follower', 'state', 'created_at', 'following'),
sa.Index('hive_follows_ix5b', 'follower', 'state', 'created_at', 'following'),
sa.Index('hive_follows_block_num_idx', 'block_num')
sa.Index('hive_follows_block_num_idx', 'block_num')
)
)
sa.Table(
sa.Table(
'hive_reblogs', metadata,
'hive_reblogs', metadata,
sa.Column('id', sa.Integer, primary_key=True ),
sa.Column('id', sa.Integer, primary_key=True ),
sa.Column('account', VARCHAR(16), nullable=False),
sa.Column('account', VARCHAR(16), nullable=False),
sa.Column('post_id', sa.Integer, nullable=False),
sa.Column('post_id', sa.Integer, nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Column('block_num', sa.Integer, nullable=False ),
sa.Column('block_num', sa.Integer, nullable=False ),
sa.ForeignKeyConstraint(['account'], ['hive_accounts.name'], name='hive_reblogs_fk1'),
sa.ForeignKeyConstraint(['account'], ['hive_accounts.name'], name='hive_reblogs_fk1'),
sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id'], name='hive_reblogs_fk2'),
sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id'], name='hive_reblogs_fk2'),
sa.ForeignKeyConstraint(['block_num'], ['hive_blocks.num'], name='hive_reblogs_fk3'),
sa.ForeignKeyConstraint(['block_num'], ['hive_blocks.num'], name='hive_reblogs_fk3'),
sa.UniqueConstraint('account', 'post_id', name='hive_reblogs_ux1'), # core
sa.UniqueConstraint('account', 'post_id', name='hive_reblogs_ux1'), # core
sa.Index('hive_reblogs_account', 'account'),
sa.Index('hive_reblogs_account', 'account'),
sa.Index('hive_reblogs_post_id', 'post_id'),
sa.Index('hive_reblogs_post_id', 'post_id'),
sa.Index('hive_reblogs_block_num_idx', 'block_num')
sa.Index('hive_reblogs_block_num_idx', 'block_num')
)
)
sa.Table(
sa.Table(
'hive_payments', metadata,
'hive_payments', metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('block_num', sa.Integer, nullable=False),
sa.Column('block_num', sa.Integer, nullable=False),
sa.Column('tx_idx', SMALLINT, nullable=False),
sa.Column('tx_idx', SMALLINT, nullable=False),
sa.Column('post_id', sa.Integer, nullable=False),
sa.Column('post_id', sa.Integer, nullable=False),
sa.Column('from_account', sa.Integer, nullable=False),
sa.Column('from_account', sa.Integer, nullable=False),
sa.Column('to_account', sa.Integer, nullable=False),
sa.Column('to_account', sa.Integer, nullable=False),
sa.Column('amount', sa.types.DECIMAL(10, 3), nullable=False),
sa.Column('amount', sa.types.DECIMAL(10, 3), nullable=False),
sa.Column('token', VARCHAR(5), nullable=False),
sa.Column('token', VARCHAR(5), nullable=False),
sa.ForeignKeyConstraint(['from_account'], ['hive_accounts.id'], name='hive_payments_fk1'),
sa.ForeignKeyConstraint(['from_account'], ['hive_accounts.id'], name='hive_payments_fk1'),
sa.ForeignKeyConstraint(['to_account'], ['hive_accounts.id'], name='hive_payments_fk2'),
sa.ForeignKeyConstraint(['to_account'], ['hive_accounts.id'], name='hive_payments_fk2'),
sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id'], name='hive_payments_fk3'),
sa.ForeignKeyConstraint(['post_id'], ['hive_posts.id'], name='hive_payments_fk3'),
sa.Index('hive_payments_from', 'from_account'),
sa.Index('hive_payments_from', 'from_account'),
sa.Index('hive_payments_to', 'to_account'),
sa.Index('hive_payments_to', 'to_account'),
sa.Index('hive_payments_post_id', 'post_id'),
sa.Index('hive_payments_post_id', 'post_id'),
)
)
sa.Table(
sa.Table(
'hive_feed_cache', metadata,
'hive_feed_cache', metadata,
sa.Column('post_id', sa.Integer, nullable=False, primary_key=True),
sa.Column('post_id', sa.Integer, nullable=False, primary_key=True),
sa.Column('account_id', sa.Integer, nullable=False),
sa.Column('account_id', sa.Integer, nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Index('hive_feed_cache_account_id', 'account_id'), # API (and rebuild?)
sa.Index('hive_feed_cache_account_id', 'account_id'), # API (and rebuild?)
sa.UniqueConstraint('account_id', 'post_id', name='hive_feed_cache_ux1')
sa.UniqueConstraint('account_id', 'post_id', name='hive_feed_cache_ux1')
)
)
sa.Table(
sa.Table(
'hive_state', metadata,
'hive_state', metadata,
sa.Column('block_num', sa.Integer, primary_key=True, autoincrement=False),
sa.Column('block_num', sa.Integer, primary_key=True, autoincrement=False),
sa.Column('db_version', sa.Integer, nullable=False),
sa.Column('db_version', sa.Integer, nullable=False),
sa.Column('steem_per_mvest', sa.types.DECIMAL(14, 6), nullable=False),
sa.Column('steem_per_mvest', sa.types.DECIMAL(14, 6), nullable=False),
sa.Column('usd_per_steem', sa.types.DECIMAL(14, 6), nullable=False),
sa.Column('usd_per_steem', sa.types.DECIMAL(14, 6), nullable=False),
sa.Column('sbd_per_steem', sa.types.DECIMAL(14, 6), nullable=False),
sa.Column('sbd_per_steem', sa.types.DECIMAL(14, 6), nullable=False),
sa.Column('dgpo', sa.Text, nullable=False),
sa.Column('dgpo', sa.Text, nullable=False),
)
)
sa.Table(
sa.Table(
'hive_posts_api_helper', metadata,
'hive_posts_api_helper', metadata,
sa.Column('id', sa.Integer, primary_key=True, autoincrement = False),
sa.Column('id', sa.Integer, primary_key=True, autoincrement = False),
sa.Column('author', VARCHAR(16, collation='C'), nullable=False),
sa.Column('author', VARCHAR(16, collation='C'), nullable=False),
sa.Column('parent_author', VARCHAR(16, collation='C'), nullable=False),
sa.Column('parent_author', VARCHAR(16, collation='C'), nullable=False),
sa.Column('parent_permlink_or_category', sa.String(255, collation='C'), nullable=False),
sa.Column('parent_permlink_or_category', sa.String(255, collation='C'), nullable=False),
sa.Index('hive_posts_api_helper_parent_permlink_or_category', 'parent_author', 'parent_permlink_or_category', 'id')
sa.Index('hive_posts_api_helper_parent_permlink_or_category', 'parent_author', 'parent_permlink_or_category', 'id')
)
)
metadata = build_metadata_community(metadata)
metadata = build_metadata_community(metadata)
return metadata
return metadata
def build_metadata_community(metadata=None):
def build_metadata_community(metadata=None):
"""Build community schema defs"""
"""Build community schema defs"""
if not metadata:
if not metadata:
metadata = sa.MetaData()
metadata = sa.MetaData()
sa.Table(
sa.Table(
'hive_communities', metadata,
'hive_communities', metadata,
sa.Column('id', sa.Integer, primary_key=True, autoincrement=False),
sa.Column('id', sa.Integer, primary_key=True, autoincrement=False),
sa.Column('type_id', SMALLINT, nullable=False),
sa.Column('type_id', SMALLINT, nullable=False),
sa.Column('lang', CHAR(2), nullable=False, server_default='en'),
sa.Column('lang', CHAR(2), nullable=False, server_default='en'),
sa.Column('name', VARCHAR(16, collation='C'), nullable=False),
sa.Column('name', VARCHAR(16, collation='C'), nullable=False),
sa.Column('title', sa.String(32), nullable=False, server_default=''),
sa.Column('title', sa.String(32), nullable=False, server_default=''),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Column('sum_pending', sa.Integer, nullable=False, server_default='0'),
sa.Column('sum_pending', sa.Integer, nullable=False, server_default='0'),
sa.Column('num_pending', sa.Integer, nullable=False, server_default='0'),
sa.Column('num_pending', sa.Integer, nullable=False, server_default='0'),
sa.Column('num_authors', sa.Integer, nullable=False, server_default='0'),
sa.Column('num_authors', sa.Integer, nullable=False, server_default='0'),
sa.Column('rank', sa.Integer, nullable=False, server_default='0'),
sa.Column('rank', sa.Integer, nullable=False, server_default='0'),
sa.Column('subscribers', sa.Integer, nullable=False, server_default='0'),
sa.Column('subscribers', sa.Integer, nullable=False, server_default='0'),
sa.Column('is_nsfw', BOOLEAN, nullable=False, server_default='0'),
sa.Column('is_nsfw', BOOLEAN, nullable=False, server_default='0'),
sa.Column('about', sa.String(120), nullable=False, server_default=''),
sa.Column('about', sa.String(120), nullable=False, server_default=''),
sa.Column('primary_tag', sa.String(32), nullable=False, server_default=''),
sa.Column('primary_tag', sa.String(32), nullable=False, server_default=''),
sa.Column('category', sa.String(32), nullable=False, server_default=''),
sa.Column('category', sa.String(32), nullable=False, server_default=''),
sa.Column('avatar_url', sa.String(1024), nullable=False, server_default=''),
sa.Column('avatar_url', sa.String(1024), nullable=False, server_default=''),
sa.Column('description', sa.String(5000), nullable=False, server_default=''),
sa.Column('description', sa.String(5000), nullable=False, server_default=''),
sa.Column('flag_text', sa.String(5000), nullable=False, server_default=''),
sa.Column('flag_text', sa.String(5000), nullable=False, server_default=''),
sa.Column('settings', TEXT, nullable=False, server_default='{}'),
sa.Column('settings', TEXT, nullable=False, server_default='{}'),
sa.Column('block_num', sa.Integer, nullable=False ),
sa.Column('block_num', sa.Integer, nullable=False ),
sa.UniqueConstraint('name', name='hive_communities_ux1'),
sa.UniqueConstraint('name', name='hive_communities_ux1'),
sa.Index('hive_communities_ix1', 'rank', 'id'),
sa.Index('hive_communities_ix1', 'rank', 'id'),
sa.Index('hive_communities_block_num_idx', 'block_num')
sa.Index('hive_communities_block_num_idx', 'block_num')
)
)
sa.Table(
sa.Table(
'hive_roles', metadata,
'hive_roles', metadata,
sa.Column('account_id', sa.Integer, nullable=False),
sa.Column('account_id', sa.Integer, nullable=False),
sa.Column('community_id', sa.Integer, nullable=False),
sa.Column('community_id', sa.Integer, nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Column('role_id', SMALLINT, nullable=False, server_default='0'),
sa.Column('role_id', SMALLINT, nullable=False, server_default='0'),
sa.Column('title', sa.String(140), nullable=False, server_default=''),
sa.Column('title', sa.String(140), nullable=False, server_default=''),
sa.PrimaryKeyConstraint('account_id', 'community_id', name='hive_roles_pk'),
sa.PrimaryKeyConstraint('account_id', 'community_id', name='hive_roles_pk'),
sa.Index('hive_roles_ix1', 'community_id', 'account_id', 'role_id'),
sa.Index('hive_roles_ix1', 'community_id', 'account_id', 'role_id'),
)
)
sa.Table(
sa.Table(
'hive_subscriptions', metadata,
'hive_subscriptions', metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('account_id', sa.Integer, nullable=False),
sa.Column('account_id', sa.Integer, nullable=False),
sa.Column('community_id', sa.Integer, nullable=False),
sa.Column('community_id', sa.Integer, nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Column('block_num', sa.Integer, nullable=False ),
sa.Column('block_num', sa.Integer, nullable=False ),
sa.UniqueConstraint('account_id', 'community_id', name='hive_subscriptions_ux1'),
sa.UniqueConstraint('account_id', 'community_id', name='hive_subscriptions_ux1'),
sa.Index('hive_subscriptions_ix1', 'community_id', 'account_id', 'created_at'),
sa.Index('hive_subscriptions_ix1', 'community_id', 'account_id', 'created_at'),
sa.Index('hive_subscriptions_block_num_idx', 'block_num')
sa.Index('hive_subscriptions_block_num_idx', 'block_num')
)
)
sa.Table(
sa.Table(
'hive_notifs', metadata,
'hive_notifs', metadata,
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('type_id', SMALLINT, nullable=False),
sa.Column('type_id', SMALLINT, nullable=False),
sa.Column('score', SMALLINT, nullable=False),
sa.Column('score', SMALLINT, nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Column('created_at', sa.DateTime, nullable=False),
sa.Column('src_id', sa.Integer, nullable=True),
sa.Column('src_id', sa.Integer, nullable=True),
sa.Column('dst_id', sa.Integer, nullable=True),
sa.Column('dst_id', sa.Integer, nullable=True),
sa.Column('post_id', sa.Integer, nullable=True),
sa.Column('post_id', sa.Integer, nullable=True),
sa.Column('community_id', sa.Integer, nullable=True),
sa.Column('community_id', sa.Integer, nullable=True),
sa.Column('block_num', sa.Integer, nullable=True),
sa.Column('block_num', sa.Integer, nullable=True),
sa.Column('payload', sa.Text, nullable=True),
sa.Column('payload', sa.Text, nullable=True),
sa.Index('hive_notifs_ix1', 'dst_id', 'id', postgresql_where=sql_text("dst_id IS NOT NULL")),
sa.Index('hive_notifs_ix1', 'dst_id', 'id', postgresql_where=sql_text("dst_id IS NOT NULL")),
sa.Index('hive_notifs_ix2', 'community_id', 'id', postgresql_where=sql_text("community_id IS NOT NULL")),
sa.Index('hive_notifs_ix2', 'community_id', 'id', postgresql_where=sql_text("community_id IS NOT NULL")),
sa.Index('hive_notifs_ix3', 'community_id', 'type_id', 'id', postgresql_where=sql_text("community_id IS NOT NULL")),
sa.Index('hive_notifs_ix3', 'community_id', 'type_id', 'id', postgresql_where=sql_text("community_id IS NOT NULL")),
sa.Index('hive_notifs_ix4', 'community_id', 'post_id', 'type_id', 'id', postgresql_where=sql_text("community_id IS NOT NULL AND post_id IS NOT NULL")),
sa.Index('hive_notifs_ix4', 'community_id', 'post_id', 'type_id', 'id', postgresql_where=sql_text("community_id IS NOT NULL AND post_id IS NOT NULL")),
sa.Index('hive_notifs_ix5', 'post_id', 'type_id', 'dst_id', 'src_id', postgresql_where=sql_text("post_id IS NOT NULL AND type_id IN (16,17)")), # filter: dedupe
sa.Index('hive_notifs_ix5', 'post_id', 'type_id', 'dst_id', 'src_id', postgresql_where=sql_text("post_id IS NOT NULL AND type_id IN (16,17)")), # filter: dedupe
sa.Index('hive_notifs_ix6', 'dst_id', 'created_at', 'score', 'id', postgresql_where=sql_text("dst_id IS NOT NULL")), # unread
sa.Index('hive_notifs_ix6', 'dst_id', 'created_at', 'score', 'id', postgresql_where=sql_text("dst_id IS NOT NULL")), # unread
)
)
return metadata
return metadata
def teardown(db):
def teardown(db):
"""Drop all tables"""
"""Drop all tables"""
build_metadata().drop_all(db.engine())
build_metadata().drop_all(db.engine())
def drop_fk(db):
def drop_fk(db):
db.query_no_return("START TRANSACTION")
db.query_no_return("START TRANSACTION")
for table in build_metadata().sorted_tables:
for table in build_metadata().sorted_tables:
for fk in table.foreign_keys:
for fk in table.foreign_keys:
sql = """ALTER TABLE {} DROP CONSTRAINT IF EXISTS {}""".format(table.name, fk.name)
sql = """ALTER TABLE {} DROP CONSTRAINT IF EXISTS {}""".format(table.name, fk.name)
db.query_no_return(sql)
db.query_no_return(sql)
db.query_no_return("COMMIT")
db.query_no_return("COMMIT")
def create_fk(db):
def create_fk(db):
from sqlalchemy.schema import AddConstraint
from sqlalchemy.schema import AddConstraint
from sqlalchemy import text
from sqlalchemy import text
connection = db.engine().connect()
connection = db.engine().connect()
connection.execute(text("START TRANSACTION"))
connection.execute(text("START TRANSACTION"))
for table in build_metadata().sorted_tables:
for table in build_metadata().sorted_tables:
for fk in table.foreign_keys:
for fk in table.foreign_keys:
connection.execute(AddConstraint(fk.constraint))
connection.execute(AddConstraint(fk.constraint))
connection.execute(text("COMMIT"))
connection.execute(text("COMMIT"))
def setup(db):
def setup(db):
"""Creates all tables and seed data"""
"""Creates all tables and seed data"""
# initialize schema
# initialize schema
build_metadata().create_all(db.engine())
build_metadata().create_all(db.engine())
# tune auto vacuum/analyze
# tune auto vacuum/analyze
reset_autovac(db)
reset_autovac(db)
# sets FILLFACTOR:
# sets FILLFACTOR:
set_fillfactor(db)
set_fillfactor(db)
# default rows
# default rows
sqls = [
sqls = [
"INSERT INTO hive_state (block_num, db_version, steem_per_mvest, usd_per_steem, sbd_per_steem, dgpo) VALUES (0, %d, 0, 0, 0, '')" % DB_VERSION,
"INSERT INTO hive_state (block_num, db_version, steem_per_mvest, usd_per_steem, sbd_per_steem, dgpo) VALUES (0, %d, 0, 0, 0, '')" % DB_VERSION,
"INSERT INTO hive_blocks (num, hash, created_at) VALUES (0, '0000000000000000000000000000000000000000', '2016-03-24 16:04:57')",
"INSERT INTO hive_blocks (num, hash, created_at) VALUES (0, '0000000000000000000000000000000000000000', '2016-03-24 16:04:57')",
"INSERT INTO hive_permlink_data (id, permlink) VALUES (0, '')",
"INSERT INTO hive_permlink_data (id, permlink) VALUES (0, '')",
"INSERT INTO hive_category_data (id, category) VALUES (0, '')",
"INSERT INTO hive_category_data (id, category) VALUES (0, '')",
"INSERT INTO hive_accounts (id, name, created_at) VALUES (0, '', '1970-01-01T00:00:00')",
"INSERT INTO hive_accounts (id, name, created_at) VALUES (0, '', '1970-01-01T00:00:00')",
"INSERT INTO hive_accounts (name, created_at) VALUES ('miners', '2016-03-24 16:05:00')",
"INSERT INTO hive_accounts (name, created_at) VALUES ('miners', '2016-03-24 16:05:00')",
"INSERT INTO hive_accounts (name, created_at) VALUES ('null', '2016-03-24 16:05:00')",
"INSERT INTO hive_accounts (name, created_at) VALUES ('null', '2016-03-24 16:05:00')",
"INSERT INTO hive_accounts (name, created_at) VALUES ('temp', '2016-03-24 16:05:00')",
"INSERT INTO hive_accounts (name, created_at) VALUES ('temp', '2016-03-24 16:05:00')",
"INSERT INTO hive_accounts (name, created_at) VALUES ('initminer', '2016-03-24 16:05:00')",
"INSERT INTO hive_accounts (name, created_at) VALUES ('initminer', '2016-03-24 16:05:00')",
"""
"""
INSERT INTO
INSERT INTO
public.hive_posts(id, root_id, parent_id, author_id, permlink_id, category_id,
public.hive_posts(id, root_id, parent_id, author_id, permlink_id, category_id,
community_id, created_at, depth, block_num
community_id, created_at, depth, block_num
)
)
VALUES
VALUES
(0, 0, 0, 0, 0, 0, 0, now(), 0, 0);
(0, 0, 0, 0, 0, 0, 0, now(), 0, 0);
"""]
"""]
for sql in sqls:
for sql in sqls:
db.query(sql)
db.query(sql)
sql = "CREATE INDEX hive_communities_ft1 ON hive_communities USING GIN (to_tsvector('english', title || ' ' || about))"
sql = "CREATE INDEX hive_communities_ft1 ON hive_communities USING GIN (to_tsvector('english', title || ' ' || about))"
db.query(sql)
db.query(sql)
sql = """
sql = """
DROP FUNCTION if exists process_hive_post_operation(character varying,character varying,character varying,character varying,timestamp without time zone,timestamp without time zone)
DROP FUNCTION IF EXISTS find_comment_id(character varying, character varying, boolean)
;
;
CREATE OR REPLACE FUNCTION process_hive_post_operation(
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,
in _parent_author hive_accounts.name%TYPE,
in _check boolean)
in _parent_permlink hive_permlink_data.permlink%TYPE,
RETURNS INT
in _date hive_posts.created_at%TYPE,
LANGUAGE 'plpgsql'
in _community_support_start_date hive_posts.created_at%TYPE,
AS
in _block_num hive_posts.block_num%TYPE)
$function$
RETURNS TABLE (is_new_post boolean, id hive_posts.id%TYPE, author_id hive_posts.author_id%TYPE, permlink_id hive_posts.permlink_id%TYPE,
DECLARE
post_category hive_category_data.category%TYPE, parent_id hive_posts.parent_id%TYPE, community_id hive_posts.community_id%TYPE,
post_id INT;
is_valid hive_posts.is_valid%TYPE, is_muted hive_posts.is_muted%TYPE, depth hive_posts.depth%TYPE)
BEGIN
LANGUAGE plpgsql
SELECT INTO post_id COALESCE( (SELECT hp.id
AS
FROM hive_posts hp
$function$
JOIN hive_accounts ha ON ha.id = hp.author_id
BEGIN
JOIN hive_permlink_data hpd ON hpd.id = hp.permlink_id
WHERE ha.name = _author AND hpd.permlink = _permlink AND hp.counter_deleted = 0
INSERT INTO hive_permlink_data
), 0 );
(permlink)
IF _check AND (_author <> '' OR _permlink <> '') AND post_id = 0 THEN
values
RAISE EXCEPTION 'Post %/% does not exist', _author, _permlink;
(
END IF;
_permlink
RETURN post_id;
)
END
ON CONFLICT DO NOTHING
$function$
;
;
if _parent_author != '' THEN
"""
RETURN QUERY INSERT INTO hive_posts as hp
db.query_no_return(sql)
(parent_id, depth, community_id, category_id,
root_id, is_muted, is_valid,
sql = """
author_id, permlink_id, created_at, updated_at, sc_hot, sc_trend, active, payout_at, cashout_time, counter_deleted, block_num)
DROP FUNCTION IF EXISTS find_account_id(character varying, boolean)
SELECT php.id AS parent_id, php.depth + 1 AS depth,
;
(CASE
CREATE OR REPLACE FUNCTION find_account_id(
WHEN _date > _community_support_start_date THEN
in _account hive_accounts.name%TYPE,
COALESCE(php.community_id, (select hc.id from hive_communities hc where hc.name = _parent_permlink))
in _check boolean)
ELSE NULL
RETURNS INT
END) AS community_id,
LANGUAGE 'plpgsql'
COALESCE(php.category_id, (select hcg.id from hive_category_data hcg where hcg.category = _parent_permlink)) AS category_id,
AS
(CASE(php.root_id)
$function$
WHEN 0 THEN php.id
DECLARE
ELSE php.root_id
account_id INT;
END) AS root_id,
BEGIN
php.is_muted AS is_muted, php.is_valid AS is_valid,
SELECT INTO account_id COALESCE( ( SELECT id FROM hive_accounts WHERE name=_account ), 0 );
ha.id AS author_id, hpd.id AS permlink_id, _date AS created_at,
IF _check AND account_id = 0 THEN
_date AS updated_at,
RAISE EXCEPTION 'Account % does not exist', _account;
calculate_time_part_of_hot(_date) AS sc_hot,
END IF;
calculate_time_part_of_trending(_date) AS sc_trend,
RETURN account_id;
_date AS active, (_date + INTERVAL '7 days') AS payout_at, (_date + INTERVAL '7 days') AS cashout_time, 0, _block_num as block_num
END
FROM hive_accounts ha,
$function$
hive_permlink_data hpd,
;
hive_posts php
"""
INNER JOIN hive_accounts pha ON pha.id = php.author_id
db.query_no_return(sql)
INNER JOIN hive_permlink_data phpd ON phpd.id = php.permlink_id
WHERE pha.name = _parent_author AND phpd.permlink = _parent_permlink AND
sql = """
ha.name = _author AND hpd.permlink = _permlink AND php.counter_deleted = 0
DROP FUNCTION if exists process_hive_post_operation(character varying,character varying,character varying,character varying,timestamp without time zone,timestamp without time zone)
;
ON CONFLICT ON CONSTRAINT hive_posts_ux1 DO UPDATE SET
CREATE OR REPLACE FUNCTION process_hive_post_operation(
--- During post update it is disallowed to change: parent-post, category, community-id
in _author hive_accounts.name%TYPE,
--- then also depth, is_valid and is_muted is impossible to change
in _permlink hive_permlink_data.permlink%TYPE,
--- post edit part
in _parent_author hive_accounts.name%TYPE,
updated_at = _date,
in _parent_permlink hive_permlink_data.permlink%TYPE,
active = _date
in _date hive_posts.created_at%TYPE,
RETURNING (xmax = 0) as is_new_post, hp.id, hp.author_id, hp.permlink_id, (SELECT hcd.category FROM hive_category_data hcd WHERE hcd.id = hp.category_id) as post_category, hp.parent_id, hp.community_id, hp.is_valid, hp.is_muted, hp.depth
in _community_support_start_date hive_posts.created_at%TYPE,
;
in _block_num hive_posts.block_num%TYPE)
ELSE
RETURNS TABLE (is_new_post boolean, id hive_posts.id%TYPE, author_id hive_posts.author_id%TYPE, permlink_id hive_posts.permlink_id%TYPE,
INSERT INTO hive_category_data
post_category hive_category_data.category%TYPE, parent_id hive_posts.parent_id%TYPE, community_id hive_posts.community_id%TYPE,
(category)
is_valid hive_posts.is_valid%TYPE, is_muted hive_posts.is_muted%TYPE, depth hive_posts.depth%TYPE)
VALUES (_parent_permlink)
LANGUAGE plpgsql
ON CONFLICT (category) DO NOTHING
AS
;
$function$
BEGIN
RETURN QUERY INSERT INTO hive_posts as hp
(parent_id, depth, community_id, category_id,
INSERT INTO hive_permlink_data
root_id, is_muted, is_valid,
(permlink)
author_id, permlink_id, created_at, updated_at, sc_hot, sc_trend, active, payout_at, cashout_time, counter_deleted, block_num)
values
SELECT 0 AS parent_id, 0 AS depth,
(
(CASE
_permlink
WHEN _date > _community_support_start_date THEN
)
(select hc.id FROM hive_communities hc WHERE hc.name = _parent_permlink)
ON CONFLICT DO NOTHING
ELSE NULL
;
END) AS community_id,
if _parent_author != '' THEN
(SELECT hcg.id FROM hive_category_data hcg WHERE hcg.category = _parent_permlink) AS category_id,
RETURN QUERY INSERT INTO hive_posts as hp
0 as root_id, -- will use id as root one if no parent
(parent_id, depth, community_id, category_id,
false AS is_muted, true AS is_valid,
root_id, is_muted, is_valid,
ha.id AS author_id, hpd.id AS permlink_id, _date AS created_at,
author_id, permlink_id, created_at, updated_at, sc_hot, sc_trend, active, payout_at, cashout_time, counter_deleted, block_num)
_date AS updated_at,
SELECT php.id AS parent_id, php.depth + 1 AS depth,
calculate_time_part_of_hot(_date) AS sc_hot,
(CASE
calculate_time_part_of_trending(_date) AS sc_trend,
WHEN _date > _community_support_start_date THEN
_date AS active, (_date + INTERVAL '7 days') AS payout_at, (_date + INTERVAL '7 days') AS cashout_time, 0, _block_num as block_num
COALESCE(php.community_id, (select hc.id from hive_communities hc where hc.name = _parent_permlink))
FROM hive_accounts ha,
ELSE NULL
hive_permlink_data hpd
END) AS community_id,
WHERE ha.name = _author and hpd.permlink = _permlink
COALESCE(php.category_id, (select hcg.id from hive_category_data hcg where hcg.category = _parent_permlink)) AS category_id,
(CASE(php.root_id)
ON CONFLICT ON CONSTRAINT hive_posts_ux1 DO UPDATE SET
WHEN 0 THEN php.id
--- During post update it is disallowed to change: parent-post, category, community-id
ELSE php.root_id
--- then also depth, is_valid and is_muted is impossible to change
END) AS root_id,
--- post edit part
php.is_muted AS is_muted, php.is_valid AS is_valid,
updated_at = _date,
ha.id AS author_id, hpd.id AS permlink_id, _date AS created_at,
active = _date,
_date AS updated_at,
block_num = _block_num
calculate_time_part_of_hot(_date) AS sc_hot,
calculate_time_part_of_trending(_date) AS sc_trend,
RETURNING (xmax = 0) as is_new_post, hp.id, hp.author_id, hp.permlink_id, _parent_permlink as post_category, hp.parent_id, hp.community_id, hp.is_valid, hp.is_muted, hp.depth
_date AS active, (_date + INTERVAL '7 days') AS payout_at, (_date + INTERVAL '7 days') AS cashout_time, 0, _block_num as block_num
;
FROM hive_accounts ha,
END IF;
hive_permlink_data hpd,
END
hive_posts php
$function$
INNER JOIN hive_accounts pha ON pha.id = php.author_id
"""
INNER JOIN hive_permlink_data phpd ON phpd.id = php.permlink_id
db.query_no_return(sql)
WHERE pha.name = _parent_author AND phpd.permlink = _parent_permlink AND
ha.name = _author AND hpd.permlink = _permlink AND php.counter_deleted = 0
sql = """
DROP FUNCTION if exists delete_hive_post(character varying,character varying,character varying)
ON CONFLICT ON CONSTRAINT hive_posts_ux1 DO UPDATE SET
;
--- During post update it is disallowed to change: parent-post, category, community-id
CREATE OR REPLACE FUNCTION delete_hive_post(
--- then also depth, is_valid and is_muted is impossible to change
in _author hive_accounts.name%TYPE,
--- post edit part
in _permlink hive_permlink_data.permlink%TYPE)
updated_at = _date,
RETURNS TABLE (id hive_posts.id%TYPE, depth hive_posts.depth%TYPE)
active = _date
LANGUAGE plpgsql
RETURNING (xmax = 0) as is_new_post, hp.id, hp.author_id, hp.permlink_id, (SELECT hcd.category FROM hive_category_data hcd WHERE hcd.id = hp.category_id) as post_category, hp.parent_id, hp.community_id, hp.is_valid, hp.is_muted, hp.depth
AS
;
$function$
ELSE
BEGIN
INSERT INTO hive_category_data
RETURN QUERY UPDATE hive_posts AS hp
(category)
SET counter_deleted =
VALUES (_parent_permlink)
(
ON CONFLICT (category) DO NOTHING
SELECT max( hps.counter_deleted ) + 1
;
FROM hive_posts hps
INNER JOIN hive_accounts ha ON hps.author_id = ha.id
RETURN QUERY INSERT INTO hive_posts as hp
INNER JOIN hive_permlink_data hpd ON hps.permlink_id = hpd.id
(parent_id, depth, community_id, category_id,
WHERE ha.name = _author AND hpd.permlink = _permlink
root_id, is_muted, is_valid,
)
author_id, permlink_id, created_at, updated_at, sc_hot, sc_trend, active, payout_at, cashout_time, counter_deleted, block_num)
FROM hive_posts hp1
SELECT 0 AS parent_id, 0 AS depth,
INNER JOIN hive_accounts ha ON hp1.author_id = ha.id
(CASE
INNER JOIN hive_permlink_data hpd ON hp1.permlink_id = hpd.id
WHEN _date > _community_support_start_date THEN
WHERE hp.id = hp1.id AND ha.name = _author AND hpd.permlink = _permlink AND hp1.counter_deleted = 0
(select hc.id FROM hive_communities hc WHERE hc.name = _parent_permlink)
RETURNING hp.id, hp.depth;
ELSE NULL
END
END) AS community_id,
$function$
(SELECT hcg.id FROM hive_category_data hcg WHERE hcg.category = _parent_permlink) AS category_id,
"""
0 as root_id, -- will use id as root one if no parent
db.query_no_return(sql)
false AS is_muted, true AS is_valid,
ha.id AS author_id, hpd.id AS permlink_id, _date AS created_at,
# In original hivemind, a value of 'active_at' was calculated from
_date AS updated_at,
# max
calculate_time_part_of_hot(_date) AS sc_hot,
# {
calculate_time_part_of_trending(_date) AS sc_trend,
# created ( account_create_operation ),
_date AS active, (_date + INTERVAL '7 days') AS payout_at, (_date + INTERVAL '7 days') AS cashout_time, 0, _block_num as block_num
# last_account_update ( account_update_operation/account_update2_operation ),
FROM hive_accounts ha,
# last_post ( comment_operation - only creation )
hive_permlink_data hpd
# last_root_post ( comment_operation - only creation + only ROOT ),
WHERE ha.name = _author and hpd.permlink = _permlink
# last_vote_time ( vote_operation )
# }
ON CONFLICT ON CONSTRAINT hive_posts_ux1 DO UPDATE SET
# In order to simplify calculations, `last_account_update` is not taken into consideration, because this updating accounts is very rare
--- During post update it is disallowed to change: parent-post, category, community-id
# and posting/voting after an account updating, fixes `active_at` value immediately.
--- then also depth, is_valid and is_muted is impossible to change
--- post edit part
sql = """
updated_at = _date,
DROP VIEW IF EXISTS public.hive_accounts_info_view;
active = _date,
block_num = _block_num
CREATE OR REPLACE VIEW public.hive_accounts_info_view
AS
RETURNING (xmax = 0) as is_new_post, hp.id, hp.author_id, hp.permlink_id, _parent_permlink as post_category, hp.parent_id, hp.community_id, hp.is_valid, hp.is_muted, hp.depth
SELECT
;
id,
END IF;
name,
END
(
$function$
select count(*) post_count
"""
FROM hive_posts hp
db.query_no_return(sql)
WHERE ha.id=hp.author_id
) post_count,
sql = """
created_at,
DROP FUNCTION if exists delete_hive_post(character varying,character varying,character varying)
(
;
SELECT GREATEST
CREATE OR REPLACE FUNCTION delete_hive_post(
(
in _author hive_accounts.name%TYPE,
created_at,
in _permlink hive_permlink_data.permlink%TYPE)
COALESCE(
RETURNS TABLE (id hive_posts.id%TYPE, depth hive_posts.depth%TYPE)
(
LANGUAGE plpgsql
select max(hp.created_at)
AS
FROM hive_posts hp
$function$
WHERE ha.id=hp.author_id
BEGIN
),
RETURN QUERY UPDATE hive_posts AS hp
'1970-01-01 00:00:00.0'
SET counter_deleted =
),
(
COALESCE(
SELECT max( hps.counter_deleted ) + 1
(
FROM hive_posts hps
select max(hv.last_update)
INNER JOIN hive_accounts ha ON hps.author_id = ha.id
from hive_votes hv
INNER JOIN hive_permlink_data hpd ON hps.permlink_id = hpd.id
WHERE ha.id=hv.voter_id
WHERE ha.name = _author AND hpd.permlink = _permlink
),
)
'1970-01-01 00:00:00.0'
FROM hive_posts hp1
)
INNER JOIN hive_accounts ha ON hp1.author_id = ha.id
)
INNER JOIN hive_permlink_data hpd ON hp1.permlink_id = hpd.id
) active_at,
WHERE hp.id = hp1.id AND ha.name = _author AND hpd.permlink = _permlink AND hp1.counter_deleted = 0
display_name,
RETURNING hp.id, hp.depth;
about,
END
reputation,
$function$
profile_image,
"""
location,
db.query_no_return(sql)
website,
cover_image,
# In original hivemind, a value of 'active_at' was calculated from
rank,
# max
following,
# {
followers,
# created ( account_create_operation ),
proxy,
# last_account_update ( account_update_operation/account_update2_operation ),
proxy_weight,
# last_post ( comment_operation - only creation )
lastread_at,
# last_root_post ( comment_operation - only creation + only ROOT ),
cached_at,
# last_vote_time ( vote_operation )
raw_json
# }
FROM
# In order to simplify calculations, `last_account_update` is not taken into consideration, because this updating accounts is very rare
hive_accounts ha
# and posting/voting after an account updating, fixes `active_at` value immediately.
"""
db.query_no_return(sql)
sql = """
DROP VIEW IF EXISTS public.hive_accounts_info_view;
sql = """
DROP VIEW IF EXISTS public.hive_posts_view;
CREATE OR REPLACE VIEW public.hive_accounts_info_view
AS
CREATE OR REPLACE VIEW public.hive_posts_view
SELECT
AS
id,
SELECT hp.id,
name,
hp.community_id,
(
hp.root_id,
select count(*) post_count
hp.parent_id,
FROM hive_posts hp
ha_a.name AS author,
WHERE ha.id=hp.author_id
hp.active,
) post_count,
hp.author_rewards,
created_at,
hp.author_id,
(
hpd_p.permlink,
SELECT GREATEST
hpd.title,
(
hpd.body,
created_at,
hpd.img_url,
COALESCE(
hpd.preview,
(
hcd.category,
select max(hp.created_at)
hp.depth,
FROM hive_posts hp
hp.promoted,
WHERE ha.id=hp.author_id
hp.payout,
),
hp.pending_payout,
'1970-01-01 00:00:00.0'
hp.payout_at,
),
hp.last_payout_at,
COALESCE(
hp.cashout_time,
(
hp.is_paidout,
select max(hv.last_update)
hp.children,
from hive_votes hv
0 AS votes,
WHERE ha.id=hv.voter_id
0 AS active_votes,
),
hp.created_at,
'1970-01-01 00:00:00.0'
hp.updated_at,
)
COALESCE(
)
(
) active_at,
SELECT SUM( v.rshares )
display_name,
FROM hive_votes v
about,
WHERE v.post_id = hp.id
reputation,
GROUP BY v.post_id
profile_image,
), 0
location,
) AS rshares,
website,
COALESCE(
cover_image,
(
rank,
SELECT SUM( CASE v.rshares >= 0 WHEN True THEN v.rshares ELSE -v.rshares END )
following,
FROM hive_votes v
followers,
WHERE v.post_id = hp.id AND NOT v.rshares = 0
proxy,
GROUP BY v.post_id
proxy_weight,
), 0
lastread_at,
) AS abs_rshares,
cached_at,
COALESCE(
raw_json
(
FROM
SELECT COUNT( 1 )
hive_accounts ha
FROM hive_votes v
"""
WHERE v.post_id = hp.id AND v.is_effective
db.query_no_return(sql)
GROUP BY v.post_id
), 0
sql = """
) AS total_votes,
DROP VIEW IF EXISTS public.hive_posts_view;
COALESCE(
(
CREATE OR REPLACE VIEW public.hive_posts_view
SELECT SUM( CASE v.rshares > 0 WHEN True THEN 1 ELSE -1 END )
AS
FROM hive_votes v
SELECT hp.id,
WHERE v.post_id = hp.id AND NOT v.rshares = 0
hp.community_id,
GROUP BY v.post_id
hp.root_id,
), 0
hp.parent_id,
) AS net_votes,
ha_a.name AS author,
hpd.json,
hp.active,
ha_a.reputation AS author_rep,
hp.author_rewards,
hp.is_hidden,
hp.author_id,
hp.is_grayed,
hpd_p.permlink,
hp.total_vote_weight,
hpd.title,
ha_pp.name AS parent_author,
hpd.body,
ha_pp.id AS parent_author_id,
hpd.img_url,
( CASE hp.depth > 0
hpd.preview,
WHEN True THEN hpd_pp.permlink
hcd.category,
ELSE hcd.category
hp.depth,
END ) AS parent_permlink_or_category,
hp.promoted,
hp.curator_payout_value,
hp.payout,
ha_rp.name AS root_author,
hp.pending_payout,
hpd_rp.permlink AS root_permlink,
hp.payout_at,
rcd.category as root_category,
hp.last_payout_at,
hp.max_accepted_payout,
hp.cashout_time,
hp.percent_hbd,
hp.is_paidout,
True AS allow_replies,
hp.children,
hp.allow_votes,
0 AS votes,
hp.allow_curation_rewards,
0 AS active_votes,
hp.beneficiaries,
hp.created_at,
CONCAT('/', rcd.category, '/@', ha_rp.name, '/', hpd_rp.permlink,
hp.updated_at,
CASE (rp.id)
COALESCE(
WHEN hp.id THEN ''
(
ELSE CONCAT('#@', ha_a.name, '/', hpd_p.permlink)
SELECT SUM( v.rshares )
END
FROM hive_votes v
) AS url,
WHERE v.post_id = hp.id
rpd.title AS root_title,
GROUP BY v.post_id
hp.sc_trend,
), 0
hp.sc_hot,
) AS rshares,
hp.is_pinned,
COALESCE(
hp.is_muted,
(
hp.is_nsfw,
SELECT SUM( CASE v.rshares >= 0 WHEN True THEN v.rshares ELSE -v.rshares END )
hp.is_valid,
FROM hive_votes v
hr.title AS role_title,
WHERE v.post_id = hp.id AND NOT v.rshares = 0
hr.role_id AS role_id,
GROUP BY v.post_id
hc.title AS community_title,
), 0
hc.name AS community_name,
) AS abs_rshares,
hp.block_num
COALESCE(
FROM hive_posts hp
(
JOIN hive_posts pp ON pp.id = hp.parent_id
SELECT COUNT( 1 )
JOIN hive_posts rp ON rp.id = hp.root_id
FROM hive_votes v
JOIN hive_accounts ha_a ON ha_a.id = hp.author_id
WHERE v.post_id = hp.id AND v.is_effective
JOIN hive_permlink_data hpd_p ON hpd_p.id = hp.permlink_id
GROUP BY v.post_id
JOIN hive_post_data hpd ON hpd.id = hp.id
), 0
JOIN hive_accounts ha_pp ON ha_pp.id = pp.author_id
) AS total_votes,
JOIN hive_permlink_data hpd_pp ON hpd_pp.id = pp.permlink_id
COALESCE(
JOIN hive_accounts ha_rp ON ha_rp.id = rp.author_id
(
JOIN hive_permlink_data hpd_rp ON hpd_rp.id = rp.permlink_id
SELECT SUM( CASE v.rshares > 0 WHEN True THEN 1 ELSE -1 END )
JOIN hive_post_data rpd ON rpd.id = rp.id
FROM hive_votes v
JOIN hive_category_data hcd ON hcd.id = hp.category_id
WHERE v.post_id = hp.id AND NOT v.rshares = 0
JOIN hive_category_data rcd ON rcd.id = rp.category_id
GROUP BY v.post_id
LEFT JOIN hive_communities hc ON hp.community_id = hc.id
), 0
LEFT JOIN hive_roles hr ON hp.author_id = hr.account_id AND hp.community_id = hr.community_id
) AS net_votes,
WHERE hp.counter_deleted = 0;
hpd.json,
"""
ha_a.reputation AS author_rep,
db.query_no_return(sql)
hp.is_hidden,
hp.is_grayed,
sql = """
hp.total_vote_weight,
DROP FUNCTION IF EXISTS public.update_hive_posts_root_id(INTEGER, INTEGER);
ha_pp.name AS parent_author,
ha_pp.id AS parent_author_id,
CREATE OR REPLACE FUNCTION public.update_hive_posts_root_id(in _first_block_num INTEGER, _last_block_num INTEGER)
( CASE hp.depth > 0
RETURNS void
WHEN True THEN hpd_pp.permlink
LANGUAGE 'plpgsql'
ELSE hcd.category
VOLATILE
END ) AS parent_permlink_or_category,
AS $BODY$
hp.curator_payout_value,
BEGIN
ha_rp.name AS root_author,
hpd_rp.permlink AS root_permlink,
--- _first_block_num can be null together with _last_block_num
rcd.category as root_category,
UPDATE hive_posts uhp
hp.max_accepted_payout,
SET root_id = id
hp.percent_hbd,
WHERE uhp.root_id = 0 AND (_first_block_num IS NULL OR (uhp.block_num >= _first_block_num AND uhp.block_num <= _last_block_num))
True AS allow_replies,
;
hp.allow_votes,
END
hp.allow_curation_rewards,
$BODY$;
hp.beneficiaries,
"""
CONCAT('/', rcd.category, '/@', ha_rp.name, '/', hpd_rp.permlink,
db.query_no_return(sql)
CASE (rp.id)
WHEN hp.id THEN ''
sql = """
ELSE CONCAT('#@', ha_a.name, '/', hpd_p.permlink)
DROP FUNCTION IF EXISTS public.update_hive_posts_children_count(INTEGER, INTEGER);
END
) AS url,
CREATE OR REPLACE FUNCTION public.update_hive_posts_children_count(in _first_block INTEGER, in _last_block INTEGER)
rpd.title AS root_title,
RETURNS void
hp.sc_trend,
LANGUAGE SQL
hp.sc_hot,
VOLATILE
hp.is_pinned,
AS $BODY$
hp.is_muted,
UPDATE hive_posts uhp
hp.is_nsfw,
SET children = data_source.children_count
hp.is_valid,
FROM
hr.title AS role_title,
(
hr.role_id AS role_id,
WITH recursive tblChild AS
hc.title AS community_title,
(
hc.name AS community_name,
SELECT s.queried_parent, s.id
hp.block_num
FROM
FROM hive_posts hp
(SELECT h1.Parent_Id AS queried_parent, h1.id
JOIN hive_posts pp ON pp.id = hp.parent_id
FROM hive_posts h1
JOIN hive_posts rp ON rp.id = hp.root_id
WHERE h1.depth > 0 AND h1.counter_deleted = 0
JOIN hive_accounts ha_a ON ha_a.id = hp.author_id
AND h1.block_num BETWEEN _first_block AND _last_block
JOIN hive_permlink_data hpd_p ON hpd_p.id = hp.permlink_id
ORDER BY h1.depth DESC
JOIN hive_post_data hpd ON hpd.id = hp.id
) s
JOIN hive_accounts ha_pp ON ha_pp.id = pp.author_id
UNION ALL
JOIN hive_permlink_data hpd_pp ON hpd_pp.id = pp.permlink_id
SELECT tblChild.queried_parent, p.id FROM hive_posts p
JOIN hive_accounts ha_rp ON ha_rp.id = rp.author_id
JOIN tblChild ON p.Parent_Id = tblChild.Id
JOIN hive_permlink_data hpd_rp ON hpd_rp.id = rp.permlink_id
WHERE p.counter_deleted = 0
JOIN hive_post_data rpd ON rpd.id = rp.id
)
JOIN hive_category_data hcd ON hcd.id = hp.category_id
SELECT queried_parent, cast(count(1) AS int) AS children_count
JOIN hive_category_data rcd ON rcd.id = rp.category_id
FROM tblChild
LEFT JOIN hive_communities hc ON hp.community_id = hc.id
GROUP BY queried_parent
LEFT JOIN hive_roles hr ON hp.author_id = hr.account_id AND hp.community_id = hr.community_id
) data_source
WHERE hp.counter_deleted = 0;
WHERE uhp.id = data_source.queried_parent
"""
;
db.query_no_return(sql)
$BODY$;
"""
sql = """
db.query_no_return(sql)
DROP FUNCTION IF EXISTS public.update_hive_posts_root_id(INTEGER, INTEGER);
sql = """
CREATE OR REPLACE FUNCTION public.update_hive_posts_root_id(in _first_block_num INTEGER, _last_block_num INTEGER)
DROP VIEW IF EXISTS hive_votes_view
RETURNS void
;
LANGUAGE 'plpgsql'
CREATE OR REPLACE VIEW hive_votes_view
VOLATILE
AS
AS $BODY$
SELECT
BEGIN
hv.voter_id as voter_id,
ha_a.name as author,
--- _first_block_num can be null together with _last_block_num
hpd.permlink as permlink,
UPDATE hive_posts uhp
vote_percent as percent,
SET root_id = id
ha_a.reputation as reputation,
WHERE uhp.root_id = 0 AND (_first_block_num IS NULL OR (uhp.block_num >= _first_block_num AND uhp.block_num <= _last_block_num))
rshares,
;
last_update,
END
ha_v.name as voter,
$BODY$;
weight,
"""
num_changes,
db.query_no_return(sql)
hv.permlink_id as permlink_id,
post_id,
sql = """
is_effective
DROP FUNCTION IF EXISTS public.update_hive_posts_children_count(INTEGER, INTEGER);
FROM
hive_votes hv
CREATE OR REPLACE FUNCTION public.update_hive_posts_children_count(in _first_block INTEGER, in _last_block INTEGER)
INNER JOIN hive_accounts ha_v ON ha_v.id = hv.voter_id
RETURNS void
INNER JOIN hive_accounts ha_a ON ha_a.id = hv.author_id
LANGUAGE SQL
INNER JOIN hive_permlink_data hpd ON hpd.id = hv.permlink_id
VOLATILE
;
AS $BODY$
"""
UPDATE hive_posts uhp
db.query_no_return(sql)
SET children = data_source.children_count
FROM
sql = """
(
DROP TYPE IF EXISTS database_api_vote CASCADE;
WITH recursive tblChild AS
(
CREATE TYPE database_api_vote AS (
SELECT s.queried_parent, s.id
voter VARCHAR(16),
FROM
author VARCHAR(16),
(SELECT h1.Parent_Id AS queried_parent, h1.id
permlink VARCHAR(255),
FROM hive_posts h1
weight NUMERIC,
WHERE h1.depth > 0 AND h1.counter_deleted = 0
rshares BIGINT,
AND h1.block_num BETWEEN _first_block AND _last_block
percent INT,
ORDER BY h1.depth DESC
last_update TIMESTAMP,
) s
num_changes INT,
UNION ALL
reputation FLOAT4
SELECT tblChild.queried_parent, p.id FROM hive_posts p
);
JOIN tblChild ON p.Parent_Id = tblChild.Id
WHERE p.counter_deleted = 0
DROP FUNCTION IF EXISTS get_account(character varying, boolean);
)
SELECT queried_parent, cast(count(1) AS int) AS children_count
CREATE OR REPLACE FUNCTION get_account(
FROM tblChild
in _account hive_accounts.name%TYPE,
GROUP BY queried_parent
in _check boolean)
) data_source
RETURNS INT
WHERE uhp.id = data_source.queried_parent
LANGUAGE 'plpgsql'
;
AS
$BODY$;
$function$
"""
DECLARE
db.query_no_return(sql)
account_id INT;
BEGIN
sql = """
SELECT INTO account_id COALESCE( ( SELECT id FROM hive_accounts WHERE name=_account ), 0 );
DROP VIEW IF EXISTS hive_votes_view
IF _check AND account_id = 0 THEN
;
RAISE EXCEPTION 'Account % does not exist', _account;
CREATE OR REPLACE VIEW hive_votes_view
END IF;
AS
SELECT
RETURN account_id;
hv.id,
END
hv.voter_id as voter_id,
$function$
ha_a.name as author,
;
hpd.permlink as permlink,
vote_percent as percent,
DROP FUNCTION IF EXISTS list_votes_by_voter_comment( character varying, character varying, character varying, int );
ha_a.reputation as reputation,
rshares,
CREATE OR REPLACE FUNCTION public.list_votes_by_voter_comment
last_update,
(
ha_v.name as voter,
in _VOTER hive_accounts.name%TYPE,
weight,
in _AUTHOR hive_accounts.name%TYPE,
num_changes,
in _PERMLINK hive_permlink_data.permlink%TYPE,
hv.permlink_id as permlink_id,
in _LIMIT INT
post_id,
)
is_effective
RETURNS SETOF database_api_vote
FROM
LANGUAGE 'plpgsql'
hive_votes hv
AS
INNER JOIN hive_accounts ha_v ON ha_v.id = hv.voter_id
$function$
INNER JOIN hive_accounts ha_a ON ha_a.id = hv.author_id
DECLARE _VOTER_ID INT;
INNER JOIN hive_permlink_data hpd ON hpd.id = hv.permlink_id
DECLARE _POST_ID INT;
;
BEGIN
"""
db.query_no_return(sql)
_VOTER_ID = get_account( _VOTER, true );
_POST_ID = find_comment_id( _AUTHOR, _PERMLINK, True);
sql = """
DROP TYPE IF EXISTS database_api_vote CASCADE;
RETURN QUERY
(
CREATE TYPE database_api_vote AS (
SELECT
id BIGINT,
v.voter,
voter VARCHAR(16),
v.author,
author VARCHAR(16),
v.permlink,
permlink VARCHAR(255),
v.weight,
weight NUMERIC,
v.rshares,
rshares BIGINT,
v.percent,
percent INT,
v.last_update,
last_update TIMESTAMP,
v.num_changes,
num_changes INT,
v.reputation
reputation FLOAT4
FROM
);
hive_votes_view v
WHERE
DROP FUNCTION IF EXISTS find_votes( character varying, character varying )
( v.voter_id = _VOTER_ID and v.post_id >= _POST_ID )
;
OR
CREATE OR REPLACE FUNCTION public.find_votes
( v.voter_id > _VOTER_ID )
(
ORDER BY
in _AUTHOR hive_accounts.name%TYPE,
voter_id,
in _PERMLINK hive_permlink_data.permlink%TYPE
post_id
)
LIMIT _LIMIT
RETURNS SETOF database_api_vote
);
LANGUAGE 'plpgsql'
AS
END
$function$
$function$;
DECLARE _POST_ID INT;
BEGIN
DROP FUNCTION IF EXISTS list_votes_by_comment_voter( character varying, character varying, character varying, int );
_POST_ID = find_comment_id( _AUTHOR, _PERMLINK, True);
CREATE OR REPLACE FUNCTION public.list_votes_by_comment_voter
RETURN QUERY
(
(
in _VOTER hive_accounts.name%TYPE,
SELECT
in _AUTHOR hive_accounts.name%TYPE,
v.id,
in _PERMLINK hive_permlink_data.permlink%TYPE,
v.voter,
in _LIMIT INT
v.author,
)
v.permlink,
RETURNS SETOF database_api_vote
v.weight,
LANGUAGE 'plpgsql'
v.rshares,
AS
v.percent,
$function$
v.last_update,
DECLARE _VOTER_ID INT;
v.num_changes,
DECLARE _POST_ID INT;
v.reputation
BEGIN
FROM
hive_votes_view v
_VOTER_ID = get_account( _VOTER, true );
WHERE
_POST_ID = find_comment_id( _AUTHOR, _PERMLINK, True);
v.post_id = _POST_ID
ORDER BY
RETURN QUERY
voter_id
(
);
SELECT
v.voter,
END
v.author,
$function$;
v.permlink,
v.weight,
DROP FUNCTION IF EXISTS list_votes_by_voter_comment( character varying, character varying, character varying, int )
v.rshares,
;
v.percent,
CREATE OR REPLACE FUNCTION public.list_votes_by_voter_comment
v.last_update,
(
v.num_changes,
in _VOTER hive_accounts.name%TYPE,
v.reputation
in _AUTHOR hive_accounts.name%TYPE,
FROM
in _PERMLINK hive_permlink_data.permlink%TYPE,
hive_votes_view v
in _LIMIT INT
WHERE
)
( v.post_id = _POST_ID and v.voter_id >= _VOTER_ID )
RETURNS SETOF database_api_vote
OR
LANGUAGE 'plpgsql'
( v.post_id > _POST_ID )
AS
ORDER BY
$function$
post_id,
DECLARE _VOTER_ID INT;
voter_id
DECLARE _POST_ID INT;
LIMIT _LIMIT
BEGIN
);
_VOTER_ID = find_account_id( _VOTER, _VOTER != '' );
END
_POST_ID = find_comment_id( _AUTHOR, _PERMLINK, _AUTHOR != '' OR _PERMLINK != '' );
$function$;
"""
RETURN QUERY
db.query_no_return(sql)
(
SELECT
sql = """
v.id,
DROP FUNCTION IF EXISTS find_comment_id(character varying, character varying, boolean)
v.voter,
;
v.author,
CREATE OR REPLACE FUNCTION find_comment_id(
v.permlink,
in _author hive_accounts.name%TYPE,
v.weight,
in _permlink hive_permlink_data.permlink%TYPE,
v.rshares,
in _check boolean)
v.percent,
RETURNS INT
v.last_update,
LANGUAGE 'plpgsql'
v.num_changes,
AS
v.reputation
$function$
FROM
DECLARE
hive_votes_view v
post_id INT;
WHERE
BEGIN
( v.voter_id = _VOTER_ID and v.post_id >= _POST_ID )
SELECT INTO post_id COALESCE( (SELECT hp.id
OR
FROM hive_posts hp
( v.voter_id > _VOTER_ID )
JOIN hive_accounts ha ON ha.id = hp.author_id
ORDER BY
JOIN hive_permlink_data hpd ON hpd.id = hp.permlink_id
voter_id,
WHERE ha.name = _author AND hpd.permlink = _permlink AND hp.counter_deleted = 0
post_id
), 0 );
LIMIT _LIMIT
IF _check AND (_author <> '' OR _permlink <> '') AND post_id = 0 THEN
);
RAISE EXCEPTION 'Post %/% does not exist', _author, _permlink;
END IF;
END
RETURN post_id;
$function$;
END
$function$
DROP FUNCTION IF EXISTS list_votes_by_comment_voter( character varying, character varying, character varying, int )
;
;
"""
CREATE OR REPLACE FUNCTION public.list_votes_by_comment_voter
db.query_no_return(sql)
(
in _VOTER hive_accounts.name%TYPE,
sql = """
in _AUTHOR hive_accounts.name%TYPE,
DROP TYPE IF EXISTS database_api_post CASCADE;
in _PERMLINK hive_permlink_data.permlink%TYPE,
CREATE TYPE database_api_post AS (
in _LIMIT INT
id INT,
)
community_id INT,
RETURNS SETOF database_api_vote
author VARCHAR(16),
LANGUAGE 'plpgsql'
permlink VARCHAR(255),
AS
title VARCHAR(512),
$function$
body TEXT,
DECLARE _VOTER_ID INT;
category VARCHAR(255),
DECLARE _POST_ID INT;
depth SMALLINT,
BEGIN
promoted DECIMAL(10,3),
payout DECIMAL(10,3),
_VOTER_ID = find_account_id( _VOTER, _VOTER != '' );
last_payout_at TIMESTAMP,
_POST_ID = find_comment_id( _AUTHOR, _PERMLINK, _AUTHOR != '' OR _PERMLINK != '' );
cashout_time TIMESTAMP,
is_paidout BOOLEAN,
RETURN QUERY
children INT,
(
votes INT,
SELECT
created_at TIMESTAMP,
v.id,
updated_at TIMESTAMP,
v.voter,
rshares NUMERIC,
v.author,
json TEXT,
v.permlink,
is_hidden BOOLEAN,
v.weight,
is_grayed BOOLEAN,
v.rshares,
total_votes BIGINT,
v.percent,
net_votes BIGINT,
v.last_update,
total_vote_weight NUMERIC,
v.num_changes,
parent_author VARCHAR(16),
v.reputation
parent_permlink_or_category VARCHAR(255),
FROM
curator_payout_value VARCHAR(30),
hive_votes_view v
root_author VARCHAR(16),
WHERE
root_permlink VARCHAR(255),
( v.post_id = _POST_ID and v.voter_id >= _VOTER_ID )
max_accepted_payout VARCHAR(30),
OR
percent_hbd INT,
( v.post_id > _POST_ID )
allow_replies BOOLEAN,
ORDER BY
allow_votes BOOLEAN,
post_id,
allow_curation_rewards BOOLEAN,
voter_id
beneficiaries JSON,
LIMIT _LIMIT
url TEXT,
);
root_title VARCHAR(512),
abs_rshares NUMERIC,
END
active TIMESTAMP,
$function$;
author_rewards BIGINT
"""
)
db.query_no_return(sql)
;
sql = """
DROP FUNCTION IF EXISTS list_comments_by_cashout_time(timestamp, character varying, character varying, int)
DROP TYPE IF EXISTS database_api_post CASCADE;
;
CREATE TYPE database_api_post AS (
CREATE OR REPLACE FUNCTION list_comments_by_cashout_time(
id INT,
in _cashout_time timestamp,
community_id INT,
in _author hive_accounts.name%TYPE,
author VARCHAR(16),
in _permlink hive_permlink_data.permlink%TYPE,
permlink VARCHAR(255),
in _limit INT)
title VARCHAR(512),
RETURNS SETOF database_api_post
body TEXT,
AS
category VARCHAR(255),
$function$
depth SMALLINT,
DECLARE
promoted DECIMAL(10,3),
__post_id INT;
payout DECIMAL(10,3),
BEGIN
last_payout_at TIMESTAMP,
__post_id = find_comment_id(_author,_permlink, False);
cashout_time TIMESTAMP,
RETURN QUERY
is_paidout BOOLEAN,
SELECT
children INT,
hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body,
votes INT,
hp.category, hp.depth, hp.promoted, hp.payout, hp.last_payout_at, hp.cashout_time, hp.is_paidout,
created_at TIMESTAMP,
hp.children, hp.votes, hp.created_at, hp.updated_at, hp.rshares, hp.json,
updated_at TIMESTAMP,
hp.is_hidden, hp.is_grayed, hp.total_votes, hp.net_votes, hp.total_vote_weight,
rshares NUMERIC,
hp.parent_author, hp.parent_permlink_or_category, hp.curator_payout_value, hp.root_author, hp.root_permlink,
json TEXT,
hp.max_accepted_payout, hp.percent_hbd, hp.allow_replies, hp.allow_votes,
is_hidden BOOLEAN,
hp.allow_curation_rewards, hp.beneficiaries, hp.url, hp.root_title, hp.abs_rshares,
is_grayed BOOLEAN,
hp.active, hp.author_rewards
total_votes BIGINT,
FROM
net_votes BIGINT,
hive_posts_view hp
total_vote_weight NUMERIC,
WHERE
parent_author VARCHAR(16),
NOT hp.is_muted AND
parent_permlink_or_category VARCHAR(255),
hp.cashout_time > _cashout_time OR
curator_payout_value VARCHAR(30),
hp.cashout_time = _cashout_time AND hp.id >= __post_id
root_author VARCHAR(16),
ORDER BY
root_permlink VARCHAR(255),
hp.cashout_time ASC,
max_accepted_payout VARCHAR(30),
hp.id ASC
percent_hbd INT,
LIMIT
allow_replies BOOLEAN,
_limit
allow_votes BOOLEAN,
;
allow_curation_rewards BOOLEAN,
END
beneficiaries JSON,
$function$
url TEXT,
LANGUAGE plpgsql
root_title VARCHAR(512),
;
abs_rshares NUMERIC,
active TIMESTAMP,
DROP FUNCTION IF EXISTS list_comments_by_permlink(character varying, character varying, int)
author_rewards BIGINT
;
)
CREATE OR REPLACE FUNCTION list_comments_by_permlink(
;
in _author hive_accounts.name%TYPE,
in _permlink hive_permlink_data.permlink%TYPE,
DROP FUNCTION IF EXISTS list_comments_by_cashout_time(timestamp, character varying, character varying, int)
in _limit INT)
;
RETURNS SETOF database_api_post
CREATE OR REPLACE FUNCTION list_comments_by_cashout_time(
AS
in _cashout_time timestamp,
$function$
in _author hive_accounts.name%TYPE,
BEGIN
in _permlink hive_permlink_data.permlink%TYPE,
RETURN QUERY
in _limit INT)
SELECT
RETURNS SETOF database_api_post
hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body,
AS
hp.category, hp.depth, hp.promoted, hp.payout, hp.last_payout_at, hp.cashout_time, hp.is_paidout,
$function$
hp.children, hp.votes, hp.created_at, hp.updated_at, hp.rshares, hp.json,
DECLARE
hp.is_hidden, hp.is_grayed, hp.total_votes, hp.net_votes, hp.total_vote_weight,
__post_id INT;
hp.parent_author, hp.parent_permlink_or_category, hp.curator_payout_value, hp.root_author, hp.root_permlink,
BEGIN
hp.max_accepted_payout, hp.percent_hbd, hp.allow_replies, hp.allow_votes,
__post_id = find_comment_id(_author,_permlink, False);
hp.allow_curation_rewards, hp.beneficiaries, hp.url, hp.root_title, hp.abs_rshares,
RETURN QUERY
hp.active, hp.author_rewards
SELECT
FROM
hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body,
hive_posts_view hp
hp.category, hp.depth, hp.promoted, hp.payout, hp.last_payout_at, hp.cashout_time, hp.is_paidout,
WHERE
hp.children, hp.votes, hp.created_at, hp.updated_at, hp.rshares, hp.json,
NOT hp.is_muted AND
hp.is_hidden, hp.is_grayed, hp.total_votes, hp.net_votes, hp.total_vote_weight,
hp.author > _author OR
hp.parent_author, hp.parent_permlink_or_category, hp.curator_payout_value, hp.root_author, hp.root_permlink,
hp.author = _author AND hp.permlink >= _permlink
hp.max_accepted_payout, hp.percent_hbd, hp.allow_replies, hp.allow_votes,
ORDER BY
hp.allow_curation_rewards, hp.beneficiaries, hp.url, hp.root_title, hp.abs_rshares,
hp.author ASC,
hp.active, hp.author_rewards
hp.permlink ASC
FROM
LIMIT
hive_posts_view hp
_limit
WHERE
;
NOT hp.is_muted AND
END
hp.cashout_time > _cashout_time OR
$function$
hp.cashout_time = _cashout_time AND hp.id >= __post_id
LANGUAGE plpgsql
ORDER BY
;
hp.cashout_time ASC,
hp.id ASC
DROP FUNCTION IF EXISTS list_comments_by_root(character varying, character varying, character varying, character varying, int)
LIMIT
;
_limit
CREATE OR REPLACE FUNCTION list_comments_by_root(
;
in _root_author hive_accounts.name%TYPE,
END
in _root_permlink hive_permlink_data.permlink%TYPE,
$function$
in _start_post_author hive_accounts.name%TYPE,
LANGUAGE plpgsql
in _start_post_permlink hive_permlink_data.permlink%TYPE,
;
in _limit INT)
RETURNS SETOF database_api_post
DROP FUNCTION IF EXISTS list_comments_by_permlink(character varying, character varying, int)
AS
;
$function$
CREATE OR REPLACE FUNCTION list_comments_by_permlink(
DECLARE
in _author hive_accounts.name%TYPE,
__root_id INT;
in _permlink hive_permlink_data.permlink%TYPE,
__post_id INT;
in _limit INT)
BEGIN
RETURNS SETOF database_api_post
__root_id = find_comment_id(_root_author, _root_permlink, True);
AS
__post_id = find_comment_id(_start_post_author, _start_post_permlink, True);
$function$
RETURN QUERY
BEGIN
SELECT
RETURN QUERY
hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body,
SELECT
hp.category, hp.depth, hp.promoted, hp.payout, hp.last_payout_at, hp.cashout_time, hp.is_paidout,
hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body,
hp.children, hp.votes, hp.created_at, hp.updated_at, hp.rshares, hp.json,
hp.category, hp.depth, hp.promoted, hp.payout, hp.last_payout_at, hp.cashout_time, hp.is_paidout,
hp.is_hidden, hp.is_grayed, hp.total_votes, hp.net_votes, hp.total_vote_weight,
hp.children, hp.votes, hp.created_at, hp.updated_at, hp.rshares, hp.json,
hp.parent_author, hp.parent_permlink_or_category, hp.curator_payout_value, hp.root_author, hp.root_permlink,
hp.is_hidden, hp.is_grayed, hp.total_votes, hp.net_votes, hp.total_vote_weight,
hp.max_accepted_payout, hp.percent_hbd, hp.allow_replies, hp.allow_votes,
hp.parent_author, hp.parent_permlink_or_category, hp.curator_payout_value, hp.root_author, hp.root_permlink,
hp.allow_curation_rewards, hp.beneficiaries, hp.url, hp.root_title, hp.abs_rshares,
hp.max_accepted_payout, hp.percent_hbd, hp.allow_replies, hp.allow_votes,
hp.active, hp.author_rewards
hp.allow_curation_rewards, hp.beneficiaries, hp.url, hp.root_title, hp.abs_rshares,
FROM hive_posts_view hp
hp.active, hp.author_rewards
INNER JOIN
FROM
(
hive_posts_view hp
SELECT hp2.id, hp2.root_id FROM hive_posts hp2
WHERE
WHERE NOT hp2.is_muted
NOT hp.is_muted AND
AND hp2.root_id > __root_id
hp.author > _author OR
OR hp2.root_id = __root_id AND hp2.id >= __post_id AND hp2.id > 0
hp.author = _author AND hp.permlink >= _permlink
ORDER BY
ORDER BY
hp2.root_id ASC
hp.author ASC,
,hp2.id ASC
hp.permlink ASC
LIMIT _limit
LIMIT
) ds on hp.id = ds.id
_limit
;
;
END
END
$function$
$function$
LANGUAGE plpgsql
LANGUAGE plpgsql
;
;
DROP FUNCTION IF EXISTS list_comments_by_parent(character varying, character varying, character varying, character varying, int)
DROP FUNCTION IF EXISTS list_comments_by_root(character varying, character varying, character varying, character varying, int)
;
;
CREATE OR REPLACE FUNCTION list_comments_by_parent(
CREATE OR REPLACE FUNCTION list_comments_by_root(
in _parent_author hive_accounts.name%TYPE,
in _root_author hive_accounts.name%TYPE,
in _parent_permlink hive_permlink_data.permlink%TYPE,
in _root_permlink hive_permlink_data.permlink%TYPE,
in _start_post_author hive_accounts.name%TYPE,
in _start_post_author hive_accounts.name%TYPE,
in _start_post_permlink hive_permlink_data.permlink%TYPE,
in _start_post_permlink hive_permlink_data.permlink%TYPE,
in _limit INT)
in _limit INT)
RETURNS SETOF database_api_post
RETURNS SETOF database_api_post
LANGUAGE sql
AS
COST 100
$function$
STABLE
DECLARE
ROWS 1000
__root_id INT;
AS $function$
__post_id INT;
SELECT
BEGIN
hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body,
__root_id = find_comment_id(_root_author, _root_permlink, True);
hp.category, hp.depth, hp.promoted, hp.payout, hp.last_payout_at, hp.cashout_time, hp.is_paidout,
__post_id = find_comment_id(_start_post_author, _start_post_permlink, True);
hp.children, hp.votes, hp.created_at, hp.updated_at, hp.rshares, hp.json,
RETURN QUERY
hp.is_hidden, hp.is_grayed, hp.total_votes, hp.net_votes, hp.total_vote_weight,
SELECT
hp.parent_author, hp.parent_permlink_or_category, hp.curator_payout_value, hp.root_author, hp.root_permlink,
hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body,
hp.max_accepted_payout, hp.percent_hbd, hp.allow_replies, hp.allow_votes,
hp.category, hp.depth, hp.promoted, hp.payout, hp.last_payout_at, hp.cashout_time, hp.is_paidout,
hp.allow_curation_rewards, hp.beneficiaries, hp.url, hp.root_title, hp.abs_rshares,
hp.children, hp.votes, hp.created_at, hp.updated_at, hp.rshares, hp.json,
hp.active, hp.author_rewards
hp.is_hidden, hp.is_grayed, hp.total_votes, hp.net_votes, hp.total_vote_weight,
FROM
hp.parent_author, hp.parent_permlink_or_category, hp.curator_payout_value, hp.root_author, hp.root_permlink,
hive_posts_view hp
hp.max_accepted_payout, hp.percent_hbd, hp.allow_replies, hp.allow_votes,
INNER JOIN
hp.allow_curation_rewards, hp.beneficiaries, hp.url, hp.root_title, hp.abs_rshares,
(
hp.active, hp.author_rewards
SELECT h.id FROM
FROM hive_posts_view hp
hive_posts_api_helper h
INNER JOIN
WHERE
(
h.parent_author > _parent_author OR
SELECT hp2.id, hp2.root_id FROM hive_posts hp2
h.parent_author = _parent_author AND ( h.parent_permlink_or_category > _parent_permlink OR
WHERE NOT hp2.is_muted
h.parent_permlink_or_category = _parent_permlink AND h.id >= find_comment_id(_start_post_author, _start_post_permlink, True) )
AND hp2.root_id > __root_id
ORDER BY
OR hp2.root_id = __root_id AND hp2.id >= __post_id AND hp2.id > 0
h.parent_author ASC,
ORDER BY
h.parent_permlink_or_category ASC,
hp2.root_id ASC
h.id ASC
,hp2.id ASC
LIMIT
LIMIT _limit
_limit
) ds on hp.id = ds.id
) ds ON ds.id = hp.id
;
WHERE
END
NOT hp.is_muted
$function$
;
LANGUAGE plpgsql
$function$
;
;
DROP FUNCTION IF EXISTS list_comments_by_parent(character varying, character varying, character varying, character varying, int)
DROP FUNCTION IF EXISTS list_comments_by_last_update(character varying, timestamp, character varying, character varying, int)
;
;
CREATE OR REPLACE FUNCTION list_comments_by_parent(
CREATE OR REPLACE FUNCTION list_comments_by_last_update(
in _parent_author hive_accounts.name%TYPE,
in _parent_author hive_accounts.name%TYPE,
in _parent_permlink hive_permlink_data.permlink%TYPE,
in _updated_at hive_posts.updated_at%TYPE,
in _start_post_author hive_accounts.name%TYPE,
in _start_post_author hive_accounts.name%TYPE,
in _start_post_permlink hive_permlink_data.permlink%TYPE,
in _start_post_permlink hive_permlink_data.permlink%TYPE,
in _limit INT)
in _limit INT)
RETURNS SETOF database_api_post
RETURNS SETOF database_api_post
LANGUAGE sql
AS
COST 100
$function$
STABLE
DECLARE
ROWS 1000
__post_id INT;
AS $function$
BEGIN
SELECT
__post_id = find_comment_id(_start_post_author, _start_post_permlink, True);
hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body,
RETURN QUERY
hp.category, hp.depth, hp.promoted, hp.payout, hp.last_payout_at, hp.cashout_time, hp.is_paidout,
SELECT
hp.children, hp.votes, hp.created_at, hp.updated_at, hp.rshares, hp.json,
hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body,
hp.is_hidden, hp.is_grayed, hp.total_votes, hp.net_votes, hp.total_vote_weight,
hp.category, hp.depth, hp.promoted, hp.payout, hp.last_payout_at, hp.cashout_time, hp.is_paidout,
hp.parent_author, hp.parent_permlink_or_category, hp.curator_payout_value, hp.root_author, hp.root_permlink,
hp.children, hp.votes, hp.created_at, hp.updated_at, hp.rshares, hp.json,
hp.max_accepted_payout, hp.percent_hbd, hp.allow_replies, hp.allow_votes,
hp.is_hidden, hp.is_grayed, hp.total_votes, hp.net_votes, hp.total_vote_weight,
hp.allow_curation_rewards, hp.beneficiaries, hp.url, hp.root_title, hp.abs_rshares,
hp.parent_author, hp.parent_permlink_or_category, hp.curator_payout_value, hp.root_author, hp.root_permlink,
hp.active, hp.author_rewards
hp.max_accepted_payout, hp.percent_hbd, hp.allow_replies, hp.allow_votes,
FROM
hp.allow_curation_rewards, hp.beneficiaries, hp.url, hp.root_title, hp.abs_rshares,
hive_posts_view hp
hp.active, hp.author_rewards
INNER JOIN
FROM
(
hive_posts_view hp
SELECT h.id FROM
WHERE
hive_posts_api_helper h
NOT hp.is_muted AND
WHERE
hp.parent_author > _parent_author OR
h.parent_author > _parent_author OR
hp.parent_author = _parent_author AND ( hp.updated_at < _updated_at OR
h.parent_author = _parent_author AND ( h.parent_permlink_or_category > _parent_permlink OR
hp.updated_at = _updated_at AND hp.id >= __post_id )
h.parent_permlink_or_category = _parent_permlink AND h.id >= find_comment_id(_start_post_author, _start_post_permlink, True) )
ORDER BY
ORDER BY
hp.parent_author ASC,
h.parent_author ASC,
hp.updated_at DESC,
h.parent_permlink_or_category ASC,
hp.id ASC
h.id ASC
LIMIT
LIMIT
_limit
_limit
;
) ds ON ds.id = hp.id
END
WHERE
$function$
NOT hp.is_muted
LANGUAGE plpgsql
;
;
$function$
;
DROP FUNCTION IF EXISTS list_comments_by_author_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)
CREATE OR REPLACE FUNCTION list_comments_by_author_last_update(
;
in _author hive_accounts.name%TYPE,
CREATE OR REPLACE FUNCTION list_comments_by_last_update(
in _updated_at hive_posts.updated_at%TYPE,
in _parent_author hive_accounts.name%TYPE,
in _start_post_author hive_accounts.name%TYPE,
in _updated_at hive_posts.updated_at%TYPE,
in _start_post_permlink hive_permlink_data.permlink%TYPE,
in _start_post_author hive_accounts.name%TYPE,
in _limit INT)
in _start_post_permlink hive_permlink_data.permlink%TYPE,
RETURNS SETOF database_api_post
in _limit INT)
AS
RETURNS SETOF database_api_post
$function$
AS
DECLARE
$function$
__post_id INT;
DECLARE
BEGIN
__post_id INT;
__post_id = find_comment_id(_start_post_author, _start_post_permlink, True);
BEGIN
RETURN QUERY
__post_id = find_comment_id(_start_post_author, _start_post_permlink, True);
SELECT
RETURN QUERY
hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body,
SELECT
hp.category, hp.depth, hp.promoted, hp.payout, hp.last_payout_at, hp.cashout_time, hp.is_paidout,
hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body,
hp.children, hp.votes, hp.created_at, hp.updated_at, hp.rshares, hp.json,
hp.category, hp.depth, hp.promoted, hp.payout, hp.last_payout_at, hp.cashout_time, hp.is_paidout,
hp.is_hidden, hp.is_grayed, hp.total_votes, hp.net_votes, hp.total_vote_weight,
hp.children, hp.votes, hp.created_at, hp.updated_at, hp.rshares, hp.json,
hp.parent_author, hp.parent_permlink_or_category, hp.curator_payout_value, hp.root_author, hp.root_permlink,
hp.is_hidden, hp.is_grayed, hp.total_votes, hp.net_votes, hp.total_vote_weight,
hp.max_accepted_payout, hp.percent_hbd, hp.allow_replies, hp.allow_votes,
hp.parent_author, hp.parent_permlink_or_category, hp.curator_payout_value, hp.root_author, hp.root_permlink,
hp.allow_curation_rewards, hp.beneficiaries, hp.url, hp.root_title, hp.abs_rshares,
hp.max_accepted_payout, hp.percent_hbd, hp.allow_replies, hp.allow_votes,
hp.active, hp.author_rewards
hp.allow_curation_rewards, hp.beneficiaries, hp.url, hp.root_title, hp.abs_rshares,
FROM
hp.active, hp.author_rewards
hive_posts_view hp
FROM
WHERE
hive_posts_view hp
NOT hp.is_muted AND
WHERE
-- fat node used wrong index (by_last_update) so the results are vastly different
NOT hp.is_muted AND
hp.author > _author OR
hp.parent_author > _parent_author OR
hp.author = _author AND ( hp.updated_at < _updated_at OR
hp.parent_author = _parent_author AND ( hp.updated_at < _updated_at OR
hp.updated_at = _updated_at AND hp.id >= __post_id )
hp.updated_at = _updated_at AND hp.id >= __post_id )
ORDER BY
ORDER BY
hp.author ASC,
hp.parent_author ASC,
hp.updated_at DESC,
hp.updated_at DESC,
hp.id ASC
hp.id ASC
LIMIT
LIMIT
_limit
_limit
;
;
END
END
$function$
$function$
LANGUAGE plpgsql
LANGUAGE plpgsql
;
;
"""
DROP FUNCTION IF EXISTS list_comments_by_author_last_update(character varying, timestamp, character varying, character varying, int)
db.query_no_return(sql)
;
CREATE OR REPLACE FUNCTION list_comments_by_author_last_update(
sql = """
in _author hive_accounts.name%TYPE,
DROP FUNCTION IF EXISTS score_for_account(in _account_id hive_accounts.id%TYPE)
in _updated_at hive_posts.updated_at%TYPE,
;
in _start_post_author hive_accounts.name%TYPE,
CREATE OR REPLACE FUNCTION score_for_account(in _account_id hive_accounts.id%TYPE)
in _start_post_permlink hive_permlink_data.permlink%TYPE,
RETURNS SMALLINT
in _limit INT)
AS
RETURNS SETOF database_api_post
$function$
AS
DECLARE
$function$
score SMALLINT;
DECLARE
BEGIN
__post_id INT;
SELECT INTO score
BEGIN
CASE
__post_id = find_comment_id(_start_post_author, _start_post_permlink, True);
WHEN rank.position < 200 THEN 70
RETURN QUERY
WHEN rank.position < 1000 THEN 60
SELECT
WHEN rank.position < 6500 THEN 50
hp.id, hp.community_id, hp.author, hp.permlink, hp.title, hp.body,
WHEN rank.position < 25000 THEN 40
hp.category, hp.depth, hp.promoted, hp.payout, hp.last_payout_at, hp.cashout_time, hp.is_paidout,
WHEN rank.position < 100000 THEN 30
hp.children, hp.votes, hp.created_at, hp.updated_at, hp.rshares, hp.json,
ELSE 20
hp.is_hidden, hp.is_grayed, hp.total_votes, hp.net_votes, hp.total_vote_weight,
END as score
hp.parent_author, hp.parent_permlink_or_category, hp.curator_payout_value, hp.root_author, hp.root_permlink,
FROM (
hp.max_accepted_payout, hp.percent_hbd, hp.allow_replies, hp.allow_votes,
SELECT
hp.allow_curation_rewards, hp.beneficiaries, hp.url, hp.root_title, hp.abs_rshares,
(
hp.active, hp.author_rewards
SELECT COUNT(*)
FROM
FROM hive_accounts ha_for_rank2
hive_posts_view hp
WHERE ha_for_rank2.reputation > ha_for_rank.reputation
WHERE
) as position
NOT hp.is_muted AND
FROM hive_accounts ha_for_rank WHERE ha_for_rank.id = _account_id
-- fat node used wrong index (by_last_update) so the results are vastly different
) as rank;
hp.author > _author OR
return score;
hp.author = _author AND ( hp.updated_at < _updated_at OR
END
hp.updated_at = _updated_at AND hp.id >= __post_id )
$function$
ORDER BY
LANGUAGE plpgsql
hp.author ASC,
;
hp.updated_at DESC,
"""
hp.id ASC
db.query_no_return(sql)
LIMIT
_limit
# hot and tranding functions
;
END
sql = """
$function$
DROP FUNCTION IF EXISTS date_diff() CASCADE
LANGUAGE plpgsql
;
;
CREATE OR REPLACE FUNCTION date_diff (units VARCHAR(30), start_t TIMESTAMP, end_t TIMESTAMP)
"""
RETURNS INT AS $$
DECLARE
db.query_no_return(sql)
diff_interval INTERVAL;
diff INT = 0;
sql = """
years_diff INT = 0;
DROP FUNCTION IF EXISTS score_for_account(in _account_id hive_accounts.id%TYPE)
BEGIN
;
IF units IN ('yy', 'yyyy', 'year', 'mm', 'm', 'month') THEN
CREATE OR REPLACE FUNCTION score_for_account(in _account_id hive_accounts.id%TYPE)
years_diff = DATE_PART('year', end_t) - DATE_PART('year', start_t);
RETURNS SMALLINT
IF units IN ('yy', 'yyyy', 'year') THEN
AS
-- SQL Server does not count full years passed (only difference between year parts)
$function$
RETURN years_diff;
DECLARE
ELSE
score SMALLINT;
-- If end month is less than start month it will subtracted
BEGIN
RETURN years_diff * 12 + (DATE_PART('month', end_t) - DATE_PART('month', start_t));
SELECT INTO score
END IF;
CASE
END IF;
WHEN rank.position < 200 THEN 70
-- Minus operator returns interval 'DDD days HH:MI:SS'
WHEN rank.position < 1000 THEN 60
diff_interval = end_t - start_t;
WHEN rank.position < 6500 THEN 50
diff = diff + DATE_PART('day', diff_interval);
WHEN rank.position < 25000 THEN 40
IF units IN ('wk', 'ww', 'week') THEN
WHEN rank.position < 100000 THEN 30
diff = diff/7;
ELSE 20
RETURN diff;
END as score
END IF;
FROM (
IF units IN ('dd', 'd', 'day') THEN
SELECT
RETURN diff;
(
END IF;
SELECT COUNT(*)
diff = diff * 24 + DATE_PART('hour', diff_interval);
FROM hive_accounts ha_for_rank2
IF units IN ('hh', 'hour') THEN
WHERE ha_for_rank2.reputation > ha_for_rank.reputation
RETURN diff;
) as position
END IF;
FROM hive_accounts ha_for_rank WHERE ha_for_rank.id = _account_id
diff = diff * 60 + DATE_PART('minute', diff_interval);
) as rank;
IF units IN ('mi', 'n', 'minute') THEN
return score;
RETURN diff;
END
END IF;
$function$
diff = diff * 60 + DATE_PART('second', diff_interval);
LANGUAGE plpgsql
RETURN diff;
;
END;
"""
$$ LANGUAGE plpgsql IMMUTABLE
db.query_no_return(sql)
"""
db.query_no_return(sql)
# hot and tranding functions
sql = """
sql = """
DROP FUNCTION IF EXISTS public.calculate_time_part_of_trending(_post_created_at hive_posts.created_at%TYPE ) CASCADE
DROP FUNCTION IF EXISTS date_diff() CASCADE
;
;
CREATE OR REPLACE FUNCTION public.calculate_time_part_of_trending(
CREATE OR REPLACE FUNCTION date_diff (units VARCHAR(30), start_t TIMESTAMP, end_t TIMESTAMP)
_post_created_at hive_posts.created_at%TYPE)
RETURNS INT AS $$
RETURNS double precision
DECLARE
LANGUAGE 'plpgsql'
diff_interval INTERVAL;
IMMUTABLE
diff INT = 0;
AS $BODY$
years_diff INT = 0;
DECLARE
BEGIN
result double precision;
IF units IN ('yy', 'yyyy', 'year', 'mm', 'm', 'month') THEN
sec_from_epoch INT = 0;
years_diff = DATE_PART('year', end_t) - DATE_PART('year', start_t);
BEGIN
IF units IN ('yy', 'yyyy', 'year') THEN
sec_from_epoch = date_diff( 'second', CAST('19700101' AS TIMESTAMP), _post_created_at );
-- SQL Server does not count full years passed (only difference between year parts)
result = sec_from_epoch/240000.0;
RETURN years_diff;
return result;
ELSE
END;
-- If end month is less than start month it will subtracted
$BODY$;
RETURN years_diff * 12 + (DATE_PART('month', end_t) - DATE_PART('month', start_t));
"""
END IF;
db.query_no_return(sql)
END IF;
-- Minus operator returns interval 'DDD days HH:MI:SS'
sql = """
diff_interval = end_t - start_t;
DROP FUNCTION IF EXISTS public.calculate_time_part_of_hot(_post_created_at hive_posts.created_at%TYPE ) CASCADE
diff = diff + DATE_PART('day', diff_interval);
;
IF units IN ('wk', 'ww', 'week') THEN
CREATE OR REPLACE FUNCTION public.calculate_time_part_of_hot(
diff = diff/7;
_post_created_at hive_posts.created_at%TYPE)
RETURN diff;
RETURNS double precision
END IF;
LANGUAGE 'plpgsql'
IF units IN ('dd', 'd', 'day') THEN
IMMUTABLE
RETURN diff;
AS $BODY$
END IF;
DECLARE
diff = diff * 24 + DATE_PART('hour', diff_interval);
result double precision;
IF units IN ('hh', 'hour') THEN
sec_from_epoch INT = 0;
RETURN diff;
BEGIN
END IF;
sec_from_epoch = date_diff( 'second', CAST('19700101' AS TIMESTAMP), _post_created_at );
diff = diff * 60 + DATE_PART('minute', diff_interval);
result = sec_from_epoch/10000.0;
IF units IN ('mi', 'n', 'minute') THEN
return result;
RETURN diff;
END;
END IF;
$BODY$;
diff = diff * 60 + DATE_PART('second', diff_interval);
"""
RETURN diff;
db.query_no_return(sql)
END;
$$ LANGUAGE plpgsql IMMUTABLE
sql = """
"""
DROP FUNCTION IF EXISTS public.calculate_rhsares_part_of_hot_and_trend(_rshares hive_votes.rshares%TYPE) CASCADE
db.query_no_return(sql)
;
CREATE OR REPLACE FUNCTION public.calculate_rhsares_part_of_hot_and_trend(_rshares hive_votes.rshares%TYPE)
sql = """
RETURNS double precision
DROP FUNCTION IF EXISTS public.calculate_time_part_of_trending(_post_created_at hive_posts.created_at%TYPE ) CASCADE
LANGUAGE 'plpgsql'
;
IMMUTABLE
CREATE OR REPLACE FUNCTION public.calculate_time_part_of_trending(
AS $BODY$
_post_created_at hive_posts.created_at%TYPE)
DECLARE
RETURNS double precision
mod_score double precision;
LANGUAGE 'plpgsql'
BEGIN
IMMUTABLE
mod_score := _rshares / 10000000.0;
AS $BODY$
IF ( mod_score > 0 )
DECLARE
THEN
result double precision;
return log( greatest( abs(mod_score), 1 ) );
sec_from_epoch INT = 0;
END IF;
BEGIN
return -1.0 * log( greatest( abs(mod_score), 1 ) );
sec_from_epoch = date_diff( 'second', CAST('19700101' AS TIMESTAMP), _post_created_at );
END;
result = sec_from_epoch/240000.0;
$BODY$;
return result;
"""
END;
db.query_no_return(sql)
$BODY$;
"""
sql = """
db.query_no_return(sql)
DROP FUNCTION IF EXISTS public.calculate_hot(hive_votes.rshares%TYPE, hive_posts.created_at%TYPE)
;
sql = """
CREATE OR REPLACE FUNCTION public.calculate_hot(
DROP FUNCTION IF EXISTS public.calculate_time_part_of_hot(_post_created_at hive_posts.created_at%TYPE ) CASCADE
_rshares hive_votes.rshares%TYPE,
;
_post_created_at hive_posts.created_at%TYPE)
CREATE OR REPLACE FUNCTION public.calculate_time_part_of_hot(
RETURNS hive_posts.sc_hot%TYPE
_post_created_at hive_posts.created_at%TYPE)
LANGUAGE 'plpgsql'
RETURNS double precision
IMMUTABLE
LANGUAGE 'plpgsql'
AS $BODY$
IMMUTABLE
BEGIN
AS $BODY$
return calculate_rhsares_part_of_hot_and_trend(_rshares) + calculate_time_part_of_hot( _post_created_at );
DECLARE
END;
result double precision;
$BODY$;
sec_from_epoch INT = 0;
"""
BEGIN
db.query_no_return(sql)
sec_from_epoch = date_diff( 'second', CAST('19700101' AS TIMESTAMP), _post_created_at );
result = sec_from_epoch/10000.0;
sql = """
return result;
DO $$
END;
BEGIN
$BODY$;
EXECUTE 'ALTER DATABASE '||current_database()||' SET join_collapse_limit TO 16';
"""
EXECUTE 'ALTER DATABASE '||current_database()||' SET from_collapse_limit TO 16';
db.query_no_return(sql)
END
$$;
sql = """
"""
DROP FUNCTION IF EXISTS public.calculate_rhsares_part_of_hot_and_trend(_rshares hive_votes.rshares%TYPE) CASCADE
db.query_no_return(sql)
;
CREATE OR REPLACE FUNCTION public.calculate_rhsares_part_of_hot_and_trend(_rshares hive_votes.rshares%TYPE)
sql = """
RETURNS double precision
DROP FUNCTION IF EXISTS public.calculate_tranding(hive_votes.rshares%TYPE, hive_posts.created_at%TYPE)
LANGUAGE 'plpgsql'
;
IMMUTABLE
CREATE OR REPLACE FUNCTION public.calculate_tranding(
AS $BODY$
_rshares hive_votes.rshares%TYPE,
DECLARE
_post_created_at hive_posts.created_at%TYPE)
mod_score double precision;
RETURNS hive_posts.sc_trend%TYPE
BEGIN
LANGUAGE 'plpgsql'
mod_score := _rshares / 10000000.0;
IMMUTABLE
IF ( mod_score > 0 )
AS $BODY$
THEN
BEGIN
return log( greatest( abs(mod_score), 1 ) );
return calculate_rhsares_part_of_hot_and_trend(_rshares) + calculate_time_part_of_trending( _post_created_at );
END IF;
END;
return -1.0 * log( greatest( abs(mod_score), 1 ) );
$BODY$;
END;
"""
$BODY$;
db.query_no_return(sql)
"""
db.query_no_return(sql)
sql = """
DROP FUNCTION IF EXISTS public.max_time_stamp() CASCADE;
sql = """
CREATE OR REPLACE FUNCTION public.max_time_stamp( _first TIMESTAMP, _second TIMESTAMP )
DROP FUNCTION IF EXISTS public.calculate_hot(hive_votes.rshares%TYPE, hive_posts.created_at%TYPE)
RETURNS TIMESTAMP
;
LANGUAGE 'plpgsql'
CREATE OR REPLACE FUNCTION public.calculate_hot(
IMMUTABLE
_rshares hive_votes.rshares%TYPE,
AS $BODY$
_post_created_at hive_posts.created_at%TYPE)
BEGIN
RETURNS hive_posts.sc_hot%TYPE
IF _first > _second THEN
LANGUAGE 'plpgsql'
RETURN _first;
IMMUTABLE
ELSE
AS $BODY$
RETURN _second;
BEGIN
END IF;
return calculate_rhsares_part_of_hot_and_trend(_rshares) + calculate_time_part_of_hot( _post_created_at );
END
END;
$BODY$;
$BODY$;
"""
"""
db.query_no_return(sql)
db.query_no_return(sql)
sql = """
sql = """
DROP FUNCTION IF EXISTS public.update_hive_posts_api_helper(INTEGER, INTEGER);
DO $$
BEGIN
CREATE OR REPLACE FUNCTION public.update_hive_posts_api_helper(in _first_block_num INTEGER, _last_block_num INTEGER)
EXECUTE 'ALTER DATABASE '||current_database()||' SET join_collapse_limit TO 16';
RETURNS void
EXECUTE 'ALTER DATABASE '||current_database()||' SET from_collapse_limit TO 16';
LANGUAGE 'plpgsql'
END
VOLATILE
$$;
AS $BODY$
"""
BEGIN
db.query_no_return(sql)
IF _first_block_num IS NULL OR _last_block_num IS NULL THEN
-- initial creation of table.
sql = """
DROP FUNCTION IF EXISTS public.calculate_tranding(hive_votes.rshares%TYPE, hive_posts.created_at%TYPE)
INSERT INTO hive_posts_api_helper
;
(id, author, parent_author, parent_permlink_or_category)
CREATE OR REPLACE FUNCTION public.calculate_tranding(
SELECT hp.id, hp.author, hp.parent_author, hp.parent_permlink_or_category
_rshares hive_votes.rshares%TYPE,
FROM hive_posts_view hp
_post_created_at hive_posts.created_at%TYPE)
;
RETURNS hive_posts.sc_trend%TYPE
ELSE
LANGUAGE 'plpgsql'
-- Regular incremental update.
IMMUTABLE
INSERT INTO hive_posts_api_helper
AS $BODY$
(id, author, parent_author, parent_permlink_or_category)
BEGIN
SELECT hp.id, hp.author, hp.parent_author, hp.parent_permlink_or_category
return calculate_rhsares_part_of_hot_and_trend(_rshares) + calculate_time_part_of_trending( _post_created_at );
FROM hive_posts_view hp
END;
WHERE hp.block_num BETWEEN _first_block_num AND _last_block_num AND
$BODY$;
NOT EXISTS (SELECT NULL FROM hive_posts_api_helper h WHERE h.id = hp.id)
"""
;
db.query_no_return(sql)
END IF;
sql = """
END
DROP FUNCTION IF EXISTS public.max_time_stamp() CASCADE;
$BODY$
CREATE OR REPLACE FUNCTION public.max_time_stamp( _first TIMESTAMP, _second TIMESTAMP )
"""
RETURNS TIMESTAMP
db.query_no_return(sql)
LANGUAGE 'plpgsql'
IMMUTABLE
sql = """
AS $BODY$
DROP FUNCTION IF EXISTS process_reputation_data(in _block_num hive_blocks.num%TYPE, in _author hive_accounts.name%TYPE,
BEGIN
in _permlink hive_permlink_data.permlink%TYPE, in _voter hive_accounts.name%TYPE, in _rshares hive_votes.rshares%TYPE)
IF _first > _second THEN
;
RETURN _first;
ELSE
CREATE OR REPLACE FUNCTION process_reputation_data(in _block_num hive_blocks.num%TYPE,
RETURN _second;
in _author hive_accounts.name%TYPE, in _permlink hive_permlink_data.permlink%TYPE,
END IF;
in _voter hive_accounts.name%TYPE, in _rshares hive_votes.rshares%TYPE)
END
RETURNS void
$BODY$;
LANGUAGE sql
"""
VOLATILE
db.query_no_return(sql)
AS $BODY$
WITH __insert_info AS (
sql = """
INSERT INTO hive_reputation_data
DROP FUNCTION IF EXISTS public.update_hive_posts_api_helper(INTEGER, INTEGER);
(author_id, voter_id, permlink, block_num, rshares)
--- Warning DISTINCT is needed here since we have to strict join to hv table and there is really made a CROSS JOIN
CREATE OR REPLACE FUNCTION public.update_hive_posts_api_helper(in _first_block_num INTEGER, _last_block_num INTEGER)
--- between ha and hv records (producing 2 duplicated records)
RETURNS void
SELECT DISTINCT ha.id as author_id, hv.id as voter_id, _permlink, _block_num, _rshares
LANGUAGE 'plpgsql'
FROM hive_accounts ha
VOLATILE
JOIN hive_accounts hv ON hv.name = _voter
AS $BODY$
JOIN hive_posts hp ON hp.author_id = ha.id
BEGIN
JOIN hive_permlink_data hpd ON hp.permlink_id = hpd.id
IF _first_block_num IS NULL OR _last_block_num IS NULL THEN
WHERE hpd.permlink = _permlink
-- initial creation of table.
AND ha.name = _author
INSERT INTO hive_posts_api_helper
AND NOT hp.is_paidout --- voting on paidout posts shall have no effect
(id, author, parent_author, parent_permlink_or_category)
AND hv.reputation >= 0 --- voter's negative reputation eliminates vote from processing
SELECT hp.id, hp.author, hp.parent_author, hp.parent_permlink_or_category
AND (_rshares >= 0
FROM hive_posts_view hp
OR (hv.reputation >= (ha.reputation - COALESCE((SELECT (hrd.rshares >> 6) -- if previous vote was a downvote we need to correct author reputation before current comparison to voter's reputation
;
FROM hive_reputation_data hrd
ELSE
WHERE hrd.author_id = ha.id
-- Regular incremental update.
AND hrd.voter_id=hv.id
INSERT INTO hive_posts_api_helper
AND hrd.permlink=_permlink
(id, author, parent_author, parent_permlink_or_category)
AND hrd.rshares < 0), 0)))
SELECT hp.id, hp.author, hp.parent_author, hp.parent_permlink_or_category
)
FROM hive_posts_view hp
ON CONFLICT ON CONSTRAINT hive_reputation_data_uk DO
WHERE hp.block_num BETWEEN _first_block_num AND _last_block_num AND
UPDATE SET
NOT EXISTS (SELECT NULL FROM hive_posts_api_helper h WHERE h.id = hp.id)
rshares = EXCLUDED.rshares
;
RETURNING (xmax = 0) AS is_new_vote,
END IF;
(SELECT hrd.rshares
FROM hive_reputation_data hrd
END
--- Warning we want OLD row here, not both, so we're using old ID to select old one (new record has different value) !!!
$BODY$
WHERE hrd.id = hive_reputation_data.id AND hrd.author_id = author_id and hrd.voter_id=voter_id and hrd.permlink=_permlink) AS old_rshares, author_id, voter_id
"""
)
db.query_no_return(sql)
UPDATE hive_accounts uha
SET reputation = CASE __insert_info.is_new_vote
sql = """
WHEN true THEN ha.reputation + (_rshares >> 6)
DROP FUNCTION IF EXISTS process_reputation_data(in _block_num hive_blocks.num%TYPE, in _author hive_accounts.name%TYPE,
ELSE ha.reputation - (__insert_info.old_rshares >> 6) + (_rshares >> 6)
in _permlink hive_permlink_data.permlink%TYPE, in _voter hive_accounts.name%TYPE, in _rshares hive_votes.rshares%TYPE)
END
;
FROM hive_accounts ha
JOIN __insert_info ON ha.id = __insert_info.author_id
CREATE OR REPLACE FUNCTION process_reputation_data(in _block_num hive_blocks.num%TYPE,
WHERE uha.id = __insert_info.author_id
in _author hive_accounts.name%TYPE, in _permlink hive_permlink_data.permlink%TYPE,
;
in _voter hive_accounts.name%TYPE, in _rshares hive_votes.rshares%TYPE)
$BODY$;
RETURNS void
"""
LANGUAGE sql
VOLATILE
db.query_no_return(sql)
AS $BODY$
sql = """
WITH __insert_info AS (
DROP FUNCTION IF EXISTS public.calculate_notify_vote_score(_payout hive_posts.payout%TYPE, _abs_rshares hive_posts_view.abs_rshares%TYPE, _rshares hive_votes.rshares%TYPE) CASCADE
INSERT INTO hive_reputation_data
;
(author_id, voter_id, permlink, block_num, rshares)
CREATE OR REPLACE FUNCTION public.calculate_notify_vote_score(_payout hive_posts.payout%TYPE, _abs_rshares hive_posts_view.abs_rshares%TYPE, _rshares hive_votes.rshares%TYPE)
--- Warning DISTINCT is needed here since we have to strict join to hv table and there is really made a CROSS JOIN
RETURNS INT
--- between ha and hv records (producing 2 duplicated records)
LANGUAGE 'sql'
SELECT DISTINCT ha.id as author_id, hv.id as voter_id, _permlink, _block_num, _rshares
IMMUTABLE
FROM hive_accounts ha
AS $BODY$
JOIN hive_accounts hv ON hv.name = _voter
SELECT CASE
JOIN hive_posts hp ON hp.author_id = ha.id
WHEN ((( _payout )/_abs_rshares) * 1000 * _rshares < 20 ) THEN -1
JOIN hive_permlink_data hpd ON hp.permlink_id = hpd.id
ELSE LEAST(100, (LENGTH(CAST( ( (( _payout )/_abs_rshares) * 1000 * _rshares ) as text)) - 1) * 25)
WHERE hpd.permlink = _permlink
END;
AND ha.name = _author
$BODY$;
"""
AND NOT hp.is_paidout --- voting on paidout posts shall have no effect
AND hv.reputation >= 0 --- voter's negative reputation eliminates vote from processing
db.query_no_return(sql)
AND (_rshares >= 0
OR (hv.reputation >= (ha.reputation - COALESCE((SELECT (hrd.rshares >> 6) -- if previous vote was a downvote we need to correct author reputation before current comparison to voter's reputation
sql = """
FROM hive_reputation_data hrd
DROP FUNCTION IF EXISTS notification_id(in _block_number INTEGER, in _notifyType INTEGER, in _id INTEGER)
WHERE hrd.author_id = ha.id
;
AND hrd.voter_id=hv.id
CREATE OR REPLACE FUNCTION notification_id(in _block_number INTEGER, in _notifyType INTEGER, in _id INTEGER)
AND hrd.permlink=_permlink
RETURNS BIGINT
AND hrd.rshares < 0), 0)))
AS
)
$function$
ON CONFLICT ON CONSTRAINT hive_reputation_data_uk DO
BEGIN
UPDATE SET
RETURN CAST( _block_number as BIGINT ) << 32
rshares = EXCLUDED.rshares
| ( _notifyType << 16 )
RETURNING (xmax = 0) AS is_new_vote,
| ( _id & CAST( x'00FF' as INTEGER) );
(SELECT hrd.rshares
END
FROM hive_reputation_data hrd
$function$
--- Warning we want OLD row here, not both, so we're using old ID to select old one (new record has different value) !!!
LANGUAGE plpgsql IMMUTABLE
WHERE hrd.id = hive_reputation_data.id AND hrd.author_id = author_id and hrd.voter_id=voter_id and hrd.permlink=_permlink) AS old_rshares, author_id, voter_id
;
)
"""
UPDATE hive_accounts uha
db.query_no_return(sql)
SET reputation = CASE __insert_info.is_new_vote
WHEN true THEN ha.reputation + (_rshares >> 6)
ELSE ha.reputation - (__insert_info.old_rshares >> 6) + (_rshares >> 6)
sql = """
END
DROP TYPE IF EXISTS bridge_api_post CASCADE;
FROM hive_accounts ha
CREATE TYPE bridge_api_post AS (
JOIN __insert_info ON ha.id = __insert_info.author_id
id INTEGER,
WHERE uha.id = __insert_info.author_id
author VARCHAR,
;
parent_author VARCHAR,
$BODY$;
author_rep FLOAT4,
"""
root_title VARCHAR,
beneficiaries JSON,
db.query_no_return(sql)
max_accepted_payout VARCHAR,
sql = """
percent_hbd INTEGER,
DROP FUNCTION IF EXISTS public.calculate_notify_vote_score(_payout hive_posts.payout%TYPE, _abs_rshares hive_posts_view.abs_rshares%TYPE, _rshares hive_votes.rshares%TYPE) CASCADE
url TEXT,
;
permlink VARCHAR,
CREATE OR REPLACE FUNCTION public.calculate_notify_vote_score(_payout hive_posts.payout%TYPE, _abs_rshares hive_posts_view.abs_rshares%TYPE, _rshares hive_votes.rshares%TYPE)
parent_permlink_or_category VARCHAR,
RETURNS INT
title VARCHAR,
LANGUAGE 'sql'
body TEXT,
IMMUTABLE
category VARCHAR,
AS $BODY$
depth SMALLINT,
SELECT CASE
promoted DECIMAL(10,3),
WHEN ((( _payout )/_abs_rshares) * 1000 * _rshares < 20 ) THEN -1
payout DECIMAL(10,3),
ELSE LEAST(100, (LENGTH(CAST( ( (( _payout )/_abs_rshares) * 1000 * _rshares ) as text)) - 1) * 25)
pending_payout DECIMAL(10,3),
END;
payout_at TIMESTAMP,
$BODY$;
is_paidout BOOLEAN,
"""
children INTEGER,
votes INTEGER,
db.query_no_return(sql)
created_at TIMESTAMP,
updated_at TIMESTAMP,
sql = """
rshares NUMERIC,
DROP FUNCTION IF EXISTS notification_id(in _block_number INTEGER, in _notifyType INTEGER, in _id INTEGER)
abs_rshares NUMERIC,
;
json TEXT,
CREATE OR REPLACE FUNCTION notification_id(in _block_number INTEGER, in _notifyType INTEGER, in _id INTEGER)
is_hidden BOOLEAN,
RETURNS BIGINT
is_grayed BOOLEAN,
AS
total_votes BIGINT,
$function$
sc_trend FLOAT4,
BEGIN
role_title VARCHAR,
RETURN CAST( _block_number as BIGINT ) << 32
community_title VARCHAR,
| ( _notifyType << 16 )
role_id SMALLINT,
| ( _id & CAST( x'00FF' as INTEGER) );
is_pinned BOOLEAN,
END
curator_payout_value VARCHAR
$function$
);
LANGUAGE plpgsql IMMUTABLE
"""
;
db.query_no_return(sql)
"""
db.query_no_return(sql)
sql = """
DROP FUNCTION IF EXISTS bridge_get_ranked_post_by_trends;
CREATE FUNCTION bridge_get_ranked_post_by_trends( in _limit SMALLINT )
sql = """
RETURNS SETOF bridge_api_post
DROP TYPE IF EXISTS bridge_api_post CASCADE;
AS
CREATE TYPE bridge_api_post AS (
$function$
id INTEGER,
SELECT
author VARCHAR,
hp.id,
parent_author VARCHAR,
hp.author,
author_rep FLOAT4,
hp.parent_author,
root_title VARCHAR,
hp.author_rep,
beneficiaries JSON,
hp.root_title,
max_accepted_payout VARCHAR,
hp.beneficiaries,
percent_hbd INTEGER,
hp.max_accepted_payout,
url TEXT,
hp.percent_hbd,
permlink VARCHAR,
hp.url,
parent_permlink_or_category VARCHAR,
hp.permlink,
title VARCHAR,
hp.parent_permlink_or_category,
body TEXT,
hp.title,
category VARCHAR,
hp.body,
depth SMALLINT,
hp.category,
promoted DECIMAL(10,3),
hp.depth,
payout DECIMAL(10,3),
hp.promoted,
pending_payout DECIMAL(10,3),
hp.payout,
payout_at TIMESTAMP,
hp.pending_payout,
is_paidout BOOLEAN,
hp.payout_at,
children INTEGER,
hp.is_paidout,
votes INTEGER,
hp.children,
created_at TIMESTAMP,
hp.votes,
updated_at TIMESTAMP,
hp.created_at,
rshares NUMERIC,
hp.updated_at,
abs_rshares NUMERIC,
hp.rshares,
json TEXT,
hp.abs_rshares,
is_hidden BOOLEAN,
hp.json,
is_grayed BOOLEAN,
hp.is_hidden,
total_votes BIGINT,
hp.is_grayed,
sc_trend FLOAT4,
hp.total_votes,
role_title VARCHAR,
hp.sc_trend,
community_title VARCHAR,
hp.role_title,
role_id SMALLINT,
hp.community_title,
is_pinned BOOLEAN,
hp.role_id,
curator_payout_value VARCHAR
hp.is_pinned,
);
hp.curator_payout_value
"""
FROM
db.query_no_return(sql)
(
SELECT
sql = """
hp1.id
DROP FUNCTION IF EXISTS bridge_get_ranked_post_by_trends;
, hp1.sc_trend as trend
CREATE FUNCTION bridge_get_ranked_post_by_trends( in _limit SMALLINT )
FROM hive_posts hp1 WHERE NOT hp1.is_paidout AND hp1.depth = 0 ORDER BY hp1.sc_trend DESC LIMIT _limit
RETURNS SETOF bridge_api_post
) as trends
AS
JOIN hive_posts_view hp ON hp.id = trends.id ORDER BY trends.trend DESC
$function$
$function$
SELECT
language sql
hp.id,
"""
hp.author,
db.query_no_return(sql)
hp.parent_author,
hp.author_rep,
def reset_autovac(db):
hp.root_title,
"""Initializes/resets per-table autovacuum/autoanalyze params.
hp.beneficiaries,
hp.max_accepted_payout,
We use a scale factor of 0 and specify exact threshold tuple counts,
hp.percent_hbd,
per-table, in the format (autovacuum_threshold, autoanalyze_threshold)."""
hp.url,
hp.permlink,
autovac_config = { # vacuum analyze
hp.parent_permlink_or_category,
'hive_accounts': (50000, 100000),
hp.title,
'hive_posts': (2500, 10000),
hp.body,
'hive_post_tags': (5000, 10000),
hp.category,
'hive_follows': (5000, 5000),
hp.depth,
'hive_feed_cache': (5000, 5000),
hp.promoted,
'hive_blocks': (5000, 25000),
hp.payout,
'hive_reblogs': (5000, 5000),
hp.pending_payout,
'hive_payments': (5000, 5000),
hp.payout_at,
}
hp.is_paidout,
hp.children,
for table, (n_vacuum, n_analyze) in autovac_config.items():
hp.votes,
sql = """ALTER TABLE %s SET (autovacuum_vacuum_scale_factor = 0,
hp.created_at,
autovacuum_vacuum_threshold = %s,
hp.updated_at,
autovacuum_analyze_scale_factor = 0,
hp.rshares,
autovacuum_analyze_threshold = %s)"""
hp.abs_rshares,
db.query(sql % (table, n_vacuum, n_analyze))
hp.json,
hp.is_hidden,
hp.is_grayed,
def set_fillfactor(db):
hp.total_votes,
"""Initializes/resets FILLFACTOR for tables which are intesively updated"""
hp.sc_trend,
hp.role_title,
fillfactor_config = {
hp.community_title,
'hive_posts': 70,
hp.role_id,
'hive_post_data': 70,
hp.is_pinned,
'hive_votes': 70,
hp.curator_payout_value
'hive_reputation_data': 50
FROM
}
(
SELECT
for table, fillfactor in fillfactor_config.items():
hp1.id
sql = """ALTER TABLE {} SET (FILLFACTOR = {})"""
, hp1.sc_trend as trend
db.query(sql.format(table, fillfactor))
FROM hive_posts hp1 WHERE NOT hp1.is_paidout AND hp1.depth = 0 ORDER BY hp1.sc_trend DESC LIMIT _limit
) as trends
def set_logged_table_attribute(db, logged):
JOIN hive_posts_view hp ON hp.id = trends.id ORDER BY trends.trend DESC
"""Initializes/resets LOGGED/UNLOGGED attribute for tables which are intesively updated"""
$function$
language sql
logged_config = [
"""
'hive_accounts',
db.query_no_return(sql)
'hive_permlink_data',
'hive_post_tags',
def reset_autovac(db):
'hive_posts',
"""Initializes/resets per-table autovacuum/autoanalyze params.
'hive_post_data',
'hive_votes',
We use a scale factor of 0 and specify exact threshold tuple counts,
'hive_reputation_data'
per-table, in the format (autovacuum_threshold, autoanalyze_threshold)."""
]
autovac_config = { # vacuum analyze
for table in logged_config:
'hive_accounts': (50000, 100000),
log.info("Setting {} attribute on a table: {}".format('LOGGED' if logged else 'UNLOGGED', table))
'hive_posts': (2500, 10000),
sql = """ALTER TABLE {} SET {}"""
'hive_post_tags': (5000, 10000),
db.query_no_return(sql.format(table, 'LOGGED' if logged else 'UNLOGGED'))
'hive_follows': (5000, 5000),
'hive_feed_cache': (5000, 5000),
def execute_sql_script(query_executor, path_to_script):
'hive_blocks': (5000, 25000),
""" Load and execute sql script from file
'hive_reblogs': (5000, 5000),
Params:
'hive_payments': (5000, 5000),
query_executor - callable to execute query with
}
path_to_script - path to script
Returns:
for table, (n_vacuum, n_analyze) in autovac_config.items():
depending on query_executor
sql = """ALTER TABLE %s SET (autovacuum_vacuum_scale_factor = 0,
autovacuum_vacuum_threshold = %s,
Example:
autovacuum_analyze_scale_factor = 0,
print(execute_sql_script(db.query_row, "./test.sql"))
autovacuum_analyze_threshold = %s)"""
where test_sql: SELECT * FROM hive_state WHERE block_num = 0;
db.query(sql % (table, n_vacuum, n_analyze))
will return something like: (0, 18, Decimal('0.000000'), Decimal('0.000000'), Decimal('0.000000'), '')
"""
try:
def set_fillfactor(db):
sql_script = None
"""Initializes/resets FILLFACTOR for tables which are intesively updated"""
with open(path_to_script, 'r') as sql_script_file:
sql_script = sql_script_file.read()
fillfactor_config = {
if sql_script is not None:
'hive_posts': 70,
return query_executor(sql_script)
'hive_post_data': 70,
except Exception as ex:
'hive_votes': 70,
log.exception("Error running sql script: {}".format(ex))
'hive_reputation_data': 50
raise ex
}
return None
 
for table, fillfactor in fillfactor_config.items():
 
sql = """ALTER TABLE {} SET (FILLFACTOR = {})"""
 
db.query(sql.format(table, fillfactor))
 
 
def set_logged_table_attribute(db, logged):
 
"""Initializes/resets LOGGED/UNLOGGED attribute for tables which are intesively updated"""
 
 
logged_config = [
 
'hive_accounts',
 
'hive_permlink_data',
 
'hive_post_tags',
 
'hive_posts',
 
'hive_post_data',
 
'hive_votes',
 
'hive_reputation_data'
 
]
 
 
for table in logged_config:
 
log.info("Setting {} attribute on a table: {}".format('LOGGED' if logged else 'UNLOGGED', table))
 
sql = """ALTER TABLE {} SET {}"""
 
db.query_no_return(sql.format(table, 'LOGGED' if logged else 'UNLOGGED'))
 
 
def execute_sql_script(query_executor, path_to_script):
 
""" Load and execute sql script from file
 
Params:
 
query_executor - callable to execute query with
 
path_to_script - path to script
 
Returns:
 
depending on query_executor
 
 
Example:
 
print(execute_sql_script(db.query_row, "./test.sql"))
 
where test_sql: SELECT * FROM hive_state WHERE block_num = 0;
 
will return something like: (0, 18, Decimal('0.000000'), Decimal('0.000000'), Decimal('0.000000'), '')
 
"""
 
try:
 
sql_script = None
 
with open(path_to_script, 'r') as sql_script_file:
 
sql_script = sql_script_file.read()
 
if sql_script is not None:
 
return query_executor(sql_script)
 
except Exception as ex:
 
log.exception("Error running sql script: {}".format(ex))
 
raise ex
 
return None
Loading