From c52ca41e32a8b7dd6bb5166983b91924e209ba5b Mon Sep 17 00:00:00 2001 From: Drewster727 Date: Sat, 2 Apr 2016 17:04:05 -0500 Subject: [PATCH] remove references to obsolete RequestedBy property + start setting the db schema to the app version, and check that in the future for migrations. --- PlexRequests.Core/Models/StatusModel.cs | 9 ++++++++ PlexRequests.Core/Setup.cs | 30 ++++++++++++++++--------- PlexRequests.Store/TableCreation.cs | 4 ++-- PlexRequests.UI/Modules/SearchModule.cs | 12 +++++----- 4 files changed, 37 insertions(+), 18 deletions(-) diff --git a/PlexRequests.Core/Models/StatusModel.cs b/PlexRequests.Core/Models/StatusModel.cs index bea494dfe..1e04b35e6 100644 --- a/PlexRequests.Core/Models/StatusModel.cs +++ b/PlexRequests.Core/Models/StatusModel.cs @@ -25,11 +25,20 @@ // ************************************************************************/ #endregion +using System.Text.RegularExpressions; + namespace PlexRequests.Core.Models { public class StatusModel { public string Version { get; set; } + public int DBVersion { + get + { + string trimStatus = new Regex("[^0-9]", RegexOptions.Compiled).Replace(Version, string.Empty).PadRight(4, '0'); + return int.Parse(trimStatus); + } + } public bool UpdateAvailable { get; set; } public string UpdateUri { get; set; } public string DownloadUri { get; set; } diff --git a/PlexRequests.Core/Setup.cs b/PlexRequests.Core/Setup.cs index 69d3f9000..b612a6301 100644 --- a/PlexRequests.Core/Setup.cs +++ b/PlexRequests.Core/Setup.cs @@ -36,13 +36,12 @@ using PlexRequests.Core.SettingModels; using PlexRequests.Helpers; using PlexRequests.Store; using PlexRequests.Store.Repository; +using System.Text.RegularExpressions; namespace PlexRequests.Core { public class Setup { - public const int SchemaVersion = 1; - private static Logger Log = LogManager.GetCurrentClassLogger(); private static DbConfiguration Db { get; set; } public string SetupDb() @@ -55,29 +54,39 @@ namespace PlexRequests.Core { CreateDefaultSettingsPage(); } + + var version = CheckSchema(); + if (version > 0) + { + if (version > 1300 && version <= 1699) + { + MigrateDbFrom1300(); + } + } - MigrateDb(); - CheckSchema(); return Db.DbConnection().ConnectionString; } public static string ConnectionString => Db.DbConnection().ConnectionString; - private void CheckSchema() + private int CheckSchema() { + var checker = new StatusChecker(); + var status = checker.GetStatus(); + var connection = Db.DbConnection(); var schema = connection.GetSchemaVersion(); if (schema == null) { - connection.CreateSchema(); // Set the default. + connection.CreateSchema(status.DBVersion); // Set the default. schema = connection.GetSchemaVersion(); } var version = schema.SchemaVersion; if (version == 0) { - connection.UpdateSchemaVersion(SchemaVersion); + connection.UpdateSchemaVersion(status.DBVersion); try { TableCreation.AlterTable(Db.DbConnection(), "RequestBlobs", "ADD COLUMN", "MusicId", false, "TEXT"); @@ -86,9 +95,10 @@ namespace PlexRequests.Core { Log.Error("Tried updating the schema to version 1"); Log.Error(e); + return -1; } - return; } + return version; } private void CreateDefaultSettingsPage() @@ -105,7 +115,7 @@ namespace PlexRequests.Core s.SaveSettings(defaultSettings); } - private void MigrateDb() // TODO: Remove in v1.7 + private void MigrateDbFrom1300() // TODO: Remove in v1.7 { var result = new List(); @@ -147,7 +157,7 @@ namespace PlexRequests.Core Issues = r.Issues, OtherMessage = r.OtherMessage, Overview = show.summary.RemoveHtml(), - RequestedBy = r.RequestedBy, + RequestedUsers = r.AllUsers, // should pull in the RequestedBy property and merge with RequestedUsers RequestedDate = r.ReleaseDate, Status = show.status }; diff --git a/PlexRequests.Store/TableCreation.cs b/PlexRequests.Store/TableCreation.cs index 6b0e07044..717bd2dd9 100644 --- a/PlexRequests.Store/TableCreation.cs +++ b/PlexRequests.Store/TableCreation.cs @@ -80,10 +80,10 @@ namespace PlexRequests.Store con.Close(); } - public static void CreateSchema(this IDbConnection con) + public static void CreateSchema(this IDbConnection con, int version) { con.Open(); - con.Query("INSERT INTO DBInfo (SchemaVersion) values (0)"); + con.Query(string.Format("INSERT INTO DBInfo (SchemaVersion) values ({0})", version)); con.Close(); } diff --git a/PlexRequests.UI/Modules/SearchModule.cs b/PlexRequests.UI/Modules/SearchModule.cs index 77783a7ba..1a84c2996 100644 --- a/PlexRequests.UI/Modules/SearchModule.cs +++ b/PlexRequests.UI/Modules/SearchModule.cs @@ -282,7 +282,7 @@ namespace PlexRequests.UI.Modules var notificationModel = new NotificationModel { Title = model.Title, - User = model.RequestedBy, + User = Username, DateTime = DateTime.Now, NotificationType = NotificationType.NewRequest }; @@ -307,7 +307,7 @@ namespace PlexRequests.UI.Modules var notificationModel = new NotificationModel { Title = model.Title, - User = model.RequestedBy, + User = Username, DateTime = DateTime.Now, NotificationType = NotificationType.NewRequest }; @@ -322,7 +322,7 @@ namespace PlexRequests.UI.Modules Log.Debug("Adding movie to database requests"); var id = RequestService.AddRequest(model); - var notificationModel = new NotificationModel { Title = model.Title, User = model.RequestedBy, DateTime = DateTime.Now, NotificationType = NotificationType.NewRequest }; + var notificationModel = new NotificationModel { Title = model.Title, User = Username, DateTime = DateTime.Now, NotificationType = NotificationType.NewRequest }; NotificationService.Publish(notificationModel); return Response.AsJson(new JsonResponseModel { Result = true, Message = $"{fullMovieName} was successfully added!" }); @@ -427,7 +427,7 @@ namespace PlexRequests.UI.Modules model.Approved = true; Log.Debug("Adding tv to database requests (No approval required & Sonarr)"); RequestService.AddRequest(model); - var notify1 = new NotificationModel { Title = model.Title, User = model.RequestedBy, DateTime = DateTime.Now, NotificationType = NotificationType.NewRequest }; + var notify1 = new NotificationModel { Title = model.Title, User = Username, DateTime = DateTime.Now, NotificationType = NotificationType.NewRequest }; NotificationService.Publish(notify1); return Response.AsJson(new JsonResponseModel { Result = true, Message = $"{fullShowName} was successfully added!" }); @@ -448,7 +448,7 @@ namespace PlexRequests.UI.Modules Log.Debug("Adding tv to database requests (No approval required & SickRage)"); RequestService.AddRequest(model); - var notify2 = new NotificationModel { Title = model.Title, User = model.RequestedBy, DateTime = DateTime.Now, NotificationType = NotificationType.NewRequest }; + var notify2 = new NotificationModel { Title = model.Title, User = Username, DateTime = DateTime.Now, NotificationType = NotificationType.NewRequest }; NotificationService.Publish(notify2); return Response.AsJson(new JsonResponseModel { Result = true, Message = $"{fullShowName} was successfully added!" }); @@ -462,7 +462,7 @@ namespace PlexRequests.UI.Modules RequestService.AddRequest(model); - var notificationModel = new NotificationModel { Title = model.Title, User = model.RequestedBy, DateTime = DateTime.Now, NotificationType = NotificationType.NewRequest }; + var notificationModel = new NotificationModel { Title = model.Title, User = Username, DateTime = DateTime.Now, NotificationType = NotificationType.NewRequest }; NotificationService.Publish(notificationModel); return Response.AsJson(new JsonResponseModel { Result = true, Message = $"{fullShowName} was successfully added!" });