Skip to content

Commit

Permalink
Cherry pick ios fixes to 5.8 github
Browse files Browse the repository at this point in the history
63a7adfbd7ad79918566150059cef7ff2b04d232
  • Loading branch information
Sergey70 committed Sep 12, 2024
1 parent 91321f1 commit b7c28eb
Show file tree
Hide file tree
Showing 8 changed files with 185 additions and 45 deletions.
2 changes: 2 additions & 0 deletions AppMetricaCore/Sources/AMAInstantFeaturesConfiguration.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@
@class AMAInstantFeaturesConfiguration;
@class AMAJSONFileKVSDataProvider;

extern NSString *const kAMAInstantFileName;

@interface AMAInstantFeaturesConfiguration : NSObject

@property (nonatomic, copy) NSString *UUID;
Expand Down
2 changes: 1 addition & 1 deletion AppMetricaCore/Sources/AMAInstantFeaturesConfiguration.m
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
#import "AMACore.h"
#import "AMAMigrationTo500Utils.h"

static NSString *const kAMAInstantFileName = @"instant.json";
NSString *const kAMAInstantFileName = @"instant.json";

@interface AMAInstantFeaturesConfiguration ()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@
#import "AMADatabaseProtocol.h"
#import "AMATableDescriptionProvider.h"
#import "AMAMigrationUtils.h"
#import "AMAInstantFeaturesConfiguration.h"

@implementation AMADataMigrationTo500

Expand All @@ -24,21 +25,28 @@ - (void)applyMigrationToDatabase:(id<AMADatabaseProtocol>)database
@synchronized (self) {
if ([AMAFileUtility fileExistsAtPath:oldDBPath]) {
[self migrateData:oldDBPath database:database];

// Reset startup update date
[database inDatabase:^(AMAFMDatabase *db) {
[AMAMigrationUtils resetStartupUpdatedAtToDistantPastInDatabase:database db:db];
}];
[self migrateExtendedStartupParametersIfNeeded:database];
}

[AMAMigrationTo500Utils migrateUUID];

// Reset startup update date
[database inDatabase:^(AMAFMDatabase *db) {
[AMAMigrationUtils resetStartupUpdatedAtToDistantPastInDatabase:database db:db];
}];

[self migrateExtendedStartupParametersIfNeeded:database];
[self migrateInstantIfNeeded:database];

[AMAMigrationTo500Utils migrateCrashReportsIfNeeded];
}
}

- (void)migrateInstantIfNeeded:(id<AMADatabaseProtocol>)database
{
NSString *instantMigrationPath = [[AMAMigrationTo500Utils migrationPath] stringByAppendingPathComponent:kAMAInstantFileName];
if ([AMAFileUtility fileExistsAtPath:instantMigrationPath]) {
[AMAMigrationTo500Utils migrateUUID];
}
}

- (void)migrateData:(NSString *)sourceDBPath
database:(id<AMADatabaseProtocol>)database
{
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -38,8 +38,8 @@ - (void)applyMigrationToDatabase:(id<AMADatabaseProtocol>)database
@synchronized (self) {
if ([AMAFileUtility fileExistsAtPath:oldDBPath]) {
[self migrateReporterData:oldDBPath database:database];
[AMAMigrationTo500Utils migrateReporterEventHashes:oldDirPath apiKey:self.apiKey];
}
[AMAMigrationTo500Utils migrateReporterEventHashes:oldDirPath apiKey:self.apiKey];
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#import "AMATableDescriptionProvider.h"
#import "AMAMigrationTo580Utils.h"
#import "AMADatabaseFactory.h"
#import "AMAEventNameHashesStorageFactory.h"

@interface AMAReporterDataMigrationTo580 ()

Expand Down Expand Up @@ -35,20 +36,33 @@ - (NSString *)migrationKey

- (void)applyMigrationToDatabase:(id<AMADatabaseProtocol>)database
{
NSString *reporterPath = [[AMAFileUtility persistentPathForApiKey:self.apiKey]
stringByAppendingPathComponent:@"data.sqlite"];
@synchronized (self) {
if ([AMAFileUtility fileExistsAtPath:reporterPath] == NO) {
return;
}
if (self.main) {
[self migrateReporterData:reporterPath database:database];
[AMAMigrationTo580Utils migrateReporterEventHashes:self.apiKey];
[self copyReporterBackup];
[self migrateReporterIfNeeded:database];
[self migrateEventHashesIfNeeded:database];
[self migrateBackupIfNeeded];
}
}
}

- (void)migrateReporterIfNeeded:(id<AMADatabaseProtocol>)database
{
NSString *reporterPath = [[AMAFileUtility persistentPathForApiKey:self.apiKey]
stringByAppendingPathComponent:@"data.sqlite"];
if ([AMAFileUtility fileExistsAtPath:reporterPath]) {
[self migrateReporterData:reporterPath database:database];
}
}

- (void)migrateEventHashesIfNeeded:(id<AMADatabaseProtocol>)database
{
NSString *migrationEventHashesPath = [[AMAFileUtility persistentPathForApiKey:self.apiKey]
stringByAppendingPathComponent:kAMAEventHashesFileName];
if ([AMAFileUtility fileExistsAtPath:migrationEventHashesPath]) {
[AMAMigrationTo580Utils migrateReporterEventHashes:self.apiKey];
}
}

- (void)migrateReporterData:(NSString *)sourceDBPath
database:(id<AMADatabaseProtocol>)database
{
Expand Down Expand Up @@ -79,20 +93,21 @@ - (void)migrateReporterData:(NSString *)sourceDBPath
[sourceDB close];
}

- (void)copyReporterBackup
- (void)migrateBackupIfNeeded
{
NSString *reporterBackPath = [[AMAFileUtility persistentPathForApiKey:self.apiKey] stringByAppendingPathComponent:@"data.bak"];
NSString *newDirPath = [[AMAFileUtility persistentPathForApiKey:kAMAMainReporterDBPath] stringByAppendingPathComponent:@"data.bak"];

NSFileManager *fileManager = [NSFileManager defaultManager];
NSString *reporterBackPath = [[AMAFileUtility persistentPathForApiKey:self.apiKey]
stringByAppendingPathComponent:@"data.bak"];
NSString *newDirPath = [[AMAFileUtility persistentPathForApiKey:kAMAMainReporterDBPath]
stringByAppendingPathComponent:@"data.bak"];

if (![fileManager fileExistsAtPath:reporterBackPath]) {
if (![AMAFileUtility fileExistsAtPath:reporterBackPath]) {
return;
}
if ([fileManager fileExistsAtPath:newDirPath]) {
if ([AMAFileUtility fileExistsAtPath:newDirPath]) {
return;
}


NSFileManager *fileManager = [NSFileManager defaultManager];
NSError *error;
if (![fileManager copyItemAtPath:reporterBackPath toPath:newDirPath error:&error]) {
AMALogWarn(@"Failed to copy reporter backup from %@ to %@: %@", reporterBackPath, newDirPath, error);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,8 @@

@class AMAEventNameHashesStorage;

extern NSString *const kAMAEventHashesFileName;

@interface AMAEventNameHashesStorageFactory : NSObject

- (instancetype)init NS_UNAVAILABLE;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,20 @@
#import "AMAEventNameHashesStorage.h"
#import "AMADatabaseFactory.h"

static NSString *const kAMAFileName = @"event_hashes.bin";
NSString *const kAMAEventHashesFileName = @"event_hashes.bin";

@implementation AMAEventNameHashesStorageFactory

+ (AMAEventNameHashesStorage *)storageForApiKey:(NSString *)apiKey main:(BOOL)main
{
NSString *dirPath = main ? kAMAMainReporterDBPath : apiKey;
NSString *filePath = [[AMAFileUtility persistentPathForApiKey:dirPath] stringByAppendingPathComponent:kAMAFileName];
NSString *filePath = [[AMAFileUtility persistentPathForApiKey:dirPath] stringByAppendingPathComponent:kAMAEventHashesFileName];
return [self storageForPath:filePath];
}

+ (AMAEventNameHashesStorage *)migrationStorageForPath:(NSString *)path
{
NSString *filePath = [path stringByAppendingPathComponent:kAMAFileName];
NSString *filePath = [path stringByAppendingPathComponent:kAMAEventHashesFileName];
return [self storageForPath:filePath];
}

Expand Down
Loading

0 comments on commit b7c28eb

Please sign in to comment.