Skip to content

Commit b59fffd

Browse files
committed
fix: site users couldn't have NULL log in time
1 parent 223f1e5 commit b59fffd

File tree

3 files changed

+52
-7
lines changed

3 files changed

+52
-7
lines changed
Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
"""site_user_timestamps_nullable
2+
3+
Revision ID: 0a2c458d1ddd
4+
Revises: a5c42bcdda5c
5+
Create Date: 2025-09-28 20:52:02.486734
6+
7+
"""
8+
from collections.abc import Sequence
9+
10+
from sqlalchemy.dialects import postgresql
11+
12+
from alembic import op
13+
14+
# revision identifiers, used by Alembic.
15+
revision: str = "0a2c458d1ddd"
16+
down_revision: str | None = "a5c42bcdda5c"
17+
branch_labels: str | Sequence[str] | None = None
18+
depends_on: str | Sequence[str] | None = None
19+
20+
21+
def upgrade() -> None:
22+
# ### commands auto generated by Alembic - please adjust! ###
23+
op.alter_column("site_user", "first_logged_in",
24+
existing_type=postgresql.TIMESTAMP(),
25+
nullable=True)
26+
op.alter_column("site_user", "last_logged_in",
27+
existing_type=postgresql.TIMESTAMP(),
28+
nullable=True)
29+
# ### end Alembic commands ###
30+
31+
32+
def downgrade() -> None:
33+
# ### commands auto generated by Alembic - please adjust! ###
34+
op.alter_column("site_user", "last_logged_in",
35+
existing_type=postgresql.TIMESTAMP(),
36+
nullable=False)
37+
op.alter_column("site_user", "first_logged_in",
38+
existing_type=postgresql.TIMESTAMP(),
39+
nullable=False)
40+
# ### end Alembic commands ###

src/auth/tables.py

Lines changed: 10 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,16 +37,21 @@ class SiteUser(Base):
3737
)
3838

3939
# first and last time logged into the CSSS API
40-
first_logged_in: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.now())
41-
last_logged_in: Mapped[datetime] = mapped_column(DateTime, nullable=False, server_default=func.now())
40+
first_logged_in: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
41+
last_logged_in: Mapped[datetime | None] = mapped_column(DateTime, nullable=True)
4242

4343
# optional user information for display purposes
4444
profile_picture_url: Mapped[str | None] = mapped_column(Text, nullable=True)
4545

4646
def serialize(self) -> dict[str, str | int | bool | None]:
47-
return {
47+
48+
res = {
4849
"computing_id": self.computing_id,
49-
"first_logged_in": self.first_logged_in.isoformat(),
50-
"last_logged_in": self.last_logged_in.isoformat(),
5150
"profile_picture_url": self.profile_picture_url
5251
}
52+
if self.first_logged_in is not None:
53+
res["first_logged_in"] = self.first_logged_in.isoformat()
54+
if self.last_logged_in is not None:
55+
res["last_logged_in"] = self.last_logged_in.isoformat()
56+
57+
return res

src/officers/crud.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -161,8 +161,8 @@ async def create_new_officer_info(
161161
# if computing_id has not been created as a site_user yet, add them
162162
db_session.add(auth.tables.SiteUser(
163163
computing_id=new_officer_info.computing_id,
164-
first_logged_in=datetime.now(),
165-
last_logged_in=datetime.now()
164+
first_logged_in=None,
165+
last_logged_in=None
166166
))
167167

168168
existing_officer_info = await db_session.scalar(

0 commit comments

Comments
 (0)