Skip to content

Commit a1dbdb3

Browse files
committed
bigquery: account for datetime/int types properly
BigQuery returns a `datetime`, which we then compare to the `int` returned from the Phab DB. Convert the `datetime` to an `int` timestamp before returning.
1 parent 0bb63ae commit a1dbdb3

File tree

1 file changed

+19
-9
lines changed

1 file changed

+19
-9
lines changed

stats.py

Lines changed: 19 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
import sys
1212
import time
1313
from dataclasses import dataclass
14-
from datetime import datetime
14+
from datetime import datetime, timezone
1515
from enum import IntEnum
1616
from typing import Optional, Any
1717

@@ -413,7 +413,7 @@ def get_comments(
413413
return comments
414414

415415

416-
def get_last_run_timestamp(bq_client: bigquery.Client) -> Optional[int]:
416+
def get_last_run_timestamp(bq_client: bigquery.Client) -> Optional[datetime]:
417417
"""Get the timestamp of the most recently added entry in BigQuery.
418418
419419
See https://github.com/googleapis/python-bigquery/blob/main/samples/query_script.py
@@ -470,17 +470,27 @@ def get_time_queries(now: datetime, bq_client: bigquery.Client) -> list:
470470
Dont take the revisions created before the last run
471471
"""
472472
queries = [
473-
DiffDb.Revision.dateCreated < now.timestamp(),
474-
DiffDb.Revision.dateModified < now.timestamp(),
473+
or_(
474+
DiffDb.Revision.dateCreated < now.timestamp(),
475+
DiffDb.Revision.dateModified < now.timestamp(),
476+
),
475477
]
476-
last_run_timestamp = get_last_run_timestamp(bq_client)
478+
last_run_datetime = get_last_run_timestamp(bq_client)
479+
480+
if last_run_datetime:
481+
last_run_datetime = last_run_datetime.replace(tzinfo=timezone.utc)
482+
last_run_timestamp = last_run_datetime.timestamp()
483+
484+
logging.info(
485+
f"Using {last_run_datetime} as the last run date ({last_run_timestamp})."
486+
)
477487

478-
if last_run_timestamp:
479-
logging.info(f"Using {last_run_timestamp} as the last run timestamp.")
480488
queries.extend(
481489
[
482-
DiffDb.Revision.dateCreated > last_run_timestamp,
483-
DiffDb.Revision.dateModified > last_run_timestamp,
490+
or_(
491+
DiffDb.Revision.dateCreated > last_run_timestamp,
492+
DiffDb.Revision.dateModified > last_run_timestamp,
493+
),
484494
]
485495
)
486496
else:

0 commit comments

Comments
 (0)