4
from sqlalchemy import Column, DateTime, func, Uuid, BigInteger, ForeignKey, Sequence, text, types
5
from sqlalchemy.ext.declarative import declared_attr
6
from sqlalchemy.orm import mapped_column, Mapped
7
from sqlalchemy_utils.types import JSONType
8
from typing import Annotated, Optional
10
from sqlalchemy import DateTime
11
from sqlalchemy.ext.compiler import compiles
12
from sqlalchemy.sql import expression
17
expression.FunctionElement
22
@compiles(UTCNow, "postgresql")
24
element, compiler, **kw
26
return "TIMEZONE('utc', CURRENT_TIMESTAMP)"
28
guid_primary_key = mapped_column(
32
server_default=text("gen_random_uuid()")
38
server_default=text("gen_random_uuid()")
41
vars: Mapped[Optional[dict]] = mapped_column(JSONType)
44
created_at: Mapped[datetime.datetime] = Column(
47
server_default=UTCNow()
50
updated_at: Mapped[datetime.datetime] = Column(
53
server_default=UTCNow(),
54
server_onupdate=UTCNow(),
59
company_id: Mapped[uuid.UUID] = mapped_column(ForeignKey("company.id"), index=True, nullable=False)
62
created_by: Mapped[uuid.UUID] = mapped_column(Uuid, index=True, nullable=False)
63
edited_by: Mapped[uuid.UUID] = mapped_column(Uuid, index=True, nullable=False)
70
f'{cls.__tablename__}_lsn_seq'
77
getattr(cls, 'lsn_seq'),
78
onupdate=getattr(cls, 'lsn_seq').next_value(), index=True
82
class AllMixin(LsnMixin, CompanyMixin, TimestampMixin, VarsMixin):
83
id: Mapped[uuid.UUID] = mapped_column(Uuid, primary_key=True, index=True, default=uuid.uuid4)