Add session index interface and database store

This commit is contained in:
nolash 2021-08-26 12:01:31 +02:00
parent a8eb20bc90
commit 9d78dd055d
Signed by: lash
GPG Key ID: 21D2E7BB88C2A746
5 changed files with 56 additions and 2 deletions

View File

@ -0,0 +1,31 @@
"""Session tx index
Revision ID: 74e890aec7b0
Revises: 7ac591b16c68
Create Date: 2021-08-26 10:51:53.651692
"""
from alembic import op
import sqlalchemy as sa
# revision identifiers, used by Alembic.
revision = '74e890aec7b0'
down_revision = '7ac591b16c68'
branch_labels = None
depends_on = None
def upgrade():
op.create_table(
'session',
sa.Column('id', sa.Integer, primary_key=True),
sa.Column('otx_id', sa.Integer, sa.ForeignKey('otx.id'), nullable=False),
sa.Column('session', sa.String(256), nullable=False),
)
op.create_index('idx_session', 'session', ['session', 'otx_id'], unique=True)
def downgrade():
op.drop_index('idx_session')
op.drop_table('session')

0
chaind/sql/__init__.py Normal file
View File

23
chaind/sql/session.py Normal file
View File

@ -0,0 +1,23 @@
# external imports
from chainqueue.sql.query import get_tx
class SessionIndex:
def __init__(self, session_id):
self.id = session_id
def add(self, chain_spec, tx_hash, session=None):
tx = get_tx(chain_spec, tx_hash, session=session)
session.execute("INSERT INTO session (otx_id, session) VALUES ({},'{}')".format(tx['otx_id'], self.id))
session.flush()
def get(self, chain_spec, adapter, session=None):
session = adapter.create_session(session=session)
otxs = session.execute("SELECT tx_hash, signed_tx FROM otx WHERE otx.id = ( SELECT otx_id FROM session where session='{}')".format(self.id))
txs = {}
for otx in otxs:
txs[otx[0]] = otx[1]
adapter.release_session(session)
return txs

View File

@ -1,5 +1,5 @@
chainlib>=0.0.9a2,<=0.1.0
chainqueue>=0.0.4a3,<=0.0.4
chainqueue>=0.0.4a4,<=0.0.4
chainsyncer>=0.0.6a1,<=0.0.6
confini>=0.4.1a1,<0.5.0
crypto-dev-signer>=0.4.15a1,<0.5.0

View File

@ -1,6 +1,6 @@
[metadata]
name = chaind
version = 0.0.2a3
version = 0.0.2a4
description = Base package for chain queue services
author = Louis Holbrook
author_email = dev@holbrook.no