diff --git a/alembic/versions/21dc1dc045b8_.py b/alembic/versions/21dc1dc045b8_.py new file mode 100644 index 0000000..7c91fe8 --- /dev/null +++ b/alembic/versions/21dc1dc045b8_.py @@ -0,0 +1,29 @@ +"""empty message + +Revision ID: 21dc1dc045b8 +Revises: 74c576cf9560 +Create Date: 2024-10-10 20:32:12.579725 + +""" + +from typing import Sequence, Union + +from alembic import op +import sqlalchemy as sa + + +# revision identifiers, used by Alembic. +revision: str = "21dc1dc045b8" +down_revision: Union[str, None] = "74c576cf9560" +branch_labels: Union[str, Sequence[str], None] = None +depends_on: Union[str, Sequence[str], None] = None + + +def upgrade() -> None: + with op.batch_alter_table("logentry", schema=None) as batch_op: + batch_op.add_column(sa.Column("environment", sa.String(length=64), nullable=True)) + + +def downgrade() -> None: + with op.batch_alter_table("logentry", schema=None) as batch_op: + batch_op.drop_column("environment") diff --git a/app/models/entry.py b/app/models/entry.py index 5a6732e..25aeda4 100644 --- a/app/models/entry.py +++ b/app/models/entry.py @@ -23,6 +23,7 @@ class LogEntry(Base): application = Column( UUID(as_uuid=True), ForeignKey("application.id", ondelete="CASCADE"), nullable=False ) + environment = Column(String(64), nullable=True, default="prod") # type of the log entry l_type = Column(Enum(LogType), nullable=False, default=LogType.INFO) # type of the transaction diff --git a/app/routes/entry.py b/app/routes/entry.py index e7090ec..a072917 100644 --- a/app/routes/entry.py +++ b/app/routes/entry.py @@ -73,6 +73,7 @@ async def get_logs( filters: FilterValues = Depends( create_filters( created_by_id=str, + environment=str, l_type=LogType, t_type=TransactionType, application=UUID, diff --git a/app/schema/entry.py b/app/schema/entry.py index 86ce7df..a7fd04c 100644 --- a/app/schema/entry.py +++ b/app/schema/entry.py @@ -6,6 +6,7 @@ from pydantic.json_schema import SkipJsonSchema class LogIN(BaseSchemaModelIN): application: UUID + environment: str = "prod" l_type: LogType = LogType.INFO t_type: TransactionType = TransactionType.UNDEFINED diff --git a/app/test_main.py b/app/test_main.py index 4ce02e2..8fcf5cb 100644 --- a/app/test_main.py +++ b/app/test_main.py @@ -24,14 +24,25 @@ def app_context(self, name: str = "Testing"): @contextlib.contextmanager def log_examples(self): LOG_EXAMPLES = [ - {"l_type": "info", "t_type": "create", "message": "User Max Mustermann created"}, - {"l_type": "info", "t_type": "update", "message": "User Max Mustermann updated"}, + { + "l_type": "info", + "t_type": "create", + "message": "User Max Mustermann created", + "environment": "dev", + }, + { + "l_type": "info", + "t_type": "update", + "message": "User Max Mustermann updated", + "environment": "dev", + }, { "l_type": "info", "t_type": "create", "author": "auth|max_muster", "message": "User Max Mustermann created a Unit", "object_reference": "1", + "environment": "dev", }, { "l_type": "info", @@ -40,8 +51,14 @@ def log_examples(self): "message": "User Max Mustermann updated Unit 1", "object_reference": "1", "previous_object": {"name": "Unit 1"}, + "environment": "prod", + }, + { + "l_type": "warning", + "t_type": "delete", + "message": "User Max Mustermann deleted", + "environment": "prod", }, - {"l_type": "warning", "t_type": "delete", "message": "User Max Mustermann deleted"}, ] with app_context(self) as app_id: for entry in LOG_EXAMPLES: @@ -129,6 +146,7 @@ class TestAPI: assert re["t_type"] == "undefined" assert re["message"] == None assert re["author"] == "system" + assert re["environment"] == "prod" assert re["object_reference"] == None assert re["previous_object"] == None assert re["created_by_id"] == CURRENT_USER @@ -211,3 +229,13 @@ class TestAPI: re = self.c.get("/log/?author%5Bne%5D=auth|max_muster") assert re["total"] == 3 assert len(re["results"]) == 3 + + # environment + re = self.c.get("/log/?environment=dev") + assert re["total"] == 3 + assert len(re["results"]) == 3 + + # application and environment + re = self.c.get("/log/?application=" + app_id + "&environment=prod") + assert re["total"] == 2 + assert len(re["results"]) == 2