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';
```
This commit is contained in:
Noi 2022-08-26 21:42:25 -07:00
parent dfec87672f
commit 5f00e8b4b2
5 changed files with 53 additions and 17 deletions

View file

@ -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<CachedUser>(entity => entity.Property(e => e.Discriminator).HasMaxLength(4).IsFixedLength());
modelBuilder.Entity<CachedGuildUser>(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<CachedGuildMessage>(e => e.Property(p => p.CreatedAt).HasDefaultValueSql("now()"));

View file

@ -6,14 +6,14 @@ namespace RegexBot.Data;
/// </summary>
[Table("cache_usersinguild")]
public class CachedGuildUser {
/// <inheritdoc cref="CachedUser.UserId"/>
public long UserId { get; set; }
/// <summary>
/// Gets the associated guild's snowflake ID.
/// </summary>
public long GuildId { get; set; }
/// <inheritdoc cref="CachedUser.UserId"/>
public long UserId { get; set; }
/// <inheritdoc cref="CachedUser.ULastUpdateTime"/>
public DateTimeOffset GULastUpdateTime { get; set; }

View file

@ -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<long>("UserId")
.HasColumnType("bigint")
.HasColumnName("user_id");
b.Property<long>("GuildId")
.HasColumnType("bigint")
.HasColumnName("guild_id");
b.Property<long>("UserId")
.HasColumnType("bigint")
.HasColumnName("user_id");
b.Property<DateTimeOffset>("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);
});

View file

@ -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" });
}
}
}

View file

@ -72,14 +72,14 @@ namespace RegexBot.Data.Migrations
modelBuilder.Entity("RegexBot.Data.CachedGuildUser", b =>
{
b.Property<long>("UserId")
.HasColumnType("bigint")
.HasColumnName("user_id");
b.Property<long>("GuildId")
.HasColumnType("bigint")
.HasColumnName("guild_id");
b.Property<long>("UserId")
.HasColumnType("bigint")
.HasColumnName("user_id");
b.Property<DateTimeOffset>("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);
});