Clean up legacy outgoing attachment duplicates
This commit is contained in:
@@ -0,0 +1,57 @@
|
||||
using Microsoft.EntityFrameworkCore;
|
||||
using QMax.Api.Data.Entities;
|
||||
|
||||
namespace QMax.Api.Data;
|
||||
|
||||
public sealed class QMaxDbContext(DbContextOptions<QMaxDbContext> options) : DbContext(options)
|
||||
{
|
||||
public DbSet<User> Users => Set<User>();
|
||||
public DbSet<UserSession> UserSessions => Set<UserSession>();
|
||||
public DbSet<Chat> Chats => Set<Chat>();
|
||||
public DbSet<Message> Messages => Set<Message>();
|
||||
public DbSet<MessageAttachment> MessageAttachments => Set<MessageAttachment>();
|
||||
public DbSet<MessageReaction> MessageReactions => Set<MessageReaction>();
|
||||
public DbSet<PushDevice> PushDevices => Set<PushDevice>();
|
||||
public DbSet<MaxAccountState> MaxAccountStates => Set<MaxAccountState>();
|
||||
|
||||
protected override void OnModelCreating(ModelBuilder modelBuilder)
|
||||
{
|
||||
modelBuilder.Entity<User>().HasIndex(x => x.PhoneNumber);
|
||||
modelBuilder.Entity<UserSession>().HasIndex(x => x.RefreshTokenHash).IsUnique();
|
||||
modelBuilder.Entity<Chat>().HasIndex(x => x.ExternalId).IsUnique();
|
||||
modelBuilder.Entity<Message>().HasIndex(x => new { x.ChatId, x.SentAt });
|
||||
modelBuilder.Entity<Message>().HasIndex(x => x.ExternalId);
|
||||
modelBuilder.Entity<MessageAttachment>().HasIndex(x => x.StorageFileName).IsUnique();
|
||||
modelBuilder.Entity<MessageAttachment>().HasIndex(x => new { x.MessageId, x.ExternalId });
|
||||
modelBuilder.Entity<MessageReaction>().HasIndex(x => new { x.MessageId, x.ActorKey }).IsUnique();
|
||||
modelBuilder.Entity<PushDevice>().HasIndex(x => x.FirebaseToken).IsUnique();
|
||||
|
||||
modelBuilder.Entity<Chat>()
|
||||
.Property(x => x.Kind)
|
||||
.HasConversion<string>();
|
||||
|
||||
modelBuilder.Entity<Message>()
|
||||
.Property(x => x.Direction)
|
||||
.HasConversion<string>();
|
||||
|
||||
modelBuilder.Entity<Message>()
|
||||
.Property(x => x.DeliveryState)
|
||||
.HasConversion<string>();
|
||||
|
||||
modelBuilder.Entity<MessageAttachment>()
|
||||
.Property(x => x.Kind)
|
||||
.HasConversion<string>();
|
||||
|
||||
modelBuilder.Entity<Message>()
|
||||
.HasMany(x => x.Attachments)
|
||||
.WithOne(x => x.Message)
|
||||
.HasForeignKey(x => x.MessageId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
|
||||
modelBuilder.Entity<Message>()
|
||||
.HasMany(x => x.Reactions)
|
||||
.WithOne(x => x.Message)
|
||||
.HasForeignKey(x => x.MessageId)
|
||||
.OnDelete(DeleteBehavior.Cascade);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user