1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-12-26 01:28:50 -05:00

Bullet-proofing for database upgrades and better fallback for failures

This commit is contained in:
Jesse Vincent 2010-01-24 20:41:04 +00:00
parent 08ed1ed389
commit ff390b388a

View File

@ -6,6 +6,7 @@ import android.content.ContentValues;
import android.content.SharedPreferences; import android.content.SharedPreferences;
import android.database.Cursor; import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase; import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.net.Uri; import android.net.Uri;
import android.text.util.Regex; import android.text.util.Regex;
import android.util.Log; import android.util.Log;
@ -116,6 +117,8 @@ public class LocalStore extends Store implements Serializable
AttachmentProvider.clear(application); AttachmentProvider.clear(application);
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
if (mDb.getVersion() < 29) if (mDb.getVersion() < 29)
@ -159,9 +162,19 @@ public class LocalStore extends Store implements Serializable
{ // in the case that we're starting out at 29 or newer, run all the needed updates { // in the case that we're starting out at 29 or newer, run all the needed updates
if (mDb.getVersion() < 30) if (mDb.getVersion() < 30)
{
try
{ {
mDb.execSQL("ALTER TABLE messages ADD deleted INTEGER default 0"); mDb.execSQL("ALTER TABLE messages ADD deleted INTEGER default 0");
} }
catch (SQLiteException e)
{
if (! e.toString().startsWith("duplicate column name: deleted"))
{
throw e;
}
}
}
if (mDb.getVersion() < 31) if (mDb.getVersion() < 31)
{ {
mDb.execSQL("DROP INDEX IF EXISTS msg_folder_id_date"); mDb.execSQL("DROP INDEX IF EXISTS msg_folder_id_date");
@ -172,17 +185,37 @@ public class LocalStore extends Store implements Serializable
mDb.execSQL("UPDATE messages SET deleted = 1 WHERE flags LIKE '%DELETED%'"); mDb.execSQL("UPDATE messages SET deleted = 1 WHERE flags LIKE '%DELETED%'");
} }
if (mDb.getVersion() < 33) if (mDb.getVersion() < 33)
{
try
{ {
mDb.execSQL("ALTER TABLE messages ADD preview TEXT"); mDb.execSQL("ALTER TABLE messages ADD preview TEXT");
// mDb.execSQL("UPDATE messages SET preview = SUBSTR(text_content,1,200) WHERE deleted = 0"); }
catch (SQLiteException e)
{
if (! e.toString().startsWith("duplicate column name: preview"))
{
throw e;
}
}
} }
} }
}
catch (SQLiteException e)
{
Log.e(K9.LOG_TAG, "Exception while upgrading database. Resetting the DB to v0");
mDb.setVersion(0);
throw new Error("Database upgrade failed! Resetting your DB version to 0 to force a full schema recreation.");
}
mDb.setVersion(DB_VERSION); mDb.setVersion(DB_VERSION);
if (mDb.getVersion() != DB_VERSION) if (mDb.getVersion() != DB_VERSION)
{ {
throw new Error("Database upgrade failed!"); throw new Error("Database upgrade failed!");