- 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

@@ -7,7 +7,6 @@ using NadekoBot.Services;
namespace NadekoBot.Common.Attributes namespace NadekoBot.Common.Attributes
{ {
// todo all getservice to getrequiredservice
[AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)] [AttributeUsage(AttributeTargets.Method | AttributeTargets.Class)]
public sealed class OwnerOnlyAttribute : PreconditionAttribute public sealed class OwnerOnlyAttribute : PreconditionAttribute
{ {

View File

@@ -4,6 +4,7 @@ using Microsoft.EntityFrameworkCore;
using Discord; using Discord;
using System.Collections.Generic; using System.Collections.Generic;
using LinqToDB; using LinqToDB;
using LinqToDB.EntityFrameworkCore;
using NadekoBot.Services.Database; using NadekoBot.Services.Database;
namespace NadekoBot.Db 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) public static void EnsureUserCreated(this NadekoContext ctx, ulong userId, string username, string discrim, string avatarId)
{ {
var rows = ctx.Database.ExecuteSqlInterpolated($@" ctx.DiscordUser
UPDATE OR IGNORE DiscordUser .ToLinqToDBTable()
SET Username={username}, .InsertOrUpdate(() => new()
Discriminator={discrim},
AvatarId={avatarId}
WHERE UserId={userId};");
if (rows == 0)
{
ctx.DiscordUser
.Add(new DiscordUser()
{ {
UserId = userId, UserId = userId,
Username = username, Username = username,
Discriminator = discrim, Discriminator = discrim,
AvatarId = avatarId, 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 //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 Username { get; set; }
public string Discriminator { get; set; } public string Discriminator { get; set; }
public string AvatarId { get; set; } public string AvatarId { get; set; }
public ClubInfo Club { get; set; } public ClubInfo Club { get; set; }
public bool IsClubAdmin { get; set; } public bool IsClubAdmin { get; set; }

View File

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

View File

@@ -9,7 +9,7 @@ using NadekoBot.Services.Database;
namespace NadekoBot.Migrations namespace NadekoBot.Migrations
{ {
[DbContext(typeof(NadekoContext))] [DbContext(typeof(NadekoContext))]
[Migration("20210621071424_cleanup")] [Migration("20210707002343_cleanup")]
partial class cleanup partial class cleanup
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
@@ -112,18 +112,24 @@ namespace NadekoBot.Migrations
.HasColumnType("TEXT"); .HasColumnType("TEXT");
b.Property<bool>("IsClubAdmin") b.Property<bool>("IsClubAdmin")
.HasColumnType("INTEGER"); .ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasDefaultValue(false);
b.Property<DateTime>("LastLevelUp") b.Property<DateTime>("LastLevelUp")
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasColumnType("TEXT") .HasColumnType("TEXT")
.HasDefaultValue(new DateTime(2017, 9, 21, 20, 53, 13, 305, DateTimeKind.Local)); .HasDefaultValueSql("datetime('now')");
b.Property<DateTime>("LastXpGain") b.Property<DateTime>("LastXpGain")
.HasColumnType("TEXT"); .ValueGeneratedOnAdd()
.HasColumnType("TEXT")
.HasDefaultValueSql("datetime('now', '-1 years')");
b.Property<int>("NotifyOnLevelUp") b.Property<int>("NotifyOnLevelUp")
.HasColumnType("INTEGER"); .ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasDefaultValue(0);
b.Property<int>("TotalXp") b.Property<int>("TotalXp")
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");

View File

@@ -7,9 +7,6 @@ namespace NadekoBot.Migrations
{ {
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)
{ {
migrationBuilder.Sql(
"DELETE FROM __EFMigrationsHistory WHERE __EFMigrationsHistory.MigrationId <> '20210621042359_squash'");
migrationBuilder.DropForeignKey( migrationBuilder.DropForeignKey(
name: "FK_GuildConfigs_Permission_RootPermissionId", name: "FK_GuildConfigs_Permission_RootPermissionId",
table: "GuildConfigs"); table: "GuildConfigs");
@@ -139,6 +136,43 @@ namespace NadekoBot.Migrations
migrationBuilder.DropColumn( migrationBuilder.DropColumn(
name: "UseCount", name: "UseCount",
table: "CustomReactions"); table: "CustomReactions");
migrationBuilder.AlterColumn<int>(
name: "NotifyOnLevelUp",
table: "DiscordUser",
type: "INTEGER",
nullable: false,
defaultValue: 0,
oldClrType: typeof(int),
oldType: "INTEGER");
migrationBuilder.AlterColumn<DateTime>(
name: "LastXpGain",
table: "DiscordUser",
type: "TEXT",
nullable: false,
defaultValueSql: "datetime('now', '-1 years')",
oldClrType: typeof(DateTime),
oldType: "TEXT");
migrationBuilder.AlterColumn<DateTime>(
name: "LastLevelUp",
table: "DiscordUser",
type: "TEXT",
nullable: false,
defaultValueSql: "datetime('now')",
oldClrType: typeof(DateTime),
oldType: "TEXT",
oldDefaultValue: new DateTime(2017, 9, 21, 20, 53, 13, 305, DateTimeKind.Local));
migrationBuilder.AlterColumn<bool>(
name: "IsClubAdmin",
table: "DiscordUser",
type: "INTEGER",
nullable: false,
defaultValue: false,
oldClrType: typeof(bool),
oldType: "INTEGER");
} }
protected override void Down(MigrationBuilder migrationBuilder) protected override void Down(MigrationBuilder migrationBuilder)
@@ -323,6 +357,43 @@ namespace NadekoBot.Migrations
nullable: false, nullable: false,
defaultValue: false); defaultValue: false);
migrationBuilder.AlterColumn<int>(
name: "NotifyOnLevelUp",
table: "DiscordUser",
type: "INTEGER",
nullable: false,
oldClrType: typeof(int),
oldType: "INTEGER",
oldDefaultValue: 0);
migrationBuilder.AlterColumn<DateTime>(
name: "LastXpGain",
table: "DiscordUser",
type: "TEXT",
nullable: false,
oldClrType: typeof(DateTime),
oldType: "TEXT",
oldDefaultValueSql: "datetime('now', '-1 years')");
migrationBuilder.AlterColumn<DateTime>(
name: "LastLevelUp",
table: "DiscordUser",
type: "TEXT",
nullable: false,
defaultValue: new DateTime(2017, 9, 21, 20, 53, 13, 305, DateTimeKind.Local),
oldClrType: typeof(DateTime),
oldType: "TEXT",
oldDefaultValueSql: "datetime('now')");
migrationBuilder.AlterColumn<bool>(
name: "IsClubAdmin",
table: "DiscordUser",
type: "INTEGER",
nullable: false,
oldClrType: typeof(bool),
oldType: "INTEGER",
oldDefaultValue: false);
migrationBuilder.AddColumn<bool>( migrationBuilder.AddColumn<bool>(
name: "IsRegex", name: "IsRegex",
table: "CustomReactions", table: "CustomReactions",

View File

@@ -110,18 +110,24 @@ namespace NadekoBot.Migrations
.HasColumnType("TEXT"); .HasColumnType("TEXT");
b.Property<bool>("IsClubAdmin") b.Property<bool>("IsClubAdmin")
.HasColumnType("INTEGER"); .ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasDefaultValue(false);
b.Property<DateTime>("LastLevelUp") b.Property<DateTime>("LastLevelUp")
.ValueGeneratedOnAdd() .ValueGeneratedOnAdd()
.HasColumnType("TEXT") .HasColumnType("TEXT")
.HasDefaultValue(new DateTime(2017, 9, 21, 20, 53, 13, 305, DateTimeKind.Local)); .HasDefaultValueSql("datetime('now')");
b.Property<DateTime>("LastXpGain") b.Property<DateTime>("LastXpGain")
.HasColumnType("TEXT"); .ValueGeneratedOnAdd()
.HasColumnType("TEXT")
.HasDefaultValueSql("datetime('now', '-1 years')");
b.Property<int>("NotifyOnLevelUp") b.Property<int>("NotifyOnLevelUp")
.HasColumnType("INTEGER"); .ValueGeneratedOnAdd()
.HasColumnType("INTEGER")
.HasDefaultValue(0);
b.Property<int>("TotalXp") b.Property<int>("TotalXp")
.HasColumnType("INTEGER"); .HasColumnType("INTEGER");