-- Append-only version history for the code-bearing entities: route handlers,
-- template content, and db libraries. Every content-changing save records a
-- snapshot here (see recordVersion in db.js). The live rows in routes/templates/
-- dbs remain the current version; this table is the "plumber's" undo trail.
CREATE TABLE versions (
    id INTEGER PRIMARY KEY AUTOINCREMENT,
    entity_type TEXT NOT NULL CHECK(entity_type IN ('route', 'template', 'db')),
    entity_id INTEGER NOT NULL,
    structure_id INTEGER,
    snapshot TEXT NOT NULL,           -- JSON of the versioned fields
    created_at TIMESTAMP DEFAULT CURRENT_TIMESTAMP
);

CREATE INDEX idx_versions_entity ON versions (entity_type, entity_id, id);
CREATE INDEX idx_versions_structure ON versions (structure_id, id);
