Wrapped the repo to catch Sqlite corrupt messages.

This commit is contained in:
tidusjar 2016-07-15 15:36:35 +01:00
parent 33ba1db20b
commit c5ad97780f

View file

@ -25,11 +25,13 @@
// ************************************************************************/ // ************************************************************************/
#endregion #endregion
using System;
using System.Collections.Generic; using System.Collections.Generic;
using System.Linq; using System.Linq;
using System.Threading.Tasks; using System.Threading.Tasks;
using Dapper.Contrib.Extensions; using Dapper.Contrib.Extensions;
using Mono.Data.Sqlite;
using NLog; using NLog;
using PlexRequests.Helpers; using PlexRequests.Helpers;
@ -37,6 +39,8 @@ namespace PlexRequests.Store.Repository
{ {
public abstract class BaseGenericRepository<T> where T : class public abstract class BaseGenericRepository<T> where T : class
{ {
private const string CorruptMessage =
"The database is corrupt, this could be due to the application exiting unexpectedly. See here to fix it: http://www.dosomethinghere.com/2013/02/20/fixing-the-sqlite-error-the-database-disk-image-is-malformed/";
protected BaseGenericRepository(ISqliteConfiguration config, ICacheProvider cache) protected BaseGenericRepository(ISqliteConfiguration config, ICacheProvider cache)
{ {
Config = config; Config = config;
@ -51,6 +55,8 @@ namespace PlexRequests.Store.Repository
public abstract Task<T> GetAsync(string id); public abstract Task<T> GetAsync(string id);
public long Insert(T entity) public long Insert(T entity)
{
try
{ {
ResetCache(); ResetCache();
using (var cnn = Config.DbConnection()) using (var cnn = Config.DbConnection())
@ -59,8 +65,16 @@ namespace PlexRequests.Store.Repository
return cnn.Insert(entity); return cnn.Insert(entity);
} }
} }
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
public void Delete(T entity) public void Delete(T entity)
{
try
{ {
ResetCache(); ResetCache();
using (var db = Config.DbConnection()) using (var db = Config.DbConnection())
@ -69,8 +83,16 @@ namespace PlexRequests.Store.Repository
db.Delete(entity); db.Delete(entity);
} }
} }
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
public async Task DeleteAsync(T entity) public async Task DeleteAsync(T entity)
{
try
{ {
ResetCache(); ResetCache();
using (var db = Config.DbConnection()) using (var db = Config.DbConnection())
@ -79,8 +101,16 @@ namespace PlexRequests.Store.Repository
await db.DeleteAsync(entity); await db.DeleteAsync(entity);
} }
} }
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
public bool Update(T entity) public bool Update(T entity)
{
try
{ {
ResetCache(); ResetCache();
using (var db = Config.DbConnection()) using (var db = Config.DbConnection())
@ -89,8 +119,16 @@ namespace PlexRequests.Store.Repository
return db.Update(entity); return db.Update(entity);
} }
} }
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
public async Task<bool> UpdateAsync(T entity) public async Task<bool> UpdateAsync(T entity)
{
try
{ {
ResetCache(); ResetCache();
using (var db = Config.DbConnection()) using (var db = Config.DbConnection())
@ -99,11 +137,18 @@ namespace PlexRequests.Store.Repository
return await db.UpdateAsync(entity); return await db.UpdateAsync(entity);
} }
} }
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
public bool UpdateAll(IEnumerable<T> entity) public bool UpdateAll(IEnumerable<T> entity)
{
try
{ {
ResetCache(); ResetCache();
Log.Trace("Updating all entities");
var result = new HashSet<bool>(); var result = new HashSet<bool>();
using (var db = Config.DbConnection()) using (var db = Config.DbConnection())
@ -116,11 +161,18 @@ namespace PlexRequests.Store.Repository
} }
return result.All(x => true); return result.All(x => true);
} }
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
public async Task<bool> UpdateAllAsync(IEnumerable<T> entity) public async Task<bool> UpdateAllAsync(IEnumerable<T> entity)
{
try
{ {
ResetCache(); ResetCache();
Log.Trace("Updating all entities");
var result = new HashSet<bool>(); var result = new HashSet<bool>();
using (var db = Config.DbConnection()) using (var db = Config.DbConnection())
@ -133,8 +185,16 @@ namespace PlexRequests.Store.Repository
} }
return result.All(x => true); return result.All(x => true);
} }
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
public async Task<int> InsertAsync(T entity) public async Task<int> InsertAsync(T entity)
{
try
{ {
ResetCache(); ResetCache();
using (var cnn = Config.DbConnection()) using (var cnn = Config.DbConnection())
@ -143,6 +203,12 @@ namespace PlexRequests.Store.Repository
return await cnn.InsertAsync(entity); return await cnn.InsertAsync(entity);
} }
} }
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
}
private void ResetCache() private void ResetCache()
{ {
@ -151,25 +217,38 @@ namespace PlexRequests.Store.Repository
} }
public IEnumerable<T> GetAll() public IEnumerable<T> GetAll()
{ {
try
{
using (var db = Config.DbConnection()) using (var db = Config.DbConnection())
{ {
db.Open(); db.Open();
var result = db.GetAll<T>(); var result = db.GetAll<T>();
return result; return result;
} }
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
} }
public async Task<IEnumerable<T>> GetAllAsync() public async Task<IEnumerable<T>> GetAllAsync()
{ {
try
{
using (var db = Config.DbConnection()) using (var db = Config.DbConnection())
{ {
db.Open(); db.Open();
var result = await db.GetAllAsync<T>(); var result = await db.GetAllAsync<T>();
return result; return result;
} }
}
catch (SqliteException e) when (e.ErrorCode == SQLiteErrorCode.Corrupt)
{
Log.Fatal(CorruptMessage);
throw;
}
} }
} }
} }