Skip to content

Commit

Permalink
Merge remote-tracking branch 'origin/master'
Browse files Browse the repository at this point in the history
  • Loading branch information
DarkRRb committed Feb 4, 2025
2 parents 8e56e77 + c098da4 commit 5372a14
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 581 deletions.
17 changes: 17 additions & 0 deletions Lagrange.OneBot/Database/MessageRecord.cs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,23 @@ namespace Lagrange.OneBot.Database;

#pragma warning disable CS8618

// # ################################################### #
// # WARNING #
// # ################################################### #
//
// When modifying MessageRecord, increment `SchemaVersion`.
// Beyond adding/removing fields (Realm auto-assigns
// defaults; complex processing requires migration logic
// in MigrationCallback), implement migration logic in
// `MigrationCallback` to handle all previous schema
// versions to current version.
//
// SchemaVersion
// Lagrange.OneBot/Extensions/HostApplicationBuilderExtension.cs#L91
//
// MigrationCallback
// Lagrange.OneBot/Extensions/HostApplicationBuilderExtension.cs#L92

public partial class MessageRecord : IRealmObject
{
public static readonly MessagePackSerializerOptions OPTIONS = MessagePackSerializerOptions.Standard
Expand Down
26 changes: 23 additions & 3 deletions Lagrange.OneBot/Extensions/HostApplicationBuilderExtension.cs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.Hosting;
using Microsoft.Extensions.Logging;
using Realms;
using JsonSerializer = System.Text.Json.JsonSerializer;

Expand Down Expand Up @@ -78,15 +79,34 @@ public static HostApplicationBuilder ConfigureLagrangeCore(this HostApplicationB
public static HostApplicationBuilder ConfigureOneBot(this HostApplicationBuilder builder)
{
builder.Services.AddOptions()
.AddSingleton(services => // Realm Helper
.AddSingleton(services => // Realm Configuration
{
var logger = services.GetRequiredService<ILogger<RealmConfiguration>>();
var configuration = services.GetRequiredService<IConfiguration>();
var host = services.GetRequiredService<IHost>();

string prefix = configuration["ConfigPath:Database"] ?? $"./lagrange-{configuration["Account:Uin"]}-db";
string? dpath = configuration["ConfigPath:Database"];

// Check LiteDB
string litedb = dpath ?? $"./lagrange-{configuration["Account:Uin"]}.db";
if (File.Exists(litedb)) {
logger.LogCritical("Found LiteDB database, currently Lagrange.OneBot has been migrated to Realm database");
logger.LogCritical("Please remove {} or refer to https://lagrangedev.github.io/Lagrange.Doc/Lagrange.OneBot/#从-litedb-迁移到-realm to migrate the database to Realm", litedb);
logger.LogCritical("Press any key to terminate the program");
Console.ReadKey(true);
host.StopAsync(default);
}

string prefix = dpath ?? $"./lagrange-{configuration["Account:Uin"]}-db";
if (!Directory.Exists(prefix)) Directory.CreateDirectory(prefix);
string path = Path.GetFullPath(Path.Join(prefix, ".realm"));

return new RealmConfiguration(path);
return new RealmConfiguration(path)
{
// Should be higher than upstream
SchemaVersion = 2,
MigrationCallback = null,
};
})
.AddSingleton<RealmHelper>()

Expand Down
Loading

0 comments on commit 5372a14

Please sign in to comment.