Fix possible NullRef in Email Encryption migration

This commit is contained in:
Bogdan 2024-01-21 16:11:28 +02:00 committed by Mark McDowall
parent cab93249ec
commit 271266b10a
2 changed files with 31 additions and 2 deletions

View File

@ -81,6 +81,35 @@ namespace NzbDrone.Core.Test.Datastore.Migration
items.First().ConfigContract.Should().Be("EmailSettings"); items.First().ConfigContract.Should().Be("EmailSettings");
items.First().Settings.UseEncryption.Should().Be((int)EmailEncryptionType.Always); items.First().Settings.UseEncryption.Should().Be((int)EmailEncryptionType.Always);
} }
[Test]
public void should_use_defaults_when_settings_are_empty()
{
var db = WithMigrationTestDb(c =>
{
c.Insert.IntoTable("Notifications").Row(new
{
OnGrab = true,
OnDownload = true,
OnUpgrade = true,
OnHealthIssue = true,
IncludeHealthWarnings = true,
OnRename = true,
Name = "Mail Sonarr",
Implementation = "Email",
Tags = "[]",
Settings = new { }.ToJson(),
ConfigContract = "EmailSettings"
});
});
var items = db.Query<NotificationDefinition201>("SELECT * FROM \"Notifications\"");
items.Should().HaveCount(1);
items.First().Implementation.Should().Be("Email");
items.First().ConfigContract.Should().Be("EmailSettings");
items.First().Settings.UseEncryption.Should().Be((int)EmailEncryptionType.Preferred);
}
} }
public class NotificationDefinition201 public class NotificationDefinition201

View File

@ -31,7 +31,7 @@ namespace NzbDrone.Core.Datastore.Migration
var id = reader.GetInt32(0); var id = reader.GetInt32(0);
var settings = Json.Deserialize<JObject>(reader.GetString(1)); var settings = Json.Deserialize<JObject>(reader.GetString(1));
settings["useEncryption"] = settings["requireEncryption"].ToObject<bool>() ? 1 : 0; settings["useEncryption"] = settings.Value<bool>("requireEncryption") ? 1 : 0;
settings["requireEncryption"] = null; settings["requireEncryption"] = null;
updated.Add(new updated.Add(new
@ -43,7 +43,7 @@ namespace NzbDrone.Core.Datastore.Migration
} }
} }
var updateSql = $"UPDATE \"Notifications\" SET \"Settings\" = @Settings WHERE \"Id\" = @Id"; var updateSql = "UPDATE \"Notifications\" SET \"Settings\" = @Settings WHERE \"Id\" = @Id";
conn.Execute(updateSql, updated, transaction: tran); conn.Execute(updateSql, updated, transaction: tran);
} }
} }