Storage: roles, companies, subscriptions, applications, agents
roledesk/storage/schema.py creates the tables at v1 with WAL and the versioned-migration machinery from newsdesk; store.py adds upsert_role (unpacks raw into gate columns, first_seen on insert, last_seen every time), roles(), mark_closed, companies and subscriptions CRUD, and save_application writing an append-only application_events row on every status entered. Key helpers company_key, title_key, place_key live in models.py. docs/decisions.md records the key decisions. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
1 parent
36dbcbb29e
commit
ac5eca874a
5 files changed
+949
-7
No files matched your search
+150
-5
@@ -19,7 +19,142 @@ from collections.abc import Callable
|
||||
log = logging.getLogger(__name__)
|
||||
|
||||
SCHEMA = """
|
||||
-- The string map a desk plugin is handed as `self.state`. One namespace shared
|
||||
-- One posting on one board. `uid` is sha1(source, source_id), the identity desk
|
||||
-- gives every Item. The gate fields are real columns, unpacked from the item's
|
||||
-- raw on upsert or filled later by extract: raw is transport, the columns are
|
||||
-- what gets queried.
|
||||
CREATE TABLE IF NOT EXISTS roles (
|
||||
uid TEXT PRIMARY KEY,
|
||||
source TEXT NOT NULL,
|
||||
source_id TEXT NOT NULL,
|
||||
subscription_id INTEGER REFERENCES subscriptions(id) ON DELETE SET NULL,
|
||||
-- The cluster key is (company_key, title_key, place_key). No foreign key
|
||||
-- to companies: a role may arrive before anyone has made a company row.
|
||||
company_key TEXT NOT NULL DEFAULT '',
|
||||
company TEXT NOT NULL DEFAULT '',
|
||||
title TEXT NOT NULL DEFAULT '',
|
||||
title_key TEXT NOT NULL DEFAULT '',
|
||||
-- Geography. place and not location, which is where an Item's original
|
||||
-- lives. remote and country are separate so a gate on remote can also
|
||||
-- require the country: "UK | Remote" is not remote from Berlin.
|
||||
place TEXT NOT NULL DEFAULT '',
|
||||
place_key TEXT NOT NULL DEFAULT '',
|
||||
country TEXT NOT NULL DEFAULT '',
|
||||
remote TEXT NOT NULL DEFAULT ''
|
||||
CHECK (remote IN ('', 'remote', 'hybrid', 'onsite')),
|
||||
salary_min INTEGER,
|
||||
salary_max INTEGER,
|
||||
currency TEXT NOT NULL DEFAULT '',
|
||||
seniority TEXT NOT NULL DEFAULT '',
|
||||
function TEXT NOT NULL DEFAULT '',
|
||||
description TEXT NOT NULL DEFAULT '', -- the posting's body text
|
||||
feed_text TEXT NOT NULL DEFAULT '', -- the teaser a feed handed over
|
||||
original_url TEXT NOT NULL DEFAULT '', -- the posting page
|
||||
published_at TEXT NOT NULL, -- what the source claims, UTC
|
||||
-- What storing postings buys. first_seen is the only honest posted date,
|
||||
-- since Greenhouse resets updated_at on every edit. closed_at is set when a
|
||||
-- board fetched in full no longer lists the role, and cleared if it returns.
|
||||
first_seen TEXT NOT NULL,
|
||||
last_seen TEXT NOT NULL,
|
||||
closed_at TEXT,
|
||||
raw TEXT NOT NULL DEFAULT '{}',
|
||||
extracted_at TEXT, -- when extract last filled columns
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
|
||||
-- A company as a target, whether or not any board of it is read. key is
|
||||
-- models.company_key(name), the slug roles.company_key holds.
|
||||
CREATE TABLE IF NOT EXISTS companies (
|
||||
key TEXT PRIMARY KEY,
|
||||
name TEXT NOT NULL,
|
||||
domain TEXT NOT NULL DEFAULT '',
|
||||
-- -1 mute, 0 neutral, 1 interested, 2 favourite.
|
||||
affinity INTEGER NOT NULL DEFAULT 0 CHECK (affinity BETWEEN -1 AND 2),
|
||||
status TEXT NOT NULL DEFAULT 'researching',
|
||||
careers_url TEXT NOT NULL DEFAULT '',
|
||||
notes TEXT NOT NULL DEFAULT '',
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
|
||||
-- The targets desk's runner hands to plugins. newsdesk's shape, with its two
|
||||
-- labels named company and board from the first migration.
|
||||
CREATE TABLE IF NOT EXISTS subscriptions (
|
||||
id INTEGER PRIMARY KEY,
|
||||
source TEXT NOT NULL, -- plugin name
|
||||
target TEXT NOT NULL, -- e.g. greenhouse:acme
|
||||
label TEXT NOT NULL, -- what Item.subscription names
|
||||
company TEXT NOT NULL DEFAULT '',
|
||||
board TEXT NOT NULL DEFAULT '',
|
||||
params TEXT NOT NULL DEFAULT '{}', -- what a query target asks with
|
||||
subscribed INTEGER NOT NULL DEFAULT 1,
|
||||
fetch INTEGER NOT NULL DEFAULT 1,
|
||||
show INTEGER NOT NULL DEFAULT 1,
|
||||
added_at TEXT NOT NULL,
|
||||
target_id TEXT NOT NULL DEFAULT '', -- the plugin's durable name for it
|
||||
UNIQUE (source, target),
|
||||
UNIQUE (source, label)
|
||||
);
|
||||
|
||||
-- The tracker, in jobhunt-hq's shape (MIT). role_uid is NULL for a role that
|
||||
-- was never posted, so an unsolicited approach is tracked like any other.
|
||||
CREATE TABLE IF NOT EXISTS applications (
|
||||
id TEXT PRIMARY KEY,
|
||||
role_uid TEXT REFERENCES roles(uid) ON DELETE SET NULL,
|
||||
company_key TEXT NOT NULL DEFAULT '',
|
||||
role_title TEXT NOT NULL DEFAULT '',
|
||||
url TEXT NOT NULL DEFAULT '',
|
||||
status TEXT NOT NULL DEFAULT 'saved'
|
||||
CHECK (status IN ('saved', 'applied', 'screen', 'technical',
|
||||
'onsite', 'offer', 'rejected', 'withdrawn')),
|
||||
source TEXT NOT NULL DEFAULT '',
|
||||
applied_at TEXT,
|
||||
next_action TEXT NOT NULL DEFAULT '',
|
||||
next_date TEXT,
|
||||
contact TEXT NOT NULL DEFAULT '',
|
||||
notes TEXT NOT NULL DEFAULT '',
|
||||
created_at TEXT NOT NULL,
|
||||
updated_at TEXT NOT NULL
|
||||
);
|
||||
|
||||
-- One row per status an application entered, written by the store and never
|
||||
-- updated: what days-to-first-screen and conversion by source are read from.
|
||||
CREATE TABLE IF NOT EXISTS application_events (
|
||||
id INTEGER PRIMARY KEY,
|
||||
application_id TEXT NOT NULL REFERENCES applications(id) ON DELETE CASCADE,
|
||||
status TEXT NOT NULL,
|
||||
at TEXT NOT NULL
|
||||
);
|
||||
|
||||
CREATE TRIGGER IF NOT EXISTS application_events_append_only
|
||||
BEFORE UPDATE ON application_events
|
||||
BEGIN
|
||||
SELECT RAISE(ABORT, 'application_events is append-only');
|
||||
END;
|
||||
|
||||
-- A search agent: a named saved search with its own intent text, gates and
|
||||
-- weights. gates is a list of {field, values, mode: require|prefer, weight}.
|
||||
CREATE TABLE IF NOT EXISTS agents (
|
||||
id INTEGER PRIMARY KEY,
|
||||
name TEXT NOT NULL UNIQUE,
|
||||
intent TEXT NOT NULL DEFAULT '',
|
||||
gates TEXT NOT NULL DEFAULT '[]',
|
||||
weights TEXT NOT NULL DEFAULT '{}',
|
||||
enabled INTEGER NOT NULL DEFAULT 1,
|
||||
created_at TEXT NOT NULL
|
||||
);
|
||||
|
||||
-- A role is scored per agent. A gated role keeps its row, with the reason.
|
||||
CREATE TABLE IF NOT EXISTS scores (
|
||||
uid TEXT NOT NULL REFERENCES roles(uid) ON DELETE CASCADE,
|
||||
agent_id INTEGER NOT NULL REFERENCES agents(id) ON DELETE CASCADE,
|
||||
score REAL NOT NULL DEFAULT 0,
|
||||
gated_reason TEXT NOT NULL DEFAULT '',
|
||||
scored_at TEXT NOT NULL,
|
||||
PRIMARY KEY (uid, agent_id)
|
||||
);
|
||||
|
||||
-- The string map a desk plugin is handed as self.state. One namespace shared
|
||||
-- by every plugin, so a plugin prefixes its keys.
|
||||
CREATE TABLE IF NOT EXISTS state (
|
||||
key TEXT PRIMARY KEY,
|
||||
@@ -30,18 +165,27 @@ CREATE TABLE IF NOT EXISTS state (
|
||||
#: Applied after `SCHEMA` or the migrations, every open. An index may name a
|
||||
#: column only a migration adds, which is why these are kept apart.
|
||||
INDEXES = """
|
||||
CREATE INDEX IF NOT EXISTS idx_roles_company ON roles(company_key);
|
||||
CREATE INDEX IF NOT EXISTS idx_roles_place ON roles(place_key);
|
||||
CREATE INDEX IF NOT EXISTS idx_roles_first_seen ON roles(first_seen DESC);
|
||||
CREATE INDEX IF NOT EXISTS idx_roles_closed ON roles(closed_at);
|
||||
CREATE INDEX IF NOT EXISTS idx_roles_cluster ON roles(company_key, title_key, place_key);
|
||||
CREATE INDEX IF NOT EXISTS idx_roles_board ON roles(source, subscription_id);
|
||||
CREATE INDEX IF NOT EXISTS idx_application_events
|
||||
ON application_events(application_id, at);
|
||||
CREATE INDEX IF NOT EXISTS idx_scores_agent ON scores(agent_id, score DESC);
|
||||
"""
|
||||
|
||||
#: The table whose absence marks a file that has never been a roledesk
|
||||
#: database, and is therefore created from `SCHEMA` rather than migrated.
|
||||
MARKER = "state"
|
||||
MARKER = "roles"
|
||||
|
||||
Step = str | Callable[[sqlite3.Connection], None]
|
||||
|
||||
MIGRATIONS: tuple[tuple[int, str, Step], ...] = (
|
||||
# Never run as a migration: a file without the marker table is created
|
||||
# from `SCHEMA` and stamped. It exists so the first real migration is 2.
|
||||
(1, "the first schema", ""),
|
||||
(1, "roles, companies, subscriptions, applications, agents, scores, state", ""),
|
||||
)
|
||||
|
||||
#: What a file created from `SCHEMA` today is already at.
|
||||
@@ -59,8 +203,9 @@ def _stamp(conn: sqlite3.Connection, version: int) -> None:
|
||||
|
||||
def _statements(script: str) -> list[str]:
|
||||
"""A script split into statements, cut where `sqlite3.complete_statement`
|
||||
says one ends. Not `executescript`, which commits before it starts and
|
||||
autocommits each statement, so a failing migration would be half applied."""
|
||||
says one ends, which knows about comments and trigger bodies. Not
|
||||
`executescript`, which commits before it starts and autocommits each
|
||||
statement, so a failing migration would be half applied."""
|
||||
statements, buffer = [], ""
|
||||
for line in script.splitlines(keepends=True):
|
||||
buffer += line
|
||||
|
||||
Reference in new issue
Block a user