From 8d081ed6370948d95c5b88a5d85e41d6393ac505 Mon Sep 17 00:00:00 2001 From: Noi Date: Thu, 12 May 2022 23:49:55 -0700 Subject: [PATCH] Fix database issues; create initial EF migration --- RegexBot/Data/BotDatabaseContext.cs | 5 +- RegexBot/Data/CachedGuildMessage.cs | 3 +- RegexBot/Data/CachedGuildUser.cs | 9 +- RegexBot/Data/CachedUser.cs | 1 + .../20220513061851_InitialEFSetup.Designer.cs | 206 ++++++++++++++++++ .../20220513061851_InitialEFSetup.cs | 116 ++++++++++ .../BotDatabaseContextModelSnapshot.cs | 204 +++++++++++++++++ 7 files changed, 536 insertions(+), 8 deletions(-) create mode 100644 RegexBot/Data/Migrations/20220513061851_InitialEFSetup.Designer.cs create mode 100644 RegexBot/Data/Migrations/20220513061851_InitialEFSetup.cs create mode 100644 RegexBot/Data/Migrations/BotDatabaseContextModelSnapshot.cs diff --git a/RegexBot/Data/BotDatabaseContext.cs b/RegexBot/Data/BotDatabaseContext.cs index c118da2..347bbfe 100644 --- a/RegexBot/Data/BotDatabaseContext.cs +++ b/RegexBot/Data/BotDatabaseContext.cs @@ -30,7 +30,10 @@ public class BotDatabaseContext : DbContext { protected override void OnModelCreating(ModelBuilder modelBuilder) { modelBuilder.Entity(entity => entity.Property(e => e.Timestamp).HasDefaultValueSql("now()")); modelBuilder.Entity(entity => entity.Property(e => e.Discriminator).HasMaxLength(4).IsFixedLength()); - modelBuilder.Entity(entity => entity.Navigation(e => e.User).AutoInclude()); + modelBuilder.Entity(entity => { + entity.Navigation(e => e.User).AutoInclude(); + entity.HasKey(e => new { e.UserId, e.GuildId }); + }); modelBuilder.Entity(entity => entity.Property(e => e.CreatedAt).HasDefaultValueSql("now()")); } } diff --git a/RegexBot/Data/CachedGuildMessage.cs b/RegexBot/Data/CachedGuildMessage.cs index da76387..023dfd9 100644 --- a/RegexBot/Data/CachedGuildMessage.cs +++ b/RegexBot/Data/CachedGuildMessage.cs @@ -5,6 +5,7 @@ namespace RegexBot.Data; [Table("cache_messages")] public class CachedGuildMessage { [Key] + [DatabaseGenerated(DatabaseGeneratedOption.None)] public long MessageId { get; set; } public long AuthorId { get; set; } @@ -28,7 +29,7 @@ public class CachedGuildMessage { [NotMapped] public DateTimeOffset LastUpdatedAt => EditedAt ?? CreatedAt; - [ForeignKey(nameof(CachedUser.UserId))] + [ForeignKey(nameof(AuthorId))] [InverseProperty(nameof(CachedUser.GuildMessages))] public CachedUser Author { get; set; } = null!; diff --git a/RegexBot/Data/CachedGuildUser.cs b/RegexBot/Data/CachedGuildUser.cs index 9afe769..8b372fc 100644 --- a/RegexBot/Data/CachedGuildUser.cs +++ b/RegexBot/Data/CachedGuildUser.cs @@ -1,19 +1,16 @@ -using System.ComponentModel.DataAnnotations; -using System.ComponentModel.DataAnnotations.Schema; +using System.ComponentModel.DataAnnotations.Schema; namespace RegexBot.Data; -[Table("cache_guilduser")] +[Table("cache_userguild")] public class CachedGuildUser { - [Key] public long UserId { get; set; } - [Key] public long GuildId { get; set; } public DateTimeOffset GULastUpdateTime { get; set; } public DateTimeOffset FirstSeenTime { get; set; } public string? Nickname { get; set; } - [ForeignKey(nameof(CachedUser.UserId))] + [ForeignKey(nameof(UserId))] [InverseProperty(nameof(CachedUser.Guilds))] public CachedUser User { get; set; } = null!; } diff --git a/RegexBot/Data/CachedUser.cs b/RegexBot/Data/CachedUser.cs index 9658f87..1b350f4 100644 --- a/RegexBot/Data/CachedUser.cs +++ b/RegexBot/Data/CachedUser.cs @@ -6,6 +6,7 @@ namespace RegexBot.Data; [Table("cache_user")] public class CachedUser { [Key] + [DatabaseGenerated(DatabaseGeneratedOption.None)] public long UserId { get; set; } public DateTimeOffset ULastUpdateTime { get; set; } public string Username { get; set; } = null!; diff --git a/RegexBot/Data/Migrations/20220513061851_InitialEFSetup.Designer.cs b/RegexBot/Data/Migrations/20220513061851_InitialEFSetup.Designer.cs new file mode 100644 index 0000000..e5ffb2a --- /dev/null +++ b/RegexBot/Data/Migrations/20220513061851_InitialEFSetup.Designer.cs @@ -0,0 +1,206 @@ +// +using System; +using System.Collections.Generic; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using RegexBot.Data; + +#nullable disable + +namespace RegexBot.Data.Migrations +{ + [DbContext(typeof(BotDatabaseContext))] + [Migration("20220513061851_InitialEFSetup")] + partial class InitialEFSetup + { + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("RegexBot.Data.CachedGuildMessage", b => + { + b.Property("MessageId") + .HasColumnType("bigint") + .HasColumnName("message_id"); + + b.Property>("AttachmentNames") + .IsRequired() + .HasColumnType("text[]") + .HasColumnName("attachment_names"); + + b.Property("AuthorId") + .HasColumnType("bigint") + .HasColumnName("author_id"); + + b.Property("ChannelId") + .HasColumnType("bigint") + .HasColumnName("channel_id"); + + b.Property("Content") + .IsRequired() + .HasColumnType("text") + .HasColumnName("content"); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasColumnName("created_at") + .HasDefaultValueSql("now()"); + + b.Property("EditedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("edited_at"); + + b.Property("GuildId") + .HasColumnType("bigint") + .HasColumnName("guild_id"); + + b.HasKey("MessageId") + .HasName("pk_cache_messages"); + + b.HasIndex("AuthorId") + .HasDatabaseName("ix_cache_messages_author_id"); + + b.ToTable("cache_messages", (string)null); + }); + + modelBuilder.Entity("RegexBot.Data.CachedGuildUser", b => + { + b.Property("UserId") + .HasColumnType("bigint") + .HasColumnName("user_id"); + + b.Property("GuildId") + .HasColumnType("bigint") + .HasColumnName("guild_id"); + + b.Property("FirstSeenTime") + .HasColumnType("timestamp with time zone") + .HasColumnName("first_seen_time"); + + b.Property("GULastUpdateTime") + .HasColumnType("timestamp with time zone") + .HasColumnName("gu_last_update_time"); + + b.Property("Nickname") + .HasColumnType("text") + .HasColumnName("nickname"); + + b.HasKey("UserId", "GuildId") + .HasName("pk_cache_userguild"); + + b.ToTable("cache_userguild", (string)null); + }); + + modelBuilder.Entity("RegexBot.Data.CachedUser", b => + { + b.Property("UserId") + .HasColumnType("bigint") + .HasColumnName("user_id"); + + b.Property("AvatarUrl") + .HasColumnType("text") + .HasColumnName("avatar_url"); + + b.Property("Discriminator") + .IsRequired() + .HasMaxLength(4) + .HasColumnType("character(4)") + .HasColumnName("discriminator") + .IsFixedLength(); + + b.Property("ULastUpdateTime") + .HasColumnType("timestamp with time zone") + .HasColumnName("u_last_update_time"); + + b.Property("Username") + .IsRequired() + .HasColumnType("text") + .HasColumnName("username"); + + b.HasKey("UserId") + .HasName("pk_cache_user"); + + b.ToTable("cache_user", (string)null); + }); + + modelBuilder.Entity("RegexBot.Data.GuildLogLine", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("GuildId") + .HasColumnType("bigint") + .HasColumnName("guild_id"); + + b.Property("Message") + .IsRequired() + .HasColumnType("text") + .HasColumnName("message"); + + b.Property("Source") + .IsRequired() + .HasColumnType("text") + .HasColumnName("source"); + + b.Property("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 => + { + b.HasOne("RegexBot.Data.CachedUser", "Author") + .WithMany("GuildMessages") + .HasForeignKey("AuthorId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired() + .HasConstraintName("fk_cache_messages_cache_user_author_id"); + + b.Navigation("Author"); + }); + + modelBuilder.Entity("RegexBot.Data.CachedGuildUser", b => + { + b.HasOne("RegexBot.Data.CachedUser", "User") + .WithMany("Guilds") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired() + .HasConstraintName("fk_cache_userguild_cache_user_user_id"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("RegexBot.Data.CachedUser", b => + { + b.Navigation("GuildMessages"); + + b.Navigation("Guilds"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/RegexBot/Data/Migrations/20220513061851_InitialEFSetup.cs b/RegexBot/Data/Migrations/20220513061851_InitialEFSetup.cs new file mode 100644 index 0000000..4c7261e --- /dev/null +++ b/RegexBot/Data/Migrations/20220513061851_InitialEFSetup.cs @@ -0,0 +1,116 @@ +using System; +using System.Collections.Generic; +using Microsoft.EntityFrameworkCore.Migrations; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; + +#nullable disable + +namespace RegexBot.Data.Migrations +{ + public partial class InitialEFSetup : Migration + { + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "cache_user", + columns: table => new + { + user_id = table.Column(type: "bigint", nullable: false), + u_last_update_time = table.Column(type: "timestamp with time zone", nullable: false), + username = table.Column(type: "text", nullable: false), + discriminator = table.Column(type: "character(4)", fixedLength: true, maxLength: 4, nullable: false), + avatar_url = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("pk_cache_user", x => x.user_id); + }); + + migrationBuilder.CreateTable( + name: "guild_log", + columns: table => new + { + id = table.Column(type: "integer", nullable: false) + .Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn), + guild_id = table.Column(type: "bigint", nullable: false), + timestamp = table.Column(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"), + source = table.Column(type: "text", nullable: false), + message = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("pk_guild_log", x => x.id); + }); + + migrationBuilder.CreateTable( + name: "cache_messages", + columns: table => new + { + message_id = table.Column(type: "bigint", nullable: false), + author_id = table.Column(type: "bigint", nullable: false), + guild_id = table.Column(type: "bigint", nullable: false), + channel_id = table.Column(type: "bigint", nullable: false), + created_at = table.Column(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"), + edited_at = table.Column(type: "timestamp with time zone", nullable: true), + attachment_names = table.Column>(type: "text[]", nullable: false), + content = table.Column(type: "text", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("pk_cache_messages", x => x.message_id); + table.ForeignKey( + name: "fk_cache_messages_cache_user_author_id", + column: x => x.author_id, + principalTable: "cache_user", + principalColumn: "user_id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateTable( + name: "cache_userguild", + columns: table => new + { + user_id = table.Column(type: "bigint", nullable: false), + guild_id = table.Column(type: "bigint", nullable: false), + gu_last_update_time = table.Column(type: "timestamp with time zone", nullable: false), + first_seen_time = table.Column(type: "timestamp with time zone", nullable: false), + nickname = table.Column(type: "text", nullable: true) + }, + constraints: table => + { + table.PrimaryKey("pk_cache_userguild", x => new { x.user_id, x.guild_id }); + table.ForeignKey( + name: "fk_cache_userguild_cache_user_user_id", + column: x => x.user_id, + principalTable: "cache_user", + principalColumn: "user_id", + onDelete: ReferentialAction.Cascade); + }); + + migrationBuilder.CreateIndex( + name: "ix_cache_messages_author_id", + table: "cache_messages", + column: "author_id"); + + migrationBuilder.CreateIndex( + name: "ix_guild_log_guild_id", + table: "guild_log", + column: "guild_id"); + } + + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "cache_messages"); + + migrationBuilder.DropTable( + name: "cache_userguild"); + + migrationBuilder.DropTable( + name: "guild_log"); + + migrationBuilder.DropTable( + name: "cache_user"); + } + } +} diff --git a/RegexBot/Data/Migrations/BotDatabaseContextModelSnapshot.cs b/RegexBot/Data/Migrations/BotDatabaseContextModelSnapshot.cs new file mode 100644 index 0000000..0a8a9a2 --- /dev/null +++ b/RegexBot/Data/Migrations/BotDatabaseContextModelSnapshot.cs @@ -0,0 +1,204 @@ +// +using System; +using System.Collections.Generic; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata; +using RegexBot.Data; + +#nullable disable + +namespace RegexBot.Data.Migrations +{ + [DbContext(typeof(BotDatabaseContext))] + partial class BotDatabaseContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder + .HasAnnotation("ProductVersion", "6.0.5") + .HasAnnotation("Relational:MaxIdentifierLength", 63); + + NpgsqlModelBuilderExtensions.UseIdentityByDefaultColumns(modelBuilder); + + modelBuilder.Entity("RegexBot.Data.CachedGuildMessage", b => + { + b.Property("MessageId") + .HasColumnType("bigint") + .HasColumnName("message_id"); + + b.Property>("AttachmentNames") + .IsRequired() + .HasColumnType("text[]") + .HasColumnName("attachment_names"); + + b.Property("AuthorId") + .HasColumnType("bigint") + .HasColumnName("author_id"); + + b.Property("ChannelId") + .HasColumnType("bigint") + .HasColumnName("channel_id"); + + b.Property("Content") + .IsRequired() + .HasColumnType("text") + .HasColumnName("content"); + + b.Property("CreatedAt") + .ValueGeneratedOnAdd() + .HasColumnType("timestamp with time zone") + .HasColumnName("created_at") + .HasDefaultValueSql("now()"); + + b.Property("EditedAt") + .HasColumnType("timestamp with time zone") + .HasColumnName("edited_at"); + + b.Property("GuildId") + .HasColumnType("bigint") + .HasColumnName("guild_id"); + + b.HasKey("MessageId") + .HasName("pk_cache_messages"); + + b.HasIndex("AuthorId") + .HasDatabaseName("ix_cache_messages_author_id"); + + b.ToTable("cache_messages", (string)null); + }); + + modelBuilder.Entity("RegexBot.Data.CachedGuildUser", b => + { + b.Property("UserId") + .HasColumnType("bigint") + .HasColumnName("user_id"); + + b.Property("GuildId") + .HasColumnType("bigint") + .HasColumnName("guild_id"); + + b.Property("FirstSeenTime") + .HasColumnType("timestamp with time zone") + .HasColumnName("first_seen_time"); + + b.Property("GULastUpdateTime") + .HasColumnType("timestamp with time zone") + .HasColumnName("gu_last_update_time"); + + b.Property("Nickname") + .HasColumnType("text") + .HasColumnName("nickname"); + + b.HasKey("UserId", "GuildId") + .HasName("pk_cache_userguild"); + + b.ToTable("cache_userguild", (string)null); + }); + + modelBuilder.Entity("RegexBot.Data.CachedUser", b => + { + b.Property("UserId") + .HasColumnType("bigint") + .HasColumnName("user_id"); + + b.Property("AvatarUrl") + .HasColumnType("text") + .HasColumnName("avatar_url"); + + b.Property("Discriminator") + .IsRequired() + .HasMaxLength(4) + .HasColumnType("character(4)") + .HasColumnName("discriminator") + .IsFixedLength(); + + b.Property("ULastUpdateTime") + .HasColumnType("timestamp with time zone") + .HasColumnName("u_last_update_time"); + + b.Property("Username") + .IsRequired() + .HasColumnType("text") + .HasColumnName("username"); + + b.HasKey("UserId") + .HasName("pk_cache_user"); + + b.ToTable("cache_user", (string)null); + }); + + modelBuilder.Entity("RegexBot.Data.GuildLogLine", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("integer") + .HasColumnName("id"); + + NpgsqlPropertyBuilderExtensions.UseIdentityByDefaultColumn(b.Property("Id")); + + b.Property("GuildId") + .HasColumnType("bigint") + .HasColumnName("guild_id"); + + b.Property("Message") + .IsRequired() + .HasColumnType("text") + .HasColumnName("message"); + + b.Property("Source") + .IsRequired() + .HasColumnType("text") + .HasColumnName("source"); + + b.Property("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 => + { + b.HasOne("RegexBot.Data.CachedUser", "Author") + .WithMany("GuildMessages") + .HasForeignKey("AuthorId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired() + .HasConstraintName("fk_cache_messages_cache_user_author_id"); + + b.Navigation("Author"); + }); + + modelBuilder.Entity("RegexBot.Data.CachedGuildUser", b => + { + b.HasOne("RegexBot.Data.CachedUser", "User") + .WithMany("Guilds") + .HasForeignKey("UserId") + .OnDelete(DeleteBehavior.Cascade) + .IsRequired() + .HasConstraintName("fk_cache_userguild_cache_user_user_id"); + + b.Navigation("User"); + }); + + modelBuilder.Entity("RegexBot.Data.CachedUser", b => + { + b.Navigation("GuildMessages"); + + b.Navigation("Guilds"); + }); +#pragma warning restore 612, 618 + } + } +}