Add user foreign key to user last contacts

Closes #247
This commit is contained in:
jvyden 2022-04-13 15:34:45 -04:00
parent e51d35df44
commit 8b8c5b256c
No known key found for this signature in database
GPG key ID: 18BCF2BE0262B278
3 changed files with 73 additions and 2 deletions

View file

@ -0,0 +1,57 @@
using Microsoft.EntityFrameworkCore.Metadata;
using Microsoft.EntityFrameworkCore.Migrations;
using LBPUnion.ProjectLighthouse;
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Storage.ValueConversion;
#nullable disable
namespace ProjectLighthouse.Migrations
{
[DbContext(typeof(Database))]
[Migration("20220413192851_AddUserForeignKeyToLastContacts")]
public partial class AddUserForeignKeyToLastContacts : Migration
{
protected override void Up(MigrationBuilder migrationBuilder)
{
// We have to delete the previous last contact information here, since we cannot guarantee that it exists.
// There's no reliance on this information for long-term usage anyways.
// See https://github.com/LBPUnion/ProjectLighthouse/issues/247 for more information
migrationBuilder.Sql("DELETE FROM LastContacts;");
migrationBuilder.AlterColumn<int>(
name: "UserId",
table: "LastContacts",
type: "int",
nullable: false,
oldClrType: typeof(int),
oldType: "int")
.OldAnnotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn);
migrationBuilder.AddForeignKey(
name: "FK_LastContacts_Users_UserId",
table: "LastContacts",
column: "UserId",
principalTable: "Users",
principalColumn: "UserId",
onDelete: ReferentialAction.Cascade);
}
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_LastContacts_Users_UserId",
table: "LastContacts");
migrationBuilder.AlterColumn<int>(
name: "UserId",
table: "LastContacts",
type: "int",
nullable: false,
oldClrType: typeof(int),
oldType: "int")
.Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn);
}
}
}

View file

@ -15,7 +15,7 @@ namespace ProjectLighthouse.Migrations
{
#pragma warning disable 612, 618
modelBuilder
.HasAnnotation("ProductVersion", "6.0.3")
.HasAnnotation("ProductVersion", "6.0.4")
.HasAnnotation("Relational:MaxIdentifierLength", 64);
modelBuilder.Entity("LBPUnion.ProjectLighthouse.Types.AuthenticationAttempt", b =>
@ -493,7 +493,6 @@ namespace ProjectLighthouse.Migrations
modelBuilder.Entity("LBPUnion.ProjectLighthouse.Types.Profiles.LastContact", b =>
{
b.Property<int>("UserId")
.ValueGeneratedOnAdd()
.HasColumnType("int");
b.Property<int>("GameVersion")
@ -989,6 +988,17 @@ namespace ProjectLighthouse.Migrations
b.Navigation("User");
});
modelBuilder.Entity("LBPUnion.ProjectLighthouse.Types.Profiles.LastContact", b =>
{
b.HasOne("LBPUnion.ProjectLighthouse.Types.User", "User")
.WithMany()
.HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade)
.IsRequired();
b.Navigation("User");
});
modelBuilder.Entity("LBPUnion.ProjectLighthouse.Types.Reports.GriefReport", b =>
{
b.HasOne("LBPUnion.ProjectLighthouse.Types.User", "ReportingPlayer")

View file

@ -1,4 +1,5 @@
using System.ComponentModel.DataAnnotations;
using System.ComponentModel.DataAnnotations.Schema;
namespace LBPUnion.ProjectLighthouse.Types.Profiles;
@ -7,6 +8,9 @@ public class LastContact
[Key]
public int UserId { get; set; }
[ForeignKey(nameof(UserId))]
public User User { get; set; }
public long Timestamp { get; set; }
public GameVersion GameVersion { get; set; } = GameVersion.Unknown;