Fix apprise server url migration

This commit is contained in:
Bogdan 2023-06-25 05:14:08 +03:00
parent c4af3e746f
commit 20bcc00662
2 changed files with 90 additions and 17 deletions

View file

@ -0,0 +1,62 @@
using System.Collections.Generic;
using System.Linq;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Common.Serializer;
using NzbDrone.Core.Datastore.Migration;
using NzbDrone.Core.Test.Framework;
namespace NzbDrone.Core.Test.Datastore.Migration
{
[TestFixture]
public class apprise_server_urlFixture : MigrationTest<apprise_server_url>
{
[Test]
public void should_rename_server_url_setting_for_apprise()
{
var db = WithMigrationTestDb(c =>
{
c.Insert.IntoTable("Notifications").Row(new
{
Name = "Apprise",
Implementation = "Apprise",
Settings = new
{
BaseUrl = "http://localhost:8000",
NotificationType = 0
}.ToJson(),
ConfigContract = "AppriseSettings",
OnHealthIssue = true,
IncludeHealthWarnings = true,
OnApplicationUpdate = true,
OnGrab = true,
IncludeManualGrabs = true
});
});
var items = db.Query<NotificationDefinition31>("SELECT * FROM \"Notifications\"");
items.Should().HaveCount(1);
items.First().Settings.Should().NotContainKey("baseUrl");
items.First().Settings.Should().ContainKey("serverUrl");
items.First().Settings.GetValueOrDefault("serverUrl").Should().Be("http://localhost:8000");
}
}
public class NotificationDefinition31
{
public int Id { get; set; }
public int Priority { get; set; }
public string Name { get; set; }
public string Implementation { get; set; }
public Dictionary<string, string> Settings { get; set; }
public string ConfigContract { get; set; }
public bool OnHealthIssue { get; set; }
public bool IncludeHealthWarnings { get; set; }
public bool OnApplicationUpdate { get; set; }
public bool OnGrab { get; set; }
public bool IncludeManualGrabs { get; set; }
public List<int> Tags { get; set; }
}
}

View file

@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Data;
using Dapper;
using FluentMigrator;
@ -17,33 +18,43 @@ protected override void MainDbUpgrade()
private void MigrateToServerUrl(IDbConnection conn, IDbTransaction tran)
{
using var selectCommand = conn.CreateCommand();
selectCommand.Transaction = tran;
selectCommand.CommandText = "SELECT \"Id\", \"Settings\" FROM \"Notifications\" WHERE \"Implementation\" = 'Apprise'";
var updatedNotifications = new List<object>();
using var reader = selectCommand.ExecuteReader();
while (reader.Read())
using (var selectCommand = conn.CreateCommand())
{
var id = reader.GetInt32(0);
var settings = reader.GetString(1);
selectCommand.Transaction = tran;
selectCommand.CommandText = "SELECT \"Id\", \"Settings\" FROM \"Notifications\" WHERE \"Implementation\" = 'Apprise'";
if (!string.IsNullOrWhiteSpace(settings))
using var reader = selectCommand.ExecuteReader();
while (reader.Read())
{
var jsonObject = Json.Deserialize<JObject>(settings);
var id = reader.GetInt32(0);
var settings = reader.GetString(1);
if (jsonObject.ContainsKey("baseUrl"))
if (!string.IsNullOrWhiteSpace(settings))
{
jsonObject.Add("serverUrl", jsonObject.Value<string>("baseUrl"));
jsonObject.Remove("baseUrl");
var jsonObject = Json.Deserialize<JObject>(settings);
if (jsonObject.ContainsKey("baseUrl"))
{
jsonObject.Add("serverUrl", jsonObject.Value<string>("baseUrl"));
jsonObject.Remove("baseUrl");
}
settings = jsonObject.ToJson();
}
settings = jsonObject.ToJson();
updatedNotifications.Add(new
{
Id = id,
Settings = settings
});
}
var parameters = new { Settings = settings, Id = id };
conn.Execute("UPDATE Notifications SET Settings = @Settings WHERE Id = @Id", parameters, transaction: tran);
}
var updateNotificationsSql = "UPDATE \"Notifications\" SET \"Settings\" = @Settings WHERE \"Id\" = @Id";
conn.Execute(updateNotificationsSql, updatedNotifications, transaction: tran);
}
}
}