From f3d26e148a564cdef3ce235e36e4edffe079aee6 Mon Sep 17 00:00:00 2001 From: Codex Date: Wed, 24 Jun 2026 11:04:38 +0800 Subject: [PATCH] =?UTF-8?q?=E6=96=B0=E5=A2=9E=E5=8F=91=E5=B8=83=E6=A0=A1?= =?UTF-8?q?=E5=87=86=E6=95=B0=E6=8D=AE=E5=BA=93=E7=BB=93=E6=9E=84?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...02_publication_performance_calibration.sql | 74 ++++++++++++++++++ src/lib/db/schema.ts | 75 +++++++++++++++++++ 2 files changed, 149 insertions(+) create mode 100644 migrations/0002_publication_performance_calibration.sql diff --git a/migrations/0002_publication_performance_calibration.sql b/migrations/0002_publication_performance_calibration.sql new file mode 100644 index 0000000..f212fbb --- /dev/null +++ b/migrations/0002_publication_performance_calibration.sql @@ -0,0 +1,74 @@ +CREATE TABLE IF NOT EXISTS rubric_versions ( + id TEXT PRIMARY KEY, + version TEXT NOT NULL, + name TEXT NOT NULL, + dimensions TEXT NOT NULL, + formula TEXT NOT NULL, + is_active INTEGER NOT NULL, + created_at TEXT NOT NULL +); + +CREATE TABLE IF NOT EXISTS scoring_runs ( + id TEXT PRIMARY KEY, + job_id TEXT NOT NULL, + revision INTEGER NOT NULL, + rubric_version_id TEXT NOT NULL, + dimension_scores TEXT NOT NULL, + composite_score REAL NOT NULL, + rationale TEXT NOT NULL, + created_at TEXT NOT NULL, + FOREIGN KEY (job_id, revision) + REFERENCES optimized_articles(job_id, revision) ON DELETE CASCADE, + FOREIGN KEY (rubric_version_id) REFERENCES rubric_versions(id) +); + +CREATE TABLE IF NOT EXISTS publication_records ( + id TEXT PRIMARY KEY, + job_id TEXT NOT NULL, + revision INTEGER NOT NULL, + platform TEXT NOT NULL, + url TEXT NOT NULL, + published_at TEXT NOT NULL, + status TEXT NOT NULL, + notes TEXT NOT NULL, + created_at TEXT NOT NULL, + updated_at TEXT NOT NULL, + FOREIGN KEY (job_id, revision) + REFERENCES optimized_articles(job_id, revision) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS performance_snapshots ( + id TEXT PRIMARY KEY, + publication_id TEXT NOT NULL, + source TEXT NOT NULL, + window_label TEXT NOT NULL, + metrics TEXT NOT NULL, + feedback_summary TEXT NOT NULL, + raw_reference TEXT, + snapshot_at TEXT NOT NULL, + FOREIGN KEY (publication_id) REFERENCES publication_records(id) ON DELETE CASCADE +); + +CREATE TABLE IF NOT EXISTS calibration_events ( + id TEXT PRIMARY KEY, + publication_id TEXT NOT NULL, + scoring_run_id TEXT NOT NULL, + performance_snapshot_id TEXT NOT NULL, + direction TEXT NOT NULL, + observations TEXT NOT NULL, + recommended_action TEXT NOT NULL, + created_at TEXT NOT NULL, + FOREIGN KEY (publication_id) REFERENCES publication_records(id) ON DELETE CASCADE, + FOREIGN KEY (scoring_run_id) REFERENCES scoring_runs(id) ON DELETE CASCADE, + FOREIGN KEY (performance_snapshot_id) + REFERENCES performance_snapshots(id) ON DELETE CASCADE +); + +CREATE INDEX IF NOT EXISTS idx_scoring_runs_job_revision + ON scoring_runs(job_id, revision); + +CREATE INDEX IF NOT EXISTS idx_publication_records_job_revision + ON publication_records(job_id, revision); + +CREATE INDEX IF NOT EXISTS idx_performance_snapshots_publication + ON performance_snapshots(publication_id); diff --git a/src/lib/db/schema.ts b/src/lib/db/schema.ts index c040abf..f39f3cc 100644 --- a/src/lib/db/schema.ts +++ b/src/lib/db/schema.ts @@ -60,5 +60,80 @@ export function initializeSchema(db: Database.Database) { foreign key (job_id, revision) references optimized_articles(job_id, revision) on delete cascade ); + + create table if not exists rubric_versions ( + id text primary key, + version text not null, + name text not null, + dimensions text not null, + formula text not null, + is_active integer not null, + created_at text not null + ); + + create table if not exists scoring_runs ( + id text primary key, + job_id text not null, + revision integer not null, + rubric_version_id text not null, + dimension_scores text not null, + composite_score real not null, + rationale text not null, + created_at text not null, + foreign key (job_id, revision) + references optimized_articles(job_id, revision) on delete cascade, + foreign key (rubric_version_id) references rubric_versions(id) + ); + + create table if not exists publication_records ( + id text primary key, + job_id text not null, + revision integer not null, + platform text not null, + url text not null, + published_at text not null, + status text not null, + notes text not null, + created_at text not null, + updated_at text not null, + foreign key (job_id, revision) + references optimized_articles(job_id, revision) on delete cascade + ); + + create table if not exists performance_snapshots ( + id text primary key, + publication_id text not null, + source text not null, + window_label text not null, + metrics text not null, + feedback_summary text not null, + raw_reference text, + snapshot_at text not null, + foreign key (publication_id) references publication_records(id) on delete cascade + ); + + create table if not exists calibration_events ( + id text primary key, + publication_id text not null, + scoring_run_id text not null, + performance_snapshot_id text not null, + direction text not null, + observations text not null, + recommended_action text not null, + created_at text not null, + foreign key (publication_id) references publication_records(id) on delete cascade, + foreign key (scoring_run_id) references scoring_runs(id) on delete cascade, + foreign key (performance_snapshot_id) + references performance_snapshots(id) on delete cascade + ); + + create index if not exists idx_scoring_runs_job_revision + on scoring_runs(job_id, revision); + + create index if not exists idx_publication_records_job_revision + on publication_records(job_id, revision); + + create index if not exists idx_performance_snapshots_publication + on performance_snapshots(publication_id); `); }