feat(db): record classify timestamp

This commit is contained in:
283375 2023-09-14 18:41:36 +08:00
parent 08e97ab03e
commit 67a794f4f1
Signed by: 283375
SSH Key Fingerprint: SHA256:UcX0qg6ZOSDOeieKPGokA5h7soykG61nz2uxuQgVLSk

View File

@ -1,4 +1,6 @@
from sqlalchemy import CHAR, TEXT
from datetime import datetime
from sqlalchemy import CHAR, TEXT, TIMESTAMP, text, event, DDL
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column
@ -27,3 +29,23 @@ class ClassifiedSample(ProjectBase):
"sample_numpy_md5", CHAR(32), primary_key=True, unique=True
)
tag: Mapped[str] = mapped_column(TEXT(), primary_key=True)
timestamp: Mapped[datetime] = mapped_column(
TIMESTAMP(), server_default=text("CURRENT_TIMESTAMP")
)
event.listen(
ClassifiedSample.__table__,
"after_create",
DDL(
"""
CREATE TRIGGER IF NOT EXISTS update_classified_samples_timestamp
UPDATE OF sample_numpy_md5, tag
ON classified_samples
BEGIN
UPDATE classified_samples
SET timestamp = CURRENT_TIMESTAMP
WHERE sample_numpy_md5 = old.sample_numpy_md5;
END;"""
),
)