Skip to content

Commit

Permalink
bump to sqla2
Browse files Browse the repository at this point in the history
  • Loading branch information
odesenfans committed Sep 16, 2023
1 parent e89d27f commit b1762fc
Show file tree
Hide file tree
Showing 11 changed files with 200 additions and 184 deletions.
4 changes: 2 additions & 2 deletions setup.cfg
Original file line number Diff line number Diff line change
Expand Up @@ -69,8 +69,8 @@ install_requires =
secp256k1==0.14.0
sentry-sdk==1.16.0
setproctitle==1.2.2
sqlalchemy[mypy]==1.4.41
sqlalchemy-utils==0.38.3
sqlalchemy==2.0.20
sqlalchemy-utils==0.41.1
substrate-interface==1.3.4
ujson==5.1.0 # required by aiocache
urllib3==1.26.11
Expand Down
26 changes: 13 additions & 13 deletions src/aleph/db/models/aggregates.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import datetime as dt
from typing import Any
from typing import Any, Dict

from sqlalchemy import Boolean, Column, ForeignKey, Index, String, TIMESTAMP
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import relationship
from sqlalchemy.orm import relationship, Mapped

from .base import Base

Expand All @@ -19,11 +19,11 @@ class AggregateElementDb(Base):

__tablename__ = "aggregate_elements"

item_hash: str = Column(String, primary_key=True)
key: str = Column(String, nullable=False)
owner: str = Column(String, nullable=False)
content: Any = Column(JSONB, nullable=False)
creation_datetime: dt.datetime = Column(TIMESTAMP(timezone=True), nullable=False)
item_hash: Mapped[str] = Column(String, primary_key=True)
key: Mapped[str] = Column(String, nullable=False)
owner: Mapped[str] = Column(String, nullable=False)
content: Mapped[Dict[Any, Any]] = Column(JSONB, nullable=False)
creation_datetime: Mapped[dt.datetime] = Column(TIMESTAMP(timezone=True), nullable=False)

__table_args__ = (
Index("ix_time_desc", creation_datetime.desc()),
Expand All @@ -40,15 +40,15 @@ class AggregateDb(Base):

__tablename__ = "aggregates"

key: str = Column(String, primary_key=True)
owner: str = Column(String, primary_key=True)
content: Any = Column(JSONB, nullable=False)
creation_datetime: dt.datetime = Column(TIMESTAMP(timezone=True), nullable=False)
last_revision_hash: str = Column(
key: Mapped[str] = Column(String, primary_key=True)
owner: Mapped[str] = Column(String, primary_key=True)
content: Mapped[Dict[Any, Any]] = Column(JSONB, nullable=False)
creation_datetime: Mapped[dt.datetime] = Column(TIMESTAMP(timezone=True), nullable=False)
last_revision_hash: Mapped[str] = Column(
ForeignKey(AggregateElementDb.item_hash), nullable=False
)
dirty = Column(Boolean, nullable=False)

__table_args__ = (Index("ix_aggregates_owner", owner),)

last_revision: AggregateElementDb = relationship(AggregateElementDb)
last_revision: Mapped[AggregateElementDb] = relationship(AggregateElementDb)
13 changes: 7 additions & 6 deletions src/aleph/db/models/balances.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@

from aleph_message.models import Chain
from sqlalchemy import Column, DECIMAL, String, Integer, UniqueConstraint, BigInteger
from sqlalchemy.orm import Mapped
from sqlalchemy_utils.types.choice import ChoiceType

from .base import Base
Expand All @@ -11,13 +12,13 @@
class AlephBalanceDb(Base):
__tablename__ = "balances"

id: int = Column(BigInteger, primary_key=True)
id: Mapped[int] = Column(BigInteger, primary_key=True)

address: str = Column(String, nullable=False, index=True)
chain: Chain = Column(ChoiceType(Chain), nullable=False)
dapp: Optional[str] = Column(String, nullable=True)
eth_height: int = Column(Integer, nullable=False)
balance: Decimal = Column(DECIMAL, nullable=False)
address: Mapped[str] = Column(String, nullable=False, index=True)
chain: Mapped[Chain] = Column(ChoiceType(Chain), nullable=False)
dapp: Mapped[Optional[str]] = Column(String, nullable=True)
eth_height: Mapped[int] = Column(Integer, nullable=False)
balance: Mapped[Decimal] = Column(DECIMAL, nullable=False)

__table_args__ = (
UniqueConstraint(
Expand Down
37 changes: 19 additions & 18 deletions src/aleph/db/models/chains.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
from aleph_message.models import Chain
from sqlalchemy import Column, Integer, String, TIMESTAMP, Boolean
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import Mapped
from sqlalchemy_utils.types.choice import ChoiceType

from aleph.toolkit.timestamp import timestamp_to_datetime
Expand All @@ -26,10 +27,10 @@ class ChainSyncStatusDb(Base):

__tablename__ = "chains_sync_status"

chain: Chain = Column(ChoiceType(Chain), primary_key=True)
type: ChainEventType = Column(ChoiceType(ChainEventType), primary_key=True)
height: int = Column(Integer, nullable=False)
last_update: dt.datetime = Column(TIMESTAMP(timezone=True), nullable=False)
chain: Mapped[Chain] = Column(ChoiceType(Chain), primary_key=True)
type: Mapped[ChainEventType] = Column(ChoiceType(ChainEventType), primary_key=True)
height: Mapped[int] = Column(Integer, nullable=False)
last_update: Mapped[dt.datetime] = Column(TIMESTAMP(timezone=True), nullable=False)


class IndexerSyncStatusDb(Base):
Expand All @@ -45,15 +46,15 @@ class IndexerSyncStatusDb(Base):

__tablename__ = "indexer_sync_status"

chain: Chain = Column(ChoiceType(Chain), primary_key=True)
event_type: ChainEventType = Column(ChoiceType(ChainEventType), primary_key=True)
start_block_datetime: dt.datetime = Column(
chain: Mapped[Chain] = Column(ChoiceType(Chain), primary_key=True)
event_type: Mapped[ChainEventType] = Column(ChoiceType(ChainEventType), primary_key=True)
start_block_datetime: Mapped[dt.datetime] = Column(
TIMESTAMP(timezone=True), primary_key=True
)
end_block_datetime: dt.datetime = Column(TIMESTAMP(timezone=True), nullable=False)
start_included: bool = Column(Boolean, nullable=False)
end_included: bool = Column(Boolean, nullable=False)
last_updated: dt.datetime = Column(TIMESTAMP(timezone=True), nullable=False)
end_block_datetime: Mapped[dt.datetime] = Column(TIMESTAMP(timezone=True), nullable=False)
start_included: Mapped[bool] = Column(Boolean, nullable=False)
end_included: Mapped[bool] = Column(Boolean, nullable=False)
last_updated: Mapped[dt.datetime] = Column(TIMESTAMP(timezone=True), nullable=False)

def to_range(self) -> Range[dt.datetime]:
return Range(
Expand All @@ -67,14 +68,14 @@ def to_range(self) -> Range[dt.datetime]:
class ChainTxDb(Base):
__tablename__ = "chain_txs"

hash: str = Column(String, primary_key=True)
chain: Chain = Column(ChoiceType(Chain), nullable=False)
height: int = Column(Integer, nullable=False)
datetime: dt.datetime = Column(TIMESTAMP(timezone=True), nullable=False)
publisher: str = Column(String, nullable=False)
protocol: ChainSyncProtocol = Column(ChoiceType(ChainSyncProtocol), nullable=False)
hash: Mapped[str] = Column(String, primary_key=True)
chain: Mapped[Chain] = Column(ChoiceType(Chain), nullable=False)
height: Mapped[int] = Column(Integer, nullable=False)
datetime: Mapped[dt.datetime] = Column(TIMESTAMP(timezone=True), nullable=False)
publisher: Mapped[str] = Column(String, nullable=False)
protocol: Mapped[ChainSyncProtocol] = Column(ChoiceType(ChainSyncProtocol), nullable=False)
protocol_version = Column(Integer, nullable=False)
content: Any = Column(JSONB, nullable=False)
content: Mapped[Any] = Column(JSONB, nullable=False)

# TODO: this method is only used in tests, make it a helper
@classmethod
Expand Down
30 changes: 15 additions & 15 deletions src/aleph/db/models/files.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
from typing import Optional, List, Any, Dict

from sqlalchemy import BigInteger, Column, String, ForeignKey, TIMESTAMP, Index, UniqueConstraint
from sqlalchemy.orm import relationship
from sqlalchemy.orm import relationship, Mapped
from sqlalchemy_utils import ChoiceType

from aleph.types.files import FileType
Expand Down Expand Up @@ -37,38 +37,38 @@ class StoredFileDb(Base):

# size: int = Column(BigInteger, nullable=False)
# TODO: compute the size from local storage
size: int = Column(BigInteger, nullable=False)
type: FileType = Column(ChoiceType(FileType), nullable=False)
size: Mapped[int] = Column(BigInteger, nullable=False)
type: Mapped[FileType] = Column(ChoiceType(FileType), nullable=False)

pins: List["FilePinDb"] = relationship("FilePinDb", back_populates="file")
tags: List["FileTagDb"] = relationship("FileTagDb", back_populates="file")
pins: Mapped[List["FilePinDb"]] = relationship("FilePinDb", back_populates="file")
tags: Mapped[List["FileTagDb"]] = relationship("FileTagDb", back_populates="file")


class FileTagDb(Base):
__tablename__ = "file_tags"

tag: FileTag = Column(String, primary_key=True)
owner: str = Column(String, nullable=False)
file_hash: str = Column(ForeignKey(StoredFileDb.hash), nullable=False, index=True)
last_updated: dt.datetime = Column(TIMESTAMP(timezone=True), nullable=False)
tag: Mapped[FileTag] = Column(String, primary_key=True)
owner: Mapped[str] = Column(String, nullable=False)
file_hash: Mapped[str] = Column(ForeignKey(StoredFileDb.hash), nullable=False, index=True)
last_updated: Mapped[dt.datetime] = Column(TIMESTAMP(timezone=True), nullable=False)

file: StoredFileDb = relationship(StoredFileDb, back_populates="tags")
file: Mapped[StoredFileDb] = relationship(StoredFileDb, back_populates="tags")


class FilePinDb(Base):
__tablename__ = "file_pins"

id: int = Column(BigInteger, primary_key=True)
id: Mapped[int] = Column(BigInteger, primary_key=True)

file_hash: str = Column(ForeignKey(StoredFileDb.hash), nullable=False)
created: dt.datetime = Column(TIMESTAMP(timezone=True), nullable=False)
type: str = Column(String, nullable=False)
file_hash: Mapped[str] = Column(ForeignKey(StoredFileDb.hash), nullable=False)
created: Mapped[dt.datetime] = Column(TIMESTAMP(timezone=True), nullable=False)
type: Mapped[str] = Column(String, nullable=False)
# TODO: these columns should be defined on Message/ContentFilePinDb instead with `use_existing`.
# This field is only available since SQLA 2.0.
owner = Column(String, nullable=True, index=True)
item_hash = Column(String, nullable=True)

file: StoredFileDb = relationship(StoredFileDb, back_populates="pins")
file: Mapped[StoredFileDb] = relationship(StoredFileDb, back_populates="pins")

__mapper_args__: Dict[str, Any] = {
"polymorphic_on": type,
Expand Down
66 changes: 33 additions & 33 deletions src/aleph/db/models/messages.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@
UniqueConstraint,
)
from sqlalchemy.dialects.postgresql import JSONB
from sqlalchemy.orm import relationship
from sqlalchemy.orm import relationship, Mapped
from sqlalchemy_utils.types.choice import ChoiceType

from aleph.toolkit.timestamp import timestamp_to_datetime
Expand Down Expand Up @@ -76,9 +76,9 @@ def validate_message_content(
class MessageStatusDb(Base):
__tablename__ = "message_status"

item_hash: str = Column(String, primary_key=True)
status: MessageStatus = Column(ChoiceType(MessageStatus), nullable=False)
reception_time: dt.datetime = Column(TIMESTAMP(timezone=True), nullable=False)
item_hash: Mapped[str] = Column(String, primary_key=True)
status: Mapped[MessageStatus] = Column(ChoiceType(MessageStatus), nullable=False)
reception_time: Mapped[dt.datetime] = Column(TIMESTAMP(timezone=True), nullable=False)


class MessageDb(Base):
Expand All @@ -88,19 +88,19 @@ class MessageDb(Base):

__tablename__ = "messages"

item_hash: str = Column(String, primary_key=True)
type: MessageType = Column(ChoiceType(MessageType), nullable=False)
chain: Chain = Column(ChoiceType(Chain), nullable=False)
sender: str = Column(String, nullable=False, index=True)
signature: Optional[str] = Column(String, nullable=True)
item_type: ItemType = Column(ChoiceType(ItemType), nullable=False)
item_content: Optional[str] = Column(String, nullable=True)
content: Any = Column(JSONB, nullable=False)
time: dt.datetime = Column(TIMESTAMP(timezone=True), nullable=False, index=True)
channel: Optional[Channel] = Column(String, nullable=True, index=True)
size: int = Column(Integer, nullable=False)

confirmations: "List[ChainTxDb]" = relationship(
item_hash: Mapped[str] = Column(String, primary_key=True)
type: Mapped[MessageType] = Column(ChoiceType(MessageType), nullable=False)
chain: Mapped[Chain] = Column(ChoiceType(Chain), nullable=False)
sender: Mapped[str] = Column(String, nullable=False, index=True)
signature: Mapped[Optional[str]] = Column(String, nullable=True)
item_type: Mapped[ItemType] = Column(ChoiceType(ItemType), nullable=False)
item_content: Mapped[Optional[str]] = Column(String, nullable=True)
content: Mapped[Any] = Column(JSONB, nullable=False)
time: Mapped[dt.datetime] = Column(TIMESTAMP(timezone=True), nullable=False, index=True)
channel: Mapped[Optional[Channel]] = Column(String, nullable=True, index=True)
size: Mapped[int] = Column(Integer, nullable=False)

confirmations: Mapped["List[ChainTxDb]"] = relationship(
"ChainTxDb", secondary=message_confirmations
)

Expand Down Expand Up @@ -180,31 +180,31 @@ def from_message_dict(cls, message_dict: Dict[str, Any]) -> "MessageDb":
class ForgottenMessageDb(Base):
__tablename__ = "forgotten_messages"

item_hash: str = Column(String, primary_key=True)
type: MessageType = Column(ChoiceType(MessageType), nullable=False)
chain: Chain = Column(ChoiceType(Chain), nullable=False)
sender: str = Column(String, nullable=False, index=True)
signature: Optional[str] = Column(String, nullable=True)
item_type: ItemType = Column(ChoiceType(ItemType), nullable=False)
time: dt.datetime = Column(TIMESTAMP(timezone=True), nullable=False, index=True)
channel: Optional[Channel] = Column(String, nullable=True, index=True)
forgotten_by: List[str] = Column(ARRAY(String), nullable=False) # type: ignore
item_hash: Mapped[str] = Column(String, primary_key=True)
type: Mapped[MessageType] = Column(ChoiceType(MessageType), nullable=False)
chain: Mapped[Chain] = Column(ChoiceType(Chain), nullable=False)
sender: Mapped[str] = Column(String, nullable=False, index=True)
signature: Mapped[Optional[str]] = Column(String, nullable=True)
item_type: Mapped[ItemType] = Column(ChoiceType(ItemType), nullable=False)
time: Mapped[dt.datetime] = Column(TIMESTAMP(timezone=True), nullable=False, index=True)
channel: Mapped[Optional[Channel]] = Column(String, nullable=True, index=True)
forgotten_by: Mapped[List[str]] = Column(ARRAY(String), nullable=False) # type: ignore


class ErrorCodeDb(Base):
__tablename__ = "error_codes"

code: int = Column(Integer, primary_key=True)
description: str = Column(String, nullable=False)
code: Mapped[int] = Column(Integer, primary_key=True)
description: Mapped[str] = Column(String, nullable=False)


class RejectedMessageDb(Base):
__tablename__ = "rejected_messages"

item_hash: str = Column(String, primary_key=True)
message: Mapping[str, Any] = Column(JSONB, nullable=False)
error_code: ErrorCode = Column(
item_hash: Mapped[str] = Column(String, primary_key=True)
message: Mapped[Mapping[str, Any]] = Column(JSONB, nullable=False)
error_code: Mapped[ErrorCode] = Column(
ChoiceType(ErrorCode, impl=Integer()), nullable=False
)
details: Optional[Dict[str, Any]] = Column(JSONB, nullable=True)
traceback: Optional[str] = Column(String, nullable=True)
details: Mapped[Optional[Dict[str, Any]]] = Column(JSONB, nullable=True)
traceback: Mapped[Optional[str]] = Column(String, nullable=True)
11 changes: 6 additions & 5 deletions src/aleph/db/models/peers.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
from enum import Enum

from sqlalchemy import Column, String, TIMESTAMP
from sqlalchemy.orm import Mapped
from sqlalchemy_utils.types.choice import ChoiceType

from .base import Base
Expand All @@ -16,8 +17,8 @@ class PeerType(str, Enum):
class PeerDb(Base):
__tablename__ = "peers"

peer_id = Column(String, primary_key=True)
peer_type: PeerType = Column(ChoiceType(PeerType), primary_key=True)
address = Column(String, nullable=False)
source: PeerType = Column(ChoiceType(PeerType), nullable=False)
last_seen: dt.datetime = Column(TIMESTAMP(timezone=True), nullable=False)
peer_id: Mapped[str] = Column(String, primary_key=True)
peer_type: Mapped[PeerType] = Column(ChoiceType(PeerType), primary_key=True)
address: Mapped[str] = Column(String, nullable=False)
source: Mapped[PeerType] = Column(ChoiceType(PeerType), nullable=False)
last_seen: Mapped[dt.datetime] = Column(TIMESTAMP(timezone=True), nullable=False)
Loading

0 comments on commit b1762fc

Please sign in to comment.