Skip to content

Commit

Permalink
Merge pull request #489 from sussol/#xxxx-resync-finalised-everything…
Browse files Browse the repository at this point in the history
…-on-v2

Add all finalised data to sync queue on data migration
  • Loading branch information
Chris-Petty authored Aug 21, 2017
2 parents ef11641 + ce4ed90 commit 491df12
Showing 1 changed file with 34 additions and 13 deletions.
47 changes: 34 additions & 13 deletions src/dataMigration.js
Original file line number Diff line number Diff line change
Expand Up @@ -21,21 +21,22 @@ export async function migrateDataToVersion(database, settings) {
AsyncStorage.setItem(APP_VERSION_KEY, fromVersion);
settings.delete(SETTINGS_KEYS.APP_VERSION);
}
// If it was in neither local storage or settings, this is a new install, so no need to migrate
if (!fromVersion || fromVersion.length === 0) {
return;
}

// Get the new version we are upgrading to
const toVersion = packageJson.version;
// If the version has not changed, we are not upgrading, so don't do anything
if (fromVersion === toVersion) return;
// Do any required version update data migrations
for (const migration of dataMigrations) {
if (
compareVersions(fromVersion, migration.version) < 0 &&
compareVersions(toVersion, migration.version) >= 0
) {
migration.migrate(database, settings);

// If it was in neither local storage or settings, this is a new install, so no need to migrate
if (fromVersion && fromVersion.length !== 0) {
// If the version has not changed, we are not upgrading, so don't do anything
if (fromVersion === toVersion) return;
// Do any required version update data migrations
for (const migration of dataMigrations) {
if (
compareVersions(fromVersion, migration.version) < 0 &&
compareVersions(toVersion, migration.version) >= 0
) {
migration.migrate(database, settings);
}
}
}
// Record the new app version
Expand Down Expand Up @@ -104,6 +105,26 @@ const dataMigrations = [
});
});
});

// Previous versions did not add requisitions, transactions, or stocktakes to the sync queue
// when they were finalised, so unless they were already on the sync queue, they may not have
// synced to the server in finalised form. Find all of them, and set them to resync
database.write(() => {
// Requisitions
const finalisedRequisitions = database.objects('Requisition')
.filtered('status == "finalised"');
finalisedRequisitions.forEach(requisition => database.save('Requisition', requisition));

// Transactions
const finalisedTransactions = database.objects('Transaction')
.filtered('status == "finalised"');
finalisedTransactions.forEach(transaction => database.save('Transaction', transaction));

// Stocktakes
const finalisedStocktakes = database.objects('Stocktake')
.filtered('status == "finalised"');
finalisedStocktakes.forEach(stocktake => database.save('Stocktake', stocktake));
});
},
},
];

0 comments on commit 491df12

Please sign in to comment.