From b7ff6569e4d5fb4099d97a66f09a31dea8f8e00b Mon Sep 17 00:00:00 2001 From: Lex Berezhny Date: Wed, 20 May 2020 17:59:26 -0400 Subject: [PATCH] added Database.temp_sqlite_regtest --- lbry/db/database.py | 9 +++++++++ lbry/db/queries.py | 19 ++++++++----------- 2 files changed, 17 insertions(+), 11 deletions(-) diff --git a/lbry/db/database.py b/lbry/db/database.py index ae8e3cf01..9a1ca9316 100644 --- a/lbry/db/database.py +++ b/lbry/db/database.py @@ -1,5 +1,6 @@ import os import asyncio +import tempfile from typing import List, Optional, Tuple, Iterable, TYPE_CHECKING from concurrent.futures import Executor, ThreadPoolExecutor, ProcessPoolExecutor from functools import partial @@ -56,6 +57,14 @@ class Database: self.multiprocess = multiprocess self.executor: Optional[Executor] = None + @classmethod + def temp_sqlite_regtest(cls): + from lbry import Config, RegTestLedger + directory = tempfile.mkdtemp() + conf = Config.with_same_dir(directory) + ledger = RegTestLedger(conf) + return cls(ledger, conf.db_url_or_default) + @classmethod def from_memory(cls, ledger): return cls(ledger, 'sqlite:///:memory:') diff --git a/lbry/db/queries.py b/lbry/db/queries.py index 9bc49fa30..e39b62107 100644 --- a/lbry/db/queries.py +++ b/lbry/db/queries.py @@ -54,16 +54,13 @@ def initialize(url: str, ledger: Ledger, track_metrics=False, block_and_filter=N def check_version_and_create_tables(): context = ctx() - if SCHEMA_VERSION: - if context.has_table('version'): - version = context.fetchone(select(Version.c.version).limit(1)) - if version and version['version'] == SCHEMA_VERSION: - return - metadata.drop_all(context.engine) - metadata.create_all(context.engine) - context.execute(Version.insert().values(version=SCHEMA_VERSION)) - else: - metadata.create_all(context.engine) + if context.has_table('version'): + version = context.fetchone(select(Version.c.version).limit(1)) + if version and version['version'] == SCHEMA_VERSION: + return + metadata.drop_all(context.engine) + metadata.create_all(context.engine) + context.execute(Version.insert().values(version=SCHEMA_VERSION)) class QueryContext(NamedTuple): @@ -375,7 +372,7 @@ def execute_fetchall(sql): def get_best_height(): return ctx().fetchone( - select(func.coalesce(func.max(TX.c.height), 0).label('total')).select_from(TX) + select(func.coalesce(func.max(TX.c.height), -1).label('total')).select_from(TX) )['total']