From 14144bd4d9d1f8d4c764fb0f0903d991c7cfb1f9 Mon Sep 17 00:00:00 2001 From: Taloth Saldono Date: Fri, 12 May 2017 19:45:22 +0200 Subject: [PATCH] Renamed IndexerStatus.IndexerId to ProviderId. --- .../PendingReleaseServiceFixture.cs | 2 +- .../Checks/IndexerStatusCheckFixture.cs | 2 +- .../CleanupOrphanedIndexerStatusFixture.cs | 6 +-- .../Migration/114_rename_indexer_status_id.cs | 14 ++++++ .../Framework/NzbDroneSqliteProcessor.cs | 49 +++++++++++++++++-- .../Download/Pending/PendingReleaseService.cs | 2 +- .../HealthCheck/Checks/IndexerStatusCheck.cs | 2 +- .../CleanupOrphanedIndexerStatus.cs | 2 +- src/NzbDrone.Core/Indexers/IndexerFactory.cs | 2 +- src/NzbDrone.Core/Indexers/IndexerStatus.cs | 2 +- .../Indexers/IndexerStatusRepository.cs | 2 +- .../Indexers/IndexerStatusService.cs | 2 +- src/NzbDrone.Core/NzbDrone.Core.csproj | 1 + 13 files changed, 73 insertions(+), 15 deletions(-) create mode 100644 src/NzbDrone.Core/Datastore/Migration/114_rename_indexer_status_id.cs diff --git a/src/NzbDrone.Core.Test/Download/Pending/PendingReleaseServiceTests/PendingReleaseServiceFixture.cs b/src/NzbDrone.Core.Test/Download/Pending/PendingReleaseServiceTests/PendingReleaseServiceFixture.cs index 37f979ba9..b640003dc 100644 --- a/src/NzbDrone.Core.Test/Download/Pending/PendingReleaseServiceTests/PendingReleaseServiceFixture.cs +++ b/src/NzbDrone.Core.Test/Download/Pending/PendingReleaseServiceTests/PendingReleaseServiceFixture.cs @@ -44,7 +44,7 @@ public void should_ignore_pending_items_from_unavailable_indexer() { Mocker.GetMock() .Setup(v => v.GetBlockedIndexers()) - .Returns(new List { new IndexerStatus { IndexerId = 1, DisabledTill = DateTime.UtcNow.AddHours(2) } }); + .Returns(new List { new IndexerStatus { ProviderId = 1, DisabledTill = DateTime.UtcNow.AddHours(2) } }); GivenPendingRelease(); diff --git a/src/NzbDrone.Core.Test/HealthCheck/Checks/IndexerStatusCheckFixture.cs b/src/NzbDrone.Core.Test/HealthCheck/Checks/IndexerStatusCheckFixture.cs index 6592e2a76..8e8a354e4 100644 --- a/src/NzbDrone.Core.Test/HealthCheck/Checks/IndexerStatusCheckFixture.cs +++ b/src/NzbDrone.Core.Test/HealthCheck/Checks/IndexerStatusCheckFixture.cs @@ -40,7 +40,7 @@ private Mock GivenIndexer(int i, double backoffHours, double failureHo { _blockedIndexers.Add(new IndexerStatus { - IndexerId = id, + ProviderId = id, InitialFailure = DateTime.UtcNow.AddHours(-failureHours), MostRecentFailure = DateTime.UtcNow.AddHours(-0.1), EscalationLevel = 5, diff --git a/src/NzbDrone.Core.Test/Housekeeping/Housekeepers/CleanupOrphanedIndexerStatusFixture.cs b/src/NzbDrone.Core.Test/Housekeeping/Housekeepers/CleanupOrphanedIndexerStatusFixture.cs index 189c1672d..c5e757188 100644 --- a/src/NzbDrone.Core.Test/Housekeeping/Housekeepers/CleanupOrphanedIndexerStatusFixture.cs +++ b/src/NzbDrone.Core.Test/Housekeeping/Housekeepers/CleanupOrphanedIndexerStatusFixture.cs @@ -28,7 +28,7 @@ private void GivenIndexer() public void should_delete_orphaned_indexerstatus() { var status = Builder.CreateNew() - .With(h => h.IndexerId = _indexer.Id) + .With(h => h.ProviderId = _indexer.Id) .BuildNew(); Db.Insert(status); @@ -42,13 +42,13 @@ public void should_not_delete_unorphaned_indexerstatus() GivenIndexer(); var status = Builder.CreateNew() - .With(h => h.IndexerId = _indexer.Id) + .With(h => h.ProviderId = _indexer.Id) .BuildNew(); Db.Insert(status); Subject.Clean(); AllStoredModels.Should().HaveCount(1); - AllStoredModels.Should().Contain(h => h.IndexerId == _indexer.Id); + AllStoredModels.Should().Contain(h => h.ProviderId == _indexer.Id); } } } \ No newline at end of file diff --git a/src/NzbDrone.Core/Datastore/Migration/114_rename_indexer_status_id.cs b/src/NzbDrone.Core/Datastore/Migration/114_rename_indexer_status_id.cs new file mode 100644 index 000000000..90accb1a6 --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/114_rename_indexer_status_id.cs @@ -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"); + } + } +} diff --git a/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneSqliteProcessor.cs b/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneSqliteProcessor.cs index 79a9eca45..9d10255e0 100644 --- a/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneSqliteProcessor.cs +++ b/src/NzbDrone.Core/Datastore/Migration/Framework/NzbDroneSqliteProcessor.cs @@ -1,4 +1,5 @@ using System; +using System.Collections.Generic; using System.Data; using System.Linq; using FluentMigrator; @@ -7,6 +8,7 @@ using FluentMigrator.Runner; using FluentMigrator.Runner.Generators.SQLite; using FluentMigrator.Runner.Processors.SQLite; +using System.Text.RegularExpressions; namespace NzbDrone.Core.Datastore.Migration.Framework { @@ -62,6 +64,46 @@ public override void Process(DeleteColumnExpression expression) 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) { var schemaDumper = new SqliteSchemaDumper(this, Announcer); @@ -70,7 +112,7 @@ protected virtual TableDefinition GetTableSchema(string tableName) return schema.Single(v => v.Name == tableName); } - protected virtual void ProcessAlterTable(TableDefinition tableDefinition) + protected virtual void ProcessAlterTable(TableDefinition tableDefinition, List oldColumnDefinitions = null) { var tableName = tableDefinition.Name; var tempTableName = tableName + "_temp"; @@ -83,11 +125,12 @@ protected virtual void ProcessAlterTable(TableDefinition tableDefinition) // What is the cleanest way to do this? Add function to Generator? 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(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 }); diff --git a/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs b/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs index 8585a1704..ae49d5f28 100644 --- a/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs +++ b/src/NzbDrone.Core/Download/Pending/PendingReleaseService.cs @@ -101,7 +101,7 @@ public List GetPending() private List FilterBlockedIndexers(List releases) { - var blockedIndexers = new HashSet(_indexerStatusService.GetBlockedIndexers().Select(v => v.IndexerId)); + var blockedIndexers = new HashSet(_indexerStatusService.GetBlockedIndexers().Select(v => v.ProviderId)); return releases.Where(release => !blockedIndexers.Contains(release.IndexerId)).ToList(); } diff --git a/src/NzbDrone.Core/HealthCheck/Checks/IndexerStatusCheck.cs b/src/NzbDrone.Core/HealthCheck/Checks/IndexerStatusCheck.cs index 29eadb180..275c2cb24 100644 --- a/src/NzbDrone.Core/HealthCheck/Checks/IndexerStatusCheck.cs +++ b/src/NzbDrone.Core/HealthCheck/Checks/IndexerStatusCheck.cs @@ -21,7 +21,7 @@ public override HealthCheck Check() var enabledIndexers = _indexerFactory.GetAvailableProviders(); var backOffIndexers = enabledIndexers.Join(_indexerStatusService.GetBlockedIndexers(), i => i.Definition.Id, - s => s.IndexerId, + s => s.ProviderId, (i, s) => new { Indexer = i, Status = s }) .Where(v => (v.Status.MostRecentFailure - v.Status.InitialFailure) > TimeSpan.FromHours(1)) .ToList(); diff --git a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedIndexerStatus.cs b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedIndexerStatus.cs index b3cf47027..056c6a9c3 100644 --- a/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedIndexerStatus.cs +++ b/src/NzbDrone.Core/Housekeeping/Housekeepers/CleanupOrphanedIndexerStatus.cs @@ -19,7 +19,7 @@ public void Clean() WHERE Id IN ( SELECT IndexerStatus.Id FROM IndexerStatus LEFT OUTER JOIN Indexers - ON IndexerStatus.IndexerId = Indexers.Id + ON IndexerStatus.ProviderId = Indexers.Id WHERE Indexers.Id IS NULL)"); } } diff --git a/src/NzbDrone.Core/Indexers/IndexerFactory.cs b/src/NzbDrone.Core/Indexers/IndexerFactory.cs index c4903c9c7..c4ef7ac79 100644 --- a/src/NzbDrone.Core/Indexers/IndexerFactory.cs +++ b/src/NzbDrone.Core/Indexers/IndexerFactory.cs @@ -70,7 +70,7 @@ public List SearchEnabled(bool filterBlockedIndexers = true) private IEnumerable FilterBlockedIndexers(IEnumerable 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) { diff --git a/src/NzbDrone.Core/Indexers/IndexerStatus.cs b/src/NzbDrone.Core/Indexers/IndexerStatus.cs index 662c9de64..92830490d 100644 --- a/src/NzbDrone.Core/Indexers/IndexerStatus.cs +++ b/src/NzbDrone.Core/Indexers/IndexerStatus.cs @@ -6,7 +6,7 @@ namespace NzbDrone.Core.Indexers { public class IndexerStatus : ModelBase { - public int IndexerId { get; set; } + public int ProviderId { get; set; } public DateTime? InitialFailure { get; set; } public DateTime? MostRecentFailure { get; set; } diff --git a/src/NzbDrone.Core/Indexers/IndexerStatusRepository.cs b/src/NzbDrone.Core/Indexers/IndexerStatusRepository.cs index 8a70b790a..79173ff6b 100644 --- a/src/NzbDrone.Core/Indexers/IndexerStatusRepository.cs +++ b/src/NzbDrone.Core/Indexers/IndexerStatusRepository.cs @@ -20,7 +20,7 @@ public IndexerStatusRepository(IMainDatabase database, IEventAggregator eventAgg public IndexerStatus FindByIndexerId(int indexerId) { - return Query.Where(c => c.IndexerId == indexerId).SingleOrDefault(); + return Query.Where(c => c.ProviderId == indexerId).SingleOrDefault(); } } } diff --git a/src/NzbDrone.Core/Indexers/IndexerStatusService.cs b/src/NzbDrone.Core/Indexers/IndexerStatusService.cs index 8e1bd1fe5..09be1f00b 100644 --- a/src/NzbDrone.Core/Indexers/IndexerStatusService.cs +++ b/src/NzbDrone.Core/Indexers/IndexerStatusService.cs @@ -57,7 +57,7 @@ public ReleaseInfo GetLastRssSyncReleaseInfo(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) diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index cd9d2f262..73b441b72 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -251,6 +251,7 @@ +