thread-safety and failing autoincrement

This commit is contained in:
AF 2020-08-05 23:26:49 +03:00
parent 94eb71488d
commit 055ca35e1d

View File

@ -1,7 +1,7 @@
from typing import Tuple, Optional, Iterable from typing import Tuple, Optional, Iterable
from nacl.bindings import crypto_sign_PUBLICKEYBYTES from nacl.bindings import crypto_sign_PUBLICKEYBYTES
from sqlalchemy import create_engine, LargeBinary, Column, REAL, BLOB, String, or_, and_, BigInteger from sqlalchemy import create_engine, LargeBinary, Column, REAL, BLOB, String, or_, and_, ForeignKeyConstraint, Integer
from sqlalchemy.ext.declarative import declarative_base from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import sessionmaker, Query from sqlalchemy.orm import sessionmaker, Query
@ -24,14 +24,19 @@ class MsgSgn(Base):
class Msg(Base): class Msg(Base):
__tablename__ = 'msgs' __tablename__ = 'msgs'
sf = Column(LargeBinary(crypto_sign_PUBLICKEYBYTES), primary_key=True) oid = Column(Integer, autoincrement=True, primary_key=True)
st = Column(LargeBinary(crypto_sign_PUBLICKEYBYTES), primary_key=True) sf = Column(LargeBinary(crypto_sign_PUBLICKEYBYTES))
idn = Column(LargeBinary(NONCE_SIZE), primary_key=True) st = Column(LargeBinary(crypto_sign_PUBLICKEYBYTES))
idn = Column(LargeBinary(NONCE_SIZE))
ts = Column(REAL, nullable=False, index=True) ts = Column(REAL, nullable=False, index=True)
en = Column(LargeBinary(NONCE_SIZE), nullable=False) en = Column(LargeBinary(NONCE_SIZE), nullable=False)
ec = Column(BLOB, nullable=False) ec = Column(BLOB, nullable=False)
flags = Column(String, nullable=False) flags = Column(String, nullable=False)
oid = Column(BigInteger, autoincrement=True, index=True, unique=True, nullable=False)
__table_args__ = (ForeignKeyConstraint((sf, st, idn,),
(MsgSgn.sf, MsgSgn.st, MsgSgn.idn,),
),
)
def sgn(self): def sgn(self):
return MsgSgn(sf=self.sf, st=self.st, idn=self.idn) return MsgSgn(sf=self.sf, st=self.st, idn=self.idn)
@ -124,7 +129,9 @@ class DBStorage(AbstractStorage):
query = query.order_by(Msg.oid.desc()) query = query.order_by(Msg.oid.desc())
if 'limit' in params: if 'limit' in params:
query = query.limit(params['limit']) query = query.limit(params['limit'])
return map(Msg.to_message, query.from_self().order_by(Msg.oid)) messages = map(Msg.to_message, list(query.from_self().order_by(Msg.oid)))
session.close()
return messages
def flags(self, m: Message, flags: str): def flags(self, m: Message, flags: str):
session = self.Session() session = self.Session()