feat: add vocabulary and handbook persistence

This commit is contained in:
xiong
2026-07-26 05:11:43 +08:00
parent e41b38e3a1
commit d73d8f20ca
7 changed files with 6891 additions and 1 deletions

View File

@@ -0,0 +1,171 @@
using System.Text.Json;
using Tiku.Domain.Common;
namespace Tiku.Domain.Content;
public sealed class VocabularyUnit : AuditableTenantEntity
{
public Guid? RegionId { get; set; }
public Guid? EntryId { get; set; }
public Guid? ContentNodeId { get; set; }
public string? LegacyId { get; set; }
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public int? WordCount { get; set; }
public int SortOrder { get; set; }
public bool IsActive { get; set; } = true;
public string? SourceHash { get; set; }
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
}
public sealed class VocabularyWord : AuditableTenantEntity
{
public Guid? UnitId { get; set; }
public Guid? EntryId { get; set; }
public Guid? ContentNodeId { get; set; }
public string? LegacyId { get; set; }
public string Word { get; set; } = string.Empty;
public string? Phonetic { get; set; }
public string? Meaning { get; set; }
public string? Example { get; set; }
public string? ExampleTranslation { get; set; }
public int? Difficulty { get; set; }
public JsonElement Tags { get; set; } = JsonDefaults.Array();
public int SortOrder { get; set; }
public bool IsActive { get; set; } = true;
public string? SourceHash { get; set; }
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
}
public sealed class UserWordProgress : AuditableTenantEntity
{
public Guid UserId { get; set; }
public Guid WordId { get; set; }
public string? LegacyId { get; set; }
public string? LegacyUserId { get; set; }
public string? LegacyWordId { get; set; }
public WordProgressStatus Status { get; set; } = WordProgressStatus.New;
public int CorrectCount { get; set; }
public int WrongCount { get; set; }
public DateTimeOffset? LastReviewAt { get; set; }
public DateTimeOffset? NextReviewAt { get; set; }
public int ReviewCount { get; set; }
public int CorrectStreak { get; set; }
public decimal EaseFactor { get; set; } = 2.50m;
public WordReviewResult? LastResult { get; set; }
public WordDueLevel DueLevel { get; set; } = WordDueLevel.New;
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
}
public sealed class UserWordFavorite : AuditableTenantEntity
{
public Guid UserId { get; set; }
public Guid WordId { get; set; }
public string? LegacyId { get; set; }
public string? LegacyUserId { get; set; }
public string? LegacyWordId { get; set; }
public string? Note { get; set; }
public DateTimeOffset? FavoritedAt { get; set; }
}
public sealed class HandbookSubject : AuditableTenantEntity
{
public Guid? RegionId { get; set; }
public Guid? SchoolId { get; set; }
public Guid? MajorId { get; set; }
public Guid? EntryId { get; set; }
public Guid? ContentNodeId { get; set; }
public string? LegacyId { get; set; }
public string Name { get; set; } = string.Empty;
public HandbookSubjectType? Type { get; set; }
public string? Icon { get; set; }
public string? Color { get; set; }
public string? Description { get; set; }
public JsonElement MajorLegacyIds { get; set; } = JsonDefaults.Array();
public int SortOrder { get; set; }
public bool IsActive { get; set; } = true;
public string? SourceHash { get; set; }
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
}
public sealed class HandbookChapter : AuditableTenantEntity
{
public Guid? SubjectId { get; set; }
public Guid? EntryId { get; set; }
public Guid? ContentNodeId { get; set; }
public string? LegacyId { get; set; }
public string Name { get; set; } = string.Empty;
public string? Description { get; set; }
public int SortOrder { get; set; }
public bool IsActive { get; set; } = true;
public string? SourceHash { get; set; }
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
}
public sealed class HandbookEntry : AuditableTenantEntity
{
public Guid? ChapterId { get; set; }
public Guid? EntryId { get; set; }
public Guid? ContentNodeId { get; set; }
public string? LegacyId { get; set; }
public string Title { get; set; } = string.Empty;
public string? Summary { get; set; }
public string? Content { get; set; }
public JsonElement Tags { get; set; } = JsonDefaults.Array();
public int SortOrder { get; set; }
public bool IsActive { get; set; } = true;
public string? SourceHash { get; set; }
public JsonElement Metadata { get; set; } = JsonDefaults.Object();
}
public sealed class QuestionTypeGroup : AuditableTenantEntity
{
public Guid? SubjectId { get; set; }
public string? LegacyId { get; set; }
public string DisplayName { get; set; } = string.Empty;
public JsonElement Types { get; set; } = JsonDefaults.Array();
public int SortOrder { get; set; }
public bool IsActive { get; set; } = true;
}
public sealed class SubjectShare : AuditableTenantEntity
{
public Guid? SourceSubjectId { get; set; }
public Guid? TargetSubjectId { get; set; }
public string? LegacyId { get; set; }
public string? LegacySourceSubjectId { get; set; }
public string? LegacyTargetSubjectId { get; set; }
}
public enum WordProgressStatus
{
New,
Learning,
Mastered,
Reviewing,
Forgotten
}
public enum WordReviewResult
{
Correct,
Wrong,
Known,
Unknown
}
public enum WordDueLevel
{
New,
Again,
Soon,
Later,
Mastered
}
public enum HandbookSubjectType
{
Cultural,
Professional,
Common
}

View File

@@ -0,0 +1,336 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Metadata.Builders;
using Tiku.Domain.Catalog;
using Tiku.Domain.Content;
using Tiku.Domain.Identity;
namespace Tiku.Infrastructure.Persistence.Configurations;
internal sealed class VocabularyUnitConfiguration : IEntityTypeConfiguration<VocabularyUnit>
{
public void Configure(EntityTypeBuilder<VocabularyUnit> builder)
{
builder.ConfigureTenantEntity("vocabulary_units");
builder.ConfigureTimestamps();
builder.Property(entity => entity.LegacyId).HasMaxLength(64);
builder.Property(entity => entity.Name).HasMaxLength(300);
builder.Property(entity => entity.Description).HasMaxLength(2000);
builder.Property(entity => entity.SourceHash).HasMaxLength(128);
builder.Property(entity => entity.Metadata).IsJson("{}");
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new
{
entity.TenantId,
entity.EntryId,
entity.ContentNodeId,
entity.RegionId,
entity.IsActive,
entity.SortOrder
});
builder.HasOne<Region>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.RegionId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<ContentEntry>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.EntryId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<ContentNode>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.ContentNodeId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
}
}
internal sealed class VocabularyWordConfiguration : IEntityTypeConfiguration<VocabularyWord>
{
public void Configure(EntityTypeBuilder<VocabularyWord> builder)
{
builder.ConfigureTenantEntity("vocabulary_words");
builder.ConfigureTimestamps();
builder.Property(entity => entity.LegacyId).HasMaxLength(64);
builder.Property(entity => entity.Word).HasMaxLength(300);
builder.Property(entity => entity.Phonetic).HasMaxLength(200);
builder.Property(entity => entity.Meaning).HasMaxLength(2000);
builder.Property(entity => entity.Example).HasMaxLength(4000);
builder.Property(entity => entity.ExampleTranslation).HasMaxLength(4000);
builder.Property(entity => entity.SourceHash).HasMaxLength(128);
builder.Property(entity => entity.Tags).IsJson("[]");
builder.Property(entity => entity.Metadata).IsJson("{}");
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new
{
entity.TenantId,
entity.EntryId,
entity.ContentNodeId,
entity.UnitId,
entity.IsActive,
entity.SortOrder
});
builder.HasIndex(entity => new
{
entity.TenantId,
entity.UnitId,
entity.IsActive,
entity.SortOrder,
entity.CreatedAt
});
builder.HasOne<VocabularyUnit>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.UnitId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<ContentEntry>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.EntryId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<ContentNode>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.ContentNodeId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
}
}
internal sealed class UserWordProgressConfiguration : IEntityTypeConfiguration<UserWordProgress>
{
public void Configure(EntityTypeBuilder<UserWordProgress> builder)
{
builder.ConfigureTenantEntity("user_word_progress");
builder.ConfigureTimestamps();
builder.Property(entity => entity.LegacyId).HasMaxLength(64);
builder.Property(entity => entity.LegacyUserId).HasMaxLength(64);
builder.Property(entity => entity.LegacyWordId).HasMaxLength(64);
builder.Property(entity => entity.Status).HasSnakeCaseEnum();
builder.Property(entity => entity.EaseFactor).HasPrecision(4, 2).HasDefaultValue(2.50m);
builder.Property(entity => entity.LastResult).HasNullableSnakeCaseEnum();
builder.Property(entity => entity.DueLevel).HasSnakeCaseEnum();
builder.Property(entity => entity.Metadata).IsJson("{}");
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new { entity.TenantId, entity.UserId, entity.WordId }).IsUnique();
builder.HasIndex(entity => new { entity.TenantId, entity.UserId, entity.Status });
builder.HasIndex(entity => new
{
entity.TenantId,
entity.UserId,
entity.NextReviewAt,
entity.Status,
entity.DueLevel
});
builder.ToTable(table =>
{
table.HasCheckConstraint("ck_user_word_progress_review_count", "review_count >= 0");
table.HasCheckConstraint("ck_user_word_progress_correct_streak", "correct_streak >= 0");
table.HasCheckConstraint("ck_user_word_progress_ease_factor", "ease_factor >= 1.30 and ease_factor <= 3.00");
});
builder.HasOne<User>().WithMany()
.HasForeignKey(entity => entity.UserId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne<VocabularyWord>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.WordId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Cascade);
}
}
internal sealed class UserWordFavoriteConfiguration : IEntityTypeConfiguration<UserWordFavorite>
{
public void Configure(EntityTypeBuilder<UserWordFavorite> builder)
{
builder.ConfigureTenantEntity("user_word_favorites");
builder.ConfigureTimestamps();
builder.Property(entity => entity.LegacyId).HasMaxLength(64);
builder.Property(entity => entity.LegacyUserId).HasMaxLength(64);
builder.Property(entity => entity.LegacyWordId).HasMaxLength(64);
builder.Property(entity => entity.Note).HasMaxLength(2000);
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new { entity.TenantId, entity.UserId, entity.WordId }).IsUnique();
builder.HasOne<User>().WithMany()
.HasForeignKey(entity => entity.UserId)
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne<VocabularyWord>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.WordId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Cascade);
}
}
internal sealed class HandbookSubjectConfiguration : IEntityTypeConfiguration<HandbookSubject>
{
public void Configure(EntityTypeBuilder<HandbookSubject> builder)
{
builder.ConfigureTenantEntity("handbook_subjects");
builder.ConfigureTimestamps();
builder.Property(entity => entity.LegacyId).HasMaxLength(64);
builder.Property(entity => entity.Name).HasMaxLength(300);
builder.Property(entity => entity.Type).HasNullableSnakeCaseEnum();
builder.Property(entity => entity.Icon).HasMaxLength(2048);
builder.Property(entity => entity.Color).HasMaxLength(50);
builder.Property(entity => entity.Description).HasMaxLength(2000);
builder.Property(entity => entity.MajorLegacyIds).IsJson("[]");
builder.Property(entity => entity.SourceHash).HasMaxLength(128);
builder.Property(entity => entity.Metadata).IsJson("{}");
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new
{
entity.TenantId,
entity.EntryId,
entity.ContentNodeId,
entity.RegionId,
entity.IsActive,
entity.SortOrder
});
builder.HasOne<Region>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.RegionId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<School>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.SchoolId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<Major>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.MajorId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<ContentEntry>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.EntryId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<ContentNode>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.ContentNodeId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
}
}
internal sealed class HandbookChapterConfiguration : IEntityTypeConfiguration<HandbookChapter>
{
public void Configure(EntityTypeBuilder<HandbookChapter> builder)
{
builder.ConfigureTenantEntity("handbook_chapters");
builder.ConfigureTimestamps();
builder.Property(entity => entity.LegacyId).HasMaxLength(64);
builder.Property(entity => entity.Name).HasMaxLength(300);
builder.Property(entity => entity.Description).HasMaxLength(2000);
builder.Property(entity => entity.SourceHash).HasMaxLength(128);
builder.Property(entity => entity.Metadata).IsJson("{}");
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new
{
entity.TenantId,
entity.EntryId,
entity.ContentNodeId,
entity.SubjectId,
entity.IsActive,
entity.SortOrder
});
builder.HasOne<HandbookSubject>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.SubjectId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne<ContentEntry>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.EntryId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<ContentNode>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.ContentNodeId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
}
}
internal sealed class HandbookEntryConfiguration : IEntityTypeConfiguration<HandbookEntry>
{
public void Configure(EntityTypeBuilder<HandbookEntry> builder)
{
builder.ConfigureTenantEntity("handbook_entries");
builder.ConfigureTimestamps();
builder.Property(entity => entity.LegacyId).HasMaxLength(64);
builder.Property(entity => entity.Title).HasMaxLength(300);
builder.Property(entity => entity.Summary).HasMaxLength(2000);
builder.Property(entity => entity.Tags).IsJson("[]");
builder.Property(entity => entity.SourceHash).HasMaxLength(128);
builder.Property(entity => entity.Metadata).IsJson("{}");
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new
{
entity.TenantId,
entity.EntryId,
entity.ContentNodeId,
entity.ChapterId,
entity.IsActive,
entity.SortOrder
});
builder.HasOne<HandbookChapter>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.ChapterId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne<ContentEntry>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.EntryId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
builder.HasOne<ContentNode>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.ContentNodeId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Restrict);
}
}
internal sealed class QuestionTypeGroupConfiguration : IEntityTypeConfiguration<QuestionTypeGroup>
{
public void Configure(EntityTypeBuilder<QuestionTypeGroup> builder)
{
builder.ConfigureTenantEntity("question_type_groups");
builder.ConfigureTimestamps();
builder.Property(entity => entity.LegacyId).HasMaxLength(64);
builder.Property(entity => entity.DisplayName).HasMaxLength(300);
builder.Property(entity => entity.Types).IsJson("[]");
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new
{
entity.TenantId,
entity.SubjectId,
entity.IsActive,
entity.SortOrder
});
builder.HasOne<Subject>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.SubjectId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Cascade);
}
}
internal sealed class SubjectShareConfiguration : IEntityTypeConfiguration<SubjectShare>
{
public void Configure(EntityTypeBuilder<SubjectShare> builder)
{
builder.ConfigureTenantEntity("subject_shares");
builder.ConfigureTimestamps();
builder.Property(entity => entity.LegacyId).HasMaxLength(64);
builder.Property(entity => entity.LegacySourceSubjectId).HasMaxLength(64);
builder.Property(entity => entity.LegacyTargetSubjectId).HasMaxLength(64);
builder.HasIndex(entity => new { entity.TenantId, entity.LegacyId }).IsUnique();
builder.HasIndex(entity => new
{
entity.TenantId,
entity.SourceSubjectId,
entity.TargetSubjectId
}).IsUnique();
builder.HasOne<Subject>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.SourceSubjectId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Cascade);
builder.HasOne<Subject>().WithMany()
.HasForeignKey(entity => new { entity.TenantId, entity.TargetSubjectId })
.HasPrincipalKey(entity => new { entity.TenantId, entity.Id })
.OnDelete(DeleteBehavior.Cascade);
}
}

View File

@@ -0,0 +1,648 @@
using System;
using System.Text.Json;
using Microsoft.EntityFrameworkCore.Migrations;
#nullable disable
namespace Tiku.Infrastructure.Persistence.Migrations
{
/// <inheritdoc />
public partial class AddVocabularyAndHandbookPersistence : Migration
{
/// <inheritdoc />
protected override void Up(MigrationBuilder migrationBuilder)
{
migrationBuilder.CreateTable(
name: "handbook_subjects",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
region_id = table.Column<Guid>(type: "uuid", nullable: true),
school_id = table.Column<Guid>(type: "uuid", nullable: true),
major_id = table.Column<Guid>(type: "uuid", nullable: true),
entry_id = table.Column<Guid>(type: "uuid", nullable: true),
content_node_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
name = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: false),
type = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: true),
icon = table.Column<string>(type: "character varying(2048)", maxLength: 2048, nullable: true),
color = table.Column<string>(type: "character varying(50)", maxLength: 50, nullable: true),
description = table.Column<string>(type: "text", nullable: true),
major_legacy_ids = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'[]'::jsonb"),
sort_order = table.Column<int>(type: "integer", nullable: false),
is_active = table.Column<bool>(type: "boolean", nullable: false),
source_hash = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_handbook_subjects", x => x.id);
table.UniqueConstraint("ak_handbook_subjects_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_handbook_subjects_content_entries_tenant_id_entry_id",
columns: x => new { x.tenant_id, x.entry_id },
principalTable: "content_entries",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_handbook_subjects_content_nodes_tenant_id_content_node_id",
columns: x => new { x.tenant_id, x.content_node_id },
principalTable: "content_nodes",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_handbook_subjects_majors_tenant_id_major_id",
columns: x => new { x.tenant_id, x.major_id },
principalTable: "majors",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_handbook_subjects_regions_tenant_id_region_id",
columns: x => new { x.tenant_id, x.region_id },
principalTable: "regions",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_handbook_subjects_schools_tenant_id_school_id",
columns: x => new { x.tenant_id, x.school_id },
principalTable: "schools",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_handbook_subjects_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "question_type_groups",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
subject_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
display_name = table.Column<string>(type: "character varying(200)", maxLength: 200, nullable: false),
types = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'[]'::jsonb"),
sort_order = table.Column<int>(type: "integer", nullable: false),
is_active = table.Column<bool>(type: "boolean", nullable: false),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_question_type_groups", x => x.id);
table.UniqueConstraint("ak_question_type_groups_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_question_type_groups_subjects_tenant_id_subject_id",
columns: x => new { x.tenant_id, x.subject_id },
principalTable: "subjects",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_question_type_groups_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "subject_shares",
columns: table => new
{
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
source_subject_id = table.Column<Guid>(type: "uuid", nullable: false),
target_subject_id = table.Column<Guid>(type: "uuid", nullable: false),
legacy_id = table.Column<string>(type: "text", nullable: true),
legacy_source_subject_id = table.Column<string>(type: "text", nullable: true),
legacy_target_subject_id = table.Column<string>(type: "text", nullable: true),
id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false)
},
constraints: table =>
{
table.PrimaryKey("pk_subject_shares", x => new { x.tenant_id, x.source_subject_id, x.target_subject_id });
table.ForeignKey(
name: "fk_subject_shares_subjects_tenant_id_source_subject_id",
columns: x => new { x.tenant_id, x.source_subject_id },
principalTable: "subjects",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_subject_shares_subjects_tenant_id_target_subject_id",
columns: x => new { x.tenant_id, x.target_subject_id },
principalTable: "subjects",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_subject_shares_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "vocabulary_units",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
region_id = table.Column<Guid>(type: "uuid", nullable: true),
entry_id = table.Column<Guid>(type: "uuid", nullable: true),
content_node_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
name = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: false),
description = table.Column<string>(type: "text", nullable: true),
word_count = table.Column<int>(type: "integer", nullable: true),
sort_order = table.Column<int>(type: "integer", nullable: false),
is_active = table.Column<bool>(type: "boolean", nullable: false),
source_hash = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_vocabulary_units", x => x.id);
table.UniqueConstraint("ak_vocabulary_units_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_vocabulary_units_content_entries_tenant_id_entry_id",
columns: x => new { x.tenant_id, x.entry_id },
principalTable: "content_entries",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_vocabulary_units_content_nodes_tenant_id_content_node_id",
columns: x => new { x.tenant_id, x.content_node_id },
principalTable: "content_nodes",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_vocabulary_units_regions_tenant_id_region_id",
columns: x => new { x.tenant_id, x.region_id },
principalTable: "regions",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_vocabulary_units_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "handbook_chapters",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
subject_id = table.Column<Guid>(type: "uuid", nullable: true),
entry_id = table.Column<Guid>(type: "uuid", nullable: true),
content_node_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
name = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: false),
description = table.Column<string>(type: "text", nullable: true),
sort_order = table.Column<int>(type: "integer", nullable: false),
is_active = table.Column<bool>(type: "boolean", nullable: false),
source_hash = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_handbook_chapters", x => x.id);
table.UniqueConstraint("ak_handbook_chapters_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_handbook_chapters_content_entries_tenant_id_entry_id",
columns: x => new { x.tenant_id, x.entry_id },
principalTable: "content_entries",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_handbook_chapters_content_nodes_tenant_id_content_node_id",
columns: x => new { x.tenant_id, x.content_node_id },
principalTable: "content_nodes",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_handbook_chapters_handbook_subjects_tenant_id_subject_id",
columns: x => new { x.tenant_id, x.subject_id },
principalTable: "handbook_subjects",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_handbook_chapters_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "vocabulary_words",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
unit_id = table.Column<Guid>(type: "uuid", nullable: true),
entry_id = table.Column<Guid>(type: "uuid", nullable: true),
content_node_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
word = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: false),
phonetic = table.Column<string>(type: "character varying(300)", maxLength: 300, nullable: true),
meaning = table.Column<string>(type: "text", nullable: true),
example = table.Column<string>(type: "text", nullable: true),
example_translation = table.Column<string>(type: "text", nullable: true),
difficulty = table.Column<int>(type: "integer", nullable: true),
tags = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'[]'::jsonb"),
sort_order = table.Column<int>(type: "integer", nullable: false),
is_active = table.Column<bool>(type: "boolean", nullable: false),
source_hash = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_vocabulary_words", x => x.id);
table.UniqueConstraint("ak_vocabulary_words_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_vocabulary_words_content_entries_tenant_id_entry_id",
columns: x => new { x.tenant_id, x.entry_id },
principalTable: "content_entries",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_vocabulary_words_content_nodes_tenant_id_content_node_id",
columns: x => new { x.tenant_id, x.content_node_id },
principalTable: "content_nodes",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_vocabulary_words_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_vocabulary_words_vocabulary_units_tenant_id_unit_id",
columns: x => new { x.tenant_id, x.unit_id },
principalTable: "vocabulary_units",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
});
migrationBuilder.CreateTable(
name: "handbook_entries",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
chapter_id = table.Column<Guid>(type: "uuid", nullable: true),
entry_id = table.Column<Guid>(type: "uuid", nullable: true),
content_node_id = table.Column<Guid>(type: "uuid", nullable: true),
legacy_id = table.Column<string>(type: "character varying(64)", maxLength: 64, nullable: true),
title = table.Column<string>(type: "character varying(500)", maxLength: 500, nullable: false),
summary = table.Column<string>(type: "text", nullable: true),
content = table.Column<string>(type: "text", nullable: true),
tags = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'[]'::jsonb"),
sort_order = table.Column<int>(type: "integer", nullable: false),
is_active = table.Column<bool>(type: "boolean", nullable: false),
source_hash = table.Column<string>(type: "character varying(128)", maxLength: 128, nullable: true),
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_handbook_entries", x => x.id);
table.UniqueConstraint("ak_handbook_entries_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_handbook_entries_content_entries_tenant_id_entry_id",
columns: x => new { x.tenant_id, x.entry_id },
principalTable: "content_entries",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_handbook_entries_content_nodes_tenant_id_content_node_id",
columns: x => new { x.tenant_id, x.content_node_id },
principalTable: "content_nodes",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Restrict);
table.ForeignKey(
name: "fk_handbook_entries_handbook_chapters_tenant_id_chapter_id",
columns: x => new { x.tenant_id, x.chapter_id },
principalTable: "handbook_chapters",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_handbook_entries_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "user_word_favorites",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
user_id = table.Column<Guid>(type: "uuid", nullable: false),
word_id = table.Column<Guid>(type: "uuid", nullable: false),
legacy_id = table.Column<string>(type: "text", nullable: true),
legacy_user_id = table.Column<string>(type: "text", nullable: true),
legacy_word_id = table.Column<string>(type: "text", nullable: true),
note = table.Column<string>(type: "character varying(1000)", maxLength: 1000, nullable: true),
favorited_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_user_word_favorites", x => x.id);
table.UniqueConstraint("ak_user_word_favorites_tenant_id_id", x => new { x.tenant_id, x.id });
table.ForeignKey(
name: "fk_user_word_favorites_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_user_word_favorites_users_user_id",
column: x => x.user_id,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_user_word_favorites_vocabulary_words_tenant_id_word_id",
columns: x => new { x.tenant_id, x.word_id },
principalTable: "vocabulary_words",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateTable(
name: "user_word_progress",
columns: table => new
{
id = table.Column<Guid>(type: "uuid", nullable: false, defaultValueSql: "gen_random_uuid()"),
user_id = table.Column<Guid>(type: "uuid", nullable: false),
word_id = table.Column<Guid>(type: "uuid", nullable: false),
legacy_id = table.Column<string>(type: "text", nullable: true),
legacy_user_id = table.Column<string>(type: "text", nullable: true),
legacy_word_id = table.Column<string>(type: "text", nullable: true),
status = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
correct_count = table.Column<int>(type: "integer", nullable: false),
wrong_count = table.Column<int>(type: "integer", nullable: false),
last_review_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
next_review_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: true),
review_count = table.Column<int>(type: "integer", nullable: false),
correct_streak = table.Column<int>(type: "integer", nullable: false),
ease_factor = table.Column<decimal>(type: "numeric(4,2)", precision: 4, scale: 2, nullable: false, defaultValue: 2.50m),
last_result = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: true),
due_level = table.Column<string>(type: "character varying(32)", maxLength: 32, nullable: false),
metadata = table.Column<JsonElement>(type: "jsonb", nullable: false, defaultValueSql: "'{}'::jsonb"),
tenant_id = table.Column<Guid>(type: "uuid", nullable: false),
created_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()"),
updated_at = table.Column<DateTimeOffset>(type: "timestamp with time zone", nullable: false, defaultValueSql: "now()")
},
constraints: table =>
{
table.PrimaryKey("pk_user_word_progress", x => x.id);
table.UniqueConstraint("ak_user_word_progress_tenant_id_id", x => new { x.tenant_id, x.id });
table.CheckConstraint("ck_user_word_progress_correct_streak", "correct_streak >= 0");
table.CheckConstraint("ck_user_word_progress_ease_factor", "ease_factor >= 1.30 and ease_factor <= 3.00");
table.CheckConstraint("ck_user_word_progress_review_count", "review_count >= 0");
table.ForeignKey(
name: "fk_user_word_progress_tenants_tenant_id",
column: x => x.tenant_id,
principalTable: "tenants",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_user_word_progress_users_user_id",
column: x => x.user_id,
principalTable: "users",
principalColumn: "id",
onDelete: ReferentialAction.Cascade);
table.ForeignKey(
name: "fk_user_word_progress_vocabulary_words_tenant_id_word_id",
columns: x => new { x.tenant_id, x.word_id },
principalTable: "vocabulary_words",
principalColumns: new[] { "tenant_id", "id" },
onDelete: ReferentialAction.Cascade);
});
migrationBuilder.CreateIndex(
name: "ix_handbook_chapters_tenant_id_content_node_id",
table: "handbook_chapters",
columns: new[] { "tenant_id", "content_node_id" });
migrationBuilder.CreateIndex(
name: "ix_handbook_chapters_tenant_id_entry_id_content_node_id_subjec~",
table: "handbook_chapters",
columns: new[] { "tenant_id", "entry_id", "content_node_id", "subject_id", "is_active", "sort_order" });
migrationBuilder.CreateIndex(
name: "ix_handbook_chapters_tenant_id_legacy_id",
table: "handbook_chapters",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_handbook_chapters_tenant_id_subject_id",
table: "handbook_chapters",
columns: new[] { "tenant_id", "subject_id" });
migrationBuilder.CreateIndex(
name: "ix_handbook_entries_tenant_id_chapter_id",
table: "handbook_entries",
columns: new[] { "tenant_id", "chapter_id" });
migrationBuilder.CreateIndex(
name: "ix_handbook_entries_tenant_id_content_node_id",
table: "handbook_entries",
columns: new[] { "tenant_id", "content_node_id" });
migrationBuilder.CreateIndex(
name: "ix_handbook_entries_tenant_id_entry_id_content_node_id_chapter~",
table: "handbook_entries",
columns: new[] { "tenant_id", "entry_id", "content_node_id", "chapter_id", "is_active", "sort_order" });
migrationBuilder.CreateIndex(
name: "ix_handbook_entries_tenant_id_legacy_id",
table: "handbook_entries",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_handbook_subjects_tenant_id_content_node_id",
table: "handbook_subjects",
columns: new[] { "tenant_id", "content_node_id" });
migrationBuilder.CreateIndex(
name: "ix_handbook_subjects_tenant_id_entry_id_content_node_id_region~",
table: "handbook_subjects",
columns: new[] { "tenant_id", "entry_id", "content_node_id", "region_id", "is_active", "sort_order" });
migrationBuilder.CreateIndex(
name: "ix_handbook_subjects_tenant_id_legacy_id",
table: "handbook_subjects",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_handbook_subjects_tenant_id_major_id",
table: "handbook_subjects",
columns: new[] { "tenant_id", "major_id" });
migrationBuilder.CreateIndex(
name: "ix_handbook_subjects_tenant_id_region_id",
table: "handbook_subjects",
columns: new[] { "tenant_id", "region_id" });
migrationBuilder.CreateIndex(
name: "ix_handbook_subjects_tenant_id_school_id",
table: "handbook_subjects",
columns: new[] { "tenant_id", "school_id" });
migrationBuilder.CreateIndex(
name: "ix_question_type_groups_tenant_id_legacy_id",
table: "question_type_groups",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_question_type_groups_tenant_id_subject_id_is_active_sort_or~",
table: "question_type_groups",
columns: new[] { "tenant_id", "subject_id", "is_active", "sort_order" });
migrationBuilder.CreateIndex(
name: "ix_subject_shares_tenant_id_target_subject_id",
table: "subject_shares",
columns: new[] { "tenant_id", "target_subject_id" });
migrationBuilder.CreateIndex(
name: "ix_user_word_favorites_tenant_id_user_id_word_id",
table: "user_word_favorites",
columns: new[] { "tenant_id", "user_id", "word_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_user_word_favorites_tenant_id_word_id",
table: "user_word_favorites",
columns: new[] { "tenant_id", "word_id" });
migrationBuilder.CreateIndex(
name: "ix_user_word_favorites_user_id",
table: "user_word_favorites",
column: "user_id");
migrationBuilder.CreateIndex(
name: "ix_user_word_progress_tenant_id_user_id_next_review_at_status_~",
table: "user_word_progress",
columns: new[] { "tenant_id", "user_id", "next_review_at", "status", "due_level" });
migrationBuilder.CreateIndex(
name: "ix_user_word_progress_tenant_id_user_id_word_id",
table: "user_word_progress",
columns: new[] { "tenant_id", "user_id", "word_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_user_word_progress_tenant_id_word_id",
table: "user_word_progress",
columns: new[] { "tenant_id", "word_id" });
migrationBuilder.CreateIndex(
name: "ix_user_word_progress_user_id",
table: "user_word_progress",
column: "user_id");
migrationBuilder.CreateIndex(
name: "ix_vocabulary_units_tenant_id_content_node_id",
table: "vocabulary_units",
columns: new[] { "tenant_id", "content_node_id" });
migrationBuilder.CreateIndex(
name: "ix_vocabulary_units_tenant_id_entry_id_content_node_id_region_~",
table: "vocabulary_units",
columns: new[] { "tenant_id", "entry_id", "content_node_id", "region_id", "is_active", "sort_order" });
migrationBuilder.CreateIndex(
name: "ix_vocabulary_units_tenant_id_legacy_id",
table: "vocabulary_units",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_vocabulary_units_tenant_id_region_id",
table: "vocabulary_units",
columns: new[] { "tenant_id", "region_id" });
migrationBuilder.CreateIndex(
name: "ix_vocabulary_words_tenant_id_content_node_id",
table: "vocabulary_words",
columns: new[] { "tenant_id", "content_node_id" });
migrationBuilder.CreateIndex(
name: "ix_vocabulary_words_tenant_id_entry_id_content_node_id_unit_id~",
table: "vocabulary_words",
columns: new[] { "tenant_id", "entry_id", "content_node_id", "unit_id", "is_active", "sort_order" });
migrationBuilder.CreateIndex(
name: "ix_vocabulary_words_tenant_id_legacy_id",
table: "vocabulary_words",
columns: new[] { "tenant_id", "legacy_id" },
unique: true);
migrationBuilder.CreateIndex(
name: "ix_vocabulary_words_tenant_id_unit_id_is_active_sort_order_cre~",
table: "vocabulary_words",
columns: new[] { "tenant_id", "unit_id", "is_active", "sort_order", "created_at" });
}
/// <inheritdoc />
protected override void Down(MigrationBuilder migrationBuilder)
{
migrationBuilder.DropTable(
name: "handbook_entries");
migrationBuilder.DropTable(
name: "question_type_groups");
migrationBuilder.DropTable(
name: "subject_shares");
migrationBuilder.DropTable(
name: "user_word_favorites");
migrationBuilder.DropTable(
name: "user_word_progress");
migrationBuilder.DropTable(
name: "handbook_chapters");
migrationBuilder.DropTable(
name: "vocabulary_words");
migrationBuilder.DropTable(
name: "handbook_subjects");
migrationBuilder.DropTable(
name: "vocabulary_units");
}
}
}

View File

@@ -34,6 +34,15 @@ public sealed class TikuDbContext(DbContextOptions<TikuDbContext> options) : DbC
public DbSet<QuestionCollection> QuestionCollections => Set<QuestionCollection>();
public DbSet<QuestionCollectionItem> QuestionCollectionItems => Set<QuestionCollectionItem>();
public DbSet<PracticeBlueprint> PracticeBlueprints => Set<PracticeBlueprint>();
public DbSet<VocabularyUnit> VocabularyUnits => Set<VocabularyUnit>();
public DbSet<VocabularyWord> VocabularyWords => Set<VocabularyWord>();
public DbSet<UserWordProgress> UserWordProgress => Set<UserWordProgress>();
public DbSet<UserWordFavorite> UserWordFavorites => Set<UserWordFavorite>();
public DbSet<HandbookSubject> HandbookSubjects => Set<HandbookSubject>();
public DbSet<HandbookChapter> HandbookChapters => Set<HandbookChapter>();
public DbSet<HandbookEntry> HandbookEntries => Set<HandbookEntry>();
public DbSet<QuestionTypeGroup> QuestionTypeGroups => Set<QuestionTypeGroup>();
public DbSet<SubjectShare> SubjectShares => Set<SubjectShare>();
public DbSet<PracticeSession> PracticeSessions => Set<PracticeSession>();
public DbSet<AnswerRecord> AnswerRecords => Set<AnswerRecord>();
public DbSet<FavoriteQuestion> FavoriteQuestions => Set<FavoriteQuestion>();

View File

@@ -1,4 +1,5 @@
using Microsoft.EntityFrameworkCore;
using Microsoft.EntityFrameworkCore.Infrastructure;
using Microsoft.EntityFrameworkCore.Metadata;
using Tiku.Domain.Content;
using Tiku.Domain.QuestionBanks;
@@ -22,13 +23,22 @@ public sealed class PersistenceModelTests
.Select(entity => entity.GetTableName())
.ToHashSet(StringComparer.Ordinal);
Assert.Equal(28, tableNames.Count);
Assert.Equal(37, tableNames.Count);
Assert.Contains("tenants", tableNames);
Assert.Contains("tenant_settings", tableNames);
Assert.Contains("content_entries", tableNames);
Assert.Contains("content_nodes", tableNames);
Assert.Contains("question_collections", tableNames);
Assert.Contains("practice_blueprints", tableNames);
Assert.Contains("vocabulary_units", tableNames);
Assert.Contains("vocabulary_words", tableNames);
Assert.Contains("user_word_progress", tableNames);
Assert.Contains("user_word_favorites", tableNames);
Assert.Contains("handbook_subjects", tableNames);
Assert.Contains("handbook_chapters", tableNames);
Assert.Contains("handbook_entries", tableNames);
Assert.Contains("question_type_groups", tableNames);
Assert.Contains("subject_shares", tableNames);
Assert.Contains("questions", tableNames);
Assert.Contains("question_versions", tableNames);
Assert.Contains("answer_records", tableNames);
@@ -99,4 +109,104 @@ public sealed class PersistenceModelTests
properties => properties.SequenceEqual(
["TenantId", "Id", "CurrentVersionId"]));
}
[Theory]
[InlineData(typeof(VocabularyWord), nameof(VocabularyWord.Tags), "'[]'::jsonb")]
[InlineData(typeof(VocabularyWord), nameof(VocabularyWord.Metadata), "'{}'::jsonb")]
[InlineData(typeof(HandbookSubject), nameof(HandbookSubject.MajorLegacyIds), "'[]'::jsonb")]
[InlineData(typeof(HandbookEntry), nameof(HandbookEntry.Tags), "'[]'::jsonb")]
[InlineData(typeof(QuestionTypeGroup), nameof(QuestionTypeGroup.Types), "'[]'::jsonb")]
[InlineData(typeof(UserWordProgress), nameof(UserWordProgress.Metadata), "'{}'::jsonb")]
public void Study_content_json_properties_are_mapped_to_jsonb(
Type entityType,
string propertyName,
string expectedDefaultValueSql)
{
using var context = new TikuDbContext(Options);
var property = context.Model.FindEntityType(entityType)!
.FindProperty(propertyName)!;
Assert.Equal("jsonb", property.GetColumnType());
Assert.Equal(expectedDefaultValueSql, property.GetDefaultValueSql());
Assert.Equal(typeof(System.Text.Json.JsonElement), property.ClrType);
}
[Fact]
public void User_word_progress_has_review_constraints_and_precision()
{
using var context = new TikuDbContext(Options);
var entityType = context.GetService<IDesignTimeModel>()
.Model
.FindEntityType(typeof(UserWordProgress))!;
var checkConstraints = entityType.GetCheckConstraints()
.Select(constraint => constraint.Name)
.ToHashSet(StringComparer.Ordinal);
var easeFactor = entityType.FindProperty(nameof(UserWordProgress.EaseFactor))!;
Assert.Equal(4, easeFactor.GetPrecision());
Assert.Equal(2, easeFactor.GetScale());
Assert.Contains("ck_user_word_progress_review_count", checkConstraints);
Assert.Contains("ck_user_word_progress_correct_streak", checkConstraints);
Assert.Contains("ck_user_word_progress_ease_factor", checkConstraints);
}
[Fact]
public void Study_content_relations_include_tenant_in_foreign_keys()
{
using var context = new TikuDbContext(Options);
var entityTypes = new[]
{
typeof(VocabularyUnit),
typeof(VocabularyWord),
typeof(UserWordProgress),
typeof(UserWordFavorite),
typeof(HandbookSubject),
typeof(HandbookChapter),
typeof(HandbookEntry),
typeof(QuestionTypeGroup),
typeof(SubjectShare)
};
var compositeForeignKeys = entityTypes
.SelectMany(entityType => context.Model.FindEntityType(entityType)!
.GetForeignKeys())
.Where(foreignKey => foreignKey.Properties.Count > 1)
.Select(foreignKey => foreignKey.Properties
.Select(property => property.Name)
.ToArray());
Assert.All(
compositeForeignKeys,
properties => Assert.Contains("TenantId", properties));
}
[Fact]
public void Word_user_join_tables_have_tenant_scoped_unique_indexes()
{
using var context = new TikuDbContext(Options);
AssertHasUniqueIndex<UserWordProgress>(
nameof(UserWordProgress.TenantId),
nameof(UserWordProgress.UserId),
nameof(UserWordProgress.WordId));
AssertHasUniqueIndex<UserWordFavorite>(
nameof(UserWordFavorite.TenantId),
nameof(UserWordFavorite.UserId),
nameof(UserWordFavorite.WordId));
void AssertHasUniqueIndex<TEntity>(params string[] propertyNames)
{
var indexes = context.Model.FindEntityType(typeof(TEntity))!.GetIndexes();
Assert.Contains(
indexes,
index => index.IsUnique
&& index.Properties
.Select(property => property.Name)
.SequenceEqual(propertyNames));
}
}
}