From a486bff40b5795c4dbe3ebb14397cac7b7a0fe9b Mon Sep 17 00:00:00 2001 From: Mark McDowall Date: Mon, 19 Sep 2016 16:10:40 -0700 Subject: [PATCH] Fixed: Migrations using old SQLite versions (Prior to 3.7.15) Closes #1466 --- .../099_extra_and_subtitle_filesFixture.cs | 14 +++++- ...103_fix_metadata_file_extensionsFixture.cs | 36 +++++++++++++++ .../NzbDrone.Core.Test.csproj | 1 + .../Migration/099_extra_and_subtitle_files.cs | 32 ++++++++++++- .../103_fix_metadata_file_extensions.cs | 45 +++++++++++++++++++ src/NzbDrone.Core/NzbDrone.Core.csproj | 1 + 6 files changed, 127 insertions(+), 2 deletions(-) create mode 100644 src/NzbDrone.Core.Test/Datastore/Migration/103_fix_metadata_file_extensionsFixture.cs create mode 100644 src/NzbDrone.Core/Datastore/Migration/103_fix_metadata_file_extensions.cs diff --git a/src/NzbDrone.Core.Test/Datastore/Migration/099_extra_and_subtitle_filesFixture.cs b/src/NzbDrone.Core.Test/Datastore/Migration/099_extra_and_subtitle_filesFixture.cs index 43396618d..d18f37fd1 100644 --- a/src/NzbDrone.Core.Test/Datastore/Migration/099_extra_and_subtitle_filesFixture.cs +++ b/src/NzbDrone.Core.Test/Datastore/Migration/099_extra_and_subtitle_filesFixture.cs @@ -22,12 +22,24 @@ public void should_set_extension_using_relative_path() Type = 3, Consumer = "XbmcMetadata" }); + + c.Insert.IntoTable("MetadataFiles").Row(new + { + SeriesId = 1, + SeasonNumber = 1, + EpisodeFileId = 1, + RelativePath = "Series.Title.S01E01.jpg", + LastUpdated = "2016-05-30 20:23:02.3725923", + Type = 5, + Consumer = "XbmcMetadata" + }); }); var items = db.Query("SELECT * FROM MetadataFiles"); - items.Should().HaveCount(1); + items.Should().HaveCount(2); items.First().Extension.Should().Be(".jpg"); + items.Last().Extension.Should().Be(".jpg"); } } } diff --git a/src/NzbDrone.Core.Test/Datastore/Migration/103_fix_metadata_file_extensionsFixture.cs b/src/NzbDrone.Core.Test/Datastore/Migration/103_fix_metadata_file_extensionsFixture.cs new file mode 100644 index 000000000..86905df18 --- /dev/null +++ b/src/NzbDrone.Core.Test/Datastore/Migration/103_fix_metadata_file_extensionsFixture.cs @@ -0,0 +1,36 @@ +using System.Linq; +using FluentAssertions; +using NUnit.Framework; +using NzbDrone.Core.Datastore.Migration; +using NzbDrone.Core.Test.Framework; + +namespace NzbDrone.Core.Test.Datastore.Migration +{ + [TestFixture] + public class fix_metadata_file_extensionsFixture : MigrationTest + { + [Test] + public void should_fix_extension_when_relative_path_contained_multiple_periods() + { + var db = WithMigrationTestDb(c => + { + c.Insert.IntoTable("MetadataFiles").Row(new + { + SeriesId = 1, + SeasonNumber = 1, + EpisodeFileId = 1, + RelativePath = "Series.Title.S01E01.jpg", + LastUpdated = "2016-05-30 20:23:02.3725923", + Type = 5, + Consumer = "XbmcMetadata", + Extension = ".S01E01.jpg" + }); + }); + + var items = db.Query("SELECT * FROM MetadataFiles"); + + items.Should().HaveCount(1); + items.First().Extension.Should().Be(".jpg"); + } + } +} diff --git a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj index 4c8e12548..41faf14cc 100644 --- a/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj +++ b/src/NzbDrone.Core.Test/NzbDrone.Core.Test.csproj @@ -119,6 +119,7 @@ + diff --git a/src/NzbDrone.Core/Datastore/Migration/099_extra_and_subtitle_files.cs b/src/NzbDrone.Core/Datastore/Migration/099_extra_and_subtitle_files.cs index 2d7f54143..60abad5fb 100644 --- a/src/NzbDrone.Core/Datastore/Migration/099_extra_and_subtitle_files.cs +++ b/src/NzbDrone.Core/Datastore/Migration/099_extra_and_subtitle_files.cs @@ -1,4 +1,5 @@ using System; +using System.Data; using FluentMigrator; using NzbDrone.Core.Datastore.Migration.Framework; @@ -33,10 +34,39 @@ protected override void MainDbUpgrade() .AddColumn("Extension").AsString().Nullable(); // Set Extension using the extension from RelativePath - Execute.Sql("UPDATE MetadataFiles SET Extension = substr(RelativePath, instr(RelativePath, '.'));"); + Execute.WithConnection(SetMetadataFileExtension); Alter.Table("MetadataFiles").AlterColumn("Extension").AsString().NotNullable(); } + + private void SetMetadataFileExtension(IDbConnection conn, IDbTransaction tran) + { + using (var cmd = conn.CreateCommand()) + { + cmd.Transaction = tran; + cmd.CommandText = "SELECT Id, RelativePath FROM MetadataFiles"; + + using (var reader = cmd.ExecuteReader()) + { + while (reader.Read()) + { + var id = reader.GetInt32(0); + var relativePath = reader.GetString(1); + var extension = relativePath.Substring(relativePath.LastIndexOf(".", StringComparison.InvariantCultureIgnoreCase)); + + using (var updateCmd = conn.CreateCommand()) + { + updateCmd.Transaction = tran; + updateCmd.CommandText = "UPDATE MetadataFiles SET Extension = ? WHERE Id = ?"; + updateCmd.AddParameter(extension); + updateCmd.AddParameter(id); + + updateCmd.ExecuteNonQuery(); + } + } + } + } + } } public class MetadataFile99 diff --git a/src/NzbDrone.Core/Datastore/Migration/103_fix_metadata_file_extensions.cs b/src/NzbDrone.Core/Datastore/Migration/103_fix_metadata_file_extensions.cs new file mode 100644 index 000000000..d4ed853a3 --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/103_fix_metadata_file_extensions.cs @@ -0,0 +1,45 @@ +using System; +using System.Data; +using FluentMigrator; +using NzbDrone.Core.Datastore.Migration.Framework; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(103)] + public class fix_metadata_file_extensions : NzbDroneMigrationBase + { + protected override void MainDbUpgrade() + { + Execute.WithConnection(SetMetadataFileExtension); + } + + private void SetMetadataFileExtension(IDbConnection conn, IDbTransaction tran) + { + using (var cmd = conn.CreateCommand()) + { + cmd.Transaction = tran; + cmd.CommandText = "SELECT Id, Extension FROM MetadataFiles"; + + using (var reader = cmd.ExecuteReader()) + { + while (reader.Read()) + { + var id = reader.GetInt32(0); + var extension = reader.GetString(1); + extension = extension.Substring(extension.LastIndexOf(".", StringComparison.InvariantCultureIgnoreCase)); + + using (var updateCmd = conn.CreateCommand()) + { + updateCmd.Transaction = tran; + updateCmd.CommandText = "UPDATE MetadataFiles SET Extension = ? WHERE Id = ?"; + updateCmd.AddParameter(extension); + updateCmd.AddParameter(id); + + updateCmd.ExecuteNonQuery(); + } + } + } + } + } + } +} diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index 6605dd5a5..3aff31e8e 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -251,6 +251,7 @@ +