Fixed: Migrations using old SQLite versions (Prior to 3.7.15)

Closes #1466
pull/3113/head
Mark McDowall 8 years ago
parent 0de1f3f17a
commit a486bff40b

@ -22,12 +22,24 @@ namespace NzbDrone.Core.Test.Datastore.Migration
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<MetadataFile99>("SELECT * FROM MetadataFiles");
items.Should().HaveCount(1);
items.Should().HaveCount(2);
items.First().Extension.Should().Be(".jpg");
items.Last().Extension.Should().Be(".jpg");
}
}
}

@ -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<fix_metadata_file_extensions>
{
[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<MetadataFile99>("SELECT * FROM MetadataFiles");
items.Should().HaveCount(1);
items.First().Extension.Should().Be(".jpg");
}
}
}

@ -119,6 +119,7 @@
<Compile Include="Datastore\DatabaseRelationshipFixture.cs" />
<Compile Include="Datastore\MappingExtentionFixture.cs" />
<Compile Include="Datastore\MarrDataLazyLoadingFixture.cs" />
<Compile Include="Datastore\Migration\103_fix_metadata_file_extensionsFixture.cs" />
<Compile Include="Datastore\Migration\099_extra_and_subtitle_filesFixture.cs" />
<Compile Include="Datastore\Migration\101_add_ultrahd_quality_in_profilesFixture.cs" />
<Compile Include="Datastore\Migration\071_unknown_quality_in_profileFixture.cs" />

@ -1,4 +1,5 @@
using System;
using System.Data;
using FluentMigrator;
using NzbDrone.Core.Datastore.Migration.Framework;
@ -33,10 +34,39 @@ namespace NzbDrone.Core.Datastore.Migration
.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

@ -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();
}
}
}
}
}
}
}

@ -251,6 +251,7 @@
<Compile Include="Datastore\Migration\070_delay_profile.cs" />
<Compile Include="Datastore\Migration\096_disable_kickass.cs" />
<Compile Include="Datastore\Migration\095_add_additional_episodes_index.cs" />
<Compile Include="Datastore\Migration\103_fix_metadata_file_extensions.cs" />
<Compile Include="Datastore\Migration\101_add_ultrahd_quality_in_profiles.cs" />
<Compile Include="Datastore\Migration\071_unknown_quality_in_profile.cs" />
<Compile Include="Datastore\Migration\072_history_grabid.cs" />

Loading…
Cancel
Save