diff --git a/chaind/db/migrations/default/versions/74e890aec7b0_session_tx_index.py b/chaind/db/migrations/default/versions/74e890aec7b0_session_tx_index.py new file mode 100644 index 0000000..50d25c6 --- /dev/null +++ b/chaind/db/migrations/default/versions/74e890aec7b0_session_tx_index.py @@ -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') diff --git a/chaind/sql/__init__.py b/chaind/sql/__init__.py new file mode 100644 index 0000000..e69de29 diff --git a/chaind/sql/session.py b/chaind/sql/session.py new file mode 100644 index 0000000..929c9c7 --- /dev/null +++ b/chaind/sql/session.py @@ -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 diff --git a/requirements.txt b/requirements.txt index 5e6278e..0cc68e6 100644 --- a/requirements.txt +++ b/requirements.txt @@ -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 diff --git a/setup.cfg b/setup.cfg index f1d662c..400509e 100644 --- a/setup.cfg +++ b/setup.cfg @@ -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