Fix possible NullRef in Email Encryption migration

(cherry picked from commit 271266b10ac51ee6dd7a7024d346b631bd5397c2)
This commit is contained in:
Bogdan 2024-01-21 16:11:28 +02:00
parent 1a857d866a
commit 031c8a4250
2 changed files with 32 additions and 2 deletions

View File

@ -83,6 +83,36 @@ namespace NzbDrone.Core.Test.Datastore.Migration
items.First().ConfigContract.Should().Be("EmailSettings");
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,
OnMovieDelete = false,
Name = "Mail Radarr",
Implementation = "Email",
Tags = "[]",
Settings = new { }.ToJson(),
ConfigContract = "EmailSettings"
});
});
var items = db.Query<NotificationDefinition235>("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 NotificationDefinition235

View File

@ -31,7 +31,7 @@ namespace NzbDrone.Core.Datastore.Migration
var id = reader.GetInt32(0);
var settings = Json.Deserialize<JObject>(reader.GetString(1));
settings["useEncryption"] = settings["requireEncryption"].ToObject<bool>() ? 1 : 0;
settings["useEncryption"] = settings.Value<bool?>("requireEncryption") ?? false ? 1 : 0;
settings["requireEncryption"] = null;
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);
}
}