Updated Db models

This commit is contained in:
Toastie (DCS Team) 2024-07-07 14:27:42 +12:00
parent 62319ebb1a
commit 56d82a5a26
Signed by: toastie_t0ast
GPG key ID: 27F3B6855AFD40A4
2 changed files with 30 additions and 30 deletions

View file

@ -149,7 +149,7 @@ public abstract class EllieContext : DbContext
.OnDelete(DeleteBehavior.Cascade); .OnDelete(DeleteBehavior.Cascade);
// start antispam // start antispam
modelBuilder.Entity<GuildConfig>() modelBuilder.Entity<GuildConfig>()
.HasOne(x => x.AntiSpamSetting) .HasOne(x => x.AntiSpamSetting)
.WithOne() .WithOne()
@ -282,10 +282,10 @@ public abstract class EllieContext : DbContext
var selfassignableRolesEntity = modelBuilder.Entity<SelfAssignedRole>(); var selfassignableRolesEntity = modelBuilder.Entity<SelfAssignedRole>();
selfassignableRolesEntity.HasIndex(s => new selfassignableRolesEntity.HasIndex(s => new
{ {
s.GuildId, s.GuildId,
s.RoleId s.RoleId
}) })
.IsUnique(); .IsUnique();
selfassignableRolesEntity.Property(x => x.Group).HasDefaultValue(0); selfassignableRolesEntity.Property(x => x.Group).HasDefaultValue(0);
@ -358,10 +358,10 @@ public abstract class EllieContext : DbContext
var xps = modelBuilder.Entity<UserXpStats>(); var xps = modelBuilder.Entity<UserXpStats>();
xps.HasIndex(x => new xps.HasIndex(x => new
{ {
x.UserId, x.UserId,
x.GuildId x.GuildId
}) })
.IsUnique(); .IsUnique();
xps.HasIndex(x => x.UserId); xps.HasIndex(x => x.UserId);
@ -407,9 +407,9 @@ public abstract class EllieContext : DbContext
.OnDelete(DeleteBehavior.SetNull); .OnDelete(DeleteBehavior.SetNull);
ci.HasIndex(x => new ci.HasIndex(x => new
{ {
x.Name x.Name
}) })
.IsUnique(); .IsUnique();
#endregion #endregion
@ -528,10 +528,10 @@ public abstract class EllieContext : DbContext
.IsUnique(false); .IsUnique(false);
rr2.HasIndex(x => new rr2.HasIndex(x => new
{ {
x.MessageId, x.MessageId,
x.Emote x.Emote
}) })
.IsUnique(); .IsUnique();
}); });
@ -606,11 +606,11 @@ public abstract class EllieContext : DbContext
{ {
// user can own only one of each item // user can own only one of each item
x.HasIndex(model => new x.HasIndex(model => new
{ {
model.UserId, model.UserId,
model.ItemType, model.ItemType,
model.ItemKey model.ItemKey
}) })
.IsUnique(); .IsUnique();
}); });
@ -635,10 +635,10 @@ public abstract class EllieContext : DbContext
#region Sticky Roles #region Sticky Roles
modelBuilder.Entity<StickyRole>(sr => sr.HasIndex(x => new modelBuilder.Entity<StickyRole>(sr => sr.HasIndex(x => new
{ {
x.GuildId, x.GuildId,
x.UserId x.UserId
}) })
.IsUnique()); .IsUnique());
#endregion #endregion

View file

@ -4,14 +4,14 @@ using Microsoft.EntityFrameworkCore;
namespace EllieBot.Db; namespace EllieBot.Db;
public sealed class EllieDbService : DbService public sealed class EllieDbService : DbService
{ {
private readonly IBotCredsProvider _creds; private readonly IBotCredsProvider _creds;
// these are props because creds can change at runtime // these are props because creds can change at runtime
private string DbType => _creds.GetCreds().Db.Type.ToLowerInvariant().Trim(); private string DbType => _creds.GetCreds().Db.Type.ToLowerInvariant().Trim();
private string ConnString => _creds.GetCreds().Db.ConnectionString; private string ConnString => _creds.GetCreds().Db.ConnectionString;
public EllieDbService(IBotCredsProvider creds) public EllieDbService(IBotCredsProvider creds)
{ {
LinqToDBForEFTools.Initialize(); LinqToDBForEFTools.Initialize();
@ -26,13 +26,13 @@ public sealed class EllieDbService : DbService
var connString = ConnString; var connString = ConnString;
await using var context = CreateRawDbContext(dbType, connString); await using var context = CreateRawDbContext(dbType, connString);
// make sure sqlite db is in wal journal mode // make sure sqlite db is in wal journal mode
if (context is SqliteContext) if (context is SqliteContext)
{ {
await context.Database.ExecuteSqlRawAsync("PRAGMA journal_mode=WAL"); await context.Database.ExecuteSqlRawAsync("PRAGMA journal_mode=WAL");
} }
await context.Database.MigrateAsync(); await context.Database.MigrateAsync();
} }
@ -52,7 +52,7 @@ public sealed class EllieDbService : DbService
throw new NotSupportedException($"The database provide type of '{dbType}' is not supported."); throw new NotSupportedException($"The database provide type of '{dbType}' is not supported.");
} }
} }
private EllieContext GetDbContextInternal() private EllieContext GetDbContextInternal()
{ {
var dbType = DbType; var dbType = DbType;