2020-06-13 17:15:50 +00:00
|
|
|
using System.Data;
|
2021-09-30 01:51:38 +00:00
|
|
|
using System.Runtime.CompilerServices;
|
2020-06-13 16:31:20 +00:00
|
|
|
|
|
|
|
using App.Metrics;
|
|
|
|
|
2020-06-13 17:15:50 +00:00
|
|
|
using Dapper;
|
|
|
|
|
|
|
|
using NodaTime;
|
|
|
|
|
2020-06-13 16:31:20 +00:00
|
|
|
using Npgsql;
|
|
|
|
|
|
|
|
using Serilog;
|
|
|
|
|
2021-09-30 01:51:38 +00:00
|
|
|
using SqlKata;
|
|
|
|
using SqlKata.Compilers;
|
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
namespace PluralKit.Core;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2022-03-30 07:00:45 +00:00
|
|
|
internal partial class Database: IDatabase
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
2020-06-13 16:31:20 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private readonly CoreConfig _config;
|
|
|
|
private readonly ILogger _logger;
|
|
|
|
private readonly IMetrics _metrics;
|
|
|
|
private readonly DbConnectionCountHolder _countHolder;
|
|
|
|
private readonly DatabaseMigrator _migrator;
|
|
|
|
private readonly string _connectionString;
|
2022-11-23 09:17:19 +00:00
|
|
|
private readonly string _messagesConnectionString;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public Database(CoreConfig config, DbConnectionCountHolder countHolder, ILogger logger,
|
|
|
|
IMetrics metrics, DatabaseMigrator migrator)
|
|
|
|
{
|
|
|
|
_config = config;
|
|
|
|
_countHolder = countHolder;
|
|
|
|
_metrics = metrics;
|
|
|
|
_migrator = migrator;
|
|
|
|
_logger = logger.ForContext<Database>();
|
2021-09-30 01:51:38 +00:00
|
|
|
|
2022-11-23 09:17:19 +00:00
|
|
|
string connectionString(string src)
|
2020-06-13 17:15:50 +00:00
|
|
|
{
|
2022-11-23 09:17:19 +00:00
|
|
|
var builder = new NpgsqlConnectionStringBuilder(src)
|
|
|
|
{
|
|
|
|
Pooling = true,
|
|
|
|
Enlist = false,
|
|
|
|
NoResetOnClose = true,
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2022-11-23 09:17:19 +00:00
|
|
|
// Lower timeout than default (15s -> 2s), should ideally fail-fast instead of hanging
|
|
|
|
Timeout = 2
|
|
|
|
};
|
2022-06-13 19:23:13 +00:00
|
|
|
|
2022-11-23 09:17:19 +00:00
|
|
|
if (_config.DatabasePassword != null)
|
|
|
|
builder.Password = _config.DatabasePassword;
|
2022-06-13 19:23:13 +00:00
|
|
|
|
2022-11-23 09:17:19 +00:00
|
|
|
return builder.ConnectionString;
|
|
|
|
}
|
|
|
|
|
|
|
|
_connectionString = connectionString(_config.Database);
|
|
|
|
_messagesConnectionString = connectionString(_config.MessagesDatabase ?? _config.Database);
|
2021-11-27 02:10:56 +00:00
|
|
|
}
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private static readonly PostgresCompiler _compiler = new();
|
2020-06-13 17:15:50 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public static void InitStatic()
|
|
|
|
{
|
|
|
|
DefaultTypeMap.MatchNamesWithUnderscores = true;
|
|
|
|
|
|
|
|
// Dapper by default tries to pass ulongs to Npgsql, which rejects them since PostgreSQL technically
|
|
|
|
// doesn't support unsigned types on its own.
|
|
|
|
// Instead we add a custom mapper to encode them as signed integers instead, converting them back and forth.
|
|
|
|
SqlMapper.RemoveTypeMap(typeof(ulong));
|
|
|
|
SqlMapper.AddTypeHandler(new UlongEncodeAsLongHandler());
|
|
|
|
SqlMapper.AddTypeHandler(new UlongArrayHandler());
|
|
|
|
|
|
|
|
NpgsqlConnection.GlobalTypeMapper.UseNodaTime();
|
|
|
|
// With the thing we add above, Npgsql already handles NodaTime integration
|
|
|
|
// This makes Dapper confused since it thinks it has to convert it anyway and doesn't understand the types
|
|
|
|
// So we add a custom type handler that literally just passes the type through to Npgsql
|
|
|
|
SqlMapper.AddTypeHandler(new PassthroughTypeHandler<Instant>());
|
|
|
|
SqlMapper.AddTypeHandler(new PassthroughTypeHandler<LocalDate>());
|
|
|
|
|
|
|
|
// Add ID types to Dapper
|
|
|
|
SqlMapper.AddTypeHandler(new NumericIdHandler<SystemId, int>(i => new SystemId(i)));
|
|
|
|
SqlMapper.AddTypeHandler(new NumericIdHandler<MemberId, int>(i => new MemberId(i)));
|
|
|
|
SqlMapper.AddTypeHandler(new NumericIdHandler<SwitchId, int>(i => new SwitchId(i)));
|
|
|
|
SqlMapper.AddTypeHandler(new NumericIdHandler<GroupId, int>(i => new GroupId(i)));
|
|
|
|
SqlMapper.AddTypeHandler(new NumericIdArrayHandler<SystemId, int>(i => new SystemId(i)));
|
|
|
|
SqlMapper.AddTypeHandler(new NumericIdArrayHandler<MemberId, int>(i => new MemberId(i)));
|
|
|
|
SqlMapper.AddTypeHandler(new NumericIdArrayHandler<SwitchId, int>(i => new SwitchId(i)));
|
|
|
|
SqlMapper.AddTypeHandler(new NumericIdArrayHandler<GroupId, int>(i => new GroupId(i)));
|
|
|
|
|
|
|
|
// Register our custom types to Npgsql
|
|
|
|
// Without these it'll still *work* but break at the first launch + probably cause other small issues
|
|
|
|
NpgsqlConnection.GlobalTypeMapper.MapComposite<ProxyTag>("proxy_tag");
|
|
|
|
NpgsqlConnection.GlobalTypeMapper.MapEnum<PrivacyLevel>("privacy_level");
|
|
|
|
}
|
2020-06-13 17:15:50 +00:00
|
|
|
|
2022-11-17 02:51:04 +00:00
|
|
|
// TODO: make sure every SQL query is behind a logged query method
|
2022-11-23 09:17:19 +00:00
|
|
|
public async Task<IPKConnection> Obtain(bool messages = false)
|
2021-11-27 02:10:56 +00:00
|
|
|
{
|
|
|
|
// Mark the request (for a handle, I guess) in the metrics
|
|
|
|
_metrics.Measure.Meter.Mark(CoreMetrics.DatabaseRequests);
|
|
|
|
|
|
|
|
// Create a connection and open it
|
|
|
|
// We wrap it in PKConnection for tracing purposes
|
2022-11-23 09:17:19 +00:00
|
|
|
var conn = new PKConnection(new NpgsqlConnection(messages ? _messagesConnectionString : _connectionString), _countHolder, _logger, _metrics);
|
2021-11-27 02:10:56 +00:00
|
|
|
await conn.OpenAsync();
|
|
|
|
return conn;
|
|
|
|
}
|
2020-06-13 17:15:50 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public async Task ApplyMigrations()
|
|
|
|
{
|
|
|
|
using var conn = await Obtain();
|
|
|
|
await _migrator.ApplyMigrations(conn);
|
|
|
|
}
|
2020-06-13 17:15:50 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private class PassthroughTypeHandler<T>: SqlMapper.TypeHandler<T>
|
|
|
|
{
|
|
|
|
public override void SetValue(IDbDataParameter parameter, T value) => parameter.Value = value;
|
|
|
|
public override T Parse(object value) => (T)value;
|
|
|
|
}
|
2020-06-13 17:15:50 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private class UlongEncodeAsLongHandler: SqlMapper.TypeHandler<ulong>
|
|
|
|
{
|
|
|
|
public override ulong Parse(object value) =>
|
|
|
|
// Cast to long to unbox, then to ulong (???)
|
|
|
|
(ulong)(long)value;
|
2020-06-14 19:37:04 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public override void SetValue(IDbDataParameter parameter, ulong value) => parameter.Value = (long)value;
|
|
|
|
}
|
2020-06-14 19:37:04 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private class UlongArrayHandler: SqlMapper.TypeHandler<ulong[]>
|
|
|
|
{
|
|
|
|
public override void SetValue(IDbDataParameter parameter, ulong[] value) => parameter.Value = Array.ConvertAll(value, i => (long)i);
|
2020-06-14 19:37:04 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public override ulong[] Parse(object value) => Array.ConvertAll((long[])value, i => (ulong)i);
|
|
|
|
}
|
2020-06-14 19:37:04 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private class NumericIdHandler<T, TInner>: SqlMapper.TypeHandler<T>
|
|
|
|
where T : INumericId<T, TInner>
|
|
|
|
where TInner : IEquatable<TInner>, IComparable<TInner>
|
|
|
|
{
|
|
|
|
private readonly Func<TInner, T> _factory;
|
2020-06-14 19:37:04 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public NumericIdHandler(Func<TInner, T> factory)
|
2020-06-14 19:37:04 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
_factory = factory;
|
|
|
|
}
|
2020-06-14 19:37:04 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public override void SetValue(IDbDataParameter parameter, T value) => parameter.Value = value.Value;
|
2020-06-14 19:37:04 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public override T Parse(object value) => _factory((TInner)value);
|
|
|
|
}
|
2020-06-14 19:37:04 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
private class NumericIdArrayHandler<T, TInner>: SqlMapper.TypeHandler<T[]>
|
|
|
|
where T : INumericId<T, TInner>
|
|
|
|
where TInner : IEquatable<TInner>, IComparable<TInner>
|
|
|
|
{
|
|
|
|
private readonly Func<TInner, T> _factory;
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public NumericIdArrayHandler(Func<TInner, T> factory)
|
2020-08-05 18:24:51 +00:00
|
|
|
{
|
2021-11-27 02:10:56 +00:00
|
|
|
_factory = factory;
|
2020-08-05 18:24:51 +00:00
|
|
|
}
|
2020-08-29 11:46:27 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public override void SetValue(IDbDataParameter parameter, T[] value) => parameter.Value = Array.ConvertAll(value, v => v.Value);
|
2021-08-27 15:03:47 +00:00
|
|
|
|
2021-11-27 02:10:56 +00:00
|
|
|
public override T[] Parse(object value) => Array.ConvertAll((TInner[])value, v => _factory(v));
|
|
|
|
}
|
2020-06-13 16:31:20 +00:00
|
|
|
}
|