Fixed: Orpheus migration fails on Postgres

pull/1201/head
Qstick 2 years ago
parent 9ff0b90626
commit 25596fc2e8

@ -0,0 +1,58 @@
using System;
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 orpheus_apiFixture : MigrationTest<orpheus_api>
{
[Test]
public void should_convert_and_disable_orpheus_instance()
{
var db = WithMigrationTestDb(c =>
{
c.Insert.IntoTable("Indexers").Row(new
{
Enable = true,
Name = "Orpheus",
Priority = 25,
Added = DateTime.UtcNow,
Implementation = "Orpheus",
Settings = new GazelleIndexerSettings021
{
Username = "some name",
Password = "some pass"
}.ToJson(),
ConfigContract = "GazelleSettings"
});
});
var items = db.Query<IndexerDefinition022>("SELECT \"Id\", \"Enable\", \"ConfigContract\", \"Settings\" FROM \"Indexers\"");
items.Should().HaveCount(1);
items.First().ConfigContract.Should().Be("OrpheusSettings");
items.First().Enable.Should().Be(false);
items.First().Settings.Should().NotContain("username");
items.First().Settings.Should().NotContain("password");
}
}
public class IndexerDefinition022
{
public int Id { get; set; }
public bool Enable { get; set; }
public string ConfigContract { get; set; }
public string Settings { get; set; }
}
public class GazelleIndexerSettings021
{
public string Username { get; set; }
public string Password { get; set; }
}
}

@ -21,7 +21,4 @@
<CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory> <CopyToOutputDirectory>PreserveNewest</CopyToOutputDirectory>
</None> </None>
</ItemGroup> </ItemGroup>
<ItemGroup>
<Folder Include="Datastore\Migration\" />
</ItemGroup>
</Project> </Project>

@ -1,3 +1,4 @@
using System.Collections.Generic;
using System.Data; using System.Data;
using FluentMigrator; using FluentMigrator;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
@ -21,6 +22,8 @@ namespace NzbDrone.Core.Datastore.Migration
cmd.Transaction = tran; cmd.Transaction = tran;
cmd.CommandText = "SELECT \"Id\", \"Settings\" FROM \"Indexers\" WHERE \"Implementation\" = 'Redacted'"; cmd.CommandText = "SELECT \"Id\", \"Settings\" FROM \"Indexers\" WHERE \"Implementation\" = 'Redacted'";
var updatedIndexers = new List<Indexer008>();
using (var reader = cmd.ExecuteReader()) using (var reader = cmd.ExecuteReader())
{ {
while (reader.Read()) while (reader.Read())
@ -45,19 +48,26 @@ namespace NzbDrone.Core.Datastore.Migration
// write new json back to db, switch to new ConfigContract, and disable the indexer // write new json back to db, switch to new ConfigContract, and disable the indexer
settings = jsonObject.ToJson(); settings = jsonObject.ToJson();
using (var updateCmd = conn.CreateCommand())
updatedIndexers.Add(new Indexer008
{ {
updateCmd.Transaction = tran; Id = id,
updateCmd.CommandText = "UPDATE \"Indexers\" SET \"Settings\" = ?, \"ConfigContract\" = ?, \"Enable\" = 0 WHERE \"Id\" = ?"; Settings = settings,
updateCmd.AddParameter(settings); ConfigContract = "RedactedSettings",
updateCmd.AddParameter("RedactedSettings"); Enable = false
updateCmd.AddParameter(id); });
updateCmd.ExecuteNonQuery();
}
} }
} }
} }
} }
} }
public class Indexer008
{
public int Id { get; set; }
public string Settings { get; set; }
public string ConfigContract { get; set; }
public bool Enable { get; set; }
}
} }
} }

@ -1,8 +1,12 @@
using System;
using System.Collections.Generic;
using System.Data; using System.Data;
using Dapper;
using FluentMigrator; using FluentMigrator;
using Newtonsoft.Json.Linq; using Newtonsoft.Json.Linq;
using NzbDrone.Common.Serializer; using NzbDrone.Common.Serializer;
using NzbDrone.Core.Datastore.Migration.Framework; using NzbDrone.Core.Datastore.Migration.Framework;
using static NzbDrone.Core.Datastore.Migration.redacted_api;
namespace NzbDrone.Core.Datastore.Migration namespace NzbDrone.Core.Datastore.Migration
{ {
@ -21,6 +25,8 @@ namespace NzbDrone.Core.Datastore.Migration
cmd.Transaction = tran; cmd.Transaction = tran;
cmd.CommandText = "SELECT \"Id\", \"Settings\" FROM \"Indexers\" WHERE \"Implementation\" = 'Orpheus'"; cmd.CommandText = "SELECT \"Id\", \"Settings\" FROM \"Indexers\" WHERE \"Implementation\" = 'Orpheus'";
var updatedIndexers = new List<Indexer008>();
using (var reader = cmd.ExecuteReader()) using (var reader = cmd.ExecuteReader())
{ {
while (reader.Read()) while (reader.Read())
@ -45,18 +51,20 @@ namespace NzbDrone.Core.Datastore.Migration
// write new json back to db, switch to new ConfigContract, and disable the indexer // write new json back to db, switch to new ConfigContract, and disable the indexer
settings = jsonObject.ToJson(); settings = jsonObject.ToJson();
using (var updateCmd = conn.CreateCommand())
updatedIndexers.Add(new Indexer008
{ {
updateCmd.Transaction = tran; Id = id,
updateCmd.CommandText = "UPDATE \"Indexers\" SET \"Settings\" = ?, \"ConfigContract\" = ?, \"Enable\" = 0 WHERE \"Id\" = ?"; Settings = settings,
updateCmd.AddParameter(settings); ConfigContract = "OrpheusSettings",
updateCmd.AddParameter("OrpheusSettings"); Enable = false
updateCmd.AddParameter(id); });
updateCmd.ExecuteNonQuery();
}
} }
} }
} }
var updateSql = "UPDATE \"Indexers\" SET \"Settings\" = @Settings, \"ConfigContract\" = @ConfigContract, \"Enable\" = @Enable WHERE \"Id\" = @Id";
conn.Execute(updateSql, updatedIndexers, transaction: tran);
} }
} }
} }

Loading…
Cancel
Save