commit 9d78dd055d214025e0c7d5b9ff362f13b9b71cd0
parent a8eb20bc908f2ef52b767f55fee1c1b9fc645edb
Author: nolash <dev@holbrook.no>
Date: Thu, 26 Aug 2021 12:01:31 +0200
Add session index interface and database store
Diffstat:
5 files changed, 56 insertions(+), 2 deletions(-)
diff --git a/chaind/db/migrations/default/versions/74e890aec7b0_session_tx_index.py 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
diff --git a/chaind/sql/session.py 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
@@ -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
@@ -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