diff --git a/ProjectLighthouse/Migrations/20220413192851_AddUserForeignKeyToLastContacts.cs b/ProjectLighthouse/Migrations/20220413192851_AddUserForeignKeyToLastContacts.cs new file mode 100644 index 00000000..08951789 --- /dev/null +++ b/ProjectLighthouse/Migrations/20220413192851_AddUserForeignKeyToLastContacts.cs @@ -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( + 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( + name: "UserId", + table: "LastContacts", + type: "int", + nullable: false, + oldClrType: typeof(int), + oldType: "int") + .Annotation("MySql:ValueGenerationStrategy", MySqlValueGenerationStrategy.IdentityColumn); + } + } +} diff --git a/ProjectLighthouse/Migrations/DatabaseModelSnapshot.cs b/ProjectLighthouse/Migrations/DatabaseModelSnapshot.cs index c57b8eaf..6acb3ff5 100644 --- a/ProjectLighthouse/Migrations/DatabaseModelSnapshot.cs +++ b/ProjectLighthouse/Migrations/DatabaseModelSnapshot.cs @@ -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("UserId") - .ValueGeneratedOnAdd() .HasColumnType("int"); b.Property("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") diff --git a/ProjectLighthouse/Types/Profiles/LastContact.cs b/ProjectLighthouse/Types/Profiles/LastContact.cs index 14073106..5e7b1fea 100644 --- a/ProjectLighthouse/Types/Profiles/LastContact.cs +++ b/ProjectLighthouse/Types/Profiles/LastContact.cs @@ -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;