Skip to main content

## MIGRATION_003

# Constant MIGRATION_003

Source

    const MIGRATION_003: &str = r#"
    -- Recreate user_data table without foreign key constraint on item_id
    -- This allows tracking playback progress for items that haven't been synced locally yet

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

        -- Playback state
        playback_position_ticks INTEGER DEFAULT 0,
        play_count INTEGER DEFAULT 0,
        is_played INTEGER DEFAULT 0,
        is_favorite INTEGER DEFAULT 0,

        -- Timestamps
        last_played_at TEXT,

        -- Sync status
        synced_at TEXT,
        pending_sync INTEGER DEFAULT 0,  -- 1 if local changes need sync

        UNIQUE(user_id, item_id)
    );

    -- Copy existing data
    INSERT OR IGNORE INTO user_data_new (id, user_id, item_id, playback_position_ticks, play_count, is_played, is_favorite, last_played_at, synced_at, pending_sync)
    SELECT id, user_id, item_id, playback_position_ticks, play_count, is_played, is_favorite, last_played_at, synced_at, pending_sync FROM user_data;

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

    -- Recreate index
    CREATE INDEX IF NOT EXISTS idx_user_data_user ON user_data(user_id);
    CREATE INDEX IF NOT EXISTS idx_user_data_item ON user_data(item_id);
    "#;

Expand description

Migration to relax foreign key constraints on user_data table Allows tracking playback progress for items not yet synced to local database