All: Improved handling of database migration failures

pull/3880/head
Laurent Cozic 2020-10-06 12:47:33 +01:00
parent 00057da17d
commit f334f4f487
1 changed files with 22 additions and 3 deletions

View File

@ -349,6 +349,8 @@ class JoplinDatabase extends Database {
+ `Expected version: ${existingDatabaseVersions[existingDatabaseVersions.length - 1]}`); + `Expected version: ${existingDatabaseVersions[existingDatabaseVersions.length - 1]}`);
} }
this.logger().info(`Upgrading database from version ${fromVersion}`);
if (currentVersionIndex == existingDatabaseVersions.length - 1) return fromVersion; if (currentVersionIndex == existingDatabaseVersions.length - 1) return fromVersion;
let latestVersion = fromVersion; let latestVersion = fromVersion;
@ -856,18 +858,32 @@ class JoplinDatabase extends Database {
queries.push('CREATE VIRTUAL TABLE notes_spellfix USING spellfix1'); queries.push('CREATE VIRTUAL TABLE notes_spellfix USING spellfix1');
} }
queries.push({ sql: 'UPDATE version SET version = ?', params: [targetVersion] }); const updateVersionQuery = { sql: 'UPDATE version SET version = ?', params: [targetVersion] };
queries.push(updateVersionQuery);
try { try {
await this.transactionExecBatch(queries); await this.transactionExecBatch(queries);
} catch (error) { } catch (error) {
// In some cases listed below, when the upgrade fail it is acceptable (a fallback will be used)
// and in those cases, even though it fails, we still want to set the version number so that the
// migration is not repeated on next upgrade.
let saveVersionAgain = false;
if (targetVersion === 15 || targetVersion === 18 || targetVersion === 33) { if (targetVersion === 15 || targetVersion === 18 || targetVersion === 33) {
this.logger().warn('Could not upgrade to database v15 or v18 or v33 - FTS feature will not be used', error); this.logger().warn('Could not upgrade to database v15 or v18 or v33 - FTS feature will not be used', error);
saveVersionAgain = true;
} else if (targetVersion === 34) { } else if (targetVersion === 34) {
// this.logger().warn('Could not upgrade to database v34 - fuzzy search will not be used', error); this.logger().warn('Could not upgrade to database v34 - fuzzy search will not be used', error);
saveVersionAgain = true;
} else { } else {
throw error; throw error;
} }
if (saveVersionAgain) {
this.logger().info('Migration failed with fallback and will not be repeated - saving version number');
await this.transactionExecBatch([updateVersionQuery]);
}
} }
latestVersion = targetVersion; latestVersion = targetVersion;
@ -933,10 +949,13 @@ class JoplinDatabase extends Database {
const version = !versionRow ? 0 : versionRow.version; const version = !versionRow ? 0 : versionRow.version;
const tableFieldsVersion = !versionRow ? 0 : versionRow.table_fields_version; const tableFieldsVersion = !versionRow ? 0 : versionRow.table_fields_version;
this.version_ = version; this.version_ = version;
this.logger().info('Current database version', version); this.logger().info('Current database version', versionRow);
const newVersion = await this.upgradeDatabase(version); const newVersion = await this.upgradeDatabase(version);
this.version_ = newVersion; this.version_ = newVersion;
this.logger().info(`New version: ${newVersion}. Previously recorded version: ${tableFieldsVersion}`);
if (newVersion !== tableFieldsVersion) await this.refreshTableFields(newVersion); if (newVersion !== tableFieldsVersion) await this.refreshTableFields(newVersion);
this.tableFields_ = {}; this.tableFields_ = {};