Update initial database model and migration

This commit is contained in:
Noi 2022-07-20 20:37:31 -07:00
parent fc88ab5cf9
commit c3ecf2a877
6 changed files with 53 additions and 138 deletions

View file

@ -59,4 +59,19 @@ public class CachedGuildMessage {
[ForeignKey(nameof(AuthorId))] [ForeignKey(nameof(AuthorId))]
[InverseProperty(nameof(CachedUser.GuildMessages))] [InverseProperty(nameof(CachedUser.GuildMessages))]
public CachedUser Author { get; set; } = null!; public CachedUser Author { get; set; } = null!;
// Used by MessageCachingSubservice
internal static CachedGuildMessage? Clone(CachedGuildMessage? original) {
if (original == null) return null;
return new() {
MessageId = original.MessageId,
AuthorId = original.AuthorId,
GuildId = original.GuildId,
ChannelId = original.ChannelId,
CreatedAt = original.CreatedAt,
EditedAt = original.EditedAt,
AttachmentNames = new(original.AttachmentNames),
Content = original.Content
};
}
} }

View file

@ -1,3 +1,4 @@
[*.cs] [*.cs]
generated_code = true
dotnet_analyzer_diagnostic.category-CodeQuality.severity = none dotnet_analyzer_diagnostic.category-CodeQuality.severity = none
dotnet_diagnostic.CS1591.severity = none dotnet_diagnostic.CS1591.severity = none

View file

@ -13,14 +13,14 @@ using RegexBot.Data;
namespace RegexBot.Data.Migrations namespace RegexBot.Data.Migrations
{ {
[DbContext(typeof(BotDatabaseContext))] [DbContext(typeof(BotDatabaseContext))]
[Migration("20220610210059_InitialMigration")] [Migration("20220721021550_InitialMigration")]
partial class InitialMigration partial class InitialMigration
{ {
protected override void BuildTargetModel(ModelBuilder modelBuilder) protected override void BuildTargetModel(ModelBuilder modelBuilder)
{ {
#pragma warning disable 612, 618 #pragma warning disable 612, 618
modelBuilder modelBuilder
.HasAnnotation("ProductVersion", "6.0.5") .HasAnnotation("ProductVersion", "6.0.7")
.HasAnnotation("Relational:MaxIdentifierLength", 63); .HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
@ -64,12 +64,12 @@ namespace RegexBot.Data.Migrations
.HasColumnName("guild_id"); .HasColumnName("guild_id");
b.HasKey("MessageId") b.HasKey("MessageId")
.HasName("pk_cache_messages"); .HasName("pk_cache_guildmessages");
b.HasIndex("AuthorId") b.HasIndex("AuthorId")
.HasDatabaseName("ix_cache_messages_author_id"); .HasDatabaseName("ix_cache_guildmessages_author_id");
b.ToTable("cache_messages", (string)null); b.ToTable("cache_guildmessages", (string)null);
}); });
modelBuilder.Entity("RegexBot.Data.CachedGuildUser", b => modelBuilder.Entity("RegexBot.Data.CachedGuildUser", b =>
@ -97,9 +97,9 @@ namespace RegexBot.Data.Migrations
.HasColumnName("nickname"); .HasColumnName("nickname");
b.HasKey("UserId", "GuildId") b.HasKey("UserId", "GuildId")
.HasName("pk_cache_userguild"); .HasName("pk_cache_usersinguild");
b.ToTable("cache_userguild", (string)null); b.ToTable("cache_usersinguild", (string)null);
}); });
modelBuilder.Entity("RegexBot.Data.CachedUser", b => modelBuilder.Entity("RegexBot.Data.CachedUser", b =>
@ -129,47 +129,9 @@ namespace RegexBot.Data.Migrations
.HasColumnName("username"); .HasColumnName("username");
b.HasKey("UserId") b.HasKey("UserId")
.HasName("pk_cache_user"); .HasName("pk_cache_users");
b.ToTable("cache_user", (string)null); b.ToTable("cache_users", (string)null);
});
modelBuilder.Entity("RegexBot.Data.GuildLogLine", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasColumnName("id");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<long>("GuildId")
.HasColumnType("bigint")
.HasColumnName("guild_id");
b.Property<string>("Message")
.IsRequired()
.HasColumnType("text")
.HasColumnName("message");
b.Property<string>("Source")
.IsRequired()
.HasColumnType("text")
.HasColumnName("source");
b.Property<DateTimeOffset>("Timestamp")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasColumnName("timestamp")
.HasDefaultValueSql("now()");
b.HasKey("Id")
.HasName("pk_guild_log");
b.HasIndex("GuildId")
.HasDatabaseName("ix_guild_log_guild_id");
b.ToTable("guild_log", (string)null);
}); });
modelBuilder.Entity("RegexBot.Data.CachedGuildMessage", b => modelBuilder.Entity("RegexBot.Data.CachedGuildMessage", b =>
@ -179,7 +141,7 @@ namespace RegexBot.Data.Migrations
.HasForeignKey("AuthorId") .HasForeignKey("AuthorId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired() .IsRequired()
.HasConstraintName("fk_cache_messages_cache_user_author_id"); .HasConstraintName("fk_cache_guildmessages_cache_users_author_id");
b.Navigation("Author"); b.Navigation("Author");
}); });
@ -191,7 +153,7 @@ namespace RegexBot.Data.Migrations
.HasForeignKey("UserId") .HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired() .IsRequired()
.HasConstraintName("fk_cache_userguild_cache_user_user_id"); .HasConstraintName("fk_cache_usersinguild_cache_users_user_id");
b.Navigation("User"); b.Navigation("User");
}); });

View file

@ -1,7 +1,6 @@
using System; using System;
using System.Collections.Generic; using System.Collections.Generic;
using Microsoft.EntityFrameworkCore.Migrations; using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
#nullable disable #nullable disable
@ -12,7 +11,7 @@ namespace RegexBot.Data.Migrations
protected override void Up(MigrationBuilder migrationBuilder) protected override void Up(MigrationBuilder migrationBuilder)
{ {
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "cache_user", name: "cache_users",
columns: table => new columns: table => new
{ {
user_id = table.Column<long>(type: "bigint", nullable: false), user_id = table.Column<long>(type: "bigint", nullable: false),
@ -23,27 +22,11 @@ namespace RegexBot.Data.Migrations
}, },
constraints: table => constraints: table =>
{ {
table.PrimaryKey("pk_cache_user", x => x.user_id); table.PrimaryKey("pk_cache_users", x => x.user_id);
}); });
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "guild_log", name: "cache_guildmessages",
columns: table => new
{
id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
guild_id = table.Column<long>(type: "bigint", nullable: false),
timestamp = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
source = table.Column<string>(type: "text", nullable: false),
message = table.Column<string>(type: "text", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("pk_guild_log", x => x.id);
});
migrationBuilder.CreateTable(
name: "cache_messages",
columns: table => new columns: table => new
{ {
message_id = table.Column<long>(type: "bigint", nullable: false), message_id = table.Column<long>(type: "bigint", nullable: false),
@ -57,17 +40,17 @@ namespace RegexBot.Data.Migrations
}, },
constraints: table => constraints: table =>
{ {
table.PrimaryKey("pk_cache_messages", x => x.message_id); table.PrimaryKey("pk_cache_guildmessages", x => x.message_id);
table.ForeignKey( table.ForeignKey(
name: "fk_cache_messages_cache_user_author_id", name: "fk_cache_guildmessages_cache_users_author_id",
column: x => x.author_id, column: x => x.author_id,
principalTable: "cache_user", principalTable: "cache_users",
principalColumn: "user_id", principalColumn: "user_id",
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
}); });
migrationBuilder.CreateTable( migrationBuilder.CreateTable(
name: "cache_userguild", name: "cache_usersinguild",
columns: table => new columns: table => new
{ {
user_id = table.Column<long>(type: "bigint", nullable: false), user_id = table.Column<long>(type: "bigint", nullable: false),
@ -78,39 +61,31 @@ namespace RegexBot.Data.Migrations
}, },
constraints: table => constraints: table =>
{ {
table.PrimaryKey("pk_cache_userguild", x => new { x.user_id, x.guild_id }); table.PrimaryKey("pk_cache_usersinguild", x => new { x.user_id, x.guild_id });
table.ForeignKey( table.ForeignKey(
name: "fk_cache_userguild_cache_user_user_id", name: "fk_cache_usersinguild_cache_users_user_id",
column: x => x.user_id, column: x => x.user_id,
principalTable: "cache_user", principalTable: "cache_users",
principalColumn: "user_id", principalColumn: "user_id",
onDelete: ReferentialAction.Cascade); onDelete: ReferentialAction.Cascade);
}); });
migrationBuilder.CreateIndex( migrationBuilder.CreateIndex(
name: "ix_cache_messages_author_id", name: "ix_cache_guildmessages_author_id",
table: "cache_messages", table: "cache_guildmessages",
column: "author_id"); column: "author_id");
migrationBuilder.CreateIndex(
name: "ix_guild_log_guild_id",
table: "guild_log",
column: "guild_id");
} }
protected override void Down(MigrationBuilder migrationBuilder) protected override void Down(MigrationBuilder migrationBuilder)
{ {
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "cache_messages"); name: "cache_guildmessages");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "cache_userguild"); name: "cache_usersinguild");
migrationBuilder.DropTable( migrationBuilder.DropTable(
name: "guild_log"); name: "cache_users");
migrationBuilder.DropTable(
name: "cache_user");
} }
} }
} }

View file

@ -18,7 +18,7 @@ namespace RegexBot.Data.Migrations
{ {
#pragma warning disable 612, 618 #pragma warning disable 612, 618
modelBuilder modelBuilder
.HasAnnotation("ProductVersion", "6.0.5") .HasAnnotation("ProductVersion", "6.0.7")
.HasAnnotation("Relational:MaxIdentifierLength", 63); .HasAnnotation("Relational:MaxIdentifierLength", 63);
NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder);
@ -62,12 +62,12 @@ namespace RegexBot.Data.Migrations
.HasColumnName("guild_id"); .HasColumnName("guild_id");
b.HasKey("MessageId") b.HasKey("MessageId")
.HasName("pk_cache_messages"); .HasName("pk_cache_guildmessages");
b.HasIndex("AuthorId") b.HasIndex("AuthorId")
.HasDatabaseName("ix_cache_messages_author_id"); .HasDatabaseName("ix_cache_guildmessages_author_id");
b.ToTable("cache_messages", (string)null); b.ToTable("cache_guildmessages", (string)null);
}); });
modelBuilder.Entity("RegexBot.Data.CachedGuildUser", b => modelBuilder.Entity("RegexBot.Data.CachedGuildUser", b =>
@ -95,9 +95,9 @@ namespace RegexBot.Data.Migrations
.HasColumnName("nickname"); .HasColumnName("nickname");
b.HasKey("UserId", "GuildId") b.HasKey("UserId", "GuildId")
.HasName("pk_cache_userguild"); .HasName("pk_cache_usersinguild");
b.ToTable("cache_userguild", (string)null); b.ToTable("cache_usersinguild", (string)null);
}); });
modelBuilder.Entity("RegexBot.Data.CachedUser", b => modelBuilder.Entity("RegexBot.Data.CachedUser", b =>
@ -127,47 +127,9 @@ namespace RegexBot.Data.Migrations
.HasColumnName("username"); .HasColumnName("username");
b.HasKey("UserId") b.HasKey("UserId")
.HasName("pk_cache_user"); .HasName("pk_cache_users");
b.ToTable("cache_user", (string)null); b.ToTable("cache_users", (string)null);
});
modelBuilder.Entity("RegexBot.Data.GuildLogLine", b =>
{
b.Property<int>("Id")
.ValueGeneratedOnAdd()
.HasColumnType("integer")
.HasColumnName("id");
NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property<int>("Id"));
b.Property<long>("GuildId")
.HasColumnType("bigint")
.HasColumnName("guild_id");
b.Property<string>("Message")
.IsRequired()
.HasColumnType("text")
.HasColumnName("message");
b.Property<string>("Source")
.IsRequired()
.HasColumnType("text")
.HasColumnName("source");
b.Property<DateTimeOffset>("Timestamp")
.ValueGeneratedOnAdd()
.HasColumnType("timestamp with time zone")
.HasColumnName("timestamp")
.HasDefaultValueSql("now()");
b.HasKey("Id")
.HasName("pk_guild_log");
b.HasIndex("GuildId")
.HasDatabaseName("ix_guild_log_guild_id");
b.ToTable("guild_log", (string)null);
}); });
modelBuilder.Entity("RegexBot.Data.CachedGuildMessage", b => modelBuilder.Entity("RegexBot.Data.CachedGuildMessage", b =>
@ -177,7 +139,7 @@ namespace RegexBot.Data.Migrations
.HasForeignKey("AuthorId") .HasForeignKey("AuthorId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired() .IsRequired()
.HasConstraintName("fk_cache_messages_cache_user_author_id"); .HasConstraintName("fk_cache_guildmessages_cache_users_author_id");
b.Navigation("Author"); b.Navigation("Author");
}); });
@ -189,7 +151,7 @@ namespace RegexBot.Data.Migrations
.HasForeignKey("UserId") .HasForeignKey("UserId")
.OnDelete(DeleteBehavior.Cascade) .OnDelete(DeleteBehavior.Cascade)
.IsRequired() .IsRequired()
.HasConstraintName("fk_cache_userguild_cache_user_user_id"); .HasConstraintName("fk_cache_usersinguild_cache_users_user_id");
b.Navigation("User"); b.Navigation("User");
}); });

View file

@ -29,7 +29,7 @@ class MessageCachingSubservice {
if (isUpdate) { if (isUpdate) {
// Alternative for Discord.Net's MessageUpdated handler: // Alternative for Discord.Net's MessageUpdated handler:
// Notify subscribers of message update using EC entry for the previous message state // Notify subscribers of message update using EC entry for the previous message state
var oldMsg = cachedMsg?.MemberwiseClone(); var oldMsg = CachedGuildMessage.Clone(cachedMsg);
await Task.Factory.StartNew(async () => await RunPreUpdateHandlersAsync(oldMsg, arg)); await Task.Factory.StartNew(async () => await RunPreUpdateHandlersAsync(oldMsg, arg));
} }