1
0
mirror of https://github.com/moparisthebest/k-9 synced 2025-02-11 12:40:22 -05:00

Fix code to reset the database when upgrading fails

Previously the app crashed when upgrading the database failed. Now we
reset the database version and run the upgrade code again (recreating
all tables).
This commit is contained in:
cketti 2013-07-03 04:56:40 +02:00
parent b6d7f4ae40
commit b9ecf30b5b

View File

@ -193,14 +193,22 @@ public class LocalStore extends Store implements Serializable {
@Override @Override
public void doDbUpgrade(final SQLiteDatabase db) { public void doDbUpgrade(final SQLiteDatabase db) {
try {
upgradeDatabase(db);
} catch (Exception e) {
Log.e(K9.LOG_TAG, "Exception while upgrading database. Resetting the DB to v0", e);
db.setVersion(0);
upgradeDatabase(db);
}
}
private void upgradeDatabase(final SQLiteDatabase db) {
Log.i(K9.LOG_TAG, String.format("Upgrading database from version %d to version %d", Log.i(K9.LOG_TAG, String.format("Upgrading database from version %d to version %d",
db.getVersion(), DB_VERSION)); db.getVersion(), DB_VERSION));
AttachmentProvider.clear(mApplication); AttachmentProvider.clear(mApplication);
db.beginTransaction(); db.beginTransaction();
try {
try { try {
// schema version 29 was when we moved to incremental updates // schema version 29 was when we moved to incremental updates
// in the case of a new db or a < v29 db, we blow away and start from scratch // in the case of a new db or a < v29 db, we blow away and start from scratch
@ -667,11 +675,6 @@ public class LocalStore extends Store implements Serializable {
"END"); "END");
} }
} }
} catch (SQLiteException e) {
Log.e(K9.LOG_TAG, "Exception while upgrading database. Resetting the DB to v0");
db.setVersion(0);
throw new Error("Database upgrade failed! Resetting your DB version to 0 to force a full schema recreation.");
}
db.setVersion(DB_VERSION); db.setVersion(DB_VERSION);
@ -681,19 +684,8 @@ public class LocalStore extends Store implements Serializable {
} }
if (db.getVersion() != DB_VERSION) { if (db.getVersion() != DB_VERSION) {
throw new Error("Database upgrade failed!"); throw new RuntimeException("Database upgrade failed!");
} }
// Unless we're blowing away the whole data store, there's no reason to prune attachments
// every time the user upgrades. it'll just cost them money and pain.
// try
//{
// pruneCachedAttachments(true);
//}
//catch (Exception me)
//{
// Log.e(K9.LOG_TAG, "Exception while force pruning attachments during DB update", me);
//}
} }
private void update41Metadata(final SQLiteDatabase db, SharedPreferences prefs, int id, String name) { private void update41Metadata(final SQLiteDatabase db, SharedPreferences prefs, int id, String name) {