Skip to content

Commit 8bfc203

Browse files
committed
feat(models): add ServiceLogs table (#248)
Closes reanahub/reana#880 Closes reanahub/reana-workflow-controller#627
1 parent f0da4c1 commit 8bfc203

File tree

2 files changed

+75
-2
lines changed

2 files changed

+75
-2
lines changed
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""Add service_logs table.
2+
3+
Revision ID: 3da4dd5d0b75
4+
Revises: 3d0994430da7
5+
Create Date: 2025-06-13 14:22:24.633881
6+
7+
"""
8+
9+
import sqlalchemy_utils
10+
import sqlalchemy as sa
11+
from alembic import op
12+
from sqlalchemy.dialects import postgresql
13+
14+
# revision identifiers, used by Alembic.
15+
revision = "3da4dd5d0b75"
16+
down_revision = "3d0994430da7"
17+
branch_labels = None
18+
depends_on = None
19+
20+
21+
def upgrade():
22+
"""Upgrade to 3da4dd5d0b75 revision."""
23+
op.create_table(
24+
"service_logs",
25+
sa.Column("id", sqlalchemy_utils.types.uuid.UUIDType(), nullable=False),
26+
sa.Column("service_id", sqlalchemy_utils.types.uuid.UUIDType(), nullable=False),
27+
sa.Column(
28+
"event_time",
29+
sa.DateTime(timezone=True),
30+
server_default=sa.text("TIMEZONE('utc', CURRENT_TIMESTAMP)"),
31+
nullable=False,
32+
),
33+
sa.Column("log", postgresql.JSONB(astext_type=sa.Text()), nullable=False),
34+
sa.ForeignKeyConstraint(
35+
["service_id"],
36+
["__reana.service.id_"],
37+
name=op.f("fk_service_logs_service_id_service"),
38+
ondelete="CASCADE",
39+
),
40+
sa.PrimaryKeyConstraint("id", name=op.f("pk_service_logs")),
41+
schema="__reana",
42+
)
43+
44+
45+
def downgrade():
46+
"""Downgrade to 3d0994430da7 revision."""
47+
op.drop_table("service_logs", schema="__reana")

reana_db/models.py

Lines changed: 28 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
# -*- coding: utf-8 -*-
22
#
33
# This file is part of REANA.
4-
# Copyright (C) 2018, 2019, 2020, 2021, 2022, 2023, 2024 CERN.
4+
# Copyright (C) 2018, 2019, 2020, 2021, 2022, 2023, 2024, 2025 CERN.
55
#
66
# REANA is free software; you can redistribute it and/or modify it
77
# under the terms of the MIT License; see LICENSE file for more details.
@@ -58,14 +58,15 @@
5858
event,
5959
func,
6060
or_,
61+
text,
6162
)
6263
from sqlalchemy.ext.declarative import declarative_base
6364
from sqlalchemy.ext.hybrid import hybrid_property
6465
from sqlalchemy.orm import relationship, backref
6566
from sqlalchemy_utils import EncryptedType, JSONType, UUIDType
6667
from sqlalchemy_utils.models import Timestamp
6768
from sqlalchemy_utils.types.encrypted.encrypted_type import AesEngine
68-
from sqlalchemy.dialects.postgresql import ARRAY
69+
from sqlalchemy.dialects.postgresql import ARRAY, JSONB
6970

7071

7172
convention = {
@@ -503,6 +504,26 @@ class ServiceType(enum.Enum):
503504
dask = 0
504505

505506

507+
class ServiceLog(Base):
508+
"""ServiceLog table."""
509+
510+
__tablename__ = "service_logs"
511+
__table_args__ = {"schema": "__reana"}
512+
513+
id = Column(UUIDType, primary_key=True, default=generate_uuid)
514+
service_id = Column(
515+
UUIDType, ForeignKey("__reana.service.id_", ondelete="CASCADE"), nullable=False
516+
)
517+
event_time = Column(
518+
DateTime(timezone=True),
519+
server_default=text("TIMEZONE('utc', CURRENT_TIMESTAMP)"),
520+
nullable=False,
521+
)
522+
log = Column(JSONB, nullable=False)
523+
524+
service = relationship("Service", back_populates="logs")
525+
526+
506527
class Service(Base):
507528
"""Service table."""
508529

@@ -516,6 +537,11 @@ class Service(Base):
516537
Enum(ServiceType),
517538
nullable=False,
518539
)
540+
logs = relationship(
541+
"ServiceLog",
542+
back_populates="service",
543+
lazy="dynamic",
544+
)
519545

520546
__table_args__ = (
521547
UniqueConstraint("name", "uri"),

0 commit comments

Comments
 (0)