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

pull/130/head
tidusjar 9 years ago
parent 2ad36fa20f
commit 425302ba12

@ -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>

@ -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);

@ -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>

@ -93,5 +93,7 @@ namespace PlexRequests.Store
Log.Error(e);
}
}
}
}

@ -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
);

@ -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<TableInfo>($"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<DbInfo>("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; }
}
}
}

@ -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

Loading…
Cancel
Save