- Recreated cleanup migration with discorduser default values

- Using LinqToDb UpdateOrInsert for .EnsureUserCreated
This commit is contained in:
Kwoth
2021-07-07 02:32:56 +02:00
parent 0fc5f540d8
commit ac9f84715b
7 changed files with 132 additions and 37 deletions

View File

@@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore;
using Discord;
using System.Collections.Generic;
using LinqToDB;
using LinqToDB.EntityFrameworkCore;
using NadekoBot.Services.Database;
namespace NadekoBot.Db
@@ -12,25 +13,26 @@ namespace NadekoBot.Db
{
public static void EnsureUserCreated(this NadekoContext ctx, ulong userId, string username, string discrim, string avatarId)
{
var rows = ctx.Database.ExecuteSqlInterpolated($@"
UPDATE OR IGNORE DiscordUser
SET Username={username},
Discriminator={discrim},
AvatarId={avatarId}
WHERE UserId={userId};");
if (rows == 0)
{
ctx.DiscordUser
.Add(new DiscordUser()
ctx.DiscordUser
.ToLinqToDBTable()
.InsertOrUpdate(() => new()
{
UserId = userId,
Username = username,
Discriminator = discrim,
AvatarId = avatarId,
TotalXp = 0,
CurrencyAmount = 0
},
old => new()
{
Username = username,
Discriminator = discrim,
AvatarId = avatarId,
}, () => new()
{
UserId = userId
});
ctx.SaveChanges();
}
}
//temp is only used in updatecurrencystate, so that i don't overwrite real usernames/discrims with Unknown

View File

@@ -9,7 +9,7 @@ namespace NadekoBot.Db.Models
public string Username { get; set; }
public string Discriminator { get; set; }
public string AvatarId { get; set; }
public ClubInfo Club { get; set; }
public bool IsClubAdmin { get; set; }

View File

@@ -170,18 +170,29 @@ namespace NadekoBot.Services.Database
#region DiscordUser
var du = modelBuilder.Entity<DiscordUser>();
du.HasAlternateKey(w => w.UserId);
du.HasOne(x => x.Club)
.WithMany(x => x.Users)
.IsRequired(false);
modelBuilder.Entity<DiscordUser>(du =>
{
du.Property(x => x.IsClubAdmin)
.HasDefaultValue(false);
du.Property(x => x.LastLevelUp)
.HasDefaultValue(new DateTime(2017, 9, 21, 20, 53, 13, 305, DateTimeKind.Local));
du.Property(x => x.NotifyOnLevelUp)
.HasDefaultValue(XpNotificationLocation.None);
du.HasIndex(x => x.TotalXp);
du.HasIndex(x => x.CurrencyAmount);
du.HasIndex(x => x.UserId);
du.Property(x => x.LastXpGain)
.HasDefaultValueSql("datetime('now', '-1 years')");
du.Property(x => x.LastLevelUp)
.HasDefaultValueSql("datetime('now')");
du.HasAlternateKey(w => w.UserId);
du.HasOne(x => x.Club)
.WithMany(x => x.Users)
.IsRequired(false);
du.HasIndex(x => x.TotalXp);
du.HasIndex(x => x.CurrencyAmount);
du.HasIndex(x => x.UserId);
});
#endregion