2022-03-29 05:03:01 +00:00
|
|
|
|
using Microsoft.EntityFrameworkCore;
|
2022-07-23 06:41:49 +00:00
|
|
|
|
using Npgsql;
|
2022-03-29 05:03:01 +00:00
|
|
|
|
|
|
|
|
|
namespace RegexBot.Data;
|
2022-07-21 01:55:08 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Represents a database connection using the settings defined in the bot's global configuration.
|
|
|
|
|
/// </summary>
|
2022-03-29 05:03:01 +00:00
|
|
|
|
public class BotDatabaseContext : DbContext {
|
2022-07-23 06:41:49 +00:00
|
|
|
|
private static readonly string _connectionString;
|
|
|
|
|
|
|
|
|
|
static BotDatabaseContext() {
|
|
|
|
|
// Get our own config loaded just for the SQL stuff
|
|
|
|
|
var conf = new InstanceConfig();
|
|
|
|
|
_connectionString = new NpgsqlConnectionStringBuilder() {
|
|
|
|
|
Host = conf.SqlHost ?? "localhost", // default to localhost
|
|
|
|
|
Database = conf.SqlDatabase,
|
|
|
|
|
Username = conf.SqlUsername,
|
|
|
|
|
Password = conf.SqlPassword
|
|
|
|
|
}.ToString();
|
2022-03-29 05:03:01 +00:00
|
|
|
|
}
|
|
|
|
|
|
2022-07-21 01:55:08 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves the <seealso cref="CachedUser">user cache</seealso>.
|
|
|
|
|
/// </summary>
|
2022-03-29 05:03:01 +00:00
|
|
|
|
public DbSet<CachedUser> UserCache { get; set; } = null!;
|
2022-07-21 01:55:08 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves the <seealso cref="CachedGuildUser">guild user cache</seealso>.
|
|
|
|
|
/// </summary>
|
2022-03-29 05:03:01 +00:00
|
|
|
|
public DbSet<CachedGuildUser> GuildUserCache { get; set; } = null!;
|
2022-07-21 01:55:08 +00:00
|
|
|
|
|
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves the <seealso cref="CachedGuildMessage">guild message cache</seealso>.
|
|
|
|
|
/// </summary>
|
2022-05-12 03:26:28 +00:00
|
|
|
|
public DbSet<CachedGuildMessage> GuildMessageCache { get; set; } = null!;
|
2022-03-29 05:03:01 +00:00
|
|
|
|
|
2022-08-16 19:37:06 +00:00
|
|
|
|
/// <summary>
|
|
|
|
|
/// Retrieves the <seealso cref="ModLogEntry">moderator logs</seealso>.
|
|
|
|
|
/// </summary>
|
|
|
|
|
public DbSet<ModLogEntry> ModLogs { get; set; } = null!;
|
|
|
|
|
|
2022-07-21 01:55:08 +00:00
|
|
|
|
/// <inheritdoc />
|
|
|
|
|
protected sealed override void OnConfiguring(DbContextOptionsBuilder optionsBuilder)
|
2022-03-29 05:03:01 +00:00
|
|
|
|
=> optionsBuilder
|
2022-07-23 06:41:49 +00:00
|
|
|
|
.UseNpgsql(_connectionString)
|
2022-03-29 05:03:01 +00:00
|
|
|
|
.UseSnakeCaseNamingConvention();
|
|
|
|
|
|
2022-07-21 01:55:08 +00:00
|
|
|
|
/// <inheritdoc />
|
2022-03-29 05:03:01 +00:00
|
|
|
|
protected override void OnModelCreating(ModelBuilder modelBuilder) {
|
|
|
|
|
modelBuilder.Entity<CachedUser>(entity => entity.Property(e => e.Discriminator).HasMaxLength(4).IsFixedLength());
|
2022-08-16 19:37:06 +00:00
|
|
|
|
modelBuilder.Entity<CachedGuildUser>(e => {
|
|
|
|
|
e.HasKey(p => new { p.UserId, p.GuildId });
|
|
|
|
|
e.Property(p => p.FirstSeenTime).HasDefaultValueSql("now()");
|
2022-05-13 06:49:55 +00:00
|
|
|
|
});
|
2022-08-16 19:37:06 +00:00
|
|
|
|
modelBuilder.Entity<CachedGuildMessage>(e => e.Property(p => p.CreatedAt).HasDefaultValueSql("now()"));
|
|
|
|
|
modelBuilder.HasPostgresEnum<ModLogType>();
|
|
|
|
|
modelBuilder.Entity<ModLogEntry>(e => e.Property(p => p.Timestamp).HasDefaultValueSql("now()"));
|
2022-03-29 05:03:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|