[MediaWiki-commits] [Gerrit] Migration fixes - Bug: T96607 Sending migration failures ... - change (apps...wikipedia)

2015-05-07 Thread Bgerstle (Code Review)
Bgerstle has submitted this change and it was merged.

Change subject: Migration fixes -  Bug: T96607 Sending migration failures to 
OTRS Adding build number to user agent string Adding logic to remove old 
backups after 30 days Refactor many methods to be instance methods to make the 
classes testable Add tests for removing 
..


Migration fixes -  Bug: T96607
Sending migration failures to OTRS
Adding build number to user agent string
Adding logic to remove old backups after 30 days
Refactor many methods to be instance methods to make the classes testable
Add tests for removing and moving the store - but they don't work in the test 
bundle :(

Change-Id: I2c9dccbfb04803dc4d80ee8dcae64b76f13a39e3
---
M OldDataSchema/Data/ArticleDataContextSingleton.h
M OldDataSchema/Data/ArticleDataContextSingleton.m
M Wikipedia/AppDelegate.m
M Wikipedia/Data/OldDataSchemaMigrator.h
M Wikipedia/Data/OldDataSchemaMigrator.m
M Wikipedia/Data/OldDataSchemaMigrator_Private.h
M Wikipedia/View Controllers/DataMigration/DataMigrationProgressViewController.h
M Wikipedia/View Controllers/DataMigration/DataMigrationProgressViewController.m
M Wikipedia/mw-utils/WikipediaAppUtils.m
M WikipediaUnitTests/OldDataSchemaMigratorTests.m
10 files changed, 287 insertions(+), 145 deletions(-)

Approvals:
  Bgerstle: Looks good to me, approved
  jenkins-bot: Verified



diff --git a/OldDataSchema/Data/ArticleDataContextSingleton.h 
b/OldDataSchema/Data/ArticleDataContextSingleton.h
index d533b7e..fd5b76a 100644
--- a/OldDataSchema/Data/ArticleDataContextSingleton.h
+++ b/OldDataSchema/Data/ArticleDataContextSingleton.h
@@ -1,5 +1,3 @@
-//  Created by Monte Hurd on 11/27/13.
-//  Copyright (c) 2013 Wikimedia Foundation. Provided under MIT-style license; 
please copy and modify!
 
 #import CoreData/CoreData.h
 
@@ -7,10 +5,33 @@
 
 + (ArticleDataContextSingleton *)sharedInstance;
 
+/**
+ *  The path to the DB
+ *
+ *  @return The path
+ */
+- (NSString*)databasePath;
+
+/**
+ *  Created lazily
+ */
 @property (nonatomic, retain) NSManagedObjectContext *mainContext;
 
+/**
+ *  Create a new background context. You are responsible for its lifecycle
+ *  and propagating changes to the store.
+ *
+ *  @return A new background context
+ */
 - (NSManagedObjectContext*)backgroundContext;
 
+/**
+ *  Automatically propagates changes to the store.
+ *  Use this for background contexts to handle the save propagation for you.
+ *
+ *  @param context The context to save
+ *  @param completionBlock a completion block fired after the save operation
+ */
 - (void)saveContextAndPropagateChangesToStore:(NSManagedObjectContext*)context 
completionBlock:(void(^)(NSError* error))completionBlock;
 
 @end
diff --git a/OldDataSchema/Data/ArticleDataContextSingleton.m 
b/OldDataSchema/Data/ArticleDataContextSingleton.m
index e6182dc..7dd54fe 100644
--- a/OldDataSchema/Data/ArticleDataContextSingleton.m
+++ b/OldDataSchema/Data/ArticleDataContextSingleton.m
@@ -1,12 +1,8 @@
-//  Created by Monte Hurd on 11/27/13.
-//  Copyright (c) 2013 Wikimedia Foundation. Provided under MIT-style license; 
please copy and modify!
 
 #import ArticleDataContextSingleton.h
 #import NSManagedObjectModel+OldDataSchema.h
 
-@interface ArticleDataContextSingleton (){
-
-}
+@interface ArticleDataContextSingleton ()
 
 @property (nonatomic, retain) NSManagedObjectContext *masterContext;
 
@@ -25,55 +21,66 @@
 return sharedInstance;
 }
 
-- (id)init
-{
-self = [super init];
-if (self) {
 
-[self setupMasterContext];
+- (NSString*)databasePath{
+
+NSString *articlesDBPath = [[self documentRootPath] 
stringByAppendingString:@/articleData6.sqlite];
+return articlesDBPath;
+}
+
+- (NSManagedObjectContext*)masterContext{
+
+if(!_masterContext){
+
+NSPersistentStoreCoordinator *persistentStoreCoordinator =
+[[NSPersistentStoreCoordinator alloc] 
initWithManagedObjectModel:[NSManagedObjectModel wmf_oldDataSchema]];
+
+NSURL *url = [NSURL fileURLWithPath:[self databasePath]];
+
+NSDictionary *options = @{
+  
NSMigratePersistentStoresAutomaticallyOption: @YES,
+  NSInferMappingModelAutomaticallyOption: @YES
+  };
+NSError *error = nil;
+NSPersistentStore *persistentStore = [persistentStoreCoordinator 
addPersistentStoreWithType:NSSQLiteStoreType
+   
   configuration:nil
+   
 URL:url
+   
 options:options
+   
   error:error];
+if (!persistentStore) {
+

[MediaWiki-commits] [Gerrit] Migration fixes - Bug: T96607 Sending migration failures ... - change (apps...wikipedia)

2015-05-05 Thread Fjalapeno (Code Review)
Fjalapeno has uploaded a new change for review.

  https://gerrit.wikimedia.org/r/209024

Change subject: Migration fixes -  Bug: T96607 Sending migration failures to 
OTRS Adding build number to user agent string Adding logic to remove old 
backups after 30 days
..

Migration fixes -  Bug: T96607
Sending migration failures to OTRS
Adding build number to user agent string
Adding logic to remove old backups after 30 days

Change-Id: I2c9dccbfb04803dc4d80ee8dcae64b76f13a39e3
---
M Wikipedia/AppDelegate.m
M Wikipedia/Data/OldDataSchemaMigrator.h
M Wikipedia/Data/OldDataSchemaMigrator.m
M Wikipedia/View Controllers/DataMigration/DataMigrationProgressViewController.h
M Wikipedia/View Controllers/DataMigration/DataMigrationProgressViewController.m
M Wikipedia/mw-utils/WikipediaAppUtils.m
6 files changed, 97 insertions(+), 13 deletions(-)


  git pull ssh://gerrit.wikimedia.org:29418/apps/ios/wikipedia 
refs/changes/24/209024/1

diff --git a/Wikipedia/AppDelegate.m b/Wikipedia/AppDelegate.m
index 32c5e32..e24558c 100644
--- a/Wikipedia/AppDelegate.m
+++ b/Wikipedia/AppDelegate.m
@@ -48,6 +48,8 @@
 #endif
 
 if (![self presentDataMigrationViewControllerIfNeeded]) {
+[self performMigrationCleanup];
+
 [self presentRootViewController:NO withSplash:YES];
 }
 
@@ -137,6 +139,10 @@
 
 #pragma mark - Migration
 
+- (void)performMigrationCleanup {
+[DataMigrationProgressViewController removeOldDataBackupIfNeeded];
+}
+
 - (BOOL)presentDataMigrationViewControllerIfNeeded {
 if ([DataMigrationProgressViewController needsMigration]) {
 UIAlertView* dialog =
@@ -160,7 +166,7 @@
 - (void)alertView:(UIAlertView*)alertView 
didDismissWithButtonIndex:(NSInteger)buttonIndex {
 if (alertView == self.dataMigrationAlert) {
 if (buttonIndex == alertView.cancelButtonIndex) {
-[DataMigrationProgressViewController removeOldData];
+[DataMigrationProgressViewController moveOldDataToBackupLocation];
 [self presentRootViewController:YES withSplash:NO];
 } else {
 DataMigrationProgressViewController* migrationVC = 
[[DataMigrationProgressViewController alloc] init];
diff --git a/Wikipedia/Data/OldDataSchemaMigrator.h 
b/Wikipedia/Data/OldDataSchemaMigrator.h
index bf7c549..c2ccdbd 100644
--- a/Wikipedia/Data/OldDataSchemaMigrator.h
+++ b/Wikipedia/Data/OldDataSchemaMigrator.h
@@ -50,6 +50,20 @@
  */
 - (void)migrateData;
 
-+ (void)removeOldData;
+/**
+ *  Moves old data to backup location
+ */
++ (void)moveOldDataToBackupLocation;
+
+/**
+ *  Removes old data if it is older than the grace period for backups
+ */
++ (void)removeOldDataIfOlderThanMaximumGracePeriod;
+
+/**
+ *  Removes backups immediately
+ */
++ (void)removeOldDataImmediately;
+
 
 @end
diff --git a/Wikipedia/Data/OldDataSchemaMigrator.m 
b/Wikipedia/Data/OldDataSchemaMigrator.m
index cb45974..aa597ec 100644
--- a/Wikipedia/Data/OldDataSchemaMigrator.m
+++ b/Wikipedia/Data/OldDataSchemaMigrator.m
@@ -13,6 +13,10 @@
 #import NSManagedObjectContext+SimpleFetch.h
 #import Article+ConvenienceAccessors.h
 
+static NSString* const kWMFOldDataSchemaBackupDateKey   = 
@kWMFOldDataSchemaBackupDateKey;
+static NSUInteger const kWMFOldDataSchemaBackupExpirationTimeInDays = 30;
+
+
 @interface OldDataSchemaMigrator ()
 
 @property (nonatomic, strong) ArticleDataContextSingleton* context;
@@ -47,7 +51,13 @@
 return [[NSFileManager defaultManager] fileExistsAtPath:filePath];
 }
 
-+ (void)removeOldData {
++ (BOOL)backupExists {
+NSString* filePath   = [self sqlitePath];
+NSString* backupPath = [filePath stringByAppendingString:@.bak];
+return [[NSFileManager defaultManager] fileExistsAtPath:backupPath];
+}
+
++ (void)moveOldDataToBackupLocation {
 NSString* filePath   = [self sqlitePath];
 NSString* backupPath = [filePath stringByAppendingString:@.bak];
 NSError* err = nil;
@@ -56,7 +66,52 @@
  error:err];
 if (err) {
 NSLog(@Error backing up %@: %@, filePath, err);
+} else {
+[self setBackDateToNow];
 }
+}
+
++ (void)removeOldDataIfOlderThanMaximumGracePeriod {
+[self setBackupDateForPreexistingBackups];
+
+if ([self shouldRemoveBackup]) {
+[self removeOldDataImmediately];
+}
+}
+
++ (void)removeOldDataImmediately {
+NSString* filePath   = [self sqlitePath];
+NSString* backupPath = [filePath stringByAppendingString:@.bak];
+[[NSFileManager defaultManager] removeItemAtPath:backupPath error:nil];
+}
+
++ (void)setBackDateToNow {
+NSDate* backupDate = [NSDate new];
+[[NSUserDefaults standardUserDefaults] 
setDouble:backupDate.timeIntervalSinceReferenceDate 
forKey:kWMFOldDataSchemaBackupDateKey];
+[[NSUserDefaults standardUserDefaults] synchronize];
+}
+
++ (void)setBackupDateForPreexistingBackups {
+if (([[NSUserDefaults standardUserDefaults]