In-Memory Database with automatic versioning Backups. When starting the server the newest backup is loaded into memory.
This commit is contained in:
parent
2aae717859
commit
8db786f965
|
@ -27,6 +27,7 @@
|
|||
<PackageReference Include="Swashbuckle.AspNetCore" Version="6.2.3" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Filters" Version="7.0.6" />
|
||||
<PackageReference Include="Swashbuckle.AspNetCore.Filters.Abstractions" Version="7.0.6" />
|
||||
<PackageReference Include="System.Data.SQLite.Core" Version="1.0.118" />
|
||||
</ItemGroup>
|
||||
|
||||
|
||||
|
|
|
@ -20,7 +20,6 @@ public class Controller : ControllerBase
|
|||
|
||||
if (user is null)
|
||||
{
|
||||
Console.WriteLine("I have no user");
|
||||
throw new Exceptions(400,"Null User Exception", "Must provide a user to log in as.", Request.Path.Value!);
|
||||
}
|
||||
|
||||
|
|
|
@ -7,54 +7,61 @@ namespace InnovEnergy.App.Backend.Database;
|
|||
|
||||
public static partial class Db
|
||||
{
|
||||
private static Boolean Insert(Object obj)
|
||||
{
|
||||
var success = Connection.Insert(obj) > 0;
|
||||
if(success) BackupDatabase();
|
||||
return success;
|
||||
}
|
||||
|
||||
public static Boolean Create(Installation installation)
|
||||
{
|
||||
// SQLite wrapper is smart and *modifies* t's Id to the one generated (autoincrement) by the insertion
|
||||
return Connection.Insert(installation) > 0;
|
||||
return Insert(installation);
|
||||
}
|
||||
|
||||
public static Boolean Create(DeletedInstallation installation)
|
||||
{
|
||||
return Connection.Insert(installation) > 0;
|
||||
return Insert(installation);
|
||||
}
|
||||
|
||||
public static Boolean Create(Folder folder)
|
||||
{
|
||||
return Connection.Insert(folder) > 0;
|
||||
return Insert(folder);
|
||||
}
|
||||
|
||||
public static Boolean Create(DeletedFolder folder)
|
||||
{
|
||||
return Connection.Insert(folder) > 0;
|
||||
return Insert(folder);
|
||||
}
|
||||
|
||||
public static Boolean Create(User user)
|
||||
{
|
||||
return Connection.Insert(user) > 0;
|
||||
return Insert(user);
|
||||
}
|
||||
|
||||
public static Boolean Create(DeletedUser user)
|
||||
{
|
||||
return Connection.Insert(user) > 0;
|
||||
return Insert(user);
|
||||
}
|
||||
|
||||
public static Boolean Create(Session session)
|
||||
{
|
||||
return Connection.Insert(session) > 0;
|
||||
return Insert(session);
|
||||
}
|
||||
|
||||
public static Boolean Create(InstallationAccess installationAccess)
|
||||
{
|
||||
return Connection.Insert(installationAccess) > 0;
|
||||
return Insert(installationAccess);
|
||||
}
|
||||
|
||||
public static Boolean Create(FolderAccess folderAccess)
|
||||
{
|
||||
return Connection.Insert(folderAccess) > 0;
|
||||
return Insert(folderAccess);
|
||||
}
|
||||
|
||||
public static Boolean Create(OrderNumber2Installation o2i)
|
||||
{
|
||||
return Connection.Insert(o2i) > 0;
|
||||
return Insert(o2i);
|
||||
}
|
||||
}
|
|
@ -1,12 +1,16 @@
|
|||
using System.Data.SQLite;
|
||||
using System.Reactive.Concurrency;
|
||||
using System.Reactive.Linq;
|
||||
using System.Runtime.InteropServices;
|
||||
using CliWrap;
|
||||
using CliWrap.Buffered;
|
||||
using InnovEnergy.App.Backend.DataTypes;
|
||||
using InnovEnergy.App.Backend.DataTypes.Methods;
|
||||
using InnovEnergy.App.Backend.Relations;
|
||||
using InnovEnergy.Lib.Utils;
|
||||
using Microsoft.Identity.Client;
|
||||
using SQLite;
|
||||
using SQLiteConnection = SQLite.SQLiteConnection;
|
||||
|
||||
|
||||
namespace InnovEnergy.App.Backend.Database;
|
||||
|
@ -16,7 +20,78 @@ public static partial class Db
|
|||
{
|
||||
internal const String DbPath = "./db.sqlite";
|
||||
|
||||
private static SQLiteConnection Connection { get; } = new SQLiteConnection(DbPath);
|
||||
private static SQLiteConnection Connection { get; } = ((Func<SQLiteConnection>)(() =>
|
||||
{
|
||||
var latestDb = new DirectoryInfo(@"DbBackups").GetFiles()
|
||||
.OrderBy(f => f.LastWriteTime)
|
||||
.First().Name;
|
||||
|
||||
var fileConnection = new SQLiteConnection("DbBackups/"+latestDb);
|
||||
|
||||
var memoryConnection = new SQLiteConnection(":memory:");
|
||||
|
||||
// fileConnection.Backup(memoryConnection.DatabasePath);
|
||||
|
||||
memoryConnection.CreateTable<User>();
|
||||
memoryConnection.CreateTable<DeletedUser>();
|
||||
memoryConnection.CreateTable<Installation>();
|
||||
memoryConnection.CreateTable<DeletedInstallation>();
|
||||
memoryConnection.CreateTable<DeletedFolder>();
|
||||
memoryConnection.CreateTable<Folder>();
|
||||
memoryConnection.CreateTable<FolderAccess>();
|
||||
memoryConnection.CreateTable<InstallationAccess>();
|
||||
memoryConnection.CreateTable<Session>();
|
||||
memoryConnection.CreateTable<OrderNumber2Installation>();
|
||||
|
||||
foreach (var obj in fileConnection.Table<Session>())
|
||||
{
|
||||
memoryConnection.Insert(obj);
|
||||
}
|
||||
foreach (var obj in fileConnection.Table<Folder>())
|
||||
{
|
||||
memoryConnection.Insert(obj);
|
||||
}
|
||||
foreach (var obj in fileConnection.Table<Installation>())
|
||||
{
|
||||
memoryConnection.Insert(obj);
|
||||
}
|
||||
foreach (var obj in fileConnection.Table<User>())
|
||||
{
|
||||
memoryConnection.Insert(obj);
|
||||
}
|
||||
foreach (var obj in fileConnection.Table<FolderAccess>())
|
||||
{
|
||||
memoryConnection.Insert(obj);
|
||||
}
|
||||
foreach (var obj in fileConnection.Table<InstallationAccess>())
|
||||
{
|
||||
memoryConnection.Insert(obj);
|
||||
}
|
||||
foreach (var obj in fileConnection.Table<OrderNumber2Installation>())
|
||||
{
|
||||
memoryConnection.Insert(obj);
|
||||
}
|
||||
foreach (var obj in fileConnection.Table<DeletedInstallation>())
|
||||
{
|
||||
memoryConnection.Insert(obj);
|
||||
}
|
||||
foreach (var obj in fileConnection.Table<DeletedUser>())
|
||||
{
|
||||
memoryConnection.Insert(obj);
|
||||
}
|
||||
foreach (var obj in fileConnection.Table<DeletedFolder>())
|
||||
{
|
||||
memoryConnection.Insert(obj);
|
||||
}
|
||||
|
||||
return memoryConnection;
|
||||
}))();
|
||||
|
||||
public static void BackupDatabase()
|
||||
{
|
||||
var filename = "db-" + DateTimeOffset.UtcNow.ToUnixTimeSeconds() + ".sqlite";
|
||||
Connection.Backup("DbBackups/"+filename);
|
||||
}
|
||||
|
||||
public static TableQuery<Session> Sessions => Connection.Table<Session>();
|
||||
public static TableQuery<Folder> Folders => Connection.Table<Folder>();
|
||||
|
|
|
@ -23,8 +23,9 @@ public static partial class Db
|
|||
{
|
||||
FolderAccess .Delete(r => r.FolderId == f.Id);
|
||||
Installations.Delete(r => r.ParentId == f.Id);
|
||||
|
||||
return Folders.Delete(r => r.Id == f.Id) > 0;
|
||||
var delete = Folders.Delete(r => r.Id == f.Id);
|
||||
BackupDatabase();
|
||||
return delete > 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -35,7 +36,9 @@ public static partial class Db
|
|||
Boolean DeleteInstallationAndItsDependencies()
|
||||
{
|
||||
InstallationAccess.Delete(i => i.InstallationId == installation.Id);
|
||||
return Installations.Delete(i => i.Id == installation.Id) > 0;
|
||||
var delete = Installations.Delete(i => i.Id == installation.Id);
|
||||
BackupDatabase();
|
||||
return delete > 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -48,7 +51,9 @@ public static partial class Db
|
|||
FolderAccess .Delete(u => u.UserId == user.Id);
|
||||
InstallationAccess.Delete(u => u.UserId == user.Id);
|
||||
|
||||
return Users.Delete(u => u.Id == user.Id) > 0;
|
||||
var delete = Users.Delete(u => u.Id == user.Id);
|
||||
BackupDatabase();
|
||||
return delete > 0;
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -58,6 +63,8 @@ public static partial class Db
|
|||
// private!!
|
||||
private static Boolean Delete(Session session)
|
||||
{
|
||||
return Sessions.Delete(s => s.Id == session.Id) > 0;
|
||||
var delete = Sessions.Delete(s => s.Id == session.Id) > 0;
|
||||
BackupDatabase();
|
||||
return delete;
|
||||
}
|
||||
}
|
|
@ -5,14 +5,21 @@ namespace InnovEnergy.App.Backend.Database;
|
|||
|
||||
public static partial class Db
|
||||
{
|
||||
private static Boolean Update(Object obj)
|
||||
{
|
||||
var success = Connection.Update(obj) > 0;
|
||||
if(success) BackupDatabase();
|
||||
return success;
|
||||
}
|
||||
|
||||
public static Boolean Update(Folder folder)
|
||||
{
|
||||
return Connection.Update(folder) > 0;
|
||||
return Update(obj: folder);
|
||||
}
|
||||
|
||||
public static Boolean Update(Installation installation)
|
||||
{
|
||||
return Connection.Update(installation) > 0;
|
||||
return Update(obj: installation);
|
||||
}
|
||||
|
||||
public static Boolean Update(User user)
|
||||
|
@ -24,7 +31,7 @@ public static partial class Db
|
|||
user.ParentId = originalUser.ParentId;
|
||||
user.Name = originalUser.Name;
|
||||
|
||||
return Connection.Update(user) > 0;
|
||||
return Update(obj: user);
|
||||
}
|
||||
|
||||
|
||||
|
|
Binary file not shown.
Loading…
Reference in New Issue