diff --git a/src/Messages/Interactions/TSMessage.m b/src/Messages/Interactions/TSMessage.m index 738e05651..cb2c51a2c 100644 --- a/src/Messages/Interactions/TSMessage.m +++ b/src/Messages/Interactions/TSMessage.m @@ -5,6 +5,31 @@ #import "TSAttachment.h" #import +static const NSUInteger OWSMessageSchemaVersion = 2; + +@interface TSMessage () + +/** + * The version of the model class's schema last used to serialize this model. Use this to manage data migrations during + * object de/serialization. + * + * e.g. + * + * - (id)initWithCoder:(NSCoder *)coder + * { + * self = [super initWithCoder:coder]; + * if (!self) { return self; } + * if (_schemaVersion < 2) { + * _newName = [coder decodeObjectForKey:@"oldName"] + * } + * ... + * _schemaVersion = 2; + * } + */ +@property (nonatomic, readonly) NSUInteger schemaVersion; + +@end + @implementation TSMessage - (instancetype)initWithTimestamp:(uint64_t)timestamp @@ -24,6 +49,24 @@ return self; } +- (instancetype)initWithCoder:(NSCoder *)coder +{ + self = [super initWithCoder:coder]; + if (!self) { + return self; + } + + if (_schemaVersion < 2) { + if (!_attachmentIds) { + _attachmentIds = [coder decodeObjectForKey:@"attachments"]; + } + } + + _schemaVersion = OWSMessageSchemaVersion; + return self; +} + + - (BOOL)hasAttachments { return self.attachmentIds ? (self.attachmentIds.count > 0) : false; diff --git a/src/Storage/OWSOrphanedDataCleaner.m b/src/Storage/OWSOrphanedDataCleaner.m index 688dd6b31..969b547ca 100644 --- a/src/Storage/OWSOrphanedDataCleaner.m +++ b/src/Storage/OWSOrphanedDataCleaner.m @@ -77,7 +77,6 @@ } }]; - // TODO Make sure we're not deleting group update avatars NSArray *filenamesToDelete = [attachmentIdFilenames allValues]; NSMutableArray *absolutePathsToDelete = [NSMutableArray arrayWithCapacity:[filenamesToDelete count]]; for (NSString *filename in filenamesToDelete) { diff --git a/src/Storage/TSYapDatabaseObject.h b/src/Storage/TSYapDatabaseObject.h index 641702378..f4315a50e 100644 --- a/src/Storage/TSYapDatabaseObject.h +++ b/src/Storage/TSYapDatabaseObject.h @@ -54,7 +54,6 @@ + (void)enumerateCollectionObjectsWithTransaction:(YapDatabaseReadTransaction *)transaction usingBlock:(void (^)(id object, BOOL *stop))block; - /** * @return A shared database connection. */ @@ -69,15 +68,12 @@ * * @return Instance of the object or nil if non-existent */ - + (instancetype)fetchObjectWithUniqueID:(NSString *)uniqueID transaction:(YapDatabaseReadTransaction *)transaction; - + (instancetype)fetchObjectWithUniqueID:(NSString *)uniqueID; /** * Saves the object with a new YapDatabaseConnection */ - - (void)save; /** @@ -85,18 +81,13 @@ * * @param transaction Database transaction */ - - (void)saveWithTransaction:(YapDatabaseReadWriteTransaction *)transaction; - /** * The unique identifier of the stored object */ - - @property (nonatomic) NSString *uniqueId; - - (void)removeWithTransaction:(YapDatabaseReadWriteTransaction *)transaction; - (void)remove;