Fix CI timezone and fresh settings initialization
This commit is contained in:
@@ -1,14 +1,32 @@
|
||||
from pathlib import Path
|
||||
from zoneinfo import ZoneInfo, ZoneInfoNotFoundError
|
||||
|
||||
from .db import db, utc_now
|
||||
|
||||
|
||||
def _normalize_timezone(value: str) -> str:
|
||||
timezone_name = value.strip()
|
||||
if not timezone_name:
|
||||
return "UTC"
|
||||
if timezone_name.startswith("/"):
|
||||
marker = "/zoneinfo/"
|
||||
if marker in timezone_name:
|
||||
timezone_name = timezone_name.split(marker, 1)[1]
|
||||
else:
|
||||
timezone_name = timezone_name.lstrip("/")
|
||||
try:
|
||||
ZoneInfo(timezone_name)
|
||||
except (ValueError, ZoneInfoNotFoundError):
|
||||
return "UTC"
|
||||
return timezone_name
|
||||
|
||||
|
||||
def _system_timezone() -> str:
|
||||
timezone_file = Path("/etc/timezone")
|
||||
if timezone_file.exists():
|
||||
value = timezone_file.read_text().strip()
|
||||
if value:
|
||||
return value
|
||||
return _normalize_timezone(value)
|
||||
return "UTC"
|
||||
|
||||
|
||||
@@ -147,3 +165,17 @@ def run_migrations() -> None:
|
||||
"INSERT INTO schema_migrations(version, applied_at) VALUES (?, ?)",
|
||||
(3, now),
|
||||
)
|
||||
if 4 not in applied:
|
||||
now = utc_now()
|
||||
row = conn.execute("SELECT value FROM settings WHERE key = 'timezone'").fetchone()
|
||||
if row:
|
||||
normalized_timezone = _normalize_timezone(row["value"])
|
||||
if normalized_timezone != row["value"]:
|
||||
conn.execute(
|
||||
"UPDATE settings SET value = ?, updated_at = ? WHERE key = 'timezone'",
|
||||
(normalized_timezone, now),
|
||||
)
|
||||
conn.execute(
|
||||
"INSERT INTO schema_migrations(version, applied_at) VALUES (?, ?)",
|
||||
(4, now),
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user