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-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-05-13 06:49:55 +00:00
|
|
|
|
modelBuilder.Entity<CachedGuildUser>(entity => {
|
|
|
|
|
entity.HasKey(e => new { e.UserId, e.GuildId });
|
2022-06-10 23:09:18 +00:00
|
|
|
|
entity.Property(e => e.FirstSeenTime).HasDefaultValueSql("now()");
|
2022-05-13 06:49:55 +00:00
|
|
|
|
});
|
2022-05-12 03:26:28 +00:00
|
|
|
|
modelBuilder.Entity<CachedGuildMessage>(entity => entity.Property(e => e.CreatedAt).HasDefaultValueSql("now()"));
|
2022-03-29 05:03:01 +00:00
|
|
|
|
}
|
|
|
|
|
}
|