Skip to content

Commit

Permalink
Add created to group membership model
Browse files Browse the repository at this point in the history
Add a `created` column to `models.GroupMembership`.

While we're doing this, might as well add `updated` as well.

There's a desire for this column to be nullable so that pre-existing
memberships can have `NULL` in this column, see:
https://hypothes-is.slack.com/archives/C4K6M7P5E/p1733330444569829

For that reason we can't use `h.db.mixins.Timestamps` because that mixin
adds *non-nullable* `created` and `updated` columns.

For the same reason we can't use `server_default` as it would set the
pre-existing rows to the current time.

I don't think it really matters but if we did change the
`GroupMembership` model to inherit from the `Timestamps` mixin that
would add the `created` and `updated` columns to the _start_ of the
table, whereas in the staging and production DBs a migration is going to
add the new columns to the _end_ of the table. Meaning that DBs created
from the models (e.g. in dev and on CI) would actually have the columns
in a different order than staging and production. Not sure if there
would be a way to fix the column order while using `mixins.Timestamps`.
Moot point anyway since we need them to be nullable.
  • Loading branch information
seanh committed Dec 5, 2024
1 parent 45f15ae commit 032a781
Showing 1 changed file with 7 additions and 0 deletions.
7 changes: 7 additions & 0 deletions h/models/group.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
import datetime
import enum
import re
from collections import namedtuple
Expand Down Expand Up @@ -78,6 +79,12 @@ class GroupMembership(Base):
nullable=False,
)

created = sa.Column(sa.DateTime, default=datetime.datetime.utcnow)

updated = sa.Column(
sa.DateTime, default=datetime.datetime.utcnow, onupdate=datetime.datetime.utcnow
)

def __repr__(self):
return helpers.repr_(
self,
Expand Down

0 comments on commit 032a781

Please sign in to comment.