Skip to content

Commit

Permalink
🗃️(mork) add created_at and updated_at fields to all models
Browse files Browse the repository at this point in the history
Adding `created_at` and `updated_at` fields to monitor entries in all database
tables.
  • Loading branch information
wilbrdt committed Nov 12, 2024
1 parent 0372fa8 commit 05c8224
Show file tree
Hide file tree
Showing 8 changed files with 89 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/app/mork/factories/tasks.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
"""Factory classes for generating fake data for testing."""
"""Factory classes for tasks."""

import factory

Expand Down
2 changes: 1 addition & 1 deletion src/app/mork/migrations/env.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
# Interpret the config file for Python logging.
# This line sets up loggers basically.
if config.config_file_name is not None:
fileConfig(config.config_file_name)
fileConfig(config.config_file_name, disable_existing_loggers=False)

# Set Database URL from Morks configuration
config.set_main_option("sqlalchemy.url", str(settings.DB_URL))
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
"""Add timestamp fields to email tables
Revision ID: 976e462b45b6
Revises: 608d075c6e99
Create Date: 2024-11-05 16:27:23.065141
"""

from typing import Sequence, Union

from alembic import op
import sqlalchemy as sa


# revision identifiers, used by Alembic.
revision: str = "976e462b45b6"
down_revision: Union[str, None] = "608d075c6e99"
branch_labels: Union[str, Sequence[str], None] = None
depends_on: Union[str, Sequence[str], None] = None


def upgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.add_column(
"email_status",
sa.Column(
"created_at", sa.DateTime(), server_default=sa.text("now()"), nullable=False
),
)
op.add_column(
"email_status", sa.Column("updated_at", sa.DateTime(), nullable=False)
)
# ### end Alembic commands ###


def downgrade() -> None:
# ### commands auto generated by Alembic - please adjust! ###
op.drop_column("email_status", "updated_at")
op.drop_column("email_status", "created_at")
# ### end Alembic commands ###
13 changes: 12 additions & 1 deletion src/app/mork/models/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
"""Mork models."""

from sqlalchemy.orm import DeclarativeBase
from datetime import datetime

from sqlalchemy import DateTime, func
from sqlalchemy.orm import DeclarativeBase, Mapped, mapped_column


class Base(DeclarativeBase):
Expand All @@ -15,3 +18,11 @@ def safe_dict(self):
for c in self.__table__.columns
if c.name not in self.filtered_attrs
}

created_at: Mapped[datetime] = mapped_column(
DateTime, server_default=func.now(), nullable=False
)

updated_at: Mapped[datetime] = mapped_column(
DateTime, default=func.now(), onupdate=func.now(), nullable=False
)
10 changes: 4 additions & 6 deletions src/app/mork/models/tasks.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
"""Mork tasks models."""

import datetime
import uuid
from datetime import datetime
from uuid import uuid4

from sqlalchemy import DateTime, String
from sqlalchemy.dialects.postgresql import UUID
Expand All @@ -17,8 +17,6 @@ class EmailStatus(Base):

filtered_attrs = ["email"]

id: Mapped[int] = mapped_column(
UUID(as_uuid=True), primary_key=True, default=uuid.uuid4
)
id: Mapped[int] = mapped_column(UUID(as_uuid=True), primary_key=True, default=uuid4)
email: Mapped[str] = mapped_column(String(254), unique=True)
sent_date: Mapped[datetime.datetime] = mapped_column(DateTime)
sent_date: Mapped[datetime] = mapped_column(DateTime)
8 changes: 7 additions & 1 deletion src/app/mork/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,12 @@
from .fixtures.app import http_client
from .fixtures.asynchronous import anyio_backend
from .fixtures.auth import auth_headers
from .fixtures.db import db_engine, db_session, edx_mongo_db, edx_mysql_db
from .fixtures.db import (
db_engine,
db_session,
edx_mongo_db,
edx_mysql_db,
override_db_test_session,
)

TEST_STATIC_PATH = Path(__file__).parent / "static"
21 changes: 21 additions & 0 deletions src/app/mork/tests/fixtures/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,15 @@

import mongomock
import pytest
from alembic import command
from alembic.config import Config
from mongoengine import connect, disconnect
from sqlalchemy import create_engine
from sqlalchemy.orm import Session as SASession

from mork.api.v1 import app as v1
from mork.conf import settings
from mork.db import get_session
from mork.edx.mysql.database import OpenEdxMySQLDB
from mork.edx.mysql.factories.base import Session, engine
from mork.edx.mysql.models.base import Base as EdxBase
Expand Down Expand Up @@ -43,6 +47,11 @@ def db_engine():
# Create database and tables
Base.metadata.create_all(engine)

# Pretend to have all migrations applied
alembic_cfg = Config(settings.ALEMBIC_CFG_PATH)
alembic_cfg.set_main_option("sqlalchemy.url", settings.TEST_DB_URL)
command.stamp(alembic_cfg, "head")

yield engine

Base.metadata.drop_all(engine)
Expand All @@ -69,3 +78,15 @@ def db_session(db_engine):
session.close()
transaction.rollback()
connection.close()


@pytest.fixture(autouse=True)
def override_db_test_session(db_session):
"""Use test database along with a test session by default."""

def get_session_override():
return db_session

v1.dependency_overrides[get_session] = get_session_override

yield
3 changes: 3 additions & 0 deletions src/app/mork/tests/models/test_tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,9 +5,12 @@

def test_models_user_safe_dict(db_session):
"""Test the `safe_dict` method for the EmailStatus model."""
EmailStatusFactory._meta.sqlalchemy_session = db_session
email_status = EmailStatusFactory()

assert email_status.safe_dict() == {
"id": email_status.id,
"sent_date": email_status.sent_date,
"created_at": email_status.created_at,
"updated_at": email_status.updated_at,
}

0 comments on commit 05c8224

Please sign in to comment.