1
0
mirror of https://github.com/Radarr/Radarr.git synced 2024-11-04 10:02:40 +01:00

Fixed: Language Migration Assigning 'Any' to MovieFiles and History

This commit is contained in:
Qstick 2020-07-18 23:18:51 -04:00
parent 539f495dbe
commit 5765961c3a

View File

@ -3,9 +3,11 @@
using System.Data;
using System.Linq;
using FluentMigrator;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Datastore.Converters;
using NzbDrone.Core.Datastore.Migration.Framework;
using NzbDrone.Core.Languages;
using NzbDrone.Core.Parser;
namespace NzbDrone.Core.Datastore.Migration
{
@ -48,7 +50,7 @@ private void UpdateLanguage(IDbConnection conn, IDbTransaction tran)
var movieLanguage = Language.English.Id;
try
{
movieLanguage = profilesReader.GetInt32(1);
movieLanguage = profilesReader.GetInt32(1) != -1 ? profilesReader.GetInt32(1) : 1;
}
catch (InvalidCastException e)
{
@ -77,40 +79,168 @@ private void UpdateLanguage(IDbConnection conn, IDbTransaction tran)
}
}
foreach (var group in movieLanguages.GroupBy(v => v.Value, v => v.Key))
{
var language = new List<Language> { Language.FindById(group.Key) };
var movieFileLanguages = new Dictionary<int, List<Language>>();
var releaseLanguages = new Dictionary<string, List<Language>>();
var movieIds = group.Select(v => v.ToString()).Join(",");
using (IDbCommand getSeriesCmd = conn.CreateCommand())
{
getSeriesCmd.Transaction = tran;
getSeriesCmd.CommandText = @"SELECT Id, MovieId, SceneName, MediaInfo FROM MovieFiles";
using (IDataReader movieFilesReader = getSeriesCmd.ExecuteReader())
{
while (movieFilesReader.Read())
{
var movieFileId = movieFilesReader.GetInt32(0);
var movieId = movieFilesReader.GetInt32(1);
var movieFileSceneName = movieFilesReader.IsDBNull(2) ? null : movieFilesReader.GetString(2);
var movieFileMediaInfo = movieFilesReader.IsDBNull(3) ? null : Json.Deserialize<MediaInfo154>(movieFilesReader.GetString(3));
var languages = new List<Language>();
if (movieFileMediaInfo != null && movieFileMediaInfo.AudioLanguages.IsNotNullOrWhiteSpace())
{
var mediaInfolanguages = movieFileMediaInfo.AudioLanguages.Split('/').Select(l => l.Trim()).Distinct().ToList();
foreach (var audioLanguage in mediaInfolanguages)
{
var language = IsoLanguages.FindByName(audioLanguage)?.Language;
languages.AddIfNotNull(language);
}
}
if (!languages.Any(l => l.Id != 0) && movieFileSceneName.IsNotNullOrWhiteSpace())
{
languages = LanguageParser.ParseLanguages(movieFileSceneName);
}
if (!languages.Any(l => l.Id != 0))
{
languages = new List<Language> { Language.FindById(movieLanguages[movieId]) };
}
if (movieFileSceneName.IsNotNullOrWhiteSpace())
{
// Store languages for this scenerelease so we can use in history later
releaseLanguages[movieFileSceneName] = languages;
}
movieFileLanguages[movieFileId] = languages;
}
}
}
var historyLanguages = new Dictionary<int, List<Language>>();
using (IDbCommand getSeriesCmd = conn.CreateCommand())
{
getSeriesCmd.Transaction = tran;
getSeriesCmd.CommandText = @"SELECT Id, SourceTitle, MovieId FROM History";
using (IDataReader historyReader = getSeriesCmd.ExecuteReader())
{
while (historyReader.Read())
{
var historyId = historyReader.GetInt32(0);
var historySourceTitle = historyReader.IsDBNull(1) ? null : historyReader.GetString(1);
var movieId = historyReader.GetInt32(2);
var languages = new List<Language>();
if (historySourceTitle.IsNotNullOrWhiteSpace() && releaseLanguages.ContainsKey(historySourceTitle))
{
languages = releaseLanguages[historySourceTitle];
}
if (!languages.Any(l => l.Id != 0) && historySourceTitle.IsNotNullOrWhiteSpace())
{
languages = LanguageParser.ParseLanguages(historySourceTitle);
}
if (!languages.Any(l => l.Id != 0))
{
languages = new List<Language> { Language.FindById(movieLanguages[movieId]) };
}
historyLanguages[historyId] = languages;
}
}
}
var blacklistLanguages = new Dictionary<int, List<Language>>();
using (IDbCommand getSeriesCmd = conn.CreateCommand())
{
getSeriesCmd.Transaction = tran;
getSeriesCmd.CommandText = @"SELECT Id, SourceTitle, MovieId FROM Blacklist";
using (IDataReader blacklistReader = getSeriesCmd.ExecuteReader())
{
while (blacklistReader.Read())
{
var blacklistId = blacklistReader.GetInt32(0);
var blacklistSourceTitle = blacklistReader.IsDBNull(1) ? null : blacklistReader.GetString(1);
var movieId = blacklistReader.GetInt32(2);
var languages = new List<Language>();
if (blacklistSourceTitle.IsNotNullOrWhiteSpace())
{
languages = LanguageParser.ParseLanguages(blacklistSourceTitle);
}
if (!languages.Any(l => l.Id != 0))
{
languages = new List<Language> { Language.FindById(movieLanguages[movieId]) };
}
blacklistLanguages[blacklistId] = languages;
}
}
}
foreach (var group in movieFileLanguages.GroupBy(v => v.Value, v => v.Key))
{
var languages = group.Key;
var movieFileIds = group.Select(v => v.ToString()).Join(",");
using (IDbCommand updateMovieFilesCmd = conn.CreateCommand())
{
updateMovieFilesCmd.Transaction = tran;
updateMovieFilesCmd.CommandText = $"UPDATE MovieFiles SET Languages = ? WHERE MovieId IN ({movieIds})";
updateMovieFilesCmd.CommandText = $"UPDATE MovieFiles SET Languages = ? WHERE Id IN ({movieFileIds})";
var param = updateMovieFilesCmd.CreateParameter();
languageConverter.SetValue(param, language);
languageConverter.SetValue(param, languages);
updateMovieFilesCmd.Parameters.Add(param);
updateMovieFilesCmd.ExecuteNonQuery();
}
}
foreach (var group in historyLanguages.GroupBy(v => v.Value, v => v.Key))
{
var languages = group.Key;
var historyIds = group.Select(v => v.ToString()).Join(",");
using (IDbCommand updateHistoryCmd = conn.CreateCommand())
{
updateHistoryCmd.Transaction = tran;
updateHistoryCmd.CommandText = $"UPDATE History SET Languages = ? WHERE MovieId IN ({movieIds})";
updateHistoryCmd.CommandText = $"UPDATE History SET Languages = ? WHERE Id IN ({historyIds})";
var param = updateHistoryCmd.CreateParameter();
languageConverter.SetValue(param, language);
languageConverter.SetValue(param, languages);
updateHistoryCmd.Parameters.Add(param);
updateHistoryCmd.ExecuteNonQuery();
}
}
foreach (var group in blacklistLanguages.GroupBy(v => v.Value, v => v.Key))
{
var languages = group.Key;
var blacklistIds = group.Select(v => v.ToString()).Join(",");
using (IDbCommand updateBlacklistCmd = conn.CreateCommand())
{
updateBlacklistCmd.Transaction = tran;
updateBlacklistCmd.CommandText = $"UPDATE Blacklist SET Languages = ? WHERE MovieId IN ({movieIds})";
updateBlacklistCmd.CommandText = $"UPDATE Blacklist SET Languages = ? WHERE Id IN ({blacklistIds})";
var param = updateBlacklistCmd.CreateParameter();
languageConverter.SetValue(param, language);
languageConverter.SetValue(param, languages);
updateBlacklistCmd.Parameters.Add(param);
updateBlacklistCmd.ExecuteNonQuery();
@ -118,4 +248,9 @@ private void UpdateLanguage(IDbConnection conn, IDbTransaction tran)
}
}
}
public class MediaInfo154
{
public string AudioLanguages { get; set; }
}
}