Renamed IndexerStatus.IndexerId to ProviderId.

This commit is contained in:
Taloth Saldono 2017-05-12 19:45:22 +02:00
parent 7b0e40d5d0
commit 14144bd4d9
13 changed files with 73 additions and 15 deletions

View File

@ -44,7 +44,7 @@ namespace NzbDrone.Core.Test.Download.Pending.PendingReleaseServiceTests
{ {
Mocker.GetMock<IIndexerStatusService>() Mocker.GetMock<IIndexerStatusService>()
.Setup(v => v.GetBlockedIndexers()) .Setup(v => v.GetBlockedIndexers())
.Returns(new List<IndexerStatus> { new IndexerStatus { IndexerId = 1, DisabledTill = DateTime.UtcNow.AddHours(2) } }); .Returns(new List<IndexerStatus> { new IndexerStatus { ProviderId = 1, DisabledTill = DateTime.UtcNow.AddHours(2) } });
GivenPendingRelease(); GivenPendingRelease();

View File

@ -40,7 +40,7 @@ namespace NzbDrone.Core.Test.HealthCheck.Checks
{ {
_blockedIndexers.Add(new IndexerStatus _blockedIndexers.Add(new IndexerStatus
{ {
IndexerId = id, ProviderId = id,
InitialFailure = DateTime.UtcNow.AddHours(-failureHours), InitialFailure = DateTime.UtcNow.AddHours(-failureHours),
MostRecentFailure = DateTime.UtcNow.AddHours(-0.1), MostRecentFailure = DateTime.UtcNow.AddHours(-0.1),
EscalationLevel = 5, EscalationLevel = 5,

View File

@ -28,7 +28,7 @@ namespace NzbDrone.Core.Test.Housekeeping.Housekeepers
public void should_delete_orphaned_indexerstatus() public void should_delete_orphaned_indexerstatus()
{ {
var status = Builder<IndexerStatus>.CreateNew() var status = Builder<IndexerStatus>.CreateNew()
.With(h => h.IndexerId = _indexer.Id) .With(h => h.ProviderId = _indexer.Id)
.BuildNew(); .BuildNew();
Db.Insert(status); Db.Insert(status);
@ -42,13 +42,13 @@ namespace NzbDrone.Core.Test.Housekeeping.Housekeepers
GivenIndexer(); GivenIndexer();
var status = Builder<IndexerStatus>.CreateNew() var status = Builder<IndexerStatus>.CreateNew()
.With(h => h.IndexerId = _indexer.Id) .With(h => h.ProviderId = _indexer.Id)
.BuildNew(); .BuildNew();
Db.Insert(status); Db.Insert(status);
Subject.Clean(); Subject.Clean();
AllStoredModels.Should().HaveCount(1); AllStoredModels.Should().HaveCount(1);
AllStoredModels.Should().Contain(h => h.IndexerId == _indexer.Id); AllStoredModels.Should().Contain(h => h.ProviderId == _indexer.Id);
} }
} }
} }

View File

@ -0,0 +1,14 @@
using FluentMigrator;
using NzbDrone.Core.Datastore.Migration.Framework;
namespace NzbDrone.Core.Datastore.Migration
{
[Migration(114)]
public class rename_indexer_status_id : NzbDroneMigrationBase
{
protected override void MainDbUpgrade()
{
Rename.Column("IndexerId").OnTable("IndexerStatus").To("ProviderId");
}
}
}

View File

@ -1,4 +1,5 @@
using System; using System;
using System.Collections.Generic;
using System.Data; using System.Data;
using System.Linq; using System.Linq;
using FluentMigrator; using FluentMigrator;
@ -7,6 +8,7 @@ using FluentMigrator.Model;
using FluentMigrator.Runner; using FluentMigrator.Runner;
using FluentMigrator.Runner.Generators.SQLite; using FluentMigrator.Runner.Generators.SQLite;
using FluentMigrator.Runner.Processors.SQLite; using FluentMigrator.Runner.Processors.SQLite;
using System.Text.RegularExpressions;
namespace NzbDrone.Core.Datastore.Migration.Framework namespace NzbDrone.Core.Datastore.Migration.Framework
{ {
@ -62,6 +64,46 @@ namespace NzbDrone.Core.Datastore.Migration.Framework
ProcessAlterTable(tableDefinition); ProcessAlterTable(tableDefinition);
} }
public override void Process(RenameColumnExpression expression)
{
var tableDefinition = GetTableSchema(expression.TableName);
var oldColumnDefinitions = tableDefinition.Columns.ToList();
var columnDefinitions = tableDefinition.Columns.ToList();
var columnIndex = columnDefinitions.FindIndex(c => c.Name == expression.OldName);
if (columnIndex == -1)
{
throw new ApplicationException(string.Format("Column {0} does not exist on table {1}.", expression.OldName, expression.TableName));
}
if (columnDefinitions.Any(c => c.Name == expression.NewName))
{
throw new ApplicationException(string.Format("Column {0} already exists on table {1}.", expression.NewName, expression.TableName));
}
oldColumnDefinitions[columnIndex] = (ColumnDefinition)columnDefinitions[columnIndex].Clone();
columnDefinitions[columnIndex].Name = expression.NewName;
foreach (var index in tableDefinition.Indexes)
{
if (index.Name.StartsWith("IX_"))
{
index.Name = Regex.Replace(index.Name, "(?<=_)" + Regex.Escape(expression.OldName) + "(?=_|$)", Regex.Escape(expression.NewName));
}
foreach (var column in index.Columns)
{
if (column.Name == expression.OldName)
{
column.Name = expression.NewName;
}
}
}
ProcessAlterTable(tableDefinition, oldColumnDefinitions);
}
protected virtual TableDefinition GetTableSchema(string tableName) protected virtual TableDefinition GetTableSchema(string tableName)
{ {
var schemaDumper = new SqliteSchemaDumper(this, Announcer); var schemaDumper = new SqliteSchemaDumper(this, Announcer);
@ -70,7 +112,7 @@ namespace NzbDrone.Core.Datastore.Migration.Framework
return schema.Single(v => v.Name == tableName); return schema.Single(v => v.Name == tableName);
} }
protected virtual void ProcessAlterTable(TableDefinition tableDefinition) protected virtual void ProcessAlterTable(TableDefinition tableDefinition, List<ColumnDefinition> oldColumnDefinitions = null)
{ {
var tableName = tableDefinition.Name; var tableName = tableDefinition.Name;
var tempTableName = tableName + "_temp"; var tempTableName = tableName + "_temp";
@ -83,11 +125,12 @@ namespace NzbDrone.Core.Datastore.Migration.Framework
// What is the cleanest way to do this? Add function to Generator? // What is the cleanest way to do this? Add function to Generator?
var quoter = new SQLiteQuoter(); var quoter = new SQLiteQuoter();
var columnsToTransfer = string.Join(", ", tableDefinition.Columns.Select(c => quoter.QuoteColumnName(c.Name))); var columnsToInsert = string.Join(", ", tableDefinition.Columns.Select(c => quoter.QuoteColumnName(c.Name)));
var columnsToFetch = string.Join(", ", (oldColumnDefinitions ?? tableDefinition.Columns).Select(c => quoter.QuoteColumnName(c.Name)));
Process(new CreateTableExpression() { TableName = tempTableName, Columns = tableDefinition.Columns.ToList() }); Process(new CreateTableExpression() { TableName = tempTableName, Columns = tableDefinition.Columns.ToList() });
Process(string.Format("INSERT INTO {0} SELECT {1} FROM {2}", quoter.QuoteTableName(tempTableName), columnsToTransfer, quoter.QuoteTableName(tableName))); Process(string.Format("INSERT INTO {0} ({1}) SELECT {2} FROM {3}", quoter.QuoteTableName(tempTableName), columnsToInsert, columnsToFetch, quoter.QuoteTableName(tableName)));
Process(new DeleteTableExpression() { TableName = tableName }); Process(new DeleteTableExpression() { TableName = tableName });

View File

@ -101,7 +101,7 @@ namespace NzbDrone.Core.Download.Pending
private List<ReleaseInfo> FilterBlockedIndexers(List<ReleaseInfo> releases) private List<ReleaseInfo> FilterBlockedIndexers(List<ReleaseInfo> releases)
{ {
var blockedIndexers = new HashSet<int>(_indexerStatusService.GetBlockedIndexers().Select(v => v.IndexerId)); var blockedIndexers = new HashSet<int>(_indexerStatusService.GetBlockedIndexers().Select(v => v.ProviderId));
return releases.Where(release => !blockedIndexers.Contains(release.IndexerId)).ToList(); return releases.Where(release => !blockedIndexers.Contains(release.IndexerId)).ToList();
} }

View File

@ -21,7 +21,7 @@ namespace NzbDrone.Core.HealthCheck.Checks
var enabledIndexers = _indexerFactory.GetAvailableProviders(); var enabledIndexers = _indexerFactory.GetAvailableProviders();
var backOffIndexers = enabledIndexers.Join(_indexerStatusService.GetBlockedIndexers(), var backOffIndexers = enabledIndexers.Join(_indexerStatusService.GetBlockedIndexers(),
i => i.Definition.Id, i => i.Definition.Id,
s => s.IndexerId, s => s.ProviderId,
(i, s) => new { Indexer = i, Status = s }) (i, s) => new { Indexer = i, Status = s })
.Where(v => (v.Status.MostRecentFailure - v.Status.InitialFailure) > TimeSpan.FromHours(1)) .Where(v => (v.Status.MostRecentFailure - v.Status.InitialFailure) > TimeSpan.FromHours(1))
.ToList(); .ToList();

View File

@ -19,7 +19,7 @@ namespace NzbDrone.Core.Housekeeping.Housekeepers
WHERE Id IN ( WHERE Id IN (
SELECT IndexerStatus.Id FROM IndexerStatus SELECT IndexerStatus.Id FROM IndexerStatus
LEFT OUTER JOIN Indexers LEFT OUTER JOIN Indexers
ON IndexerStatus.IndexerId = Indexers.Id ON IndexerStatus.ProviderId = Indexers.Id
WHERE Indexers.Id IS NULL)"); WHERE Indexers.Id IS NULL)");
} }
} }

View File

@ -70,7 +70,7 @@ namespace NzbDrone.Core.Indexers
private IEnumerable<IIndexer> FilterBlockedIndexers(IEnumerable<IIndexer> indexers) private IEnumerable<IIndexer> FilterBlockedIndexers(IEnumerable<IIndexer> indexers)
{ {
var blockedIndexers = _indexerStatusService.GetBlockedIndexers().ToDictionary(v => v.IndexerId, v => v); var blockedIndexers = _indexerStatusService.GetBlockedIndexers().ToDictionary(v => v.ProviderId, v => v);
foreach (var indexer in indexers) foreach (var indexer in indexers)
{ {

View File

@ -6,7 +6,7 @@ namespace NzbDrone.Core.Indexers
{ {
public class IndexerStatus : ModelBase public class IndexerStatus : ModelBase
{ {
public int IndexerId { get; set; } public int ProviderId { get; set; }
public DateTime? InitialFailure { get; set; } public DateTime? InitialFailure { get; set; }
public DateTime? MostRecentFailure { get; set; } public DateTime? MostRecentFailure { get; set; }

View File

@ -20,7 +20,7 @@ namespace NzbDrone.Core.Indexers
public IndexerStatus FindByIndexerId(int indexerId) public IndexerStatus FindByIndexerId(int indexerId)
{ {
return Query.Where(c => c.IndexerId == indexerId).SingleOrDefault(); return Query.Where(c => c.ProviderId == indexerId).SingleOrDefault();
} }
} }
} }

View File

@ -57,7 +57,7 @@ namespace NzbDrone.Core.Indexers
private IndexerStatus GetIndexerStatus(int indexerId) private IndexerStatus GetIndexerStatus(int indexerId)
{ {
return _indexerStatusRepository.FindByIndexerId(indexerId) ?? new IndexerStatus { IndexerId = indexerId }; return _indexerStatusRepository.FindByIndexerId(indexerId) ?? new IndexerStatus { ProviderId = indexerId };
} }
private TimeSpan CalculateBackOffPeriod(IndexerStatus status) private TimeSpan CalculateBackOffPeriod(IndexerStatus status)

View File

@ -251,6 +251,7 @@
<Compile Include="Datastore\Migration\070_delay_profile.cs" /> <Compile Include="Datastore\Migration\070_delay_profile.cs" />
<Compile Include="Datastore\Migration\102_add_language_to_episodeFiles_history_and_blacklist.cs" /> <Compile Include="Datastore\Migration\102_add_language_to_episodeFiles_history_and_blacklist.cs" />
<Compile Include="Datastore\Migration\113_consolidate_indexer_baseurl.cs" /> <Compile Include="Datastore\Migration\113_consolidate_indexer_baseurl.cs" />
<Compile Include="Datastore\Migration\114_rename_indexer_status_id.cs" />
<Compile Include="Datastore\Migration\112_added_regex_to_scenemapping.cs" /> <Compile Include="Datastore\Migration\112_added_regex_to_scenemapping.cs" />
<Compile Include="Datastore\Migration\110_fix_extra_files_config.cs" /> <Compile Include="Datastore\Migration\110_fix_extra_files_config.cs" />
<Compile Include="Datastore\Migration\109_import_extra_files.cs" /> <Compile Include="Datastore\Migration\109_import_extra_files.cs" />