1
0
mirror of https://github.com/Sonarr/Sonarr.git synced 2024-12-14 11:23:42 +02:00

Fixed: Slow db migration when upgrading from v2 to v3 with a large collection

This commit is contained in:
Taloth Saldono 2019-05-10 23:08:31 +02:00
parent 83c09b4540
commit 9e68653949

View File

@ -1,9 +1,12 @@
using System.Data;
using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using FluentMigrator;
using NzbDrone.Common.Extensions;
using NzbDrone.Core.Datastore.Migration.Framework;
using NzbDrone.Core.Datastore.Converters;
using NzbDrone.Core.Languages;
using System;
namespace NzbDrone.Core.Datastore.Migration
{
@ -28,6 +31,31 @@ private void UpdateLanguage(IDbConnection conn, IDbTransaction tran)
{
var LanguageConverter = new EmbeddedDocumentConverter(new LanguageIntConverter());
var profileLanguages = new Dictionary<int, int>();
using (IDbCommand getProfileCmd = conn.CreateCommand())
{
getProfileCmd.Transaction = tran;
getProfileCmd.CommandText = "SELECT Id, Language FROM Profiles";
IDataReader profilesReader = getProfileCmd.ExecuteReader();
while (profilesReader.Read())
{
var profileId = profilesReader.GetInt32(0);
var episodeLanguage = Language.English.Id;
try
{
episodeLanguage = profilesReader.GetInt32(1);
}
catch (InvalidCastException e)
{
_logger.Debug("Language field not found in Profiles, using English as default." + e.Message);
}
profileLanguages[profileId] = episodeLanguage;
}
}
var seriesLanguages = new Dictionary<int, int>();
using (IDbCommand getSeriesCmd = conn.CreateCommand())
{
getSeriesCmd.Transaction = tran;
@ -39,31 +67,22 @@ private void UpdateLanguage(IDbConnection conn, IDbTransaction tran)
var seriesId = seriesReader.GetInt32(0);
var seriesProfileId = seriesReader.GetInt32(1);
using (IDbCommand getProfileCmd = conn.CreateCommand())
{
getProfileCmd.Transaction = tran;
getProfileCmd.CommandText = "SELECT Language FROM Profiles WHERE Id = ?";
getProfileCmd.AddParameter(seriesProfileId);
IDataReader profilesReader = getProfileCmd.ExecuteReader();
while (profilesReader.Read())
{
var episodeLanguage = Language.English.Id;
try
{
episodeLanguage = profilesReader.GetInt32(0);
} catch (InvalidCastException e)
{
_logger.Debug("Language field not found in Profiles, using English as default." + e.Message);
seriesLanguages[seriesId] = profileLanguages.GetValueOrDefault(seriesProfileId, Language.English.Id);
}
}
}
var validJson = LanguageConverter.ToDB(Language.FindById(episodeLanguage));
foreach (var group in seriesLanguages.GroupBy(v => v.Value, v => v.Key))
{
var languageJson = LanguageConverter.ToDB(Language.FindById(group.Key));
var seriesIds = group.Select(v => v.ToString()).Join(",");
using (IDbCommand updateEpisodeFilesCmd = conn.CreateCommand())
{
updateEpisodeFilesCmd.Transaction = tran;
updateEpisodeFilesCmd.CommandText = "UPDATE EpisodeFiles SET Language = ? WHERE SeriesId = ?";
updateEpisodeFilesCmd.AddParameter(validJson);
updateEpisodeFilesCmd.AddParameter(seriesId);
updateEpisodeFilesCmd.CommandText = $"UPDATE EpisodeFiles SET Language = ? WHERE SeriesId IN ({seriesIds})";
updateEpisodeFilesCmd.AddParameter(languageJson);
updateEpisodeFilesCmd.ExecuteNonQuery();
}
@ -71,9 +90,8 @@ private void UpdateLanguage(IDbConnection conn, IDbTransaction tran)
using (IDbCommand updateHistoryCmd = conn.CreateCommand())
{
updateHistoryCmd.Transaction = tran;
updateHistoryCmd.CommandText = "UPDATE History SET Language = ? WHERE SeriesId = ?";
updateHistoryCmd.AddParameter(validJson);
updateHistoryCmd.AddParameter(seriesId);
updateHistoryCmd.CommandText = $"UPDATE History SET Language = ? WHERE SeriesId IN ({seriesIds})";
updateHistoryCmd.AddParameter(languageJson);
updateHistoryCmd.ExecuteNonQuery();
}
@ -81,9 +99,8 @@ private void UpdateLanguage(IDbConnection conn, IDbTransaction tran)
using (IDbCommand updateBlacklistCmd = conn.CreateCommand())
{
updateBlacklistCmd.Transaction = tran;
updateBlacklistCmd.CommandText = "UPDATE Blacklist SET Language = ? WHERE SeriesId = ?";
updateBlacklistCmd.AddParameter(validJson);
updateBlacklistCmd.AddParameter(seriesId);
updateBlacklistCmd.CommandText = $"UPDATE Blacklist SET Language = ? WHERE SeriesId IN ({seriesIds})";
updateBlacklistCmd.AddParameter(languageJson);
updateBlacklistCmd.ExecuteNonQuery();
}
@ -91,7 +108,3 @@ private void UpdateLanguage(IDbConnection conn, IDbTransaction tran)
}
}
}
}
}
}
}