Modello del dominio e interfacce degli archivi in Febe.Core; implementazione SQLite in Febe.Data (WAL, chiavi esterne, migrazioni numerate in transazione, coda di scrittura su un solo thread); logica della libreria in Febe.Core/Library; 164 prove MSTest, compresa la ricostruzione del database da una libreria di prova e la scala a 200.000 elementi. Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01Mzbb6zeFTjT6Rp8b92AVkA
292 lines
12 KiB
SQL
292 lines
12 KiB
SQL
-- Febe, schema version 1 (spec §8.3).
|
|
-- Times are UTC instants stored as INTEGER Unix milliseconds. Enumerations are stored as their English names.
|
|
-- Instagram identifiers are TEXT: numeric today, but no assumption is made on their size or form (rule 10).
|
|
-- Run by the migration runner inside a transaction; PRAGMA user_version is set by the runner.
|
|
|
|
CREATE TABLE setting (
|
|
key TEXT PRIMARY KEY NOT NULL,
|
|
value TEXT NOT NULL
|
|
) WITHOUT ROWID;
|
|
|
|
CREATE TABLE account (
|
|
id INTEGER PRIMARY KEY,
|
|
instagram_id TEXT UNIQUE,
|
|
username TEXT NOT NULL COLLATE NOCASE CHECK (length(username) > 0),
|
|
display_name TEXT,
|
|
is_private INTEGER NOT NULL DEFAULT 0 CHECK (is_private IN (0, 1)),
|
|
is_followed INTEGER NOT NULL DEFAULT 0 CHECK (is_followed IN (0, 1)),
|
|
is_mine INTEGER NOT NULL DEFAULT 0 CHECK (is_mine IN (0, 1)),
|
|
state TEXT NOT NULL,
|
|
state_changed_at INTEGER,
|
|
unreachable_reason TEXT,
|
|
keep_updated INTEGER NOT NULL DEFAULT 1 CHECK (keep_updated IN (0, 1)),
|
|
frequency TEXT NOT NULL,
|
|
custom_post_interval_min INTEGER CHECK (custom_post_interval_min IS NULL OR custom_post_interval_min > 0),
|
|
custom_story_interval_min INTEGER CHECK (custom_story_interval_min IS NULL OR custom_story_interval_min > 0),
|
|
content_types INTEGER NOT NULL CHECK (content_types >= 0),
|
|
history_depth TEXT NOT NULL,
|
|
history_last_posts INTEGER CHECK (history_last_posts IS NULL OR history_last_posts >= 0),
|
|
history_from INTEGER,
|
|
backfill_posts_per_day INTEGER NOT NULL CHECK (backfill_posts_per_day >= 0),
|
|
max_video_bytes INTEGER CHECK (max_video_bytes IS NULL OR max_video_bytes > 0),
|
|
story_videos INTEGER NOT NULL DEFAULT 1 CHECK (story_videos IN (0, 1)),
|
|
folder TEXT,
|
|
backfill_cursor TEXT,
|
|
backfill_completed_at INTEGER,
|
|
post_count INTEGER,
|
|
last_profile_check_at INTEGER,
|
|
next_profile_check_at INTEGER,
|
|
last_stories_check_at INTEGER,
|
|
next_stories_check_at INTEGER,
|
|
notices INTEGER NOT NULL DEFAULT 0,
|
|
previous_username TEXT,
|
|
renamed_at INTEGER,
|
|
format_unrecognized_at INTEGER,
|
|
added_at INTEGER NOT NULL
|
|
);
|
|
|
|
CREATE INDEX ix_account_username ON account (username);
|
|
CREATE INDEX ix_account_state ON account (state);
|
|
-- At most one account is Alberto's own profile («Tu»).
|
|
CREATE UNIQUE INDEX ux_account_mine ON account (is_mine) WHERE is_mine = 1;
|
|
|
|
CREATE TABLE entry (
|
|
id INTEGER PRIMARY KEY,
|
|
account_id INTEGER NOT NULL REFERENCES account (id) ON DELETE CASCADE,
|
|
kind TEXT NOT NULL,
|
|
instagram_id TEXT,
|
|
code TEXT,
|
|
published_at INTEGER NOT NULL,
|
|
caption TEXT,
|
|
author_username TEXT,
|
|
author_instagram_id TEXT,
|
|
state TEXT NOT NULL,
|
|
provenance TEXT NOT NULL,
|
|
discovered_at INTEGER NOT NULL,
|
|
downloaded_at INTEGER,
|
|
deleted_at INTEGER,
|
|
expires_at INTEGER,
|
|
-- Lowercase text without accents (caption and author), for the search of the library.
|
|
search_text TEXT,
|
|
UNIQUE (account_id, kind, instagram_id)
|
|
);
|
|
|
|
CREATE INDEX ix_entry_account_published ON entry (account_id, published_at);
|
|
-- The library mosaic: keyset windows by publication time, with the filter columns in the index so that windows,
|
|
-- totals and counts per day never visit the table (spec §4.6: fluid with 200,000 entries).
|
|
CREATE INDEX ix_entry_library ON entry (published_at, kind, state, account_id, provenance);
|
|
CREATE INDEX ix_entry_kind_published ON entry (kind, published_at);
|
|
CREATE INDEX ix_entry_state ON entry (state);
|
|
-- Accounts list counts and the per-account daily backfill limit, answered from the index alone.
|
|
CREATE INDEX ix_entry_account_kind ON entry (account_id, kind, state, discovered_at);
|
|
|
|
CREATE TABLE media (
|
|
id INTEGER PRIMARY KEY,
|
|
entry_id INTEGER NOT NULL REFERENCES entry (id) ON DELETE CASCADE,
|
|
position INTEGER NOT NULL CHECK (position >= 1),
|
|
kind TEXT NOT NULL,
|
|
instagram_id TEXT,
|
|
width INTEGER,
|
|
height INTEGER,
|
|
duration_ms INTEGER,
|
|
state TEXT NOT NULL,
|
|
relative_path TEXT,
|
|
bytes INTEGER CHECK (bytes IS NULL OR bytes >= 0),
|
|
sha256 TEXT CHECK (sha256 IS NULL OR length(sha256) = 64),
|
|
phash INTEGER,
|
|
has_thumbnail INTEGER NOT NULL DEFAULT 0 CHECK (has_thumbnail IN (0, 1)),
|
|
source_url TEXT,
|
|
cover_url TEXT,
|
|
source_seen_at INTEGER,
|
|
attempts INTEGER NOT NULL DEFAULT 0,
|
|
last_error TEXT,
|
|
downloaded_at INTEGER,
|
|
UNIQUE (entry_id, position)
|
|
);
|
|
|
|
CREATE INDEX ix_media_sha256 ON media (sha256) WHERE sha256 IS NOT NULL;
|
|
CREATE INDEX ix_media_phash ON media (phash) WHERE phash IS NOT NULL;
|
|
CREATE INDEX ix_media_state ON media (state);
|
|
|
|
CREATE TABLE highlight (
|
|
id INTEGER PRIMARY KEY,
|
|
account_id INTEGER NOT NULL REFERENCES account (id) ON DELETE CASCADE,
|
|
instagram_id TEXT,
|
|
title TEXT NOT NULL,
|
|
position INTEGER NOT NULL DEFAULT 0,
|
|
discovered_at INTEGER NOT NULL,
|
|
last_checked_at INTEGER,
|
|
removed_at INTEGER,
|
|
UNIQUE (account_id, instagram_id)
|
|
);
|
|
|
|
CREATE INDEX ix_highlight_account_position ON highlight (account_id, position);
|
|
|
|
CREATE TABLE highlight_entry (
|
|
highlight_id INTEGER NOT NULL REFERENCES highlight (id) ON DELETE CASCADE,
|
|
entry_id INTEGER NOT NULL REFERENCES entry (id) ON DELETE CASCADE,
|
|
position INTEGER NOT NULL,
|
|
PRIMARY KEY (highlight_id, entry_id)
|
|
) WITHOUT ROWID;
|
|
|
|
CREATE INDEX ix_highlight_entry_entry ON highlight_entry (entry_id);
|
|
|
|
CREATE TABLE task (
|
|
id INTEGER PRIMARY KEY,
|
|
kind TEXT NOT NULL,
|
|
account_id INTEGER REFERENCES account (id) ON DELETE CASCADE,
|
|
due_at INTEGER NOT NULL,
|
|
priority INTEGER NOT NULL DEFAULT 0,
|
|
state TEXT NOT NULL,
|
|
payload TEXT,
|
|
is_manual INTEGER NOT NULL DEFAULT 0 CHECK (is_manual IN (0, 1)),
|
|
attempts INTEGER NOT NULL DEFAULT 0,
|
|
created_at INTEGER NOT NULL,
|
|
started_at INTEGER,
|
|
completed_at INTEGER,
|
|
last_error TEXT
|
|
);
|
|
|
|
CREATE INDEX ix_task_state_due ON task (state, due_at);
|
|
CREATE INDEX ix_task_account ON task (account_id);
|
|
|
|
CREATE TABLE session (
|
|
id INTEGER PRIMARY KEY,
|
|
started_at INTEGER NOT NULL,
|
|
ended_at INTEGER,
|
|
action_count INTEGER NOT NULL DEFAULT 0,
|
|
outcome TEXT NOT NULL,
|
|
summary TEXT
|
|
);
|
|
|
|
CREATE INDEX ix_session_started ON session (started_at);
|
|
|
|
CREATE TABLE action_log (
|
|
id INTEGER PRIMARY KEY,
|
|
at INTEGER NOT NULL,
|
|
category TEXT NOT NULL,
|
|
account_id INTEGER REFERENCES account (id) ON DELETE SET NULL,
|
|
session_id INTEGER REFERENCES session (id) ON DELETE SET NULL,
|
|
amount INTEGER NOT NULL DEFAULT 1 CHECK (amount >= 0)
|
|
);
|
|
|
|
CREATE INDEX ix_action_log_category_at ON action_log (category, at);
|
|
CREATE INDEX ix_action_log_account_at ON action_log (account_id, category, at);
|
|
CREATE INDEX ix_action_log_at ON action_log (at);
|
|
|
|
CREATE TABLE incident (
|
|
id INTEGER PRIMARY KEY,
|
|
at INTEGER NOT NULL,
|
|
level TEXT NOT NULL,
|
|
signal TEXT NOT NULL,
|
|
url TEXT,
|
|
stopped_until INTEGER,
|
|
resolved_at INTEGER,
|
|
session_id INTEGER REFERENCES session (id) ON DELETE SET NULL,
|
|
note TEXT
|
|
);
|
|
|
|
CREATE INDEX ix_incident_at ON incident (at);
|
|
CREATE INDEX ix_incident_open ON incident (resolved_at) WHERE resolved_at IS NULL;
|
|
|
|
CREATE TABLE event (
|
|
id INTEGER PRIMARY KEY,
|
|
at INTEGER NOT NULL,
|
|
severity TEXT NOT NULL,
|
|
code TEXT NOT NULL,
|
|
message TEXT NOT NULL,
|
|
account_id INTEGER REFERENCES account (id) ON DELETE SET NULL,
|
|
session_id INTEGER REFERENCES session (id) ON DELETE SET NULL
|
|
);
|
|
|
|
CREATE INDEX ix_event_at ON event (at);
|
|
CREATE INDEX ix_event_account ON event (account_id, id);
|
|
|
|
CREATE TABLE "following" (
|
|
id INTEGER PRIMARY KEY,
|
|
instagram_id TEXT UNIQUE,
|
|
username TEXT NOT NULL COLLATE NOCASE,
|
|
display_name TEXT,
|
|
is_private INTEGER CHECK (is_private IS NULL OR is_private IN (0, 1)),
|
|
position INTEGER NOT NULL DEFAULT 0,
|
|
read_at INTEGER NOT NULL
|
|
);
|
|
|
|
CREATE INDEX ix_following_username ON "following" (username);
|
|
CREATE INDEX ix_following_position ON "following" (position);
|
|
|
|
CREATE TABLE import_run (
|
|
id INTEGER PRIMARY KEY,
|
|
source_path TEXT NOT NULL,
|
|
source_read_only INTEGER NOT NULL DEFAULT 0 CHECK (source_read_only IN (0, 1)),
|
|
mode TEXT NOT NULL,
|
|
state TEXT NOT NULL,
|
|
started_at INTEGER NOT NULL,
|
|
analyzed_at INTEGER,
|
|
completed_at INTEGER,
|
|
last_error TEXT
|
|
);
|
|
|
|
CREATE TABLE import_file (
|
|
id INTEGER PRIMARY KEY,
|
|
run_id INTEGER NOT NULL REFERENCES import_run (id) ON DELETE CASCADE,
|
|
relative_path TEXT NOT NULL,
|
|
folder TEXT NOT NULL,
|
|
bytes INTEGER NOT NULL DEFAULT 0,
|
|
modified_at INTEGER,
|
|
media_kind TEXT,
|
|
content_type TEXT,
|
|
width INTEGER,
|
|
height INTEGER,
|
|
duration_ms INTEGER,
|
|
sha256 TEXT CHECK (sha256 IS NULL OR length(sha256) = 64),
|
|
phash INTEGER,
|
|
taken_at INTEGER,
|
|
date_source TEXT,
|
|
instagram_id TEXT,
|
|
code TEXT,
|
|
probable_account TEXT,
|
|
probable_kind TEXT,
|
|
highlight_title TEXT,
|
|
outcome TEXT NOT NULL,
|
|
matched_media_id INTEGER REFERENCES media (id) ON DELETE SET NULL,
|
|
target_account_id INTEGER REFERENCES account (id) ON DELETE SET NULL,
|
|
decision TEXT NOT NULL,
|
|
state TEXT NOT NULL,
|
|
entry_id INTEGER REFERENCES entry (id) ON DELETE SET NULL,
|
|
destination_path TEXT,
|
|
last_error TEXT,
|
|
UNIQUE (run_id, relative_path)
|
|
);
|
|
|
|
CREATE INDEX ix_import_file_run_state ON import_file (run_id, state);
|
|
CREATE INDEX ix_import_file_run_folder ON import_file (run_id, folder);
|
|
CREATE INDEX ix_import_file_sha256 ON import_file (sha256) WHERE sha256 IS NOT NULL;
|
|
-- Foreign keys with ON DELETE actions need an index on the child column, or each parent delete scans the table.
|
|
CREATE INDEX ix_import_file_matched_media ON import_file (matched_media_id) WHERE matched_media_id IS NOT NULL;
|
|
CREATE INDEX ix_import_file_entry ON import_file (entry_id) WHERE entry_id IS NOT NULL;
|
|
CREATE INDEX ix_import_file_target_account ON import_file (target_account_id) WHERE target_account_id IS NOT NULL;
|
|
|
|
CREATE TABLE duplicate_pair (
|
|
id INTEGER PRIMARY KEY,
|
|
origin TEXT NOT NULL,
|
|
reason TEXT NOT NULL,
|
|
existing_media_id INTEGER REFERENCES media (id) ON DELETE CASCADE,
|
|
existing_import_file_id INTEGER REFERENCES import_file (id) ON DELETE CASCADE,
|
|
candidate_media_id INTEGER REFERENCES media (id) ON DELETE CASCADE,
|
|
candidate_import_file_id INTEGER REFERENCES import_file (id) ON DELETE CASCADE,
|
|
distance INTEGER,
|
|
decision TEXT NOT NULL,
|
|
created_at INTEGER NOT NULL,
|
|
decided_at INTEGER,
|
|
-- Exactly one reference per side.
|
|
CHECK ((existing_media_id IS NULL) <> (existing_import_file_id IS NULL)),
|
|
CHECK ((candidate_media_id IS NULL) <> (candidate_import_file_id IS NULL))
|
|
);
|
|
|
|
CREATE INDEX ix_duplicate_pair_decision ON duplicate_pair (decision, id);
|
|
CREATE INDEX ix_duplicate_pair_existing_media ON duplicate_pair (existing_media_id);
|
|
CREATE INDEX ix_duplicate_pair_candidate_media ON duplicate_pair (candidate_media_id);
|
|
CREATE INDEX ix_duplicate_pair_existing_file ON duplicate_pair (existing_import_file_id);
|
|
CREATE INDEX ix_duplicate_pair_candidate_file ON duplicate_pair (candidate_import_file_id);
|