Skip to main content

## MIGRATION_005

# Constant MIGRATION_005

Source

    const MIGRATION_005: &str = r#"
    -- Recreate downloads table without foreign key constraint on item_id
    -- This allows downloading items that haven't been synced locally yet

    CREATE TABLE IF NOT EXISTS downloads_new (
        id INTEGER PRIMARY KEY AUTOINCREMENT,
        item_id TEXT NOT NULL,  -- No foreign key constraint - item may not be synced yet
        user_id TEXT NOT NULL REFERENCES users(id) ON DELETE CASCADE,

        -- File info
        file_path TEXT NOT NULL,
        file_size INTEGER,
        mime_type TEXT,

        -- Download state
        status TEXT DEFAULT 'pending',  -- pending, downloading, completed, failed, paused
        progress REAL DEFAULT 0,        -- 0.0 to 1.0

        -- Transcoding options used
        bitrate INTEGER,
        container TEXT,

        -- Timestamps
        queued_at TEXT DEFAULT CURRENT_TIMESTAMP,
        started_at TEXT,
        completed_at TEXT,

        -- Error tracking
        error_message TEXT,
        retry_count INTEGER DEFAULT 0,

        -- Priority and progress tracking (from migration 004)
        priority INTEGER DEFAULT 0,
        bytes_downloaded INTEGER DEFAULT 0,

        UNIQUE(item_id, user_id)
    );

    -- Copy existing data
    INSERT OR IGNORE INTO downloads_new (
        id, item_id, user_id, file_path, file_size, mime_type, status, progress,
        bitrate, container, queued_at, started_at, completed_at, error_message,
        retry_count, priority, bytes_downloaded
    )
    SELECT
        id, item_id, user_id, file_path, file_size, mime_type, status, progress,
        bitrate, container, queued_at, started_at, completed_at, error_message,
        retry_count, priority, bytes_downloaded
    FROM downloads;

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

    -- Recreate indexes
    CREATE INDEX IF NOT EXISTS idx_downloads_status ON downloads(status);
    CREATE INDEX IF NOT EXISTS idx_downloads_queue
      ON downloads(status, priority DESC, queued_at ASC)
      WHERE status IN ('pending', 'downloading');
    "#;

Expand description

Migration to relax foreign key constraint on downloads.item_id Allows downloading items that haven’t been synced to local database yet