feat(git-provider): Initial git registration storage in database
- update PostBuild.ps1 to not delete data directory created during debug - move sqlite-net-pcl to ModuleCore
This commit is contained in:
parent
caa65ca7c3
commit
633b35f7e9
5 changed files with 95 additions and 14 deletions
|
|
@ -2,6 +2,7 @@
|
|||
using System.Diagnostics;
|
||||
using ModuleCore.Database;
|
||||
using ModuleCore.Git.Models;
|
||||
using SQLite;
|
||||
|
||||
namespace ModuleCore.Git;
|
||||
|
||||
|
|
@ -18,6 +19,11 @@ public class GitManager
|
|||
|
||||
_registrations = new ConcurrentDictionary<string, InternalGitRegistration>();
|
||||
_db = new DatabaseManager("git.db");
|
||||
|
||||
_db.InConnection(conn =>
|
||||
{
|
||||
conn.CreateTable<InternalGitRegistration>();
|
||||
});
|
||||
}
|
||||
|
||||
public static GitManager Instance => GitManagerInstance.Value;
|
||||
|
|
@ -40,16 +46,46 @@ public class GitManager
|
|||
? new DirectoryInfo(absoluteRepositoryLocation).Name
|
||||
: registrationName;
|
||||
|
||||
if (_registrations.TryAdd(registrationName, new InternalGitRegistration
|
||||
{
|
||||
Name = registrationName,
|
||||
Location = absoluteRepositoryLocation,
|
||||
}))
|
||||
var gitRegistration = new InternalGitRegistration
|
||||
{
|
||||
return registrationName;
|
||||
}
|
||||
Name = registrationName,
|
||||
Location = absoluteRepositoryLocation,
|
||||
Id = Guid.CreateVersion7(),
|
||||
};
|
||||
|
||||
throw new Exception($"Git repo already registered with the name {registrationName}");
|
||||
return _db.InConnection<string>(conn =>
|
||||
{
|
||||
// Query if we already have a registration either by name or location.
|
||||
// tbh this is a bit of a janky way to do exists when I have to pass the query in anyway, but I just didn't
|
||||
// want to do null checks and a truthy check so I wrap it in a barely-valuable method.
|
||||
var registrationExists = _db.Exists(
|
||||
$"""
|
||||
SELECT 1
|
||||
FROM {InternalGitRegistration.TableName}
|
||||
WHERE Name = ? OR
|
||||
Location = ?
|
||||
""",
|
||||
gitRegistration.Name,
|
||||
gitRegistration.Location
|
||||
);
|
||||
|
||||
if (registrationExists)
|
||||
{
|
||||
throw new Exception($"A Git repo is already registered with the name {registrationName} or location {absoluteRepositoryLocation}");
|
||||
}
|
||||
|
||||
// Insert the new record
|
||||
conn.Insert(gitRegistration);
|
||||
|
||||
if (_registrations.TryAdd(registrationName, gitRegistration))
|
||||
{
|
||||
return registrationName;
|
||||
}
|
||||
|
||||
// This error case should be unlikely, but if a registration was removed but the registrations wasn't updated
|
||||
// correctly then we'd unable to re-add a repo with the same name
|
||||
throw new Exception("An error occured during registration.");
|
||||
});
|
||||
}
|
||||
|
||||
public List<GitRegistration> ListRepos()
|
||||
|
|
@ -83,12 +119,22 @@ public class GitManager
|
|||
/// <summary>
|
||||
/// Used for internal git registration and handles getting the current branch
|
||||
/// </summary>
|
||||
[Table(TableName)]
|
||||
private class InternalGitRegistration
|
||||
{
|
||||
private string _currentBranch = string.Empty;
|
||||
private long _nextCheckTime;
|
||||
public required string Name { get; set; }
|
||||
public required string Location { get; set; }
|
||||
internal const string TableName = "GitRegistration";
|
||||
|
||||
[PrimaryKey]
|
||||
public Guid Id { get; set; }
|
||||
|
||||
[Indexed(Unique = true)]
|
||||
public string Name { get; set; } = null!;
|
||||
|
||||
[Indexed(Unique = true)]
|
||||
public string Location { get; set; } = null!;
|
||||
|
||||
public string CurrentBranch => GetCurrentBranch();
|
||||
|
||||
// TODO: not fully decided on if I want this feature or not, but keeping it in for now
|
||||
|
|
|
|||
Loading…
Reference in a new issue