Beispiel #1
0
class utcmin(FunctionElement):
    """
    Exactly the same as utcnow(), but uses '0001-01-01 00:00' instead of now.

    Useful for datetime columns that you would like to index. We often need
    to create datetime columns that are NULL until a particular event happens,
    like an attendee checks in to an event. For those columns that we'd like
    to query (either for "IS NULL", or "IS NOT NULL"), an index isn't helpful,
    because Postgres doesn't index NULL values.

    In those cases where we'd like to query against a NULL datetime column,
    instead of using NULLable, we can use a NOT NULL datetime column, and make
    the default value utcmin(). We can consider any value in the column
    greater than '0001-01-01 00:00' to be NOT NULL.

    Instead of::

        Attendee.checkin_time != None

    We can get the benefits of indexing by doing::

        Attendee.checkin_time > utcmin.datetime

    """
    datetime = datetime(1, 1, 1, 0, 0, 0, tzinfo=pytz.UTC)
    type = UTCDateTime()
Beispiel #2
0
class utcmax(FunctionElement):
    """
    Exactly the same as utcnow(), but uses '9999-12-31 23:59' instead of now.

    See utcmin and utcnow for more details.

    """
    datetime = datetime(9999, 12, 31, 23, 59, 59, tzinfo=pytz.UTC)
    type = UTCDateTime()
Beispiel #3
0
class utcnow(FunctionElement):
    """
    We have some tables where we want to save a timestamp on each row
    indicating when the row was first created.  Normally we could do something
    like this::

        created = Column(UTCDateTime, default=lambda: datetime.now(UTC))

    Unfortunately, there are some cases where we instantiate a model and then
    don't save it until sometime later.  This happens when someone registers
    themselves and then doesn't pay until later, since we don't save them to
    the database until they've paid.  Therefore, we use this class so that we
    can set a timestamp based on when the row was inserted rather than when
    the model was instantiated::

        created = Column(UTCDateTime, server_default=utcnow())

    The pg_utcnow and sqlite_utcnow functions below define the implementation
    for postgres and sqlite, and new functions will need to be written if/when
    we decided to support other databases.
    """
    type = UTCDateTime()