mirror of https://github.com/oxen-io/session-ios
migrate existing attachments to album-compatible gallery schema
parent
57681bd6f3
commit
e096406e56
@ -0,0 +1,72 @@
|
||||
//
|
||||
// Copyright (c) 2018 Open Whisper Systems. All rights reserved.
|
||||
//
|
||||
|
||||
import Foundation
|
||||
import SignalServiceKit
|
||||
|
||||
@objc
|
||||
public class OWS113MultiAttachmentMediaMessages: OWSDatabaseMigration {
|
||||
|
||||
// MARK: - Dependencies
|
||||
|
||||
// MARK: -
|
||||
|
||||
// Increment a similar constant for each migration.
|
||||
@objc
|
||||
class func migrationId() -> String {
|
||||
return "113"
|
||||
}
|
||||
|
||||
override public func runUp(completion: @escaping OWSDatabaseMigrationCompletion) {
|
||||
Logger.debug("")
|
||||
BenchAsync(title: "\(self.logTag)") { (benchCompletion) in
|
||||
self.doMigrationAsync(completion: {
|
||||
benchCompletion()
|
||||
completion()
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
private func doMigrationAsync(completion : @escaping OWSDatabaseMigrationCompletion) {
|
||||
DispatchQueue.global().async {
|
||||
var legacyAttachments: [(attachmentId: String, messageId: String)] = []
|
||||
|
||||
self.dbReadWriteConnection().read { transaction in
|
||||
TSMessage.enumerateCollectionObjects(with: transaction) { object, _ in
|
||||
autoreleasepool {
|
||||
guard let message: TSMessage = object as? TSMessage else {
|
||||
Logger.debug("ignoring message with type: \(object)")
|
||||
return
|
||||
}
|
||||
|
||||
guard let messageId = message.uniqueId else {
|
||||
owsFailDebug("messageId was unexpectedly nil")
|
||||
return
|
||||
}
|
||||
|
||||
for attachmentId in message.attachmentIds {
|
||||
legacyAttachments.append((attachmentId: attachmentId as! String, messageId: messageId))
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
self.dbReadWriteConnection().readWrite { transaction in
|
||||
for (attachmentId, messageId) in legacyAttachments {
|
||||
autoreleasepool {
|
||||
guard let attachment = TSAttachment.fetch(uniqueId: attachmentId, transaction: transaction) else {
|
||||
Logger.warn("missing attachment for messageId: \(messageId)")
|
||||
return
|
||||
}
|
||||
|
||||
attachment.migrateAlbumMessageId(messageId)
|
||||
attachment.save(with: transaction)
|
||||
}
|
||||
}
|
||||
self.save(with: transaction)
|
||||
}
|
||||
|
||||
completion()
|
||||
}
|
||||
}
|
||||
}
|
Loading…
Reference in New Issue