using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore.Metadata.Builders; using Tiku.Domain.Import; using Tiku.Domain.Tenancy; namespace Tiku.Infrastructure.Persistence.Configurations; internal sealed class PocketBaseImportRunConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder builder) { builder.ConfigureEntity("pb_import_runs"); builder.HasAlternateKey(entity => new { entity.TenantId, entity.Id }); builder.Property(entity => entity.SourceName).HasMaxLength(500); builder.Property(entity => entity.SourceKind).HasSnakeCaseEnum(); builder.Property(entity => entity.Status).HasSnakeCaseEnum(); builder.Property(entity => entity.Stats).IsJson("{}"); builder.Property(entity => entity.StartedAt).HasDefaultValueSql("now()"); builder.HasIndex(entity => new { entity.TenantId, entity.Status, entity.StartedAt }); builder.HasOne().WithMany().HasForeignKey(entity => entity.TenantId).OnDelete(DeleteBehavior.Cascade); } } internal sealed class PocketBaseRawRecordConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder builder) { builder.ToTable("pb_raw_records"); builder.HasKey(entity => new { entity.RunId, entity.CollectionName, entity.LegacyId }); builder.Property(entity => entity.CollectionName).HasMaxLength(100); builder.Property(entity => entity.LegacyId).HasMaxLength(100); builder.Property(entity => entity.Record).IsJson("{}"); builder.Property(entity => entity.Errors).IsJson("[]"); builder.Property(entity => entity.ImportedAt).HasDefaultValueSql("now()"); builder.HasIndex(entity => new { entity.TenantId, entity.CollectionName, entity.LegacyId }); builder.HasOne().WithMany() .HasForeignKey(entity => new { entity.TenantId, entity.RunId }) .HasPrincipalKey(entity => new { entity.TenantId, entity.Id }) .OnDelete(DeleteBehavior.Cascade); builder.HasOne().WithMany().HasForeignKey(entity => entity.TenantId).OnDelete(DeleteBehavior.Cascade); } } internal sealed class PocketBaseImportIssueConfiguration : IEntityTypeConfiguration { public void Configure(EntityTypeBuilder builder) { builder.ConfigureEntity("pb_import_issues"); builder.HasAlternateKey(entity => new { entity.TenantId, entity.Id }); builder.Property(entity => entity.CollectionName).HasMaxLength(100); builder.Property(entity => entity.LegacyId).HasMaxLength(100); builder.Property(entity => entity.Severity).HasSnakeCaseEnum(); builder.Property(entity => entity.IssueCode).HasMaxLength(100); builder.Property(entity => entity.FieldPath).HasMaxLength(500); builder.Property(entity => entity.RawValueSample).HasMaxLength(2000); builder.Property(entity => entity.CreatedAt).HasDefaultValueSql("now()"); builder.HasIndex(entity => new { entity.RunId, entity.Severity, entity.CollectionName }); builder.HasIndex(entity => new { entity.TenantId, entity.CollectionName, entity.LegacyId }); builder.HasOne().WithMany() .HasForeignKey(entity => new { entity.TenantId, entity.RunId }) .HasPrincipalKey(entity => new { entity.TenantId, entity.Id }) .OnDelete(DeleteBehavior.Cascade); builder.HasOne().WithMany().HasForeignKey(entity => entity.TenantId).OnDelete(DeleteBehavior.Cascade); } }