fix: migrate to IDbContextFactory and remove direct AppDbContext from DI (#11)

Reviewed-on: #11
Co-authored-by: Marek Jasiński <jasins.marek@gmail.com>
Co-committed-by: Marek Jasiński <jasins.marek@gmail.com>
This commit was merged in pull request #11.
This commit is contained in:
2026-05-07 16:39:21 +00:00
committed by Marek Jaisński
parent 3faecbb639
commit 2248a2b757
35 changed files with 983 additions and 215 deletions
@@ -0,0 +1,399 @@
using System;
using Microsoft.EntityFrameworkCore.Migrations;
using Npgsql.EntityFrameworkCore.PostgreSQL.Metadata;
using Pgvector;
#nullable disable
#pragma warning disable CA1814 // Prefer jagged arrays over multidimensional
namespace NexusReader.Data.Migrations
{
/// <inheritdoc />
public partial class FinalNormalizedSubscriptionArchitecture : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropColumn(
name: "CurrentPlan",
table: "AspNetUsers");
migrationBuilder.AlterDatabase()
.Annotation("Npgsql:PostgresExtension:vector", ",,");
migrationBuilder.AlterColumn<DateTime>(
name: "CreatedAt",
table: "SemanticKnowledgeCache",
type: "timestamp with time zone",
nullable: false,
oldClrType: typeof(DateTime),
oldType: "timestamp without time zone");
migrationBuilder.AddColumn<string>(
name: "OriginalText",
table: "SemanticKnowledgeCache",
type: "text",
nullable: false,
defaultValue: "");
migrationBuilder.AddColumn<string>(
name: "TenantId",
table: "SemanticKnowledgeCache",
type: "character varying(128)",
maxLength: 128,
nullable: false,
defaultValue: "");
migrationBuilder.AddColumn<Vector>(
name: "Vector",
table: "SemanticKnowledgeCache",
type: "vector(1536)",
nullable: true);
migrationBuilder.AlterColumn<DateTime>(
name: "CompletedDate",
table: "QuizResults",
type: "timestamp with time zone",
nullable: false,
oldClrType: typeof(DateTime),
oldType: "timestamp without time zone");
migrationBuilder.AddColumn<string>(
name: "TenantId",
table: "QuizResults",
type: "character varying(128)",
maxLength: 128,
nullable: false,
defaultValue: "");
migrationBuilder.AlterColumn<DateTime>(
name: "LastReadDate",
table: "Ebooks",
type: "timestamp with time zone",
nullable: true,
oldClrType: typeof(DateTime),
oldType: "timestamp without time zone",
oldNullable: true);
migrationBuilder.AlterColumn<DateTime>(
name: "AddedDate",
table: "Ebooks",
type: "timestamp with time zone",
nullable: false,
oldClrType: typeof(DateTime),
oldType: "timestamp without time zone");
migrationBuilder.AddColumn<string>(
name: "TenantId",
table: "Ebooks",
type: "character varying(128)",
maxLength: 128,
nullable: false,
defaultValue: "");
migrationBuilder.AlterColumn<string>(
name: "TenantId",
table: "AspNetUsers",
type: "character varying(128)",
maxLength: 128,
nullable: false,
oldClrType: typeof(Guid),
oldType: "uuid");
migrationBuilder.AddColumn<string>(
name: "DisplayName",
table: "AspNetUsers",
type: "character varying(100)",
maxLength: 100,
nullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "LastAiActionDate",
table: "AspNetUsers",
type: "timestamp with time zone",
nullable: true);
migrationBuilder.AddColumn<DateTime>(
name: "LastReadAt",
table: "AspNetUsers",
type: "timestamp with time zone",
nullable: true);
migrationBuilder.AddColumn<string>(
name: "LastReadPageId",
table: "AspNetUsers",
type: "character varying(255)",
maxLength: 255,
nullable: true);
migrationBuilder.AddColumn<int>(
name: "SubscriptionPlanId",
table: "AspNetUsers",
type: "integer",
nullable: false,
defaultValue: 1);
migrationBuilder.CreateTable(
name: "KnowledgeUnits",
columns: table => new
{
Id = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
SourceId = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
Version = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
Type = table.Column<int>(type: "integer", nullable: false),
Content = table.Column<string>(type: "text", nullable: false),
MetadataJson = table.Column<string>(type: "text", nullable: true),
TenantId = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
Vector = table.Column<Vector>(type: "vector(768)", nullable: true),
CreatedAt = table.Column<DateTime>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_KnowledgeUnits", x => x.Id);
});
migrationBuilder.CreateTable(
name: "SubscriptionPlans",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
PlanName = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false),
AITokenLimit = table.Column<int>(type: "integer", nullable: false),
MonthlyPrice = table.Column<decimal>(type: "numeric", nullable: false),
StripeProductId = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_SubscriptionPlans", x => x.Id);
});
migrationBuilder.CreateTable(
name: "KnowledgeUnitLinks",
columns: table => new
{
Id = table.Column<int>(type: "integer", nullable: false)
.Annotation("Npgsql:ValueGenerationStrategy", NpgsqlValueGenerationStrategy.IdentityByDefaultColumn),
SourceUnitId = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
TargetUnitId = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: false),
RelationType = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: false)
},
constraints: table =>
{
table.PrimaryKey("PK_KnowledgeUnitLinks", x => x.Id);
table.ForeignKey(
name: "FK_KnowledgeUnitLinks_KnowledgeUnits_SourceUnitId",
column: x => x.SourceUnitId,
principalTable: "KnowledgeUnits",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "FK_KnowledgeUnitLinks_KnowledgeUnits_TargetUnitId",
column: x => x.TargetUnitId,
principalTable: "KnowledgeUnits",
principalColumn: "Id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.InsertData(
table: "SubscriptionPlans",
columns: new[] { "Id", "AITokenLimit", "MonthlyPrice", "PlanName", "StripeProductId" },
values: new object[,]
{
{ 1, 1000, 0m, "Free", "" },
{ 2, 10000, 9.99m, "Basic", "prod_basic_placeholder" },
{ 3, 50000, 19.99m, "Pro", "prod_pro_placeholder" },
{ 4, 500000, 99.99m, "Enterprise", "prod_enterprise_placeholder" }
});
migrationBuilder.CreateIndex(
name: "IX_SemanticKnowledgeCache_TenantId",
table: "SemanticKnowledgeCache",
column: "TenantId");
migrationBuilder.CreateIndex(
name: "IX_QuizResults_TenantId",
table: "QuizResults",
column: "TenantId");
migrationBuilder.CreateIndex(
name: "IX_Ebooks_TenantId",
table: "Ebooks",
column: "TenantId");
migrationBuilder.CreateIndex(
name: "IX_AspNetUsers_SubscriptionPlanId",
table: "AspNetUsers",
column: "SubscriptionPlanId");
migrationBuilder.CreateIndex(
name: "IX_AspNetUsers_TenantId",
table: "AspNetUsers",
column: "TenantId");
migrationBuilder.CreateIndex(
name: "IX_KnowledgeUnitLinks_SourceUnitId",
table: "KnowledgeUnitLinks",
column: "SourceUnitId");
migrationBuilder.CreateIndex(
name: "IX_KnowledgeUnitLinks_TargetUnitId",
table: "KnowledgeUnitLinks",
column: "TargetUnitId");
migrationBuilder.CreateIndex(
name: "IX_KnowledgeUnits_SourceId",
table: "KnowledgeUnits",
column: "SourceId");
migrationBuilder.CreateIndex(
name: "IX_KnowledgeUnits_TenantId",
table: "KnowledgeUnits",
column: "TenantId");
migrationBuilder.CreateIndex(
name: "IX_SubscriptionPlans_PlanName",
table: "SubscriptionPlans",
column: "PlanName",
unique: true);
migrationBuilder.AddForeignKey(
name: "FK_AspNetUsers_SubscriptionPlans_SubscriptionPlanId",
table: "AspNetUsers",
column: "SubscriptionPlanId",
principalTable: "SubscriptionPlans",
principalColumn: "Id",
onDelete: ReferentialAction.Restrict);
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropForeignKey(
name: "FK_AspNetUsers_SubscriptionPlans_SubscriptionPlanId",
table: "AspNetUsers");
migrationBuilder.DropTable(
name: "KnowledgeUnitLinks");
migrationBuilder.DropTable(
name: "SubscriptionPlans");
migrationBuilder.DropTable(
name: "KnowledgeUnits");
migrationBuilder.DropIndex(
name: "IX_SemanticKnowledgeCache_TenantId",
table: "SemanticKnowledgeCache");
migrationBuilder.DropIndex(
name: "IX_QuizResults_TenantId",
table: "QuizResults");
migrationBuilder.DropIndex(
name: "IX_Ebooks_TenantId",
table: "Ebooks");
migrationBuilder.DropIndex(
name: "IX_AspNetUsers_SubscriptionPlanId",
table: "AspNetUsers");
migrationBuilder.DropIndex(
name: "IX_AspNetUsers_TenantId",
table: "AspNetUsers");
migrationBuilder.DropColumn(
name: "OriginalText",
table: "SemanticKnowledgeCache");
migrationBuilder.DropColumn(
name: "TenantId",
table: "SemanticKnowledgeCache");
migrationBuilder.DropColumn(
name: "Vector",
table: "SemanticKnowledgeCache");
migrationBuilder.DropColumn(
name: "TenantId",
table: "QuizResults");
migrationBuilder.DropColumn(
name: "TenantId",
table: "Ebooks");
migrationBuilder.DropColumn(
name: "DisplayName",
table: "AspNetUsers");
migrationBuilder.DropColumn(
name: "LastAiActionDate",
table: "AspNetUsers");
migrationBuilder.DropColumn(
name: "LastReadAt",
table: "AspNetUsers");
migrationBuilder.DropColumn(
name: "LastReadPageId",
table: "AspNetUsers");
migrationBuilder.DropColumn(
name: "SubscriptionPlanId",
table: "AspNetUsers");
migrationBuilder.AlterDatabase()
.OldAnnotation("Npgsql:PostgresExtension:vector", ",,");
migrationBuilder.AlterColumn<DateTime>(
name: "CreatedAt",
table: "SemanticKnowledgeCache",
type: "timestamp without time zone",
nullable: false,
oldClrType: typeof(DateTime),
oldType: "timestamp with time zone");
migrationBuilder.AlterColumn<DateTime>(
name: "CompletedDate",
table: "QuizResults",
type: "timestamp without time zone",
nullable: false,
oldClrType: typeof(DateTime),
oldType: "timestamp with time zone");
migrationBuilder.AlterColumn<DateTime>(
name: "LastReadDate",
table: "Ebooks",
type: "timestamp without time zone",
nullable: true,
oldClrType: typeof(DateTime),
oldType: "timestamp with time zone",
oldNullable: true);
migrationBuilder.AlterColumn<DateTime>(
name: "AddedDate",
table: "Ebooks",
type: "timestamp without time zone",
nullable: false,
oldClrType: typeof(DateTime),
oldType: "timestamp with time zone");
migrationBuilder.AlterColumn<Guid>(
name: "TenantId",
table: "AspNetUsers",
type: "uuid",
nullable: false,
oldClrType: typeof(string),
oldType: "character varying(128)",
oldMaxLength: 128);
migrationBuilder.AddColumn<string>(
name: "CurrentPlan",
table: "AspNetUsers",
type: "text",
nullable: false,
defaultValue: "");
}
}
}