Skip to main content

## MIGRATION_007

# Constant MIGRATION_007

Source

    const MIGRATION_007: &str = r#"
    -- Recreate thumbnails table without foreign key constraint on item_id
    -- and add LRU eviction support columns
    CREATE TABLE IF NOT EXISTS thumbnails_new (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        item_id TEXT NOT NULL,  -- No foreign key constraint - item may not be synced yet
        image_type TEXT NOT NULL,  -- Primary, Backdrop, Thumb, Logo, etc.
        image_tag TEXT NOT NULL,
        file_path TEXT NOT NULL,
        width INTEGER,
        height INTEGER,
        file_size INTEGER DEFAULT 0,  -- Size in bytes for cache limit tracking
        cached_at TEXT DEFAULT CURRENT_TIMESTAMP,
        last_accessed TEXT DEFAULT CURRENT_TIMESTAMP,  -- For LRU eviction
        UNIQUE(item_id, image_type, image_tag)
    );

    -- Copy existing data (if any)
    INSERT OR IGNORE INTO thumbnails_new (id, item_id, image_type, image_tag, file_path, width, height, cached_at)
    SELECT id, item_id, image_type, image_tag, file_path, width, height, cached_at FROM thumbnails;

    -- Drop old table and rename new one
    DROP TABLE IF EXISTS thumbnails;
    ALTER TABLE thumbnails_new RENAME TO thumbnails;

    -- Create indexes for efficient queries
    CREATE INDEX IF NOT EXISTS idx_thumbnails_item ON thumbnails(item_id);
    CREATE INDEX IF NOT EXISTS idx_thumbnails_lru ON thumbnails(last_accessed ASC);

    -- Cache settings table for configurable limits
    CREATE TABLE IF NOT EXISTS cache_settings (
        key TEXT PRIMARY KEY,
        value TEXT NOT NULL,
        updated_at TEXT DEFAULT CURRENT_TIMESTAMP
    );

    -- Insert default settings
    INSERT OR IGNORE INTO cache_settings (key, value) VALUES ('image_cache_limit_bytes', '1073741824');  -- 1GB default
    INSERT OR IGNORE INTO cache_settings (key, value) VALUES ('image_cache_enabled', 'true');
    "#;

Expand description

Migration to enhance thumbnail caching with LRU eviction support

- Relaxes foreign key constraint on item_id (allows caching for items not yet synced)
- Adds last_accessed for LRU eviction
- Adds file_size for cache limit tracking
- Creates cache_settings table for configurable limits