-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmixins.py
33 lines (20 loc) · 850 Bytes
/
mixins.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
from sqlalchemy import Boolean, Column, ForeignKey, Integer, String, DateTime
from sqlalchemy.sql import expression
from sqlalchemy.ext.compiler import compiles
from sqlalchemy.types import DateTime
from datetime import datetime
# https://docs.sqlalchemy.org/en/14/core/compiler.html#further-examples
class utcnow(expression.FunctionElement):
type = DateTime()
inherit_cache = True
@compiles(utcnow, 'postgresql')
def pg_utcnow(element, compiler, **kw):
return "TIMEZONE('utc', CURRENT_TIMESTAMP)"
class CreatedAtMixin:
created_at = Column(DateTime, default=datetime.utcnow())
class UpdatedAtMixin:
updated_at = Column(DateTime, default=datetime.utcnow(), onupdate=datetime.utcnow)
class TrackTimeMixin(CreatedAtMixin, UpdatedAtMixin):
pass
class SoftDeleteMixin:
deleted_at = Column(DateTime, nullable=True)