Added transactions around all of the CUD operations

pull/3049/head
Jamie Rees 5 years ago
parent 135e9842f6
commit 1fa034908c

@ -72,7 +72,11 @@ namespace Ombi.Schedule.Jobs.Couchpotato
if (movies != null)
{
// Let's remove the old cached data
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.Database.ExecuteSqlCommandAsync("DELETE FROM CouchPotatoCache");
tran.Commit();
}
// Save
var movieIds = new List<CouchPotatoCache>();
@ -92,9 +96,14 @@ namespace Ombi.Schedule.Jobs.Couchpotato
_log.LogError("TMDBId is not > 0 for movie {0}", m.title);
}
}
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.CouchPotatoCache.AddRangeAsync(movieIds);
await _ctx.SaveChangesAsync();
tran.Commit();
}
}
}
catch (Exception e)

@ -48,7 +48,11 @@ namespace Ombi.Schedule.Jobs.Lidarr
if (albums != null && albums.Any())
{
// Let's remove the old cached data
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.Database.ExecuteSqlCommandAsync("DELETE FROM LidarrAlbumCache");
tran.Commit();
}
var albumCache = new List<LidarrAlbumCache>();
foreach (var a in albums)
@ -68,9 +72,14 @@ namespace Ombi.Schedule.Jobs.Lidarr
});
}
}
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.LidarrAlbumCache.AddRangeAsync(albumCache);
await _ctx.SaveChangesAsync();
tran.Commit();
}
}
}
catch (System.Exception ex)

@ -49,7 +49,11 @@ namespace Ombi.Schedule.Jobs.Lidarr
if (artists != null && artists.Any())
{
// Let's remove the old cached data
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.Database.ExecuteSqlCommandAsync("DELETE FROM LidarrArtistCache");
tran.Commit();
}
var artistCache = new List<LidarrArtistCache>();
foreach (var a in artists)
@ -65,9 +69,14 @@ namespace Ombi.Schedule.Jobs.Lidarr
});
}
}
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.LidarrArtistCache.AddRangeAsync(artistCache);
await _ctx.SaveChangesAsync();
tran.Commit();
}
}
}
catch (Exception ex)

@ -199,7 +199,7 @@ namespace Ombi.Schedule.Jobs.Plex
if (count > 30)
{
await Repo.SaveChangesAsync();
count = 0;
}
}

@ -47,7 +47,11 @@ namespace Ombi.Schedule.Jobs.Radarr
if (movies != null)
{
// Let's remove the old cached data
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.Database.ExecuteSqlCommandAsync("DELETE FROM RadarrCache");
tran.Commit();
}
var movieIds = new List<RadarrCache>();
foreach (var m in movies)
@ -65,9 +69,14 @@ namespace Ombi.Schedule.Jobs.Radarr
Logger.LogError("TMDBId is not > 0 for movie {0}", m.title);
}
}
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.RadarrCache.AddRangeAsync(movieIds);
await _ctx.SaveChangesAsync();
tran.Commit();
}
}
}
catch (System.Exception ex)

@ -46,8 +46,12 @@ namespace Ombi.Schedule.Jobs.SickRage
{
var srShows = shows.data.Values;
var ids = srShows.Select(x => x.tvdbid);
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.Database.ExecuteSqlCommandAsync("DELETE FROM SickRageCache");
tran.Commit();
}
var entites = ids.Select(id => new SickRageCache { TvDbId = id }).ToList();
await _ctx.SickRageCache.AddRangeAsync(entites);
@ -74,8 +78,12 @@ namespace Ombi.Schedule.Jobs.SickRage
}
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.SickRageEpisodeCache.AddRangeAsync(episodesToAdd);
await _ctx.SaveChangesAsync();
tran.Commit();
}
}
}
catch (Exception e)

@ -48,14 +48,22 @@ namespace Ombi.Schedule.Jobs.Sonarr
{
var sonarrSeries = series as ImmutableHashSet<SonarrSeries> ?? series.ToImmutableHashSet();
var ids = sonarrSeries.Select(x => x.tvdbId);
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.Database.ExecuteSqlCommandAsync("DELETE FROM SonarrCache");
tran.Commit();
}
var entites = ids.Select(id => new SonarrCache { TvDbId = id }).ToImmutableHashSet();
await _ctx.SonarrCache.AddRangeAsync(entites);
entites.Clear();
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.Database.ExecuteSqlCommandAsync("DELETE FROM SonarrEpisodeCache");
tran.Commit();
}
foreach (var s in sonarrSeries)
{
if (!s.monitored)
@ -68,7 +76,10 @@ namespace Ombi.Schedule.Jobs.Sonarr
// Add to DB
_log.LogDebug("We have the episodes, adding to db transaction");
await _ctx.SonarrEpisodeCache.AddRangeAsync(monitoredEpisodes.Select(episode => new SonarrEpisodeCache
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
await _ctx.SonarrEpisodeCache.AddRangeAsync(monitoredEpisodes.Select(episode =>
new SonarrEpisodeCache
{
EpisodeNumber = episode.episodeNumber,
SeasonNumber = episode.seasonNumber,
@ -77,6 +88,8 @@ namespace Ombi.Schedule.Jobs.Sonarr
}));
_log.LogDebug("Commiting the transaction");
await _ctx.SaveChangesAsync();
tran.Commit();
}
}
}

@ -63,7 +63,12 @@ namespace Ombi.Store.Context
{
// VACUUM;
Database.ExecuteSqlCommand("VACUUM;");
using (var tran = Database.BeginTransaction())
{
SaveChanges();
tran.Commit();
}
}
}
}

@ -88,6 +88,9 @@ namespace Ombi.Store.Context
public void Seed()
{
using (var tran = Database.BeginTransaction())
{
// Make sure we have the API User
var apiUserExists = Users.Any(x => x.UserName.Equals("Api", StringComparison.CurrentCultureIgnoreCase));
@ -101,6 +104,8 @@ namespace Ombi.Store.Context
});
SaveChanges();
tran.Commit();
}
}
//Check if templates exist
@ -237,8 +242,13 @@ namespace Ombi.Store.Context
}
if (needToSave)
{
using (var tran = Database.BeginTransaction())
{
SaveChanges();
tran.Commit();
}
}
}
}

@ -32,6 +32,9 @@ namespace Ombi.Store.Context
}
public void Seed()
{
using (var tran = Database.BeginTransaction())
{
// Add the tokens
var fanArt = ApplicationConfigurations.FirstOrDefault(x => x.Type == ConfigurationTypes.FanartTv);
@ -44,6 +47,7 @@ namespace Ombi.Store.Context
});
SaveChanges();
}
var movieDb = ApplicationConfigurations.FirstOrDefault(x => x.Type == ConfigurationTypes.FanartTv);
if (movieDb == null)
{
@ -54,7 +58,9 @@ namespace Ombi.Store.Context
});
SaveChanges();
}
var notification = ApplicationConfigurations.FirstOrDefault(x => x.Type == ConfigurationTypes.Notification);
var notification =
ApplicationConfigurations.FirstOrDefault(x => x.Type == ConfigurationTypes.Notification);
if (notification == null)
{
ApplicationConfigurations.Add(new ApplicationConfiguration
@ -64,6 +70,8 @@ namespace Ombi.Store.Context
});
SaveChanges();
}
tran.Commit();
}
}
}
}

@ -23,6 +23,8 @@ namespace Ombi.Store.Repository
}
public async Task Record(AuditType type, AuditArea area, string description, string user)
{
using (var tran = await Ctx.Database.BeginTransactionAsync())
{
await Ctx.Audit.AddAsync(new Audit
{
@ -34,6 +36,8 @@ namespace Ombi.Store.Repository
});
await Ctx.SaveChangesAsync();
tran.Commit();
}
}
}
}

@ -94,7 +94,15 @@ namespace Ombi.Store.Repository
TimeSpan.FromSeconds(3)
});
var result = await policy.ExecuteAndCaptureAsync(async () => await _ctx.SaveChangesAsync());
var result = await policy.ExecuteAndCaptureAsync(async () =>
{
using (var tran = await _ctx.Database.BeginTransactionAsync())
{
var r = await _ctx.SaveChangesAsync();
tran.Commit();
return r;
}
});
return result.Result;
}

@ -8,9 +8,9 @@ using Ombi.Store.Entities.Requests;
namespace Ombi.Store.Repository.Requests
{
public class TvRequestRepository : ITvRequestRepository
public class TvRequestRepository : BaseRepository<TvRequests, IOmbiContext>, ITvRequestRepository
{
public TvRequestRepository(IOmbiContext ctx)
public TvRequestRepository(IOmbiContext ctx) : base(ctx)
{
Db = ctx;
}
@ -151,10 +151,5 @@ namespace Ombi.Store.Repository.Requests
await InternalSaveChanges();
}
private async Task<int> InternalSaveChanges()
{
return await Db.SaveChangesAsync();
}
}
}

@ -24,18 +24,29 @@ namespace Ombi.Store.Repository
public GlobalSettings Insert(GlobalSettings entity)
{
//_cache.Remove(GetName(entity.SettingsName));
using (var tran = Db.Database.BeginTransaction())
{
var settings = Db.Settings.Add(entity);
Db.SaveChanges();
tran.Commit();
return settings.Entity;
}
}
public async Task<GlobalSettings> InsertAsync(GlobalSettings entity)
{
using (var tran = Db.Database.BeginTransaction())
{
//_cache.Remove(GetName(entity.SettingsName));
var settings = await Db.Settings.AddAsync(entity).ConfigureAwait(false);
await Db.SaveChangesAsync().ConfigureAwait(false);
var settings = await Db.Settings.AddAsync(entity);
await Db.SaveChangesAsync();
tran.Commit();
return settings.Entity;
}
}
public GlobalSettings Get(string pageName)
@ -75,15 +86,24 @@ namespace Ombi.Store.Repository
public void Delete(GlobalSettings entity)
{
//_cache.Remove(GetName(entity.SettingsName));
using (var tran = Db.Database.BeginTransaction())
{
Db.Settings.Remove(entity);
Db.SaveChanges();
tran.Commit();
}
}
public void Update(GlobalSettings entity)
{
using (var tran = Db.Database.BeginTransaction())
{
Db.Update(entity);
//_cache.Remove(GetName(entity.SettingsName));
Db.SaveChanges();
tran.Commit();
}
}
private string GetName(string entity)
@ -93,7 +113,13 @@ namespace Ombi.Store.Repository
private async Task<int> InternalSaveChanges()
{
return await Db.SaveChangesAsync();
using (var tran = Db.Database.BeginTransaction())
{
var r = await Db.SaveChangesAsync();
tran.Commit();
return r;
}
}
private bool _disposed;

@ -8,9 +8,9 @@ using Ombi.Helpers;
namespace Ombi.Store.Repository
{
public class TokenRepository : ITokenRepository
public class TokenRepository : BaseRepository<Tokens, IOmbiContext>, ITokenRepository
{
public TokenRepository(IOmbiContext db)
public TokenRepository(IOmbiContext db) : base(db)
{
Db = db;
}
@ -27,9 +27,5 @@ namespace Ombi.Store.Repository
{
return Db.Tokens.Where(x => x.Token == tokenId);
}
private async Task<int> InternalSaveChanges()
{
return await Db.SaveChangesAsync();
}
}
}

@ -62,15 +62,25 @@ namespace Ombi
Type = ConfigurationTypes.Url,
Value = "http://*:5000"
};
using (var tran = ctx.Database.BeginTransaction())
{
ctx.ApplicationConfigurations.Add(url);
ctx.SaveChanges();
tran.Commit();
}
urlValue = url.Value;
}
if (!url.Value.Equals(host))
{
url.Value = UrlArgs;
using (var tran = ctx.Database.BeginTransaction())
{
ctx.SaveChanges();
tran.Commit();
}
urlValue = url.Value;
}
@ -83,14 +93,24 @@ namespace Ombi
Type = ConfigurationTypes.BaseUrl,
Value = baseUrl
};
using (var tran = ctx.Database.BeginTransaction())
{
ctx.ApplicationConfigurations.Add(dbBaseUrl);
ctx.SaveChanges();
tran.Commit();
}
}
}
else if (baseUrl.HasValue() && !baseUrl.Equals(dbBaseUrl.Value))
{
dbBaseUrl.Value = baseUrl;
using (var tran = ctx.Database.BeginTransaction())
{
ctx.SaveChanges();
tran.Commit();
}
}
Console.WriteLine($"We are running on {urlValue}");
@ -129,6 +149,9 @@ namespace Ombi
var settings = new SettingsContext();
try
{
using (var tran = settings.Database.BeginTransaction())
{
if (ombi.Settings.Any() && !settings.Settings.Any())
{
@ -149,6 +172,8 @@ namespace Ombi
}
settings.SaveChanges();
tran.Commit();
}
}
catch (Exception e)
{
@ -156,16 +181,24 @@ namespace Ombi
throw;
}
using (var tran = ombi.Database.BeginTransaction())
{
// Now delete the old stuff
if (doneGlobal)
ombi.Database.ExecuteSqlCommand("DELETE FROM GlobalSettings");
if (doneConfig)
ombi.Database.ExecuteSqlCommand("DELETE FROM ApplicationConfiguration");
tran.Commit();
}
// Now migrate all the external stuff
var external = new ExternalContext();
try
{
using (var tran = external.Database.BeginTransaction())
{
if (ombi.PlexEpisode.Any())
{
@ -178,11 +211,13 @@ namespace Ombi
external.PlexSeasonsContent.AddRange(ombi.PlexSeasonsContent.ToList());
ombi.Database.ExecuteSqlCommand("DELETE FROM PlexSeasonsContent");
}
if (ombi.PlexServerContent.Any())
{
external.PlexServerContent.AddRange(ombi.PlexServerContent.ToList());
ombi.Database.ExecuteSqlCommand("DELETE FROM PlexServerContent");
}
if (ombi.EmbyEpisode.Any())
{
external.EmbyEpisode.AddRange(ombi.EmbyEpisode.ToList());
@ -194,36 +229,43 @@ namespace Ombi
external.EmbyContent.AddRange(ombi.EmbyContent.ToList());
ombi.Database.ExecuteSqlCommand("DELETE FROM EmbyContent");
}
if (ombi.RadarrCache.Any())
{
external.RadarrCache.AddRange(ombi.RadarrCache.ToList());
ombi.Database.ExecuteSqlCommand("DELETE FROM RadarrCache");
}
if (ombi.SonarrCache.Any())
{
external.SonarrCache.AddRange(ombi.SonarrCache.ToList());
ombi.Database.ExecuteSqlCommand("DELETE FROM SonarrCache");
}
if (ombi.LidarrAlbumCache.Any())
{
external.LidarrAlbumCache.AddRange(ombi.LidarrAlbumCache.ToList());
ombi.Database.ExecuteSqlCommand("DELETE FROM LidarrAlbumCache");
}
if (ombi.LidarrArtistCache.Any())
{
external.LidarrArtistCache.AddRange(ombi.LidarrArtistCache.ToList());
ombi.Database.ExecuteSqlCommand("DELETE FROM LidarrArtistCache");
}
if (ombi.SickRageEpisodeCache.Any())
{
external.SickRageEpisodeCache.AddRange(ombi.SickRageEpisodeCache.ToList());
ombi.Database.ExecuteSqlCommand("DELETE FROM SickRageEpisodeCache");
}
if (ombi.SickRageCache.Any())
{
external.SickRageCache.AddRange(ombi.SickRageCache.ToList());
ombi.Database.ExecuteSqlCommand("DELETE FROM SickRageCache");
}
if (ombi.CouchPotatoCache.Any())
{
external.CouchPotatoCache.AddRange(ombi.CouchPotatoCache.ToList());
@ -231,6 +273,8 @@ namespace Ombi
}
external.SaveChanges();
tran.Commit();
}
}
catch (Exception e)
{

Loading…
Cancel
Save