Now the migration helps recreate mis-references post attachments

This commit is contained in:
LittleSheep 2025-06-02 20:29:03 +08:00
parent 782cf56927
commit 0550c4f6de
2 changed files with 40 additions and 60 deletions

View File

@ -25,11 +25,9 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach
Duration? duration = null) Duration? duration = null)
{ {
// Calculate expiration time if needed // Calculate expiration time if needed
Instant? finalExpiration = expiredAt; var finalExpiration = expiredAt;
if (duration.HasValue) if (duration.HasValue)
{
finalExpiration = SystemClock.Instance.GetCurrentInstant() + duration.Value; finalExpiration = SystemClock.Instance.GetCurrentInstant() + duration.Value;
}
var reference = new CloudFileReference var reference = new CloudFileReference
{ {
@ -42,8 +40,6 @@ public class FileReferenceService(AppDatabase db, FileService fileService, ICach
db.FileReferences.Add(reference); db.FileReferences.Add(reference);
await db.SaveChangesAsync(); await db.SaveChangesAsync();
// Purge cache for the file since its usage count has effectively changed
await fileService._PurgeCacheAsync(fileId); await fileService._PurgeCacheAsync(fileId);
return reference; return reference;

View File

@ -1,4 +1,6 @@
using EFCore.BulkExtensions;
using Microsoft.EntityFrameworkCore; using Microsoft.EntityFrameworkCore;
using NodaTime;
namespace DysonNetwork.Sphere.Storage; namespace DysonNetwork.Sphere.Storage;
@ -35,38 +37,33 @@ public class FileReferenceMigrationService(AppDatabase db)
.Where(p => p.OutdatedAttachments.Any()) .Where(p => p.OutdatedAttachments.Any())
.ToListAsync(); .ToListAsync();
var attachmentsId = posts.SelectMany(p => p.OutdatedAttachments.Select(a => a.Id)).ToList();
var attachments = await db.Files.Where(f => attachmentsId.Contains(f.Id)).ToListAsync();
var attachmentsDict = attachments
.GroupBy(a => posts.First(p => p.OutdatedAttachments.Any(oa => oa.Id == a.Id)).Id)
.ToDictionary(g => g.Key, g => g.ToList());
var fileReferences = posts.SelectMany(post =>
attachmentsDict[post.Id].Select(attachment =>
new CloudFileReference
{
FileId = attachment.Id,
File = attachment,
Usage = "post",
ResourceId = post.Id.ToString(),
CreatedAt = SystemClock.Instance.GetCurrentInstant(),
UpdatedAt = SystemClock.Instance.GetCurrentInstant()
})
)
.ToList();
foreach (var post in posts) foreach (var post in posts)
{ {
var updatedAttachments = new List<CloudFileReferenceObject>(); post.Attachments = post.OutdatedAttachments.Select(a => a.ToReferenceObject()).ToList();
foreach (var attachment in post.OutdatedAttachments)
{
var file = await db.Files.FirstOrDefaultAsync(f => f.Id == attachment.Id);
if (file != null)
{
// Create a reference for the file
var reference = new CloudFileReference
{
FileId = file.Id,
File = file,
Usage = "post",
ResourceId = post.Id.ToString()
};
await db.FileReferences.AddAsync(reference);
updatedAttachments.Add(file.ToReferenceObject());
}
else
{
// Keep the existing reference object if file not found
updatedAttachments.Add(attachment.ToReferenceObject());
}
}
post.Attachments = updatedAttachments;
db.Posts.Update(post); db.Posts.Update(post);
} }
await db.BulkInsertAsync(fileReferences);
await db.SaveChangesAsync(); await db.SaveChangesAsync();
} }
@ -74,41 +71,28 @@ public class FileReferenceMigrationService(AppDatabase db)
{ {
var messages = await db.ChatMessages var messages = await db.ChatMessages
.Include(m => m.OutdatedAttachments) .Include(m => m.OutdatedAttachments)
.Where(m => m.OutdatedAttachments.Any() && m.DeletedAt == null) .Where(m => m.OutdatedAttachments.Any())
.ToListAsync(); .ToListAsync();
var fileReferences = messages.SelectMany(message => message.OutdatedAttachments.Select(attachment =>
new CloudFileReference
{
FileId = attachment.Id,
File = attachment,
Usage = "chat",
ResourceId = message.Id.ToString(),
CreatedAt = SystemClock.Instance.GetCurrentInstant(),
UpdatedAt = SystemClock.Instance.GetCurrentInstant()
})
).ToList();
foreach (var message in messages) foreach (var message in messages)
{ {
var updatedAttachments = new List<CloudFileReferenceObject>(); message.Attachments = message.OutdatedAttachments.Select(a => a.ToReferenceObject()).ToList();
foreach (var attachment in message.OutdatedAttachments)
{
var file = await db.Files.FirstOrDefaultAsync(f => f.Id == attachment.Id);
if (file != null)
{
// Create a reference for the file
var reference = new CloudFileReference
{
FileId = file.Id,
File = file,
Usage = "chat",
ResourceId = message.Id.ToString()
};
await db.FileReferences.AddAsync(reference);
updatedAttachments.Add(file.ToReferenceObject());
}
else
{
// Keep the existing reference object if file not found
updatedAttachments.Add(attachment.ToReferenceObject());
}
}
message.Attachments = updatedAttachments;
db.ChatMessages.Update(message); db.ChatMessages.Update(message);
} }
await db.BulkInsertAsync(fileReferences);
await db.SaveChangesAsync(); await db.SaveChangesAsync();
} }