From ace426e69fceac4393ba30ff0f030480883fbd54 Mon Sep 17 00:00:00 2001 From: Leonardo Galli Date: Sun, 30 Apr 2017 14:05:41 +0200 Subject: [PATCH] Added initial migration. --- .../137_add_import_exclusions_table.cs | 50 +++++++++++++++++++ src/NzbDrone.Core/NzbDrone.Core.csproj | 1 + 2 files changed, 51 insertions(+) create mode 100644 src/NzbDrone.Core/Datastore/Migration/137_add_import_exclusions_table.cs diff --git a/src/NzbDrone.Core/Datastore/Migration/137_add_import_exclusions_table.cs b/src/NzbDrone.Core/Datastore/Migration/137_add_import_exclusions_table.cs new file mode 100644 index 000000000..f865e5754 --- /dev/null +++ b/src/NzbDrone.Core/Datastore/Migration/137_add_import_exclusions_table.cs @@ -0,0 +1,50 @@ +using System.Data; +using FluentMigrator; +using NzbDrone.Core.Datastore.Migration.Framework; +using System.Text; +using System.Collections.Generic; +using System.Collections; +using System.Linq; +using System.Text.RegularExpressions; + +namespace NzbDrone.Core.Datastore.Migration +{ + [Migration(137)] + public class add_import_exclusions_table : NzbDroneMigrationBase + { + protected override void MainDbUpgrade() + { + if (!this.Schema.Schema("dbo").Table("ImportExclusions").Exists()) + { + Create.Table("ImportExclusions").WithColumn("tmdbid").AsInt64().NotNullable().Unique().PrimaryKey(); + } + Execute.WithConnection(AddExisting); + } + + private void AddExisting(IDbConnection conn, IDbTransaction tran) + { + using (IDbCommand getSeriesCmd = conn.CreateCommand()) + { + getSeriesCmd.Transaction = tran; + getSeriesCmd.CommandText = @"SELECT Key, Value FROM Config WHERE Key = 'importexclusions'"; + using (IDataReader seriesReader = getSeriesCmd.ExecuteReader()) + { + while (seriesReader.Read()) + { + var Key = seriesReader.GetString(0); + var Value = seriesReader.GetString(1); + var importExclusions = Value.Split(',').Select(x => "(\""+Regex.Replace(x, @"^.*\-(.*)$", "$1")+"\")").ToList(); + + using (IDbCommand updateCmd = conn.CreateCommand()) + { + updateCmd.Transaction = tran; + updateCmd.CommandText = "INSERT INTO ImportExclusions (tmdbid) VALUES " + string.Join(", ", importExclusions); + + updateCmd.ExecuteNonQuery(); + } + } + } + } + } + } +} \ No newline at end of file diff --git a/src/NzbDrone.Core/NzbDrone.Core.csproj b/src/NzbDrone.Core/NzbDrone.Core.csproj index 62f95b9fd..770261960 100644 --- a/src/NzbDrone.Core/NzbDrone.Core.csproj +++ b/src/NzbDrone.Core/NzbDrone.Core.csproj @@ -1280,6 +1280,7 @@ +