From 6b0870d3c97824a1182482079dfccc0b08ba160b Mon Sep 17 00:00:00 2001 From: sneak Date: Sun, 9 Aug 2026 00:43:21 +0000 Subject: [PATCH] fix: fold cache eviction schema into 001_initial_schema.sql Pre-1.0 with no installed base to migrate: REPO_POLICIES.md forbids adding numbered migration files beyond 001 before a tagged release. Fold the variant_content table and source_content.last_accessed_at column (previously 002_cache_eviction.sql) directly into 001_initial_schema.sql and delete the 002 file. The migration runner is generic over whatever *.sql files exist in schema/, so no runner code changes are needed. --- .../database/schema/001_initial_schema.sql | 23 ++++++++++++++++- .../database/schema/002_cache_eviction.sql | 25 ------------------- 2 files changed, 22 insertions(+), 26 deletions(-) delete mode 100644 internal/database/schema/002_cache_eviction.sql diff --git a/internal/database/schema/001_initial_schema.sql b/internal/database/schema/001_initial_schema.sql index 5552ac1..c4ca863 100644 --- a/internal/database/schema/001_initial_schema.sql +++ b/internal/database/schema/001_initial_schema.sql @@ -3,12 +3,17 @@ -- Source content blobs -- Files stored at: cache/src-content/// +-- last_accessed_at is NULL until the first LRU touch; eviction falls +-- back to fetched_at for rows that have never been touched. CREATE TABLE IF NOT EXISTS source_content ( content_hash TEXT PRIMARY KEY, content_type TEXT NOT NULL, size_bytes INTEGER NOT NULL, - fetched_at DATETIME DEFAULT CURRENT_TIMESTAMP + fetched_at DATETIME DEFAULT CURRENT_TIMESTAMP, + last_accessed_at DATETIME ); +CREATE INDEX IF NOT EXISTS idx_source_content_last_accessed + ON source_content(last_accessed_at); -- Source URL metadata - maps URLs to content hashes -- JSON stored at: cache/src-metadata//.json @@ -34,6 +39,22 @@ CREATE INDEX IF NOT EXISTS idx_source_meta_path_hash ON source_metadata(path_has CREATE INDEX IF NOT EXISTS idx_source_meta_expires ON source_metadata(expires_at); CREATE INDEX IF NOT EXISTS idx_source_meta_content_hash ON source_metadata(content_hash); +-- Processed variant blobs +-- Files stored at: cache/variants/// (plus a .meta +-- sidecar with the content type). Tracked here (like source content +-- blobs above) so total cache usage can be computed with a SUM query, +-- never a directory scan, and so LRU eviction has a timestamp to order +-- on. +CREATE TABLE IF NOT EXISTS variant_content ( + cache_key TEXT PRIMARY KEY, + size_bytes INTEGER NOT NULL, + content_type TEXT NOT NULL, + created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, + last_accessed_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP +); +CREATE INDEX IF NOT EXISTS idx_variant_content_last_accessed + ON variant_content(last_accessed_at); + -- Output/transformed content blobs -- Files stored at: cache/dst-content/// CREATE TABLE IF NOT EXISTS output_content ( diff --git a/internal/database/schema/002_cache_eviction.sql b/internal/database/schema/002_cache_eviction.sql deleted file mode 100644 index 42b4787..0000000 --- a/internal/database/schema/002_cache_eviction.sql +++ /dev/null @@ -1,25 +0,0 @@ --- Migration 002: cache size accounting and eviction --- --- Tracks processed variants in the database (source content blobs are --- already tracked in source_content) so total cache usage can be --- computed without directory scans, and adds last-access timestamps --- for LRU eviction ordering. - --- Processed variant blobs --- Files stored at: cache/variants/// (plus a --- .meta sidecar with the content type) -CREATE TABLE IF NOT EXISTS variant_content ( - cache_key TEXT PRIMARY KEY, - size_bytes INTEGER NOT NULL, - content_type TEXT NOT NULL, - created_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP, - last_accessed_at DATETIME NOT NULL DEFAULT CURRENT_TIMESTAMP -); -CREATE INDEX IF NOT EXISTS idx_variant_content_last_accessed - ON variant_content(last_accessed_at); - --- LRU timestamp for source content blobs. Rows written before this --- migration have NULL here; eviction falls back to fetched_at. -ALTER TABLE source_content ADD COLUMN last_accessed_at DATETIME; -CREATE INDEX IF NOT EXISTS idx_source_content_last_accessed - ON source_content(last_accessed_at);