1
0
mirror of https://github.com/moparisthebest/k-9 synced 2024-11-13 13:05:03 -05:00

Bullet-proofing for database upgrades and better fallback for failures

This commit is contained in:
Jesse Vincent 2010-01-24 20:42:02 +00:00
parent f5f9f5e8d2
commit 12b7b95bf5

View File

@ -6,6 +6,7 @@ import android.content.ContentValues;
import android.content.SharedPreferences;
import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;
import android.net.Uri;
import android.text.util.Regex;
import android.util.Log;
@ -116,6 +117,8 @@ public class LocalStore extends Store implements Serializable
AttachmentProvider.clear(application);
try
{
// 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
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
if (mDb.getVersion() < 30)
{
try
{
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)
{
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%'");
}
if (mDb.getVersion() < 33)
{
try
{
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);
if (mDb.getVersion() != DB_VERSION)
{
throw new Error("Database upgrade failed!");