You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Lidarr/src/NzbDrone.Core/Datastore/Migration/072_history_grabid.cs

67 lines
2.1 KiB

using System;
using System.Collections.Generic;
using System.Data;
using System.Linq;
using FluentMigrator;
using NzbDrone.Common.Extensions;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Datastore.Migration.Framework;
namespace NzbDrone.Core.Datastore.Migration
{
[Migration(72)]
public class history_downloadId : NzbDroneMigrationBase
{
protected override void MainDbUpgrade()
{
Alter.Table("History")
.AddColumn("DownloadId").AsString()
.Nullable()
.Indexed();
Execute.WithConnection(MoveToColumn);
}
private void MoveToColumn(IDbConnection conn, IDbTransaction tran)
{
using (IDbCommand getHistory = conn.CreateCommand())
{
getHistory.Transaction = tran;
getHistory.CommandText = @"SELECT Id, Data FROM History WHERE Data LIKE '%downloadClientId%'";
using (var historyReader = getHistory.ExecuteReader())
{
while (historyReader.Read())
{
var id = historyReader.GetInt32(0);
var data = historyReader.GetString(1);
UpdateHistory(tran, conn, id, data);
}
}
}
}
private void UpdateHistory(IDbTransaction tran, IDbConnection conn, int id, string data)
{
var dic = Json.Deserialize<Dictionary<string, string>>(data);
var downloadId = dic["downloadClientId"];
dic.Remove("downloadClientId");
using (var updateHistoryCmd = conn.CreateCommand())
{
updateHistoryCmd.Transaction = tran;
updateHistoryCmd.CommandText = @"UPDATE History SET DownloadId = ? , Data = ? WHERE Id = ?";
updateHistoryCmd.AddParameter(downloadId);
updateHistoryCmd.AddParameter(dic.ToJson());
updateHistoryCmd.AddParameter(id);
updateHistoryCmd.ExecuteNonQuery();
}
}
}
}