From 5f00e8b4b235204cc988c3c9b3827e40305a6fd6 Mon Sep 17 00:00:00 2001 From: Noi Date: Fri, 26 Aug 2022 21:42:25 -0700 Subject: [PATCH] Fix incorrect foreign key reference Replaces previously committed migration with a new one. If updating the database, run these first before the migration: ``` drop table modlogs; drop type mod_log_type; delete from "__EFMigrationsHistory" where "migration_id" = '20220824023321_AddModLogs'; ``` --- Data/BotDatabaseContext.cs | 7 +++-- Data/CachedGuildUser.cs | 6 ++-- ... => 20220827041853_AddModLogs.Designer.cs} | 15 ++++++---- ...odLogs.cs => 20220827041853_AddModLogs.cs} | 29 ++++++++++++++++++- .../BotDatabaseContextModelSnapshot.cs | 13 +++++---- 5 files changed, 53 insertions(+), 17 deletions(-) rename Data/Migrations/{20220824023321_AddModLogs.Designer.cs => 20220827041853_AddModLogs.Designer.cs} (97%) rename Data/Migrations/{20220824023321_AddModLogs.cs => 20220827041853_AddModLogs.cs} (68%) diff --git a/Data/BotDatabaseContext.cs b/Data/BotDatabaseContext.cs index d1e1a1b..319594d 100644 --- a/Data/BotDatabaseContext.cs +++ b/Data/BotDatabaseContext.cs @@ -12,6 +12,9 @@ public class BotDatabaseContext : DbContext { // Get our own config loaded just for the SQL stuff var conf = new InstanceConfig(); _connectionString = new NpgsqlConnectionStringBuilder() { +#if DEBUG + IncludeErrorDetail = true, +#endif Host = conf.SqlHost ?? "localhost", // default to localhost Database = conf.SqlDatabase, Username = conf.SqlUsername, @@ -49,7 +52,7 @@ public class BotDatabaseContext : DbContext { protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity(entity => entity.Property(e => e.Discriminator).HasMaxLength(4).IsFixedLength()); modelBuilder.Entity(e => { - e.HasKey(p => new { p.UserId, p.GuildId }); + e.HasKey(p => new { p.GuildId, p.UserId }); e.Property(p => p.FirstSeenTime).HasDefaultValueSql("now()"); }); modelBuilder.Entity(e => e.Property(p => p.CreatedAt).HasDefaultValueSql("now()")); @@ -58,7 +61,7 @@ public class BotDatabaseContext : DbContext { e.Property(p => p.Timestamp).HasDefaultValueSql("now()"); e.HasOne(entry => entry.User) .WithMany(gu => gu.Logs) - .HasForeignKey(entry => new {entry.GuildId, entry.UserId}); + .HasForeignKey(entry => new { entry.GuildId, entry.UserId }); }); } } diff --git a/Data/CachedGuildUser.cs b/Data/CachedGuildUser.cs index 6d57b1c..e259e96 100644 --- a/Data/CachedGuildUser.cs +++ b/Data/CachedGuildUser.cs @@ -6,14 +6,14 @@ namespace RegexBot.Data; /// [Table("cache_usersinguild")] public class CachedGuildUser { - /// - public long UserId { get; set; } - /// /// Gets the associated guild's snowflake ID. /// public long GuildId { get; set; } + /// + public long UserId { get; set; } + /// public DateTimeOffset GULastUpdateTime { get; set; } diff --git a/Data/Migrations/20220824023321_AddModLogs.Designer.cs b/Data/Migrations/20220827041853_AddModLogs.Designer.cs similarity index 97% rename from Data/Migrations/20220824023321_AddModLogs.Designer.cs rename to Data/Migrations/20220827041853_AddModLogs.Designer.cs index 70fd9f9..06cd61e 100644 --- a/Data/Migrations/20220824023321_AddModLogs.Designer.cs +++ b/Data/Migrations/20220827041853_AddModLogs.Designer.cs @@ -13,7 +13,7 @@ using RegexBot.Data; namespace RegexBot.Data.Migrations { [DbContext(typeof(BotDatabaseContext))] - [Migration("20220824023321_AddModLogs")] + [Migration("20220827041853_AddModLogs")] partial class AddModLogs { protected override void BuildTargetModel(ModelBuilder modelBuilder) @@ -74,14 +74,14 @@ namespace RegexBot.Data.Migrations modelBuilder.Entity("RegexBot.Data.CachedGuildUser", b => { - b.Property("UserId") - .HasColumnType("bigint") - .HasColumnName("user_id"); - b.Property("GuildId") .HasColumnType("bigint") .HasColumnName("guild_id"); + b.Property("UserId") + .HasColumnType("bigint") + .HasColumnName("user_id"); + b.Property("FirstSeenTime") .ValueGeneratedOnAdd() .HasColumnType("timestamp with time zone") @@ -96,9 +96,12 @@ namespace RegexBot.Data.Migrations .HasColumnType("text") .HasColumnName("nickname"); - b.HasKey("UserId", "GuildId") + b.HasKey("GuildId", "UserId") .HasName("pk_cache_usersinguild"); + b.HasIndex("UserId") + .HasDatabaseName("ix_cache_usersinguild_user_id"); + b.ToTable("cache_usersinguild", (string)null); }); diff --git a/Data/Migrations/20220824023321_AddModLogs.cs b/Data/Migrations/20220827041853_AddModLogs.cs similarity index 68% rename from Data/Migrations/20220824023321_AddModLogs.cs rename to Data/Migrations/20220827041853_AddModLogs.cs index 3112a19..ef19f9d 100644 --- a/Data/Migrations/20220824023321_AddModLogs.cs +++ b/Data/Migrations/20220827041853_AddModLogs.cs @@ -10,9 +10,18 @@ namespace RegexBot.Data.Migrations { protected override void Up(MigrationBuilder migrationBuilder) { + migrationBuilder.DropPrimaryKey( + name: "pk_cache_usersinguild", + table: "cache_usersinguild"); + migrationBuilder.AlterDatabase() .Annotation("Npgsql:Enum:mod_log_type", "other,note,warn,timeout,kick,ban"); + migrationBuilder.AddPrimaryKey( + name: "pk_cache_usersinguild", + table: "cache_usersinguild", + columns: new[] { "guild_id", "user_id" }); + migrationBuilder.CreateTable( name: "modlogs", columns: table => new @@ -33,10 +42,15 @@ namespace RegexBot.Data.Migrations name: "fk_modlogs_cache_usersinguild_user_temp_id", columns: x => new { x.guild_id, x.user_id }, principalTable: "cache_usersinguild", - principalColumns: new[] { "user_id", "guild_id" }, + principalColumns: new[] { "guild_id", "user_id" }, onDelete: ReferentialAction.Cascade); }); + migrationBuilder.CreateIndex( + name: "ix_cache_usersinguild_user_id", + table: "cache_usersinguild", + column: "user_id"); + migrationBuilder.CreateIndex( name: "ix_modlogs_guild_id_user_id", table: "modlogs", @@ -48,8 +62,21 @@ namespace RegexBot.Data.Migrations migrationBuilder.DropTable( name: "modlogs"); + migrationBuilder.DropPrimaryKey( + name: "pk_cache_usersinguild", + table: "cache_usersinguild"); + + migrationBuilder.DropIndex( + name: "ix_cache_usersinguild_user_id", + table: "cache_usersinguild"); + migrationBuilder.AlterDatabase() .OldAnnotation("Npgsql:Enum:mod_log_type", "other,note,warn,timeout,kick,ban"); + + migrationBuilder.AddPrimaryKey( + name: "pk_cache_usersinguild", + table: "cache_usersinguild", + columns: new[] { "user_id", "guild_id" }); } } } diff --git a/Data/Migrations/BotDatabaseContextModelSnapshot.cs b/Data/Migrations/BotDatabaseContextModelSnapshot.cs index 75b2917..e19c408 100644 --- a/Data/Migrations/BotDatabaseContextModelSnapshot.cs +++ b/Data/Migrations/BotDatabaseContextModelSnapshot.cs @@ -72,14 +72,14 @@ namespace RegexBot.Data.Migrations modelBuilder.Entity("RegexBot.Data.CachedGuildUser", b => { - b.Property("UserId") - .HasColumnType("bigint") - .HasColumnName("user_id"); - b.Property("GuildId") .HasColumnType("bigint") .HasColumnName("guild_id"); + b.Property("UserId") + .HasColumnType("bigint") + .HasColumnName("user_id"); + b.Property("FirstSeenTime") .ValueGeneratedOnAdd() .HasColumnType("timestamp with time zone") @@ -94,9 +94,12 @@ namespace RegexBot.Data.Migrations .HasColumnType("text") .HasColumnName("nickname"); - b.HasKey("UserId", "GuildId") + b.HasKey("GuildId", "UserId") .HasName("pk_cache_usersinguild"); + b.HasIndex("UserId") + .HasDatabaseName("ix_cache_usersinguild_user_id"); + b.ToTable("cache_usersinguild", (string)null); });