From ac8e5320ef85973eaa8c409ed701406e6d8546b4 Mon Sep 17 00:00:00 2001 From: "Jamie.Rees" Date: Fri, 21 Oct 2016 17:25:16 +0100 Subject: [PATCH] Sorting out the current state of migrations --- PlexRequests.Core.Migration/IMigration.cs | 36 +++++++++ .../IMigrationRunner.cs | 7 ++ PlexRequests.Core.Migration/Migrate.cs | 33 +++++++++ .../MigrationAttribute.cs | 43 +++++++++++ .../MigrationRunner.cs | 73 +++++++++++++++++++ .../Migrations/BaseMigration.cs | 40 ++++++++++ .../Migrations/Version195.cs | 42 +++++++++++ .../PlexRequests.Core.Migration.csproj | 70 ++++++++++++++++++ .../Properties/AssemblyInfo.cs | 36 +++++++++ PlexRequests.Core/PlexRequests.Core.csproj | 4 + PlexRequests.Store/SqlTables.sql | 7 ++ PlexRequests.Store/TableCreation.cs | 7 ++ .../NinjectModules/ConfigurationModule.cs | 2 + PlexRequests.UI/PlexRequests.UI.csproj | 4 + PlexRequests.UI/Startup.cs | 5 ++ PlexRequests.sln | 8 +- 16 files changed, 416 insertions(+), 1 deletion(-) create mode 100644 PlexRequests.Core.Migration/IMigration.cs create mode 100644 PlexRequests.Core.Migration/IMigrationRunner.cs create mode 100644 PlexRequests.Core.Migration/Migrate.cs create mode 100644 PlexRequests.Core.Migration/MigrationAttribute.cs create mode 100644 PlexRequests.Core.Migration/MigrationRunner.cs create mode 100644 PlexRequests.Core.Migration/Migrations/BaseMigration.cs create mode 100644 PlexRequests.Core.Migration/Migrations/Version195.cs create mode 100644 PlexRequests.Core.Migration/PlexRequests.Core.Migration.csproj create mode 100644 PlexRequests.Core.Migration/Properties/AssemblyInfo.cs diff --git a/PlexRequests.Core.Migration/IMigration.cs b/PlexRequests.Core.Migration/IMigration.cs new file mode 100644 index 000000000..d9579d56e --- /dev/null +++ b/PlexRequests.Core.Migration/IMigration.cs @@ -0,0 +1,36 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: IMigration.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion + +using System.Data; + +namespace PlexRequests.Core.Migration +{ + public interface IMigration + { + void Start(IDbConnection con); + } +} \ No newline at end of file diff --git a/PlexRequests.Core.Migration/IMigrationRunner.cs b/PlexRequests.Core.Migration/IMigrationRunner.cs new file mode 100644 index 000000000..4ccd615e8 --- /dev/null +++ b/PlexRequests.Core.Migration/IMigrationRunner.cs @@ -0,0 +1,7 @@ +namespace PlexRequests.Core.Migration +{ + public interface IMigrationRunner + { + void MigrateToLatest(); + } +} \ No newline at end of file diff --git a/PlexRequests.Core.Migration/Migrate.cs b/PlexRequests.Core.Migration/Migrate.cs new file mode 100644 index 000000000..e5266aa5b --- /dev/null +++ b/PlexRequests.Core.Migration/Migrate.cs @@ -0,0 +1,33 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: Migrate.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion +namespace PlexRequests.Core.Migration +{ + public class Migrate + { + + } +} \ No newline at end of file diff --git a/PlexRequests.Core.Migration/MigrationAttribute.cs b/PlexRequests.Core.Migration/MigrationAttribute.cs new file mode 100644 index 000000000..b117c1127 --- /dev/null +++ b/PlexRequests.Core.Migration/MigrationAttribute.cs @@ -0,0 +1,43 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: Migration.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion + +using System; + +namespace PlexRequests.Core.Migration +{ + [AttributeUsage(AttributeTargets.Class)] + public class Migration : Attribute + { + public Migration(int version, string description) + { + Version = version; + Description = description; + } + public int Version { get; private set; } + public string Description { get; private set; } + } +} \ No newline at end of file diff --git a/PlexRequests.Core.Migration/MigrationRunner.cs b/PlexRequests.Core.Migration/MigrationRunner.cs new file mode 100644 index 000000000..9d85cf485 --- /dev/null +++ b/PlexRequests.Core.Migration/MigrationRunner.cs @@ -0,0 +1,73 @@ +using System; +using System.Collections.Generic; +using System.Data.Common; +using System.Linq; +using System.Reflection; +using PlexRequests.Store; + +namespace PlexRequests.Core.Migration +{ + public class MigrationRunner : IMigrationRunner + { + public MigrationRunner(ISqliteConfiguration db) + { + Db = db; + } + + private ISqliteConfiguration Db { get; } + + public void MigrateToLatest() + { + var con = Db.DbConnection(); + var versions = GetMigrations().OrderBy(x => x.Key); + + var dbVersion = con.GetSchemaVersion(); + + foreach (var v in versions) + { + if (v.Value.Version > dbVersion.SchemaVersion) + { + var method = v.Key.GetMethod("Start"); + if (method != null) + { + object result = null; + var classInstance = Activator.CreateInstance(v.Key, null); + + var parametersArray = new object[] { Db.DbConnection() }; + + method.Invoke(classInstance, parametersArray); + } + } + } + } + + private Dictionary GetMigrations() + { + var migrationTypes = GetTypesWithHelpAttribute(Assembly.GetAssembly(typeof(MigrationRunner))); + + var version = new Dictionary(); + + foreach (var t in migrationTypes) + { + var customAttributes = (Migration[])t.GetCustomAttributes(typeof(Migration), true); + if (customAttributes.Length > 0) + { + var attr = customAttributes[0]; + + version.Add(t, new MigrationModel { Version = attr.Version, Description = attr.Description }); + } + } + return version; + } + private static IEnumerable GetTypesWithHelpAttribute(Assembly assembly) + { + return assembly.GetTypes().Where(type => type.GetCustomAttributes(typeof(Migration), true).Length > 0); + } + + private class MigrationModel + { + public int Version { get; set; } + public string Description { get; set; } + } + } +} diff --git a/PlexRequests.Core.Migration/Migrations/BaseMigration.cs b/PlexRequests.Core.Migration/Migrations/BaseMigration.cs new file mode 100644 index 000000000..9df877682 --- /dev/null +++ b/PlexRequests.Core.Migration/Migrations/BaseMigration.cs @@ -0,0 +1,40 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: BaseMigration.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion + +using System.Data; +using System.Data.Common; + +namespace PlexRequests.Core.Migration.Migrations +{ + public abstract class BaseMigration + { + protected void UpdateSchema(IDbConnection con) + { + + } + } +} \ No newline at end of file diff --git a/PlexRequests.Core.Migration/Migrations/Version195.cs b/PlexRequests.Core.Migration/Migrations/Version195.cs new file mode 100644 index 000000000..b1f4921fc --- /dev/null +++ b/PlexRequests.Core.Migration/Migrations/Version195.cs @@ -0,0 +1,42 @@ +#region Copyright +// /************************************************************************ +// Copyright (c) 2016 Jamie Rees +// File: Version195.cs +// Created By: Jamie Rees +// +// Permission is hereby granted, free of charge, to any person obtaining +// a copy of this software and associated documentation files (the +// "Software"), to deal in the Software without restriction, including +// without limitation the rights to use, copy, modify, merge, publish, +// distribute, sublicense, and/or sell copies of the Software, and to +// permit persons to whom the Software is furnished to do so, subject to +// the following conditions: +// +// The above copyright notice and this permission notice shall be +// included in all copies or substantial portions of the Software. +// +// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, +// EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF +// MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND +// NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE +// LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION +// OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION +// WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. +// ************************************************************************/ +#endregion + +using System.Data; + +namespace PlexRequests.Core.Migration.Migrations +{ + [Migration(1950, "v1.9.5.0")] + public class Version195 : BaseMigration, IMigration + { + public void Start(IDbConnection con) + { + + + UpdateSchema(con); + } + } +} \ No newline at end of file diff --git a/PlexRequests.Core.Migration/PlexRequests.Core.Migration.csproj b/PlexRequests.Core.Migration/PlexRequests.Core.Migration.csproj new file mode 100644 index 000000000..db1f55060 --- /dev/null +++ b/PlexRequests.Core.Migration/PlexRequests.Core.Migration.csproj @@ -0,0 +1,70 @@ + + + + + Debug + AnyCPU + {8406EE57-D533-47C0-9302-C6B5F8C31E55} + Library + Properties + PlexRequests.Core.Migration + PlexRequests.Core.Migration + v4.5 + 512 + + + + true + full + false + bin\Debug\ + DEBUG;TRACE + prompt + 4 + + + pdbonly + true + bin\Release\ + TRACE + prompt + 4 + + + + ..\Assemblies\Mono.Data.Sqlite.dll + + + + + + + + + + + + + + + + + + + + + + + {92433867-2B7B-477B-A566-96C382427525} + PlexRequests.Store + + + + + \ No newline at end of file diff --git a/PlexRequests.Core.Migration/Properties/AssemblyInfo.cs b/PlexRequests.Core.Migration/Properties/AssemblyInfo.cs new file mode 100644 index 000000000..6e44d89b7 --- /dev/null +++ b/PlexRequests.Core.Migration/Properties/AssemblyInfo.cs @@ -0,0 +1,36 @@ +using System.Reflection; +using System.Runtime.CompilerServices; +using System.Runtime.InteropServices; + +// General Information about an assembly is controlled through the following +// set of attributes. Change these attribute values to modify the information +// associated with an assembly. +[assembly: AssemblyTitle("PlexRequests.Core.Migration")] +[assembly: AssemblyDescription("")] +[assembly: AssemblyConfiguration("")] +[assembly: AssemblyCompany("")] +[assembly: AssemblyProduct("PlexRequests.Core.Migration")] +[assembly: AssemblyCopyright("Copyright © 2016")] +[assembly: AssemblyTrademark("")] +[assembly: AssemblyCulture("")] + +// Setting ComVisible to false makes the types in this assembly not visible +// to COM components. If you need to access a type in this assembly from +// COM, set the ComVisible attribute to true on that type. +[assembly: ComVisible(false)] + +// The following GUID is for the ID of the typelib if this project is exposed to COM +[assembly: Guid("8406ee57-d533-47c0-9302-c6b5f8c31e55")] + +// Version information for an assembly consists of the following four values: +// +// Major Version +// Minor Version +// Build Number +// Revision +// +// You can specify all the values or you can default the Build and Revision Numbers +// by using the '*' as shown below: +// [assembly: AssemblyVersion("1.0.*")] +[assembly: AssemblyVersion("1.0.0.0")] +[assembly: AssemblyFileVersion("1.0.0.0")] diff --git a/PlexRequests.Core/PlexRequests.Core.csproj b/PlexRequests.Core/PlexRequests.Core.csproj index 143508e1e..af40d401d 100644 --- a/PlexRequests.Core/PlexRequests.Core.csproj +++ b/PlexRequests.Core/PlexRequests.Core.csproj @@ -133,6 +133,10 @@ {8CB8D235-2674-442D-9C6A-35FCAEEB160D} PlexRequests.Api + + {8406ee57-d533-47c0-9302-c6b5f8c31e55} + PlexRequests.Core.Migration + {1252336D-42A3-482A-804C-836E60173DFA} PlexRequests.Helpers diff --git a/PlexRequests.Store/SqlTables.sql b/PlexRequests.Store/SqlTables.sql index 74f31d0be..ef3754069 100644 --- a/PlexRequests.Store/SqlTables.sql +++ b/PlexRequests.Store/SqlTables.sql @@ -59,6 +59,13 @@ CREATE TABLE IF NOT EXISTS DBInfo SchemaVersion INTEGER ); +CREATE TABLE IF NOT EXISTS VersionInfo +( + Version INTEGER NOT NULL, + Description VARCHAR(100) NOT NULL +); + + CREATE TABLE IF NOT EXISTS ScheduledJobs ( Id INTEGER PRIMARY KEY AUTOINCREMENT, diff --git a/PlexRequests.Store/TableCreation.cs b/PlexRequests.Store/TableCreation.cs index 3e6336deb..836a36c54 100644 --- a/PlexRequests.Store/TableCreation.cs +++ b/PlexRequests.Store/TableCreation.cs @@ -117,6 +117,13 @@ namespace PlexRequests.Store public int SchemaVersion { get; set; } } + [Table("VersionInfo")] + public class VersionInfo + { + public int Version { get; set; } + public string Description { get; set; } + } + [Table("sqlite_master")] public class SqliteMasterTable { diff --git a/PlexRequests.UI/NinjectModules/ConfigurationModule.cs b/PlexRequests.UI/NinjectModules/ConfigurationModule.cs index 1382fd9c1..57cb76235 100644 --- a/PlexRequests.UI/NinjectModules/ConfigurationModule.cs +++ b/PlexRequests.UI/NinjectModules/ConfigurationModule.cs @@ -32,6 +32,7 @@ using Nancy.Authentication.Forms; using Ninject.Modules; using PlexRequests.Core; +using PlexRequests.Core.Migration; using PlexRequests.Helpers; using PlexRequests.Services.Interfaces; using PlexRequests.Services.Notification; @@ -47,6 +48,7 @@ namespace PlexRequests.UI.NinjectModules Bind().To().WithConstructorArgument("provider", new SqliteFactory()); Bind().To().WithConstructorArgument("provider", new SqliteFactory()); Bind().To(); + Bind().To(); Bind().To(); diff --git a/PlexRequests.UI/PlexRequests.UI.csproj b/PlexRequests.UI/PlexRequests.UI.csproj index 6c377cad6..d51e3a499 100644 --- a/PlexRequests.UI/PlexRequests.UI.csproj +++ b/PlexRequests.UI/PlexRequests.UI.csproj @@ -754,6 +754,10 @@ {8CB8D235-2674-442D-9C6A-35FCAEEB160D} PlexRequests.Api + + {8406EE57-D533-47C0-9302-C6B5F8C31E55} + PlexRequests.Core.Migration + {DD7DC444-D3BF-4027-8AB9-EFC71F5EC581} PlexRequests.Core diff --git a/PlexRequests.UI/Startup.cs b/PlexRequests.UI/Startup.cs index c61f8766b..992e62bc2 100644 --- a/PlexRequests.UI/Startup.cs +++ b/PlexRequests.UI/Startup.cs @@ -32,7 +32,9 @@ using Ninject.Planning.Bindings.Resolvers; using NLog; using Owin; +using PlexRequests.Core.Migration; using PlexRequests.Services.Jobs; +using PlexRequests.Store; using PlexRequests.UI.Helpers; using PlexRequests.UI.Jobs; using PlexRequests.UI.NinjectModules; @@ -67,6 +69,9 @@ namespace PlexRequests.UI Debug.WriteLine("Finished bootstrapper"); var scheduler = new Scheduler(); scheduler.StartScheduler(); + + var db = kernel.Get(); + db.MigrateToLatest(); } catch (Exception exception) { diff --git a/PlexRequests.sln b/PlexRequests.sln index ec6287fad..831a31b25 100644 --- a/PlexRequests.sln +++ b/PlexRequests.sln @@ -1,7 +1,7 @@  Microsoft Visual Studio Solution File, Format Version 12.00 # Visual Studio 14 -VisualStudioVersion = 14.0.25123.0 +VisualStudioVersion = 14.0.25420.1 MinimumVisualStudioVersion = 10.0.40219.1 Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PlexRequests.UI", "PlexRequests.UI\PlexRequests.UI.csproj", "{68F5F5F3-B8BB-4911-875F-6F00AAE04EA6}" EndProject @@ -45,6 +45,8 @@ Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PlexRequests.Automation.Pag EndProject Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PlexRequestes.Automation.Helpers", "PlexRequestes.Automation.Helpers\PlexRequestes.Automation.Helpers.csproj", "{DC8BACEF-C284-4A8F-A9AA-7F49EFABA288}" EndProject +Project("{FAE04EC0-301F-11D3-BF4B-00C04F79EFBC}") = "PlexRequests.Core.Migration", "PlexRequests.Core.Migration\PlexRequests.Core.Migration.csproj", "{8406EE57-D533-47C0-9302-C6B5F8C31E55}" +EndProject Global GlobalSection(SolutionConfigurationPlatforms) = preSolution Debug|Any CPU = Debug|Any CPU @@ -108,6 +110,10 @@ Global {DC8BACEF-C284-4A8F-A9AA-7F49EFABA288}.Debug|Any CPU.ActiveCfg = Debug|Any CPU {DC8BACEF-C284-4A8F-A9AA-7F49EFABA288}.Debug|Any CPU.Build.0 = Debug|Any CPU {DC8BACEF-C284-4A8F-A9AA-7F49EFABA288}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8406EE57-D533-47C0-9302-C6B5F8C31E55}.Debug|Any CPU.ActiveCfg = Debug|Any CPU + {8406EE57-D533-47C0-9302-C6B5F8C31E55}.Debug|Any CPU.Build.0 = Debug|Any CPU + {8406EE57-D533-47C0-9302-C6B5F8C31E55}.Release|Any CPU.ActiveCfg = Release|Any CPU + {8406EE57-D533-47C0-9302-C6B5F8C31E55}.Release|Any CPU.Build.0 = Release|Any CPU EndGlobalSection GlobalSection(SolutionProperties) = preSolution HideSolutionNode = FALSE