mirror of
https://gitlab.com/Kwoth/nadekobot.git
synced 2025-09-11 09:48:26 -04:00
WIP db provider support for Mysql and Postgres
This commit is contained in:
@@ -1,7 +1,6 @@
|
||||
#nullable disable
|
||||
using LinqToDB.Common;
|
||||
using LinqToDB.EntityFrameworkCore;
|
||||
using Microsoft.Data.Sqlite;
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using NadekoBot.Services.Database;
|
||||
|
||||
@@ -9,49 +8,68 @@ namespace NadekoBot.Services;
|
||||
|
||||
public class DbService
|
||||
{
|
||||
private readonly DbContextOptions<NadekoContext> _options;
|
||||
private readonly DbContextOptions<NadekoContext> _migrateOptions;
|
||||
private readonly IBotCredsProvider _creds;
|
||||
|
||||
public DbService(IBotCredentials creds)
|
||||
// these are props because creds can change at runtime
|
||||
private string DbType => _creds.GetCreds().Db.Type.ToLowerInvariant().Trim();
|
||||
private string ConnString => _creds.GetCreds().Db.ConnectionString;
|
||||
|
||||
public DbService(IBotCredsProvider creds)
|
||||
{
|
||||
LinqToDBForEFTools.Initialize();
|
||||
Configuration.Linq.DisableQueryCache = true;
|
||||
|
||||
var builder = new SqliteConnectionStringBuilder(creds.Db.ConnectionString);
|
||||
builder.DataSource = Path.Combine(AppContext.BaseDirectory, builder.DataSource);
|
||||
|
||||
var optionsBuilder = new DbContextOptionsBuilder<NadekoContext>();
|
||||
optionsBuilder.UseSqlite(builder.ToString());
|
||||
_options = optionsBuilder.Options;
|
||||
|
||||
optionsBuilder = new();
|
||||
optionsBuilder.UseSqlite(builder.ToString());
|
||||
_migrateOptions = optionsBuilder.Options;
|
||||
_creds = creds;
|
||||
}
|
||||
|
||||
public void Setup()
|
||||
public async Task SetupAsync()
|
||||
{
|
||||
using var context = new NadekoContext(_options);
|
||||
if (context.Database.GetPendingMigrations().Any())
|
||||
{
|
||||
using var mContext = new NadekoContext(_migrateOptions);
|
||||
mContext.Database.Migrate();
|
||||
mContext.SaveChanges();
|
||||
}
|
||||
var dbType = DbType;
|
||||
var connString = ConnString;
|
||||
|
||||
context.Database.ExecuteSqlRaw("PRAGMA journal_mode=WAL");
|
||||
context.SaveChanges();
|
||||
await using var context = CreateRawDbContext(dbType, connString);
|
||||
|
||||
// make sure sqlite db is in wal journal mode
|
||||
if (context is SqliteContext)
|
||||
{
|
||||
await context.Database.ExecuteSqlRawAsync("PRAGMA journal_mode=WAL");
|
||||
}
|
||||
|
||||
await context.Database.MigrateAsync();
|
||||
}
|
||||
|
||||
private static NadekoContext CreateRawDbContext(string dbType, string connString)
|
||||
{
|
||||
switch (dbType)
|
||||
{
|
||||
case "postgresql":
|
||||
case "postgres":
|
||||
case "pgsql":
|
||||
return new PostgreSqlContext(connString);
|
||||
case "mysql":
|
||||
return new MysqlContext(connString);
|
||||
case "sqlite":
|
||||
return new SqliteContext(connString);
|
||||
default:
|
||||
throw new NotSupportedException($"The database provide type of '{dbType}' is not supported.");
|
||||
}
|
||||
}
|
||||
|
||||
private NadekoContext GetDbContextInternal()
|
||||
{
|
||||
var context = new NadekoContext(_options);
|
||||
context.Database.SetCommandTimeout(60);
|
||||
var conn = context.Database.GetDbConnection();
|
||||
conn.Open();
|
||||
using var com = conn.CreateCommand();
|
||||
com.CommandText = "PRAGMA journal_mode=WAL; PRAGMA synchronous=OFF";
|
||||
com.ExecuteNonQuery();
|
||||
var dbType = DbType;
|
||||
var connString = ConnString;
|
||||
|
||||
var context = CreateRawDbContext(dbType, connString);
|
||||
if (context is SqliteContext)
|
||||
{
|
||||
var conn = context.Database.GetDbConnection();
|
||||
conn.Open();
|
||||
using var com = conn.CreateCommand();
|
||||
com.CommandText = "PRAGMA synchronous=OFF";
|
||||
com.ExecuteNonQuery();
|
||||
}
|
||||
|
||||
return context;
|
||||
}
|
||||
|
||||
|
@@ -52,5 +52,5 @@ public interface IStatsService
|
||||
/// <summary>
|
||||
/// Gets total amount of private memory currently in use by the bot, in Megabytes.
|
||||
/// </summary>
|
||||
double GetPrivateMemory();
|
||||
double GetPrivateMemoryMegabytes();
|
||||
}
|
@@ -166,9 +166,9 @@ public sealed class BotCredsProvider : IBotCredsProvider
|
||||
if (File.Exists(CREDS_FILE_NAME))
|
||||
{
|
||||
var creds = Yaml.Deserializer.Deserialize<Creds>(File.ReadAllText(CREDS_FILE_NAME));
|
||||
if (creds.Version <= 3)
|
||||
if (creds.Version <= 4)
|
||||
{
|
||||
creds.Version = 4;
|
||||
creds.Version = 5;
|
||||
File.WriteAllText(CREDS_FILE_NAME, Yaml.Serializer.Serialize(creds));
|
||||
}
|
||||
}
|
||||
|
@@ -183,9 +183,9 @@ public sealed class StatsService : IStatsService, IReadyExecutor, INService
|
||||
return time.Humanize(3, maxUnit: TimeUnit.Day, minUnit: TimeUnit.Minute);
|
||||
}
|
||||
|
||||
public double GetPrivateMemory()
|
||||
public double GetPrivateMemoryMegabytes()
|
||||
{
|
||||
_currentProcess.Refresh();
|
||||
return _currentProcess.PrivateMemorySize64 / (double)1.MiB();
|
||||
return _currentProcess.PrivateMemorySize64 / 1.Megabytes().Bytes;
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user