Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Pull Sonarr commit 'New: Parse language tags from existing subtitles files' #9036

Open
wants to merge 1 commit into
base: develop
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
using System;
using System.Collections.Generic;
using System.Linq;
using Dapper;
using FluentAssertions;
using NUnit.Framework;
using NzbDrone.Core.Datastore.Migration;
using NzbDrone.Core.Test.Framework;

namespace NzbDrone.Core.Test.Datastore.Migration
{
[TestFixture]
public class parse_language_tags_from_existing_subtitle_filesFixture : MigrationTest<parse_language_tags_from_existing_subtitle_files>
{
[Test]
public void should_process_file_with_missing_null_language_tags()
{
var now = DateTime.UtcNow;

var db = WithDapperMigrationTestDb(c =>
{
c.Insert.IntoTable("SubtitleFiles").Row(new
{
SeriesId = 1,
SeasonNumber = 1,
EpisodeFileId = 1,
RelativePath = "S01E05.eng.srt",
Added = now,
LastUpdated = now,
Extension = ".srt",
Language = 1
});
});

var files = db.Query<SubtitleFile195>("SELECT * FROM \"SubtitleFiles\"").ToList();

files.Should().HaveCount(1);
files.First().LanguageTags.Should().HaveCount(0);
}

[Test]
public void should_process_file_with_missing_language_tags()
{
var now = DateTime.UtcNow;

var db = WithDapperMigrationTestDb(c =>
{
c.Insert.IntoTable("SubtitleFiles").Row(new
{
SeriesId = 1,
SeasonNumber = 1,
EpisodeFileId = 1,
RelativePath = "S01E05.eng.forced.srt",
Added = now,
LastUpdated = now,
Extension = ".srt",
Language = 1
});
});

var files = db.Query<SubtitleFile195>("SELECT * FROM \"SubtitleFiles\"").ToList();

files.Should().HaveCount(1);

var languageTags = files.First().LanguageTags;

languageTags.Should().HaveCount(1);
languageTags.Should().Contain("forced");
}

[Test]
public void should_not_process_file_with_empty_language_tags()
{
var now = DateTime.UtcNow;

var db = WithDapperMigrationTestDb(c =>
{
c.Insert.IntoTable("SubtitleFiles").Row(new
{
SeriesId = 1,
SeasonNumber = 1,
EpisodeFileId = 1,
RelativePath = "S01E05.eng.srt",
Added = now,
LastUpdated = now,
Extension = ".srt",
Language = 1,
LanguageTags = "[]"
});
});

var files = db.Query<SubtitleFile195>("SELECT * FROM \"SubtitleFiles\"").ToList();

files.Should().HaveCount(1);
files.First().LastUpdated.Should().BeCloseTo(now, TimeSpan.FromMilliseconds(999));
files.First().LanguageTags.Should().HaveCount(0);
}
}

public class SubtitleFile195
{
public int Id { get; set; }
public int SeriesId { get; set; }
public int? EpisodeFileId { get; set; }
public int? SeasonNumber { get; set; }
public string RelativePath { get; set; }
public DateTime Added { get; set; }
public DateTime LastUpdated { get; set; }
public string Extension { get; set; }
public int Language { get; set; }
public List<string> LanguageTags { get; set; }
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
using System;
using System.Collections.Generic;
using System.Data;
using System.Text.Json;
using System.Text.Json.Serialization;
using Dapper;
using FluentMigrator;
using NzbDrone.Core.Datastore.Migration.Framework;
using NzbDrone.Core.Parser;

namespace NzbDrone.Core.Datastore.Migration
{
[Migration(195)]
public class parse_language_tags_from_existing_subtitle_files : NzbDroneMigrationBase
{
protected override void MainDbUpgrade()
{
Execute.WithConnection(UpdateLanguageTags);
}

private void UpdateLanguageTags(IDbConnection conn, IDbTransaction tran)
{
var updatedLanguageTags = new List<object>();
var now = DateTime.Now;

using (var cmd = conn.CreateCommand())
{
cmd.Transaction = tran;
cmd.CommandText = "SELECT \"Id\", \"RelativePath\" FROM \"SubtitleFiles\" WHERE \"LanguageTags\" IS NULL";

using var reader = cmd.ExecuteReader();
while (reader.Read())
{
var id = reader.GetInt32(0);
var relativePath = reader.GetString(1);
var languageTags = LanguageParser.ParseLanguageTags(relativePath);

updatedLanguageTags.Add(new
{
Id = id,
LanguageTags = languageTags
});
}
}

var serializerSettings = new JsonSerializerOptions
{
AllowTrailingCommas = true,
DefaultIgnoreCondition = JsonIgnoreCondition.WhenWritingNull,
PropertyNameCaseInsensitive = true,
DictionaryKeyPolicy = JsonNamingPolicy.CamelCase,
PropertyNamingPolicy = JsonNamingPolicy.CamelCase,
WriteIndented = true
};

var updateSubtitleFilesSql = "UPDATE \"SubtitleFiles\" SET \"LanguageTags\" = @LanguageTags, \"LastUpdated\" = CURRENT_TIMESTAMP WHERE \"Id\" = @Id";
conn.Execute(updateSubtitleFilesSql, updatedLanguageTags, transaction: tran);
}
}
}