function hasColumn(db, tableName, columnName) {
  const pragma = db.exec(`PRAGMA table_info(${tableName})`);
  if (!pragma.length) return false;
  return pragma[0].values.some((row) => row[1] === columnName);
}

/**
 * Idempotent runtime migrations for existing DB files.
 * @param {import('sql.js').Database} db
 */
export function applyRuntimeMigrations(db) {
  const hasRecipesTable = db.exec("SELECT name FROM sqlite_master WHERE type='table' AND name='recipes'");
  if (!hasRecipesTable.length || !hasRecipesTable[0].values.length) {
    return;
  }

  if (!hasColumn(db, 'recipes', 'image_url')) {
    db.exec('ALTER TABLE recipes ADD COLUMN image_url TEXT');
  }
}
