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

pull/3127/head
Taloth Saldono 6 years ago
parent 83c09b4540
commit 9e68653949

@ -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,42 +31,58 @@ namespace NzbDrone.Core.Datastore.Migration
{
var LanguageConverter = new EmbeddedDocumentConverter(new LanguageIntConverter());
using (IDbCommand getSeriesCmd = conn.CreateCommand())
{
getSeriesCmd.Transaction = tran;
getSeriesCmd.CommandText = @"SELECT Id, ProfileId FROM Series";
using (IDataReader seriesReader = getSeriesCmd.ExecuteReader())
{
while (seriesReader.Read())
{
var seriesId = seriesReader.GetInt32(0);
var seriesProfileId = seriesReader.GetInt32(1);
var profileLanguages = new Dictionary<int, int>();
using (IDbCommand getProfileCmd = conn.CreateCommand())
{
getProfileCmd.Transaction = tran;
getProfileCmd.CommandText = "SELECT Language FROM Profiles WHERE Id = ?";
getProfileCmd.AddParameter(seriesProfileId);
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(0);
} catch (InvalidCastException e)
episodeLanguage = profilesReader.GetInt32(1);
}
catch (InvalidCastException e)
{
_logger.Debug("Language field not found in Profiles, using English as default." + e.Message);
}
var validJson = LanguageConverter.ToDB(Language.FindById(episodeLanguage));
profileLanguages[profileId] = episodeLanguage;
}
}
var seriesLanguages = new Dictionary<int, int>();
using (IDbCommand getSeriesCmd = conn.CreateCommand())
{
getSeriesCmd.Transaction = tran;
getSeriesCmd.CommandText = @"SELECT Id, ProfileId FROM Series";
using (IDataReader seriesReader = getSeriesCmd.ExecuteReader())
{
while (seriesReader.Read())
{
var seriesId = seriesReader.GetInt32(0);
var seriesProfileId = seriesReader.GetInt32(1);
seriesLanguages[seriesId] = profileLanguages.GetValueOrDefault(seriesProfileId, Language.English.Id);
}
}
}
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 @@ namespace NzbDrone.Core.Datastore.Migration
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 @@ namespace NzbDrone.Core.Datastore.Migration
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 @@ namespace NzbDrone.Core.Datastore.Migration
}
}
}
}
}
}
}

Loading…
Cancel
Save