Files
brov-macbook/ops/brovops/db.py
T
maksarsanjeev 18aed2beb9 Subscriptions belong to a company; BroV owns its Keychain token
- ops: subscriptions get a client (company) column (migrated in place); the brain fills it
  from «компания сага / для планеты»; /subs and «Весь план» group by company with
  per-company monthly totals; the company shows in agenda rows and the calendar.
- BroV: the API token handed over in ops-token.import is saved by BroV itself, so the
  Keychain item belongs to BroV and macOS stops asking for the password.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-10-10 03:42:41 +03:00

80 lines
3.1 KiB
Python

"""SQLite storage for BroV Ops: tasks (with subtasks and time tracking), subscriptions
with their payments, routines with their history, and a log of sent reminders."""
import sqlite3
SCHEMA = """
CREATE TABLE IF NOT EXISTS tasks (
id INTEGER PRIMARY KEY,
parent_id INTEGER REFERENCES tasks(id) ON DELETE CASCADE,
title TEXT NOT NULL,
notes TEXT NOT NULL DEFAULT '',
client TEXT NOT NULL DEFAULT '', -- saga / planet9 / home / …
status TEXT NOT NULL DEFAULT 'todo', -- todo | doing | done
due TEXT, -- YYYY-MM-DD
created_at TEXT NOT NULL DEFAULT (datetime('now')),
done_at TEXT
);
CREATE TABLE IF NOT EXISTS time_entries (
id INTEGER PRIMARY KEY,
task_id INTEGER NOT NULL REFERENCES tasks(id) ON DELETE CASCADE,
started TEXT NOT NULL, -- ISO datetime, UTC
stopped TEXT
);
CREATE TABLE IF NOT EXISTS subscriptions (
id INTEGER PRIMARY KEY,
name TEXT NOT NULL,
amount REAL NOT NULL DEFAULT 0,
currency TEXT NOT NULL DEFAULT 'RUB',
period TEXT NOT NULL DEFAULT 'month', -- day | week | month | year
every INTEGER NOT NULL DEFAULT 1, -- every N periods
next_date TEXT NOT NULL, -- YYYY-MM-DD of the next charge
remind_days INTEGER NOT NULL DEFAULT 3,
notes TEXT NOT NULL DEFAULT '',
active INTEGER NOT NULL DEFAULT 1,
client TEXT NOT NULL DEFAULT '' -- company it's paid for: saga / planet9 / home / …
);
CREATE TABLE IF NOT EXISTS payments (
id INTEGER PRIMARY KEY,
sub_id INTEGER NOT NULL REFERENCES subscriptions(id) ON DELETE CASCADE,
paid_on TEXT NOT NULL,
amount REAL NOT NULL
);
CREATE TABLE IF NOT EXISTS routines (
id INTEGER PRIMARY KEY,
title TEXT NOT NULL,
every_days INTEGER NOT NULL,
next_date TEXT NOT NULL,
notes TEXT NOT NULL DEFAULT '',
active INTEGER NOT NULL DEFAULT 1
);
CREATE TABLE IF NOT EXISTS routine_log (
id INTEGER PRIMARY KEY,
routine_id INTEGER NOT NULL REFERENCES routines(id) ON DELETE CASCADE,
done_on TEXT NOT NULL,
note TEXT NOT NULL DEFAULT ''
);
CREATE TABLE IF NOT EXISTS settings (
key TEXT PRIMARY KEY,
value TEXT NOT NULL
);
CREATE TABLE IF NOT EXISTS reminders_sent (
kind TEXT NOT NULL, -- task | sub | routine | digest
ref INTEGER NOT NULL,
day TEXT NOT NULL, -- YYYY-MM-DD the reminder was sent for
PRIMARY KEY (kind, ref, day)
);
"""
def connect(path: str) -> sqlite3.Connection:
con = sqlite3.connect(path, check_same_thread=False, isolation_level=None)
con.row_factory = sqlite3.Row
con.execute("PRAGMA foreign_keys = ON")
con.execute("PRAGMA journal_mode = WAL")
con.executescript(SCHEMA)
# Databases created before subscriptions had a company.
cols = {r["name"] for r in con.execute("PRAGMA table_info(subscriptions)")}
if "client" not in cols:
con.execute("ALTER TABLE subscriptions ADD COLUMN client TEXT NOT NULL DEFAULT ''")
return con