Skip to main content

MIGRATION_V2_SQL

Constant MIGRATION_V2_SQL 

Source
pub const MIGRATION_V2_SQL: &str = r"
CREATE INDEX IF NOT EXISTS idx_items_state_urgency_updated
    ON items(state, urgency, updated_at_us DESC);

CREATE INDEX IF NOT EXISTS idx_items_kind_state
    ON items(kind, state);

CREATE INDEX IF NOT EXISTS idx_items_parent
    ON items(parent_id);

CREATE INDEX IF NOT EXISTS idx_items_deleted_updated
    ON items(is_deleted, updated_at_us DESC);

CREATE INDEX IF NOT EXISTS idx_item_labels_label
    ON item_labels(label, item_id);

CREATE INDEX IF NOT EXISTS idx_item_assignees_agent
    ON item_assignees(agent, item_id);

CREATE INDEX IF NOT EXISTS idx_item_dependencies_target_type
    ON item_dependencies(depends_on_item_id, link_type, item_id);

CREATE INDEX IF NOT EXISTS idx_item_comments_item_created
    ON item_comments(item_id, created_at_us DESC);

CREATE INDEX IF NOT EXISTS idx_event_redactions_item
    ON event_redactions(item_id);

CREATE VIRTUAL TABLE IF NOT EXISTS items_fts USING fts5(
    title,
    description,
    labels,
    item_id UNINDEXED,
    tokenize='porter unicode61',
    prefix='2 3'
);

CREATE TRIGGER IF NOT EXISTS items_ai
AFTER INSERT ON items
BEGIN
    INSERT INTO items_fts(rowid, title, description, labels, item_id)
    VALUES (
        new.rowid,
        new.title,
        COALESCE(new.description, ''),
        COALESCE(new.search_labels, ''),
        new.item_id
    );
END;

CREATE TRIGGER IF NOT EXISTS items_au
AFTER UPDATE ON items
BEGIN
    DELETE FROM items_fts WHERE rowid = old.rowid;

    INSERT INTO items_fts(rowid, title, description, labels, item_id)
    VALUES (
        new.rowid,
        new.title,
        COALESCE(new.description, ''),
        COALESCE(new.search_labels, ''),
        new.item_id
    );
END;

CREATE TRIGGER IF NOT EXISTS items_ad
AFTER DELETE ON items
BEGIN
    DELETE FROM items_fts WHERE rowid = old.rowid;
END;

DELETE FROM items_fts;
INSERT INTO items_fts(rowid, title, description, labels, item_id)
SELECT
    rowid,
    title,
    COALESCE(description, ''),
    COALESCE(search_labels, ''),
    item_id
FROM items;

UPDATE projection_meta
SET schema_version = 2
WHERE id = 1;
";
Expand description

Migration v2: read-path indexes and FTS5 table/triggers.