feat: implement database backup and restore functionality with JSON support
All checks were successful
Publish Container / publish (push) Successful in 3m53s
All checks were successful
Publish Container / publish (push) Successful in 3m53s
Co-authored-by: Copilot <copilot@github.com>
This commit is contained in:
parent
e872fe200b
commit
e8bbae0496
8 changed files with 657 additions and 0 deletions
|
|
@ -7,10 +7,15 @@ namespace WorkTracker.Services.Storage;
|
|||
public sealed class CouchbaseLiteDatabaseProvider : IDisposable
|
||||
{
|
||||
private const string AppSettingsCollectionName = "app_settings";
|
||||
private const string SystemCollectionName = "system";
|
||||
private const string UsersCollectionName = "users";
|
||||
private const string WorkDaysCollectionName = "workdays";
|
||||
private const string SchemaVersionDocumentId = "database_schema";
|
||||
|
||||
public const int CurrentDatabaseSchemaVersion = 1;
|
||||
|
||||
private readonly Database database;
|
||||
private readonly IReadOnlyDictionary<string, Collection> backupCollections;
|
||||
|
||||
public CouchbaseLiteDatabaseProvider(IOptions<CouchbaseLiteOptions> options, IHostEnvironment environment)
|
||||
{
|
||||
|
|
@ -25,17 +30,39 @@ public sealed class CouchbaseLiteDatabaseProvider : IDisposable
|
|||
Directory = databaseDirectory
|
||||
});
|
||||
|
||||
System = database.GetCollection(SystemCollectionName) ?? database.CreateCollection(SystemCollectionName);
|
||||
AppSettings = database.GetCollection(AppSettingsCollectionName) ?? database.CreateCollection(AppSettingsCollectionName);
|
||||
Users = database.GetCollection(UsersCollectionName) ?? database.CreateCollection(UsersCollectionName);
|
||||
WorkDays = database.GetCollection(WorkDaysCollectionName) ?? database.CreateCollection(WorkDaysCollectionName);
|
||||
|
||||
backupCollections = new Dictionary<string, Collection>(StringComparer.Ordinal)
|
||||
{
|
||||
[AppSettingsCollectionName] = AppSettings,
|
||||
[UsersCollectionName] = Users,
|
||||
[WorkDaysCollectionName] = WorkDays
|
||||
};
|
||||
|
||||
DatabaseSchemaVersion = EnsureDatabaseSchemaVersion();
|
||||
}
|
||||
|
||||
public Collection System { get; }
|
||||
|
||||
public Collection AppSettings { get; }
|
||||
|
||||
public Collection Users { get; }
|
||||
|
||||
public Collection WorkDays { get; }
|
||||
|
||||
public int DatabaseSchemaVersion { get; }
|
||||
|
||||
public IReadOnlyDictionary<string, Collection> BackupCollections => backupCollections;
|
||||
|
||||
public void ExecuteInBatch(Action action)
|
||||
{
|
||||
ArgumentNullException.ThrowIfNull(action);
|
||||
database.InBatch(action);
|
||||
}
|
||||
|
||||
public void Dispose()
|
||||
{
|
||||
database.Close();
|
||||
|
|
@ -53,4 +80,21 @@ public sealed class CouchbaseLiteDatabaseProvider : IDisposable
|
|||
? configuredDirectory
|
||||
: Path.GetFullPath(Path.Combine(contentRootPath, configuredDirectory));
|
||||
}
|
||||
|
||||
private int EnsureDatabaseSchemaVersion()
|
||||
{
|
||||
var document = System.GetDocument(SchemaVersionDocumentId);
|
||||
if (document is not null && document.Contains("schemaVersion"))
|
||||
{
|
||||
return document.GetInt("schemaVersion");
|
||||
}
|
||||
|
||||
var now = DateTimeOffset.UtcNow;
|
||||
var mutableDocument = new MutableDocument(SchemaVersionDocumentId);
|
||||
mutableDocument.SetInt("schemaVersion", CurrentDatabaseSchemaVersion);
|
||||
mutableDocument.SetString("createdAtUtc", now.ToString("O"));
|
||||
mutableDocument.SetString("updatedAtUtc", now.ToString("O"));
|
||||
System.Save(mutableDocument);
|
||||
return CurrentDatabaseSchemaVersion;
|
||||
}
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue