Fix MAX emoji attachments
This commit is contained in:
@@ -160,9 +160,12 @@ static async Task EnsureCompatibilitySchemaAsync(QMaxDbContext db)
|
||||
await db.Database.ExecuteSqlRawAsync("ALTER TABLE MessageAttachments ADD COLUMN ExternalId TEXT;");
|
||||
}
|
||||
|
||||
await RemoveDuplicateMessageAttachmentsAsync(db);
|
||||
await db.Database.ExecuteSqlRawAsync("DROP INDEX IF EXISTS IX_MessageAttachments_MessageId_ExternalId;");
|
||||
await db.Database.ExecuteSqlRawAsync("""
|
||||
CREATE INDEX IF NOT EXISTS IX_MessageAttachments_MessageId_ExternalId
|
||||
ON MessageAttachments (MessageId, ExternalId);
|
||||
CREATE UNIQUE INDEX IF NOT EXISTS IX_MessageAttachments_MessageId_ExternalId
|
||||
ON MessageAttachments (MessageId, ExternalId)
|
||||
WHERE ExternalId IS NOT NULL AND ExternalId <> '';
|
||||
""");
|
||||
|
||||
await db.Database.ExecuteSqlRawAsync("""
|
||||
@@ -189,6 +192,29 @@ static async Task EnsureCompatibilitySchemaAsync(QMaxDbContext db)
|
||||
await RemoveGenericMediaLabelsAsync(db);
|
||||
}
|
||||
|
||||
static async Task RemoveDuplicateMessageAttachmentsAsync(QMaxDbContext db)
|
||||
{
|
||||
var attachments = await db.MessageAttachments
|
||||
.Where(attachment => attachment.ExternalId != null && attachment.ExternalId != "")
|
||||
.ToListAsync();
|
||||
|
||||
var duplicates = attachments
|
||||
.OrderBy(attachment => attachment.MessageId)
|
||||
.ThenBy(attachment => attachment.ExternalId)
|
||||
.ThenByDescending(attachment => attachment.FileSizeBytes)
|
||||
.ThenBy(attachment => attachment.CreatedAt)
|
||||
.GroupBy(attachment => new { attachment.MessageId, attachment.ExternalId })
|
||||
.SelectMany(group => group.Skip(1))
|
||||
.ToList();
|
||||
if (duplicates.Count == 0)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
db.MessageAttachments.RemoveRange(duplicates);
|
||||
await db.SaveChangesAsync();
|
||||
}
|
||||
|
||||
static async Task RemoveGenericMediaLabelsAsync(QMaxDbContext db)
|
||||
{
|
||||
var placeholderMessages = await db.Messages
|
||||
|
||||
Reference in New Issue
Block a user