2024-05-14 22:24:53 +02:00
|
|
|
using Foxchat.Core;
|
|
|
|
using Foxchat.Core.Database;
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
|
|
|
using Microsoft.EntityFrameworkCore.Design;
|
2024-05-19 17:20:45 +02:00
|
|
|
using Npgsql;
|
2024-05-14 22:24:53 +02:00
|
|
|
|
|
|
|
namespace Foxchat.Chat.Database;
|
|
|
|
|
|
|
|
public class ChatContext : IDatabaseContext
|
|
|
|
{
|
2024-05-19 17:20:45 +02:00
|
|
|
private readonly NpgsqlDataSource _dataSource;
|
2024-05-14 22:24:53 +02:00
|
|
|
|
|
|
|
public override DbSet<Instance> Instance { get; set; }
|
|
|
|
|
|
|
|
public ChatContext(InstanceConfig config)
|
|
|
|
{
|
2024-05-19 17:20:45 +02:00
|
|
|
var connString = new NpgsqlConnectionStringBuilder(config.Database.Url)
|
2024-05-14 22:24:53 +02:00
|
|
|
{
|
|
|
|
Timeout = config.Database.Timeout ?? 5,
|
|
|
|
MaxPoolSize = config.Database.MaxPoolSize ?? 50,
|
|
|
|
}.ConnectionString;
|
2024-05-19 17:20:45 +02:00
|
|
|
|
|
|
|
var dataSourceBuilder = new NpgsqlDataSourceBuilder(connString);
|
|
|
|
dataSourceBuilder.UseNodaTime();
|
|
|
|
_dataSource = dataSourceBuilder.Build();
|
2024-05-14 22:24:53 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
|
|
|
=> optionsBuilder
|
2024-05-19 17:20:45 +02:00
|
|
|
.UseNpgsql(_dataSource, o => o.UseNodaTime())
|
2024-05-14 22:24:53 +02:00
|
|
|
.UseSnakeCaseNamingConvention();
|
|
|
|
|
|
|
|
protected override void ConfigureConventions(ModelConfigurationBuilder configurationBuilder)
|
|
|
|
{
|
|
|
|
// ULIDs are stored as UUIDs in the database
|
|
|
|
configurationBuilder.Properties<Ulid>().HaveConversion<UlidConverter>();
|
|
|
|
}
|
|
|
|
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
public class DesignTimeIdentityContextFactory : IDesignTimeDbContextFactory<ChatContext>
|
|
|
|
{
|
|
|
|
public ChatContext CreateDbContext(string[] args)
|
|
|
|
{
|
|
|
|
// Read the configuration file
|
|
|
|
var config = new ConfigurationBuilder()
|
|
|
|
.AddConfiguration("identity.ini")
|
|
|
|
.Build()
|
|
|
|
// Get the configuration as our config class
|
|
|
|
.Get<InstanceConfig>() ?? new();
|
|
|
|
|
|
|
|
return new ChatContext(config);
|
|
|
|
}
|
|
|
|
}
|