Added a DBSchema so we have an easier way to update the DB

This commit is contained in:
tidusjar 2016-04-01 22:13:12 +01:00
commit 425302ba12
7 changed files with 116 additions and 4 deletions

View file

@ -46,6 +46,10 @@
<HintPath>..\packages\Newtonsoft.Json.8.0.2\lib\net45\Newtonsoft.Json.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="NLog, Version=4.0.0.0, Culture=neutral, PublicKeyToken=5120e14c03d0593c, processorArchitecture=MSIL">
<HintPath>..\packages\NLog.4.2.3\lib\net45\NLog.dll</HintPath>
<Private>True</Private>
</Reference>
<Reference Include="Octokit, Version=0.19.0.0, Culture=neutral, processorArchitecture=MSIL">
<HintPath>..\packages\Octokit.0.19.0\lib\net45\Octokit.dll</HintPath>
<Private>True</Private>

View file

@ -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<long>();
RequestedModel[] requestedModels;
var repo = new GenericRepository<RequestedModel>(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);

View file

@ -3,6 +3,7 @@
<package id="Nancy" version="1.4.3" targetFramework="net452" />
<package id="Nancy.Authentication.Forms" version="1.4.1" targetFramework="net452" />
<package id="Newtonsoft.Json" version="8.0.2" targetFramework="net452" />
<package id="NLog" version="4.2.3" targetFramework="net46" />
<package id="Octokit" version="0.19.0" targetFramework="net46" />
<package id="valueinjecter" version="3.1.1.2" targetFramework="net452" />
</packages>