Repository: xbmc/xbmc
File: xbmc/video/VideoDatabase.cpp (C++)

Code before:
  {
    assert(m_pDB->in_transaction());
    std::string sql =
        PrepareSQL("UPDATE episode SET idFile=%i WHERE idEpisode=%i", idFile, idEpisode);
    m_pDS->exec(sql);
    DeleteFile(oldIdFile);

    return true;
  }
  catch (...)
  {
    CLog::Log(LOGERROR, "{} ({}) failed", __FUNCTION__, idEpisode);
  }
  return false;
}

bool CVideoDatabase::SetFileForMovie(const std::string& fileAndPath, int idMovie)
{
  const int idFile{AddFile(fileAndPath)};
  if (idFile < 0)
    return false;

  const int oldIdFile{GetFileIdByMovie(idMovie)};
  if (oldIdFile < 0)
    return false;

  try
  {
    assert(m_pDB->in_transaction());
    std::string sql = PrepareSQL("UPDATE movie SET idFile=%i WHERE idFile=%i AND idMovie=%i",
                                 idFile, oldIdFile, idMovie);
    m_pDS->exec(sql);

    sql = PrepareSQL(
        "UPDATE videoversion SET idFile=%i WHERE idFile=%i AND media_type='movie' AND idMedia=%i",
        idFile, oldIdFile, idMovie);
    m_pDS->exec(sql);

    sql = PrepareSQL("UPDATE art SET media_id=%i WHERE media_id=%i AND media_type='videoversion'",
                     idFile, oldIdFile);
    m_pDS->exec(sql);

    if (GetSingleValueInt(PrepareSQL("SELECT idFile FROM streamdetails WHERE idFile = %i", idFile),
                          m_pDS) == 0)
    {
      sql = PrepareSQL("UPDATE streamdetails SET idFile=%i WHERE idFile=%i", idFile, oldIdFile);
      m_pDS->exec(sql);
    }
    DeleteFile(oldIdFile);

    return true;

Review comment (suggestion):
As mentioned in other comment, the original file id needs to come from the caller, because GetFileIdByMovie can only retrieve the file of the default version of a movie.

Code after:
  }
  catch (...)
  {
    CLog::LogF(LOGERROR, " idFile {}, fileAndPath {}, idEpisode {}, oldIdFile {} - failed", idFile,
               fileAndPath, idEpisode, oldIdFile);
  }
  return false;
}

bool CVideoDatabase::SetFileForMovie(const std::string& fileAndPath, int idMovie, int oldIdFile)
{
  assert(m_pDB->in_transaction());

  const int idFile{AddFile(fileAndPath)};
  if (idFile < 0)
    return false;

  try
  {
    std::string sql = PrepareSQL("UPDATE movie SET idFile=%i WHERE idFile=%i AND idMovie=%i",
                                 idFile, oldIdFile, idMovie);
    m_pDS->exec(sql);

    sql = PrepareSQL(
        "UPDATE videoversion SET idFile=%i WHERE idFile=%i AND media_type='movie' AND idMedia=%i",
        idFile, oldIdFile, idMovie);
    m_pDS->exec(sql);

    sql = PrepareSQL("UPDATE art SET media_id=%i WHERE media_id=%i AND media_type='videoversion'",
                     idFile, oldIdFile);
    m_pDS->exec(sql);

    sql = PrepareSQL("UPDATE streamdetails SET idFile=%i WHERE idFile=%i AND NOT EXISTS (SELECT 1 "
                     "FROM streamdetails WHERE idFile=%i)",
                     idFile, oldIdFile, idFile);
    m_pDS->exec(sql);

    return DeleteFile(oldIdFile);
  }
  catch (...)
  {
    CLog::LogF(LOGERROR, " idFile {}, fileAndPath {}, idMovie {}, oldIdFile {} - failed", idFile,
               fileAndPath, idMovie, oldIdFile);
  }
  return false;
}

bool CVideoDatabase::DeleteFile(int idFile)
{
  try
  {