diff --git a/src/NzbDrone.Core/Datastore/Migration/154_add_language_to_file_history_blacklist.cs b/src/NzbDrone.Core/Datastore/Migration/154_add_language_to_file_history_blacklist.cs index 7099a5b7c..fda7fdaec 100644 --- a/src/NzbDrone.Core/Datastore/Migration/154_add_language_to_file_history_blacklist.cs +++ b/src/NzbDrone.Core/Datastore/Migration/154_add_language_to_file_history_blacklist.cs @@ -3,9 +3,11 @@ using System.Collections.Generic; 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 @@ namespace NzbDrone.Core.Datastore.Migration 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 @@ namespace NzbDrone.Core.Datastore.Migration } } - foreach (var group in movieLanguages.GroupBy(v => v.Value, v => v.Key)) + var movieFileLanguages = new Dictionary>(); + var releaseLanguages = new Dictionary>(); + + 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(movieFilesReader.GetString(3)); + var languages = new List(); + + 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.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>(); + + 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(); + + 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.FindById(movieLanguages[movieId]) }; + } + + historyLanguages[historyId] = languages; + } + } + } + + var blacklistLanguages = new Dictionary>(); + + using (IDbCommand getSeriesCmd = conn.CreateCommand()) { - var language = new List { Language.FindById(group.Key) }; + 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(); + + if (blacklistSourceTitle.IsNotNullOrWhiteSpace()) + { + languages = LanguageParser.ParseLanguages(blacklistSourceTitle); + } - var movieIds = group.Select(v => v.ToString()).Join(","); + if (!languages.Any(l => l.Id != 0)) + { + languages = new List { 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 @@ namespace NzbDrone.Core.Datastore.Migration } } } + + public class MediaInfo154 + { + public string AudioLanguages { get; set; } + } }