From a1d1e5dca44eb5e70605e4487b19393c01e31bfb Mon Sep 17 00:00:00 2001 From: ModerRAS Date: Tue, 21 Apr 2026 20:06:13 +0800 Subject: [PATCH] fix: add search cache EF migrations Add design-time factory and generated EF Core migrations for SearchCacheDbContext, and switch startup initialization from EnsureCreatedAsync to MigrateAsync so SearchCache.sqlite follows the project's standard migration workflow. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> --- ...120404_InitSearchCacheDatabase.Designer.cs | 50 +++++++++++++++++++ .../20260421120404_InitSearchCacheDatabase.cs | 42 ++++++++++++++++ .../SearchCacheDbContextModelSnapshot.cs | 47 +++++++++++++++++ .../Model/SearchCacheDbContextFactory.cs | 17 +++++++ .../AppBootstrap/GeneralBootstrap.cs | 2 +- 5 files changed, 157 insertions(+), 1 deletion(-) create mode 100644 TelegramSearchBot.Database/Migrations/SearchCache/20260421120404_InitSearchCacheDatabase.Designer.cs create mode 100644 TelegramSearchBot.Database/Migrations/SearchCache/20260421120404_InitSearchCacheDatabase.cs create mode 100644 TelegramSearchBot.Database/Migrations/SearchCache/SearchCacheDbContextModelSnapshot.cs create mode 100644 TelegramSearchBot.Database/Model/SearchCacheDbContextFactory.cs diff --git a/TelegramSearchBot.Database/Migrations/SearchCache/20260421120404_InitSearchCacheDatabase.Designer.cs b/TelegramSearchBot.Database/Migrations/SearchCache/20260421120404_InitSearchCacheDatabase.Designer.cs new file mode 100644 index 00000000..881c0434 --- /dev/null +++ b/TelegramSearchBot.Database/Migrations/SearchCache/20260421120404_InitSearchCacheDatabase.Designer.cs @@ -0,0 +1,50 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Migrations; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using TelegramSearchBot.Model; + +#nullable disable + +namespace TelegramSearchBot.Database.Migrations.SearchCache +{ + [DbContext(typeof(SearchCacheDbContext))] + [Migration("20260421120404_InitSearchCacheDatabase")] + partial class InitSearchCacheDatabase + { + /// + protected override void BuildTargetModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.3"); + + modelBuilder.Entity("TelegramSearchBot.Model.Data.SearchPageCache", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("CreatedTime") + .HasColumnType("TEXT"); + + b.Property("SearchOptionJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("UUID") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UUID") + .IsUnique(); + + b.ToTable("SearchPageCaches"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/TelegramSearchBot.Database/Migrations/SearchCache/20260421120404_InitSearchCacheDatabase.cs b/TelegramSearchBot.Database/Migrations/SearchCache/20260421120404_InitSearchCacheDatabase.cs new file mode 100644 index 00000000..0cd01e92 --- /dev/null +++ b/TelegramSearchBot.Database/Migrations/SearchCache/20260421120404_InitSearchCacheDatabase.cs @@ -0,0 +1,42 @@ +using System; +using Microsoft.EntityFrameworkCore.Migrations; + +#nullable disable + +namespace TelegramSearchBot.Database.Migrations.SearchCache +{ + /// + public partial class InitSearchCacheDatabase : Migration + { + /// + protected override void Up(MigrationBuilder migrationBuilder) + { + migrationBuilder.CreateTable( + name: "SearchPageCaches", + columns: table => new + { + Id = table.Column(type: "TEXT", nullable: false), + UUID = table.Column(type: "TEXT", nullable: false), + SearchOptionJson = table.Column(type: "TEXT", nullable: false), + CreatedTime = table.Column(type: "TEXT", nullable: false) + }, + constraints: table => + { + table.PrimaryKey("PK_SearchPageCaches", x => x.Id); + }); + + migrationBuilder.CreateIndex( + name: "IX_SearchPageCaches_UUID", + table: "SearchPageCaches", + column: "UUID", + unique: true); + } + + /// + protected override void Down(MigrationBuilder migrationBuilder) + { + migrationBuilder.DropTable( + name: "SearchPageCaches"); + } + } +} diff --git a/TelegramSearchBot.Database/Migrations/SearchCache/SearchCacheDbContextModelSnapshot.cs b/TelegramSearchBot.Database/Migrations/SearchCache/SearchCacheDbContextModelSnapshot.cs new file mode 100644 index 00000000..0a2cfc95 --- /dev/null +++ b/TelegramSearchBot.Database/Migrations/SearchCache/SearchCacheDbContextModelSnapshot.cs @@ -0,0 +1,47 @@ +// +using System; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Infrastructure; +using Microsoft.EntityFrameworkCore.Storage.ValueConversion; +using TelegramSearchBot.Model; + +#nullable disable + +namespace TelegramSearchBot.Database.Migrations.SearchCache +{ + [DbContext(typeof(SearchCacheDbContext))] + partial class SearchCacheDbContextModelSnapshot : ModelSnapshot + { + protected override void BuildModel(ModelBuilder modelBuilder) + { +#pragma warning disable 612, 618 + modelBuilder.HasAnnotation("ProductVersion", "10.0.3"); + + modelBuilder.Entity("TelegramSearchBot.Model.Data.SearchPageCache", b => + { + b.Property("Id") + .ValueGeneratedOnAdd() + .HasColumnType("TEXT"); + + b.Property("CreatedTime") + .HasColumnType("TEXT"); + + b.Property("SearchOptionJson") + .IsRequired() + .HasColumnType("TEXT"); + + b.Property("UUID") + .IsRequired() + .HasColumnType("TEXT"); + + b.HasKey("Id"); + + b.HasIndex("UUID") + .IsUnique(); + + b.ToTable("SearchPageCaches"); + }); +#pragma warning restore 612, 618 + } + } +} diff --git a/TelegramSearchBot.Database/Model/SearchCacheDbContextFactory.cs b/TelegramSearchBot.Database/Model/SearchCacheDbContextFactory.cs new file mode 100644 index 00000000..2133b080 --- /dev/null +++ b/TelegramSearchBot.Database/Model/SearchCacheDbContextFactory.cs @@ -0,0 +1,17 @@ +using System.IO; +using Microsoft.EntityFrameworkCore; +using Microsoft.EntityFrameworkCore.Design; +using TelegramSearchBot.Common; + +namespace TelegramSearchBot.Model { + public class SearchCacheDbContextFactory : IDesignTimeDbContextFactory { + public SearchCacheDbContext CreateDbContext(string[] args) { + var optionsBuilder = new DbContextOptionsBuilder(); + var databasePath = Path.Combine(Env.WorkDir, "SearchCache.sqlite"); + + optionsBuilder.UseSqlite($"Data Source={databasePath};Cache=Shared;Mode=ReadWriteCreate;"); + + return new SearchCacheDbContext(optionsBuilder.Options); + } + } +} diff --git a/TelegramSearchBot/AppBootstrap/GeneralBootstrap.cs b/TelegramSearchBot/AppBootstrap/GeneralBootstrap.cs index 5fadf98f..c31422d9 100644 --- a/TelegramSearchBot/AppBootstrap/GeneralBootstrap.cs +++ b/TelegramSearchBot/AppBootstrap/GeneralBootstrap.cs @@ -183,7 +183,7 @@ public static async Task Startup(string[] args) { var searchCacheContext = serviceScope.ServiceProvider.GetRequiredService(); //context.Database.EnsureCreated(); context.Database.Migrate(); - await searchCacheContext.Database.EnsureCreatedAsync(); + await searchCacheContext.Database.MigrateAsync(); } // 启动Host,SchedulerService作为HostedService会自动启动