metadata db load fixes
This commit is contained in:
@@ -44,8 +44,8 @@ async def main():
|
|||||||
log.error('backfill requires a memcache server')
|
log.error('backfill requires a memcache server')
|
||||||
await memcache.connect()
|
await memcache.connect()
|
||||||
redis_state = RedisState([recent_ohlcs]) # NOTE: ONLY the ohlc's are pushed to Redis. We do not want to touch anything else.
|
redis_state = RedisState([recent_ohlcs]) # NOTE: ONLY the ohlc's are pushed to Redis. We do not want to touch anything else.
|
||||||
if db:
|
|
||||||
db.connect()
|
db.connect()
|
||||||
|
if db:
|
||||||
db_state = DbState(BlockData.by_opt('db'))
|
db_state = DbState(BlockData.by_opt('db'))
|
||||||
with db.session:
|
with db.session:
|
||||||
state = await db_state.load()
|
state = await db_state.load()
|
||||||
|
|||||||
@@ -1,7 +1,7 @@
|
|||||||
import logging
|
import logging
|
||||||
from asyncio import CancelledError
|
from asyncio import CancelledError
|
||||||
|
|
||||||
from dexorder import db, blockchain
|
from dexorder import db, blockchain, config
|
||||||
from dexorder.base.chain import current_chain
|
from dexorder.base.chain import current_chain
|
||||||
from dexorder.bin.executable import execute
|
from dexorder.bin.executable import execute
|
||||||
from dexorder.blockstate import current_blockstate
|
from dexorder.blockstate import current_blockstate
|
||||||
@@ -78,8 +78,8 @@ async def main():
|
|||||||
if memcache:
|
if memcache:
|
||||||
await memcache.connect()
|
await memcache.connect()
|
||||||
redis_state = RedisState(BlockData.by_opt('redis'))
|
redis_state = RedisState(BlockData.by_opt('redis'))
|
||||||
if db:
|
|
||||||
db.connect()
|
db.connect()
|
||||||
|
if db:
|
||||||
db_state = DbState(BlockData.by_opt('db'))
|
db_state = DbState(BlockData.by_opt('db'))
|
||||||
with db.session:
|
with db.session:
|
||||||
state = await db_state.load()
|
state = await db_state.load()
|
||||||
|
|||||||
@@ -45,9 +45,10 @@ class Db:
|
|||||||
def __init__(self, db_url_config_key='db_url'):
|
def __init__(self, db_url_config_key='db_url'):
|
||||||
self.kv = Kv()
|
self.kv = Kv()
|
||||||
self.db_url_config_key = db_url_config_key
|
self.db_url_config_key = db_url_config_key
|
||||||
|
self.connected = False
|
||||||
|
|
||||||
def __bool__(self):
|
def __bool__(self):
|
||||||
return bool(config.db_url)
|
return self.connected
|
||||||
|
|
||||||
def transaction(self) -> SessionTransaction:
|
def transaction(self) -> SessionTransaction:
|
||||||
"""
|
"""
|
||||||
@@ -95,9 +96,16 @@ class Db:
|
|||||||
# noinspection PyShadowingNames
|
# noinspection PyShadowingNames
|
||||||
def connect(self, url=None, migrate=True, reconnect=False, dump_sql=None):
|
def connect(self, url=None, migrate=True, reconnect=False, dump_sql=None):
|
||||||
if _engine.get() is not None and not reconnect:
|
if _engine.get() is not None and not reconnect:
|
||||||
return
|
return None
|
||||||
if url is None:
|
if url is None:
|
||||||
url = config[self.db_url_config_key]
|
url = config.get(self.db_url_config_key)
|
||||||
|
if not url:
|
||||||
|
# noinspection PyTypeChecker
|
||||||
|
_engine.set(None)
|
||||||
|
# noinspection PyTypeChecker
|
||||||
|
_session.set(None)
|
||||||
|
self.connected = False
|
||||||
|
return None
|
||||||
if dump_sql is None:
|
if dump_sql is None:
|
||||||
dump_sql = config.dump_sql
|
dump_sql = config.dump_sql
|
||||||
engine = sqlalchemy.create_engine(url, echo=dump_sql, json_serializer=json.dumps, json_deserializer=json.loads)
|
engine = sqlalchemy.create_engine(url, echo=dump_sql, json_serializer=json.dumps, json_deserializer=json.loads)
|
||||||
@@ -109,7 +117,8 @@ class Db:
|
|||||||
for row in result:
|
for row in result:
|
||||||
log.info(f'{url} database revision {row[0]}')
|
log.info(f'{url} database revision {row[0]}')
|
||||||
_engine.set(engine)
|
_engine.set(engine)
|
||||||
return db
|
self.connected = True
|
||||||
|
return self
|
||||||
raise Exception(f'{url} database version not found')
|
raise Exception(f'{url} database version not found')
|
||||||
|
|
||||||
db = Db()
|
db = Db()
|
||||||
|
|||||||
@@ -35,6 +35,7 @@ async def load_pool(address: str) -> OldPoolDict:
|
|||||||
found = None
|
found = None
|
||||||
chain_id = current_chain.get().id
|
chain_id = current_chain.get().id
|
||||||
# todo other exchanges
|
# todo other exchanges
|
||||||
|
if db:
|
||||||
pool = db.session.get(Pool, (chain_id, address))
|
pool = db.session.get(Pool, (chain_id, address))
|
||||||
if pool is not None:
|
if pool is not None:
|
||||||
return OldPoolDict(type='Pool', chain=chain_id, address=address, exchange=pool.exchange.value,
|
return OldPoolDict(type='Pool', chain=chain_id, address=address, exchange=pool.exchange.value,
|
||||||
|
|||||||
@@ -29,6 +29,7 @@ async def get_token(address) -> Optional[OldTokenDict]:
|
|||||||
async def load_token(address: str) -> Optional[OldTokenDict]:
|
async def load_token(address: str) -> Optional[OldTokenDict]:
|
||||||
contract = ERC20(address)
|
contract = ERC20(address)
|
||||||
chain_id = current_chain.get().id
|
chain_id = current_chain.get().id
|
||||||
|
if db:
|
||||||
token = db.session.get(Token, (chain_id, address))
|
token = db.session.get(Token, (chain_id, address))
|
||||||
if token is not None:
|
if token is not None:
|
||||||
return OldTokenDict(type='Token', chain=chain_id, address=address,
|
return OldTokenDict(type='Token', chain=chain_id, address=address,
|
||||||
|
|||||||
Reference in New Issue
Block a user