Files
nanobot-runtime/scripts/remind_add.py
2026-07-22 12:32:23 +02:00

72 lines
2.2 KiB
Python

#!/usr/bin/env python3
"""One-off helper to add a reminder directly to the SQLite DB."""
import sqlite3
import sys
from datetime import datetime, timezone
from pathlib import Path
WORKSPACE = Path(__file__).resolve().parent.parent
DB_PATH = WORKSPACE / "db" / "reminders.sqlite"
def main() -> int:
if len(sys.argv) < 3:
print("usage: remind_add.py <at_iso> <text>", file=sys.stderr)
return 1
at_str = sys.argv[1]
text = sys.argv[2]
try:
datetime.fromisoformat(at_str)
except ValueError as exc:
print(f"invalid --at datetime: {exc}", file=sys.stderr)
return 1
DB_PATH.parent.mkdir(parents=True, exist_ok=True)
conn = sqlite3.connect(DB_PATH)
conn.row_factory = sqlite3.Row
conn.executescript(
"""
PRAGMA journal_mode = WAL;
PRAGMA foreign_keys = ON;
CREATE TABLE IF NOT EXISTS reminders (
id INTEGER PRIMARY KEY AUTOINCREMENT,
text TEXT NOT NULL CHECK(text <> ''),
enabled INTEGER NOT NULL DEFAULT 1 CHECK(enabled IN (0, 1)),
timezone TEXT NOT NULL DEFAULT 'Europe/Prague',
created_at TEXT NOT NULL,
updated_at TEXT NOT NULL,
deleted_at TEXT
);
CREATE TABLE IF NOT EXISTS schedule_at (
id INTEGER PRIMARY KEY AUTOINCREMENT,
reminder_id INTEGER NOT NULL REFERENCES reminders(id) ON DELETE CASCADE,
at_datetime TEXT NOT NULL
);
CREATE INDEX IF NOT EXISTS idx_reminders_text ON reminders(text);
CREATE INDEX IF NOT EXISTS idx_at_datetime ON schedule_at(reminder_id, at_datetime);
"""
)
now = datetime.now(timezone.utc).isoformat(timespec="seconds")
cur = conn.execute(
"INSERT INTO reminders (text, enabled, timezone, created_at, updated_at) VALUES (?, 1, 'Europe/Prague', ?, ?)",
(text, now, now),
)
reminder_id = cur.lastrowid
conn.execute(
"INSERT INTO schedule_at (reminder_id, at_datetime) VALUES (?, ?)",
(reminder_id, at_str),
)
conn.commit()
conn.close()
print(f"added reminder #{reminder_id} at {at_str}: {text}")
return 0
if __name__ == "__main__":
sys.exit(main())