From d8fb31aeddc4c953d49a4b11804339381c67acd8 Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Mon, 22 Jun 2020 10:52:17 -0400 Subject: [PATCH] addeed greatest sql function that supports postgres and sqlite --- lbry/blockchain/sync.py | 3 ++- lbry/console.py | 7 +++++++ lbry/db/query_context.py | 2 +- lbry/db/utils.py | 19 ++++++++++++++++++- 4 files changed, 28 insertions(+), 3 deletions(-) diff --git a/lbry/blockchain/sync.py b/lbry/blockchain/sync.py index 75e97abe9..adec68368 100644 --- a/lbry/blockchain/sync.py +++ b/lbry/blockchain/sync.py @@ -18,6 +18,7 @@ from lbry.db.sync import ( condition_spent_claims, condition_spent_supports, select_missing_supports, process_claim_changes ) +from lbry.db.utils import greatest from lbry.schema.url import normalize_name from .lbrycrd import Lbrycrd @@ -368,7 +369,7 @@ def process_metadata(starting_height: int, ending_height: int, initial_sync: boo [(Claim.c.claim_hash == takeover['claim_hash'], takeover['height'])], else_=None ), - activation_height=func.min(Claim.c.activation_height, takeover['height']), + activation_height=greatest(Claim.c.activation_height, takeover['height']), ) ) p.ctx.execute(update_claims) diff --git a/lbry/console.py b/lbry/console.py index b69dead87..9aa226022 100644 --- a/lbry/console.py +++ b/lbry/console.py @@ -100,6 +100,11 @@ class Advanced(Basic): if event == "save" and d['step'] == d['total']: bar.close() + def update_other_bars(self, e, d): + bar = self.get_or_create_bar(e, e, d['unit'], d['total'], leave=True) + diff = d['step']-bar.last_print_n + bar.update(diff) + def on_sync_progress(self, event): e, d = event['event'], event.get('data', {}) if e.endswith("start"): @@ -110,3 +115,5 @@ class Advanced(Basic): self.update_sync_block_bars("read", d) elif e.endswith("block.save"): self.update_sync_block_bars("save", d) + else: + self.update_other_bars(e, d) diff --git a/lbry/db/query_context.py b/lbry/db/query_context.py index 507df04ac..9b4dad933 100644 --- a/lbry/db/query_context.py +++ b/lbry/db/query_context.py @@ -463,7 +463,7 @@ class BulkLoader: if claim.is_signed: claim_record['channel_hash'] = claim.signing_channel_hash claim_record['signature'] = txo.get_encoded_signature() - claim_record['signature_digest'] = txo.get_signature_digest(None) + claim_record['signature_digest'] = txo.get_signature_digest(self.ledger) tags = [ {'claim_hash': claim_hash, 'tag': tag} for tag in clean_tags(claim.message.tags) diff --git a/lbry/db/utils.py b/lbry/db/utils.py index fdca9571d..97ee208f8 100644 --- a/lbry/db/utils.py +++ b/lbry/db/utils.py @@ -2,7 +2,9 @@ from itertools import islice from typing import List, Union from sqlalchemy import text, and_ -from sqlalchemy.sql.expression import Select +from sqlalchemy.sql.expression import Select, FunctionElement +from sqlalchemy.types import Numeric +from sqlalchemy.ext.compiler import compiles try: from sqlalchemy.dialects.postgresql import insert as pg_insert # pylint: disable=unused-import except ImportError: @@ -11,6 +13,21 @@ except ImportError: from .tables import AccountAddress +class greatest(FunctionElement): + type = Numeric() + name = 'greatest' + + +@compiles(greatest) +def default_greatest(element, compiler, **kw): + return "greatest(%s)" % compiler.process(element.clauses, **kw) + + +@compiles(greatest, 'sqlite') +def sqlite_greatest(element, compiler, **kw): + return "max(%s)" % compiler.process(element.clauses, **kw) + + def chunk(rows, step): it, total = iter(rows), len(rows) for _ in range(0, total, step):