fix: cast date_found to TIMESTAMP in PG comparisons + migration
Browse files- backend/database.py +16 -5
backend/database.py
CHANGED
|
@@ -45,6 +45,12 @@ def _start_of_day_sql():
|
|
| 45 |
return "datetime('now', 'start of day')"
|
| 46 |
|
| 47 |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 48 |
def _fix_sql(sql: str) -> str:
|
| 49 |
if not _is_pg():
|
| 50 |
return sql
|
|
@@ -98,7 +104,7 @@ CREATE TABLE IF NOT EXISTS global_jobs (
|
|
| 98 |
board_category TEXT,
|
| 99 |
job_category TEXT DEFAULT 'other',
|
| 100 |
posted_date TEXT,
|
| 101 |
-
date_found
|
| 102 |
is_active INTEGER DEFAULT 1,
|
| 103 |
is_graduate INTEGER DEFAULT 0,
|
| 104 |
has_full_info INTEGER DEFAULT 0,
|
|
@@ -269,6 +275,11 @@ def init_db():
|
|
| 269 |
with get_db() as conn:
|
| 270 |
with conn.cursor() as cur:
|
| 271 |
cur.execute(_PG_DDL)
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
| 272 |
_seed_categories(conn)
|
| 273 |
_seed_admin_user(conn)
|
| 274 |
else:
|
|
@@ -681,7 +692,7 @@ def get_global_stats():
|
|
| 681 |
|
| 682 |
recent = _exec(
|
| 683 |
conn,
|
| 684 |
-
f"SELECT COUNT(*) as cnt FROM global_jobs WHERE is_active = 1 AND date_found >= {_now_offset_sql('-1 day')}",
|
| 685 |
).fetchone()
|
| 686 |
recent_cnt = recent["cnt"] if recent else 0
|
| 687 |
|
|
@@ -690,13 +701,13 @@ def get_global_stats():
|
|
| 690 |
|
| 691 |
today = _exec(
|
| 692 |
conn,
|
| 693 |
-
f"SELECT COUNT(*) as cnt FROM global_jobs WHERE is_active = 1 AND date_found >= {_start_of_day_sql()}",
|
| 694 |
).fetchone()
|
| 695 |
today_cnt = today["cnt"] if today else 0
|
| 696 |
|
| 697 |
yesterday = _exec(
|
| 698 |
conn,
|
| 699 |
-
f"SELECT COUNT(*) as cnt FROM global_jobs WHERE is_active = 1 AND date_found >= {_now_offset_sql('-1 day')}",
|
| 700 |
).fetchone()
|
| 701 |
yesterday_cnt = yesterday["cnt"] if yesterday else 0
|
| 702 |
|
|
@@ -717,7 +728,7 @@ def deactivate_old_jobs(days: int = 7):
|
|
| 717 |
with get_db() as conn:
|
| 718 |
c = _exec(
|
| 719 |
conn,
|
| 720 |
-
f"UPDATE global_jobs SET is_active = 0 WHERE date_found < {_now_offset_param(days)}",
|
| 721 |
)
|
| 722 |
removed = c.rowcount
|
| 723 |
_exec(
|
|
|
|
| 45 |
return "datetime('now', 'start of day')"
|
| 46 |
|
| 47 |
|
| 48 |
+
def _date_col(col: str):
|
| 49 |
+
if _is_pg():
|
| 50 |
+
return f"{col}::timestamp"
|
| 51 |
+
return col
|
| 52 |
+
|
| 53 |
+
|
| 54 |
def _fix_sql(sql: str) -> str:
|
| 55 |
if not _is_pg():
|
| 56 |
return sql
|
|
|
|
| 104 |
board_category TEXT,
|
| 105 |
job_category TEXT DEFAULT 'other',
|
| 106 |
posted_date TEXT,
|
| 107 |
+
date_found TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
|
| 108 |
is_active INTEGER DEFAULT 1,
|
| 109 |
is_graduate INTEGER DEFAULT 0,
|
| 110 |
has_full_info INTEGER DEFAULT 0,
|
|
|
|
| 275 |
with get_db() as conn:
|
| 276 |
with conn.cursor() as cur:
|
| 277 |
cur.execute(_PG_DDL)
|
| 278 |
+
try:
|
| 279 |
+
with conn.cursor() as cur:
|
| 280 |
+
cur.execute("ALTER TABLE global_jobs ALTER COLUMN date_found TYPE TIMESTAMP USING date_found::timestamp")
|
| 281 |
+
except Exception:
|
| 282 |
+
pass
|
| 283 |
_seed_categories(conn)
|
| 284 |
_seed_admin_user(conn)
|
| 285 |
else:
|
|
|
|
| 692 |
|
| 693 |
recent = _exec(
|
| 694 |
conn,
|
| 695 |
+
f"SELECT COUNT(*) as cnt FROM global_jobs WHERE is_active = 1 AND {_date_col('date_found')} >= {_now_offset_sql('-1 day')}",
|
| 696 |
).fetchone()
|
| 697 |
recent_cnt = recent["cnt"] if recent else 0
|
| 698 |
|
|
|
|
| 701 |
|
| 702 |
today = _exec(
|
| 703 |
conn,
|
| 704 |
+
f"SELECT COUNT(*) as cnt FROM global_jobs WHERE is_active = 1 AND {_date_col('date_found')} >= {_start_of_day_sql()}",
|
| 705 |
).fetchone()
|
| 706 |
today_cnt = today["cnt"] if today else 0
|
| 707 |
|
| 708 |
yesterday = _exec(
|
| 709 |
conn,
|
| 710 |
+
f"SELECT COUNT(*) as cnt FROM global_jobs WHERE is_active = 1 AND {_date_col('date_found')} >= {_now_offset_sql('-1 day')}",
|
| 711 |
).fetchone()
|
| 712 |
yesterday_cnt = yesterday["cnt"] if yesterday else 0
|
| 713 |
|
|
|
|
| 728 |
with get_db() as conn:
|
| 729 |
c = _exec(
|
| 730 |
conn,
|
| 731 |
+
f"UPDATE global_jobs SET is_active = 0 WHERE {_date_col('date_found')} < {_now_offset_param(days)}",
|
| 732 |
)
|
| 733 |
removed = c.rowcount
|
| 734 |
_exec(
|