moonstream/moonstreamdb-v3/moonstreamdbv3/models_indexes.py

66 wiersze
1.9 KiB
Python
Czysty Zwykły widok Historia

2024-05-22 11:58:26 +00:00
"""
Moonstream database V3 indexes
"""
import uuid
from sqlalchemy import (
VARCHAR,
BigInteger,
Column,
DateTime,
Index,
Integer,
MetaData,
Text,
)
from sqlalchemy.dialects.postgresql import JSONB, UUID
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.sql import expression, text
"""
Naming conventions doc
https://docs.sqlalchemy.org/en/20/core/constraints.html#configuring-constraint-naming-conventions
"""
convention = {
"ix": "ix_%(column_0_label)s",
"uq": "uq_%(table_name)s_%(column_0_name)s",
"ck": "ck_%(table_name)s_%(constraint_name)s",
"fk": "fk_%(table_name)s_%(column_0_name)s_%(referred_table_name)s",
"pk": "pk_%(table_name)s",
}
metadata = MetaData(naming_convention=convention)
Base = declarative_base(metadata=metadata)
"""
Creating a utcnow function which runs on the Posgres database server when created_at and updated_at
fields are populated.
Following:
1. https://docs.sqlalchemy.org/en/13/core/compiler.html#utc-timestamp-function
2. https://www.postgresql.org/docs/current/functions-datetime.html#FUNCTIONS-DATETIME-CURRENT
3. https://stackoverflow.com/a/33532154/13659585
"""
class utcnow(expression.FunctionElement):
type = DateTime # type: ignore
@compiles(utcnow, "postgresql")
def pg_utcnow(element, compiler, **kwargs):
return "TIMEZONE('utc', statement_timestamp())"
class EvmBasedBlocks(Base):
__abstract__ = True
block_number = Column(BigInteger, primary_key=True, nullable=False, index=True)
block_hash = Column(VARCHAR(256), nullable=False, index=False)
block_timestamp = Column(BigInteger, nullable=False, index=True)
parent_hash = Column(VARCHAR(256), nullable=False)
path = Column(Text, nullable=False)
indexed_at = Column(
DateTime(timezone=True), server_default=utcnow(), nullable=False
)