From 4bb4236eb32d4ffa942037b223958023105ca328 Mon Sep 17 00:00:00 2001 From: Timothy Schenk Date: Wed, 22 Nov 2023 07:59:08 +0100 Subject: [PATCH] feat: annotation changes (index, delete, etc.) --- Server/DB/Documents/Account.cs | 5 +- Server/DB/Documents/Character.cs | 4 +- Server/DB/Documents/Guild.cs | 7 +- Server/DB/Documents/GuildMember.cs | 5 +- ...xingAndVariousOtherAnnotations.Designer.cs | 352 ++++++++++++++++++ ...4508_IndexingAndVariousOtherAnnotations.cs | 205 ++++++++++ .../WonderkingContextModelSnapshot.cs | 28 +- 7 files changed, 593 insertions(+), 13 deletions(-) create mode 100644 Server/DB/Migrations/20231122064508_IndexingAndVariousOtherAnnotations.Designer.cs create mode 100644 Server/DB/Migrations/20231122064508_IndexingAndVariousOtherAnnotations.cs diff --git a/Server/DB/Documents/Account.cs b/Server/DB/Documents/Account.cs index 75526ae..c2e04cb 100644 --- a/Server/DB/Documents/Account.cs +++ b/Server/DB/Documents/Account.cs @@ -23,14 +23,13 @@ public class Account [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } - [Column(TypeName = "varchar(20)")] - [MaxLength(20)] - public string Username { get; set; } + [MaxLength(20)] public string Username { get; set; } [Column(TypeName = "bytea")] public byte[] Password { get; set; } [EmailAddress] public string Email { get; set; } public byte PermissionLevel { get; set; } [Column(TypeName = "bytea")] public byte[] Salt { get; set; } + public virtual ICollection Characters { get; } = new List(); } diff --git a/Server/DB/Documents/Character.cs b/Server/DB/Documents/Character.cs index 1040c93..da3de09 100644 --- a/Server/DB/Documents/Character.cs +++ b/Server/DB/Documents/Character.cs @@ -12,6 +12,7 @@ namespace Server.DB.Documents; [Index(nameof(Id), IsUnique = true)] public class Character { + [DeleteBehavior(DeleteBehavior.Restrict)] public virtual Account Account { get; set; } [Key] @@ -20,7 +21,6 @@ public class Character public ushort MapId { get; set; } - [Column(TypeName = "varchar(20)")] [MaxLength(20)] public string Name { get; set; } @@ -38,5 +38,7 @@ public class Character public JobData JobData { get; set; } public int Health { get; set; } public int Mana { get; set; } + + [DeleteBehavior(DeleteBehavior.Restrict)] public virtual Guild Guild { get; set; } } diff --git a/Server/DB/Documents/Guild.cs b/Server/DB/Documents/Guild.cs index 681a23e..2ca1fbf 100644 --- a/Server/DB/Documents/Guild.cs +++ b/Server/DB/Documents/Guild.cs @@ -2,16 +2,21 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using Microsoft.EntityFrameworkCore; namespace Server.DB.Documents; +[Index(nameof(Name), IsUnique = true)] +[Index(nameof(Id), IsUnique = true)] public class Guild { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } - public string Name { get; set; } + [MaxLength(16)] public string Name { get; set; } public string Notice { get; set; } + + [DeleteBehavior(DeleteBehavior.Cascade)] public virtual ICollection GuildMembers { get; set; } } diff --git a/Server/DB/Documents/GuildMember.cs b/Server/DB/Documents/GuildMember.cs index cc6e72e..597cbc8 100644 --- a/Server/DB/Documents/GuildMember.cs +++ b/Server/DB/Documents/GuildMember.cs @@ -2,16 +2,19 @@ using System.ComponentModel.DataAnnotations; using System.ComponentModel.DataAnnotations.Schema; +using Microsoft.EntityFrameworkCore; namespace Server.DB.Documents; +[Index(nameof(Id), IsUnique = true)] public class GuildMember { [Key] [DatabaseGenerated(DatabaseGeneratedOption.Identity)] public Guid Id { get; set; } - + [DeleteBehavior(DeleteBehavior.Restrict)] public virtual Character Character { get; set; } + [DeleteBehavior(DeleteBehavior.Restrict)] public virtual Guild Guild { get; set; } public GuildRank Rank { get; set; } } diff --git a/Server/DB/Migrations/20231122064508_IndexingAndVariousOtherAnnotations.Designer.cs b/Server/DB/Migrations/20231122064508_IndexingAndVariousOtherAnnotations.Designer.cs new file mode 100644 index 0000000..46a0608 --- /dev/null +++ b/Server/DB/Migrations/20231122064508_IndexingAndVariousOtherAnnotations.Designer.cs @@ -0,0 +1,352 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using Server.DB; + +#nullable disable + +namespace Server.DB.Migrations +{ + [DbContext(typeof(WonderkingContext))] + [Migration("20231122064508_IndexingAndVariousOtherAnnotations")] + partial class IndexingAndVariousOtherAnnotations + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "8.0.0") + .HasAnnotation("Proxies:ChangeTracking", false) + .HasAnnotation("Proxies:CheckEquality", false) + .HasAnnotation("Proxies:LazyLoading", true) + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("Server.DB.Documents.Account", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Email") + .HasColumnType("text"); + + b.Property("Password") + .HasColumnType("bytea"); + + b.Property("PermissionLevel") + .HasColumnType("smallint"); + + b.Property("Salt") + .HasColumnType("bytea"); + + b.Property("Username") + .HasMaxLength(20) + .HasColumnType("character varying(20)"); + + b.HasKey("Id"); + + b.HasIndex("Id") + .IsUnique(); + + b.HasIndex("Username") + .IsUnique(); + + b.ToTable("Accounts"); + }); + + modelBuilder.Entity("Server.DB.Documents.Character", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AccountId") + .HasColumnType("uuid"); + + b.Property("Experience") + .HasColumnType("bigint"); + + b.Property("Gender") + .HasColumnType("smallint"); + + b.Property("GuildId") + .HasColumnType("uuid"); + + b.Property("Health") + .HasColumnType("integer"); + + b.Property("LastXCoordinate") + .HasColumnType("smallint"); + + b.Property("LastYCoordinate") + .HasColumnType("smallint"); + + b.Property("Level") + .HasColumnType("smallint"); + + b.Property("Mana") + .HasColumnType("integer"); + + b.Property("MapId") + .HasColumnType("integer"); + + b.Property("Name") + .HasMaxLength(20) + .HasColumnType("character varying(20)"); + + b.Property("PvPLevel") + .HasColumnType("smallint"); + + b.HasKey("Id"); + + b.HasIndex("AccountId"); + + b.HasIndex("GuildId"); + + b.HasIndex("Id") + .IsUnique(); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("Characters"); + }); + + modelBuilder.Entity("Server.DB.Documents.Guild", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("Name") + .HasMaxLength(16) + .HasColumnType("character varying(16)"); + + b.Property("Notice") + .HasColumnType("text"); + + b.HasKey("Id"); + + b.HasIndex("Id") + .IsUnique(); + + b.HasIndex("Name") + .IsUnique(); + + b.ToTable("Guilds"); + }); + + modelBuilder.Entity("Server.DB.Documents.GuildMember", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("CharacterId") + .HasColumnType("uuid"); + + b.Property("GuildId") + .HasColumnType("uuid"); + + b.Property("Rank") + .HasColumnType("smallint"); + + b.HasKey("Id"); + + b.HasIndex("CharacterId"); + + b.HasIndex("GuildId"); + + b.HasIndex("Id") + .IsUnique(); + + b.ToTable("GuildMember"); + }); + + modelBuilder.Entity("Server.DB.Documents.InventoryItem", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("uuid"); + + b.Property("AddOption") + .HasColumnType("smallint"); + + b.Property("AddOption2") + .HasColumnType("smallint"); + + b.Property("AddOption3") + .HasColumnType("smallint"); + + b.Property("CharacterId") + .HasColumnType("uuid"); + + b.Property("Count") + .HasColumnType("integer"); + + b.Property("InventoryTab") + .HasColumnType("smallint"); + + b.Property("ItemId") + .HasColumnType("integer"); + + b.Property("Level") + .HasColumnType("smallint"); + + b.Property("Option") + .HasColumnType("smallint"); + + b.Property("Option2") + .HasColumnType("smallint"); + + b.Property("Option3") + .HasColumnType("smallint"); + + b.Property("Rarity") + .HasColumnType("smallint"); + + b.Property("Slot") + .HasColumnType("smallint"); + + b.HasKey("Id"); + + b.HasIndex("CharacterId"); + + b.ToTable("InventoryItems"); + }); + + modelBuilder.Entity("Server.DB.Documents.Character", b => + { + b.HasOne("Server.DB.Documents.Account", "Account") + .WithMany("Characters") + .HasForeignKey("AccountId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("Server.DB.Documents.Guild", "Guild") + .WithMany() + .HasForeignKey("GuildId") + .OnDelete(DeleteBehavior.Restrict); + + b.OwnsOne("Wonderking.Packets.Outgoing.Data.BaseStats", "BaseStats", b1 => + { + b1.Property("CharacterId") + .HasColumnType("uuid"); + + b1.Property("Dexterity") + .HasColumnType("smallint") + .HasAnnotation("Relational:JsonPropertyName", "dexterity"); + + b1.Property("Intelligence") + .HasColumnType("smallint") + .HasAnnotation("Relational:JsonPropertyName", "intelligence"); + + b1.Property("Luck") + .HasColumnType("smallint") + .HasAnnotation("Relational:JsonPropertyName", "luck"); + + b1.Property("Strength") + .HasColumnType("smallint") + .HasAnnotation("Relational:JsonPropertyName", "strength"); + + b1.Property("Vitality") + .HasColumnType("smallint") + .HasAnnotation("Relational:JsonPropertyName", "vitality"); + + b1.Property("Wisdom") + .HasColumnType("smallint") + .HasAnnotation("Relational:JsonPropertyName", "wisdom"); + + b1.HasKey("CharacterId"); + + b1.ToTable("Characters"); + + b1.WithOwner() + .HasForeignKey("CharacterId"); + }); + + b.OwnsOne("Wonderking.Packets.Outgoing.Data.JobData", "JobData", b1 => + { + b1.Property("CharacterId") + .HasColumnType("uuid"); + + b1.Property("FirstJob") + .HasColumnType("smallint"); + + b1.Property("FourthJob") + .HasColumnType("smallint"); + + b1.Property("SecondJob") + .HasColumnType("smallint"); + + b1.Property("ThirdJob") + .HasColumnType("smallint"); + + b1.HasKey("CharacterId"); + + b1.ToTable("Characters"); + + b1.WithOwner() + .HasForeignKey("CharacterId"); + }); + + b.Navigation("Account"); + + b.Navigation("BaseStats"); + + b.Navigation("Guild"); + + b.Navigation("JobData"); + }); + + modelBuilder.Entity("Server.DB.Documents.GuildMember", b => + { + b.HasOne("Server.DB.Documents.Character", "Character") + .WithMany() + .HasForeignKey("CharacterId") + .OnDelete(DeleteBehavior.Restrict); + + b.HasOne("Server.DB.Documents.Guild", "Guild") + .WithMany("GuildMembers") + .HasForeignKey("GuildId") + .OnDelete(DeleteBehavior.Cascade); + + b.Navigation("Character"); + + b.Navigation("Guild"); + }); + + modelBuilder.Entity("Server.DB.Documents.InventoryItem", b => + { + b.HasOne("Server.DB.Documents.Character", "Character") + .WithMany("InventoryItems") + .HasForeignKey("CharacterId") + .OnDelete(DeleteBehavior.Cascade); + + b.Navigation("Character"); + }); + + modelBuilder.Entity("Server.DB.Documents.Account", b => + { + b.Navigation("Characters"); + }); + + modelBuilder.Entity("Server.DB.Documents.Character", b => + { + b.Navigation("InventoryItems"); + }); + + modelBuilder.Entity("Server.DB.Documents.Guild", b => + { + b.Navigation("GuildMembers"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/Server/DB/Migrations/20231122064508_IndexingAndVariousOtherAnnotations.cs b/Server/DB/Migrations/20231122064508_IndexingAndVariousOtherAnnotations.cs new file mode 100644 index 0000000..82980fb --- /dev/null +++ b/Server/DB/Migrations/20231122064508_IndexingAndVariousOtherAnnotations.cs @@ -0,0 +1,205 @@ +// Copyright (c) 2023 Timothy Schenk. Subject to the GNU AGPL Version 3 License. + +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace Server.DB.Migrations; + +/// +public partial class IndexingAndVariousOtherAnnotations : Migration +{ + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Characters_Accounts_AccountId", + table: "Characters"); + + migrationBuilder.DropForeignKey( + name: "FK_Characters_Guilds_GuildId", + table: "Characters"); + + migrationBuilder.DropForeignKey( + name: "FK_GuildMember_Characters_CharacterId", + table: "GuildMember"); + + migrationBuilder.DropForeignKey( + name: "FK_GuildMember_Guilds_GuildId", + table: "GuildMember"); + + migrationBuilder.AlterColumn( + name: "Name", + table: "Guilds", + type: "character varying(16)", + maxLength: 16, + nullable: true, + oldClrType: typeof(string), + oldType: "text", + oldNullable: true); + + migrationBuilder.AlterColumn( + name: "Name", + table: "Characters", + type: "character varying(20)", + maxLength: 20, + nullable: true, + oldClrType: typeof(string), + oldType: "varchar(20)", + oldMaxLength: 20, + oldNullable: true); + + migrationBuilder.AlterColumn( + name: "Username", + table: "Accounts", + type: "character varying(20)", + maxLength: 20, + nullable: true, + oldClrType: typeof(string), + oldType: "varchar(20)", + oldMaxLength: 20, + oldNullable: true); + + migrationBuilder.CreateIndex( + name: "IX_Guilds_Id", + table: "Guilds", + column: "Id", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_Guilds_Name", + table: "Guilds", + column: "Name", + unique: true); + + migrationBuilder.CreateIndex( + name: "IX_GuildMember_Id", + table: "GuildMember", + column: "Id", + unique: true); + + migrationBuilder.AddForeignKey( + name: "FK_Characters_Accounts_AccountId", + table: "Characters", + column: "AccountId", + principalTable: "Accounts", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_Characters_Guilds_GuildId", + table: "Characters", + column: "GuildId", + principalTable: "Guilds", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_GuildMember_Characters_CharacterId", + table: "GuildMember", + column: "CharacterId", + principalTable: "Characters", + principalColumn: "Id", + onDelete: ReferentialAction.Restrict); + + migrationBuilder.AddForeignKey( + name: "FK_GuildMember_Guilds_GuildId", + table: "GuildMember", + column: "GuildId", + principalTable: "Guilds", + principalColumn: "Id", + onDelete: ReferentialAction.Cascade); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropForeignKey( + name: "FK_Characters_Accounts_AccountId", + table: "Characters"); + + migrationBuilder.DropForeignKey( + name: "FK_Characters_Guilds_GuildId", + table: "Characters"); + + migrationBuilder.DropForeignKey( + name: "FK_GuildMember_Characters_CharacterId", + table: "GuildMember"); + + migrationBuilder.DropForeignKey( + name: "FK_GuildMember_Guilds_GuildId", + table: "GuildMember"); + + migrationBuilder.DropIndex( + name: "IX_Guilds_Id", + table: "Guilds"); + + migrationBuilder.DropIndex( + name: "IX_Guilds_Name", + table: "Guilds"); + + migrationBuilder.DropIndex( + name: "IX_GuildMember_Id", + table: "GuildMember"); + + migrationBuilder.AlterColumn( + name: "Name", + table: "Guilds", + type: "text", + nullable: true, + oldClrType: typeof(string), + oldType: "character varying(16)", + oldMaxLength: 16, + oldNullable: true); + + migrationBuilder.AlterColumn( + name: "Name", + table: "Characters", + type: "varchar(20)", + maxLength: 20, + nullable: true, + oldClrType: typeof(string), + oldType: "character varying(20)", + oldMaxLength: 20, + oldNullable: true); + + migrationBuilder.AlterColumn( + name: "Username", + table: "Accounts", + type: "varchar(20)", + maxLength: 20, + nullable: true, + oldClrType: typeof(string), + oldType: "character varying(20)", + oldMaxLength: 20, + oldNullable: true); + + migrationBuilder.AddForeignKey( + name: "FK_Characters_Accounts_AccountId", + table: "Characters", + column: "AccountId", + principalTable: "Accounts", + principalColumn: "Id"); + + migrationBuilder.AddForeignKey( + name: "FK_Characters_Guilds_GuildId", + table: "Characters", + column: "GuildId", + principalTable: "Guilds", + principalColumn: "Id"); + + migrationBuilder.AddForeignKey( + name: "FK_GuildMember_Characters_CharacterId", + table: "GuildMember", + column: "CharacterId", + principalTable: "Characters", + principalColumn: "Id"); + + migrationBuilder.AddForeignKey( + name: "FK_GuildMember_Guilds_GuildId", + table: "GuildMember", + column: "GuildId", + principalTable: "Guilds", + principalColumn: "Id"); + } +} diff --git a/Server/DB/Migrations/WonderkingContextModelSnapshot.cs b/Server/DB/Migrations/WonderkingContextModelSnapshot.cs index 5e958ba..27ac844 100644 --- a/Server/DB/Migrations/WonderkingContextModelSnapshot.cs +++ b/Server/DB/Migrations/WonderkingContextModelSnapshot.cs @@ -45,7 +45,7 @@ namespace Server.DB.Migrations b.Property("Username") .HasMaxLength(20) - .HasColumnType("varchar(20)"); + .HasColumnType("character varying(20)"); b.HasKey("Id"); @@ -96,7 +96,7 @@ namespace Server.DB.Migrations b.Property("Name") .HasMaxLength(20) - .HasColumnType("varchar(20)"); + .HasColumnType("character varying(20)"); b.Property("PvPLevel") .HasColumnType("smallint"); @@ -123,13 +123,20 @@ namespace Server.DB.Migrations .HasColumnType("uuid"); b.Property("Name") - .HasColumnType("text"); + .HasMaxLength(16) + .HasColumnType("character varying(16)"); b.Property("Notice") .HasColumnType("text"); b.HasKey("Id"); + b.HasIndex("Id") + .IsUnique(); + + b.HasIndex("Name") + .IsUnique(); + b.ToTable("Guilds"); }); @@ -154,6 +161,9 @@ namespace Server.DB.Migrations b.HasIndex("GuildId"); + b.HasIndex("Id") + .IsUnique(); + b.ToTable("GuildMember"); }); @@ -213,11 +223,13 @@ namespace Server.DB.Migrations { b.HasOne("Server.DB.Documents.Account", "Account") .WithMany("Characters") - .HasForeignKey("AccountId"); + .HasForeignKey("AccountId") + .OnDelete(DeleteBehavior.Restrict); b.HasOne("Server.DB.Documents.Guild", "Guild") .WithMany() - .HasForeignKey("GuildId"); + .HasForeignKey("GuildId") + .OnDelete(DeleteBehavior.Restrict); b.OwnsOne("Wonderking.Packets.Outgoing.Data.BaseStats", "BaseStats", b1 => { @@ -294,11 +306,13 @@ namespace Server.DB.Migrations { b.HasOne("Server.DB.Documents.Character", "Character") .WithMany() - .HasForeignKey("CharacterId"); + .HasForeignKey("CharacterId") + .OnDelete(DeleteBehavior.Restrict); b.HasOne("Server.DB.Documents.Guild", "Guild") .WithMany("GuildMembers") - .HasForeignKey("GuildId"); + .HasForeignKey("GuildId") + .OnDelete(DeleteBehavior.Cascade); b.Navigation("Character");