Skip to content

Commit 6534fce

Browse files
committed
MOBILE-1902 wiki: Migrate new pages db
1 parent f93b524 commit 6534fce

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

www/addons/mod/wiki/services/wiki_offline.js

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@
1414

1515
angular.module('mm.addons.mod_wiki')
1616

17-
.constant('mmaModWikiNewPagesStore', 'mma_mod_wiki_new_pages')
17+
.constant('mmaModWikiNewPagesStore', 'mma_mod_wiki_new_pages_store')
1818

1919
.config(function($mmSitesFactoryProvider, mmaModWikiNewPagesStore) {
2020
var stores = [

www/core/lib/updatemanager.js

Lines changed: 77 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ angular.module('mm.core')
7777
if (versionCode >= 2017 && versionApplied < 2017) {
7878
promises.push(setCalendarDefaultNotifTime());
7979
promises.push(setSitesConfig());
80+
promises.push(migrateWikiNewPagesStore());
8081
}
8182

8283
return $q.all(promises).then(function() {
@@ -424,6 +425,7 @@ angular.module('mm.core')
424425
/**
425426
* Store the config of a site.
426427
*
428+
* @param {String} siteId Site ID.
427429
* @return {Promise} Promise resolved when the config is loaded for the site.
428430
*/
429431
function setSiteConfig(siteId) {
@@ -443,5 +445,80 @@ angular.module('mm.core')
443445
});
444446
}
445447

448+
/**
449+
* The store for new wiki pages had changed the number of index in the keyPath. To avoid problems and loosing data, old store
450+
* is going to be migrated to a new one and old entries deleted once migrated.
451+
* Since it can be slow, we'll only block migrating the db of current site, the rest will be in background.
452+
*
453+
* @return {Promise} Promise resolved when the db is migrated.
454+
*/
455+
function migrateWikiNewPagesStore() {
456+
return $mmSitesManager.getSitesIds().then(function(siteIds) {
457+
458+
return $mmSitesManager.getStoredCurrentSiteId().catch(function() {
459+
// Error getting current site.
460+
}).then(function(currentSiteId) {
461+
var promise;
462+
463+
// Load the config of current site first.
464+
if (currentSiteId) {
465+
promise = migrateWikiNewPagesSiteStore(currentSiteId);
466+
} else {
467+
promise = $q.when();
468+
}
469+
470+
// Load the config of rest of sites in background.
471+
angular.forEach(siteIds, function(siteId) {
472+
if (siteId != currentSiteId) {
473+
migrateWikiNewPagesSiteStore(siteId);
474+
}
475+
});
476+
477+
return promise;
478+
});
479+
});
480+
}
481+
482+
/**
483+
* Migrate the new wiki pages store of one site. If any error, data will be lost without asking.
484+
*
485+
* @param {String} siteId Site ID.
486+
* @return {Promise} Promise resolved when the data is migraded for the site.
487+
*/
488+
function migrateWikiNewPagesSiteStore(siteId) {
489+
return $mmSitesManager.getSite(siteId).then(function(site) {
490+
var $mmaModWikiOffline = $injector.get('$mmaModWikiOffline'),
491+
oldStorageName = 'mma_mod_wiki_new_pages', // Old mmaModWikiNewPagesStore constant.
492+
db = site.getDb();
493+
494+
try {
495+
return db.getAll(oldStorageName).then(function(pages) {
496+
if (pages.length > 0) {
497+
$log.debug('Found ' + pages.length + ' new wiki pages from old store to migrate on site' + siteId);
498+
499+
var promises = [];
500+
angular.forEach(pages, function(page) {
501+
if (page.subwikiid > 0) {
502+
promises.push($mmaModWikiOffline.saveNewPage(page.title, page.cachedcontent, page.subwikiid, 0, 0,
503+
0, siteId));
504+
}
505+
});
506+
507+
return $q.all(promises).finally(function() {
508+
db.removeAll(oldStorageName);
509+
});
510+
}
511+
}).catch(function() {
512+
// Fail silently.
513+
return $q.when();
514+
});
515+
} catch (e) {
516+
// Fail silently.
517+
}
518+
// Fail silently.
519+
return $q.when();
520+
});
521+
}
522+
446523
return self;
447524
});

0 commit comments

Comments
 (0)