-- version: 12
-- SP17: encrypted DB backup metadata
--
-- Tracks every backup the BackupService has taken. The actual
-- encrypted blob lives in a directory outside the DB (default
-- ~/.local/share/cyclone/backups/); this table is just the index
-- the operator queries via GET /api/admin/backup/list.
--
-- Status values:
--   pending  - row inserted, .backup() in progress or crashed before commit
--   ok       - encrypted blob + sidecar written successfully
--   error    - creation failed; error_message populated
--   pruned   - retention policy removed the file; row kept for audit

CREATE TABLE db_backups (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    filename TEXT NOT NULL,
    backup_dir TEXT NOT NULL,
    size_bytes INTEGER NOT NULL DEFAULT 0,
    db_fingerprint TEXT,
    table_count INTEGER NOT NULL DEFAULT 0,
    created_at TEXT NOT NULL,
    completed_at TEXT,
    status TEXT NOT NULL,
    error_message TEXT
);
CREATE UNIQUE INDEX ux_db_backups_filename ON db_backups(backup_dir, filename);
CREATE INDEX ix_db_backups_created_at ON db_backups(created_at DESC);
CREATE INDEX ix_db_backups_status ON db_backups(status);
