diff --git a/PlexRequests.Core/PlexRequests.Core.csproj b/PlexRequests.Core/PlexRequests.Core.csproj index 13a1e74ec..735938db4 100644 --- a/PlexRequests.Core/PlexRequests.Core.csproj +++ b/PlexRequests.Core/PlexRequests.Core.csproj @@ -46,6 +46,10 @@ ..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll True + + ..\packages\NLog.4.2.3\lib\net45\NLog.dll + True + ..\packages\Octokit.0.19.0\lib\net45\Octokit.dll True diff --git a/PlexRequests.Core/Setup.cs b/PlexRequests.Core/Setup.cs index 93d8003e7..0dc10a44d 100644 --- a/PlexRequests.Core/Setup.cs +++ b/PlexRequests.Core/Setup.cs @@ -30,6 +30,7 @@ using System.Collections.Generic; using System.Linq; using Mono.Data.Sqlite; +using NLog; using PlexRequests.Api; using PlexRequests.Core.SettingModels; using PlexRequests.Helpers; @@ -40,6 +41,9 @@ 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() { @@ -53,11 +57,40 @@ namespace PlexRequests.Core } MigrateDb(); + CheckSchema(); return Db.DbConnection().ConnectionString; } public static string ConnectionString => Db.DbConnection().ConnectionString; + + private void CheckSchema() + { + var connection = Db.DbConnection(); + var schema = connection.GetSchemaVersion(); + if (schema == null) + { + connection.CreateSchema(); // Set the default. + schema = connection.GetSchemaVersion(); + } + + var version = schema.SchemaVersion; + if (version == 0) + { + connection.UpdateSchemaVersion(SchemaVersion); + try + { + TableCreation.AlterTable(Db.DbConnection(), "RequestBlobs", "ADD COLUMN", "MusicId", false, "INTEGER"); + } + catch (Exception e) + { + Log.Error("Tried updating the schema to version 1"); + Log.Error(e); + } + return; + } + } + private void CreateDefaultSettingsPage() { var defaultSettings = new PlexRequestSettings @@ -74,6 +107,7 @@ namespace PlexRequests.Core private void MigrateDb() // TODO: Remove in v1.7 { + var result = new List(); RequestedModel[] requestedModels; var repo = new GenericRepository(Db, new MemoryCacheProvider()); @@ -121,7 +155,7 @@ namespace PlexRequests.Core result.Add(id); } - foreach (var source in requestedModels.Where(x => x.Type== RequestType.Movie)) + foreach (var source in requestedModels.Where(x => x.Type == RequestType.Movie)) { var id = jsonRepo.AddRequest(source); result.Add(id); diff --git a/PlexRequests.Core/packages.config b/PlexRequests.Core/packages.config index ddcb2361b..6fae42bd4 100644 --- a/PlexRequests.Core/packages.config +++ b/PlexRequests.Core/packages.config @@ -3,6 +3,7 @@ + \ No newline at end of file diff --git a/PlexRequests.Store/DbConfiguration.cs b/PlexRequests.Store/DbConfiguration.cs index 28cd9cb98..7ddd60483 100644 --- a/PlexRequests.Store/DbConfiguration.cs +++ b/PlexRequests.Store/DbConfiguration.cs @@ -93,5 +93,7 @@ namespace PlexRequests.Store Log.Error(e); } } + + } } diff --git a/PlexRequests.Store/SqlTables.sql b/PlexRequests.Store/SqlTables.sql index f23b3c5fc..a35dcf4b7 100644 --- a/PlexRequests.Store/SqlTables.sql +++ b/PlexRequests.Store/SqlTables.sql @@ -40,3 +40,9 @@ CREATE TABLE IF NOT EXISTS Logs Exception varchar(100) NOT NULL ); CREATE UNIQUE INDEX IF NOT EXISTS Logs_Id ON Logs (Id); + +CREATE TABLE IF NOT EXISTS DBInfo +( + SchemaVersion INTEGER + +); \ No newline at end of file diff --git a/PlexRequests.Store/TableCreation.cs b/PlexRequests.Store/TableCreation.cs index 437105ffc..6b0e07044 100644 --- a/PlexRequests.Store/TableCreation.cs +++ b/PlexRequests.Store/TableCreation.cs @@ -25,7 +25,7 @@ // *********************************************************************** #endregion using System.Data; - +using System.Linq; using Dapper; using Dapper.Contrib.Extensions; @@ -44,6 +44,57 @@ namespace PlexRequests.Store connection.Close(); } + public static void AlterTable(IDbConnection connection, string tableName, string alterType, string newColumn, bool isNullable, string dataType) + { + connection.Open(); + var result = connection.Query($"PRAGMA table_info({tableName});"); + if (result.Any(x => x.name == newColumn)) + { + return; + } + + var query = $"ALTER TABLE {tableName} {alterType} {newColumn} {dataType}"; + if (isNullable) + { + query = query + " NOT NULL"; + } + + connection.Execute(query); + + connection.Close(); + } + + public static DbInfo GetSchemaVersion(this IDbConnection con) + { + con.Open(); + var result = con.Query("SELECT * FROM DBInfo"); + con.Close(); + + return result.FirstOrDefault(); + } + + public static void UpdateSchemaVersion(this IDbConnection con, int version) + { + con.Open(); + con.Query($"UPDATE DBInfo SET SchemaVersion = {version}"); + con.Close(); + } + + public static void CreateSchema(this IDbConnection con) + { + con.Open(); + con.Query("INSERT INTO DBInfo (SchemaVersion) values (0)"); + con.Close(); + } + + + + [Table("DBInfo")] + public class DbInfo + { + public int SchemaVersion { get; set; } + } + [Table("sqlite_master")] public class SqliteMasterTable { @@ -54,5 +105,17 @@ namespace PlexRequests.Store public long rootpage { get; set; } public string sql { get; set; } } + + [Table("table_info")] + public class TableInfo + { + public int cid { get; set; } + public string name { get; set; } + public int notnull { get; set; } + public string dflt_value { get; set; } + public int pk { get; set; } + } + + } } diff --git a/README.md b/README.md index 373655988..efe061498 100644 --- a/README.md +++ b/README.md @@ -84,7 +84,9 @@ end script ####Reboot, then open up your browser to check that it's running! -```sudo shutdown -r 00``` +``` +sudo shutdown -r 00 +``` # Contributors @@ -97,4 +99,4 @@ If you feel like donating you can [here!](https://paypal.me/PlexRequestsNet) ## A massive thanks to everyone below for all their help! -[heartisall](https://github.com/heartisall), [Stuke00](https://github.com/Stuke00), [shiitake](https://github.com/shiitake), [Drewster727](https://github.com/Drewster727) +[heartisall](https://github.com/heartisall), [Stuke00](https://github.com/Stuke00), [shiitake](https://github.com/shiitake), [Drewster727](https://github.com/Drewster727), Majawat