am 9298c9b1: Merge "Reset the client database on version upgrade."

* commit '9298c9b1e110f8ca67db443e586b829ea150d59f':
  Reset the client database on version upgrade.
main
Jean Chalard 2014-04-04 05:18:02 +00:00 committed by Android Git Automerger
commit e36667587d
1 changed files with 31 additions and 10 deletions

View File

@ -45,10 +45,8 @@ public class MetadataDbHelper extends SQLiteOpenHelper {
// This is the first released version of the database that implements CLIENTID. It is // This is the first released version of the database that implements CLIENTID. It is
// used to identify the versions for upgrades. This should never change going forward. // used to identify the versions for upgrades. This should never change going forward.
private static final int METADATA_DATABASE_VERSION_WITH_CLIENTID = 6; private static final int METADATA_DATABASE_VERSION_WITH_CLIENTID = 6;
// This is the current database version. It should be updated when the database schema // The current database version.
// gets updated. It is passed to the framework constructor of SQLiteOpenHelper, so private static final int CURRENT_METADATA_DATABASE_VERSION = 7;
// that's what the framework uses to track our database version.
private static final int METADATA_DATABASE_VERSION = 6;
private final static long NOT_A_DOWNLOAD_ID = -1; private final static long NOT_A_DOWNLOAD_ID = -1;
@ -169,7 +167,7 @@ public class MetadataDbHelper extends SQLiteOpenHelper {
private MetadataDbHelper(final Context context, final String clientId) { private MetadataDbHelper(final Context context, final String clientId) {
super(context, super(context,
METADATA_DATABASE_NAME_STEM + (TextUtils.isEmpty(clientId) ? "" : "." + clientId), METADATA_DATABASE_NAME_STEM + (TextUtils.isEmpty(clientId) ? "" : "." + clientId),
null, METADATA_DATABASE_VERSION); null, CURRENT_METADATA_DATABASE_VERSION);
mContext = context; mContext = context;
mClientId = clientId; mClientId = clientId;
} }
@ -219,22 +217,45 @@ public class MetadataDbHelper extends SQLiteOpenHelper {
/** /**
* Upgrade the database. Upgrade from version 3 is supported. * Upgrade the database. Upgrade from version 3 is supported.
* Version 3 has a DB named METADATA_DATABASE_NAME_STEM containing a table METADATA_TABLE_NAME.
* Version 6 and above has a DB named METADATA_DATABASE_NAME_STEM containing a
* table CLIENT_TABLE_NAME, and for each client a table called METADATA_TABLE_STEM + "." + the
* name of the client and contains a table METADATA_TABLE_NAME.
* For schemas, see the above create statements. The schemas have never changed so far.
*
* This method is called by the framework. See {@link SQLiteOpenHelper#onUpgrade}
* @param db The database we are upgrading
* @param oldVersion The old database version (the one on the disk)
* @param newVersion The new database version as supplied to the constructor of SQLiteOpenHelper
*/ */
@Override @Override
public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) { public void onUpgrade(final SQLiteDatabase db, final int oldVersion, final int newVersion) {
if (METADATA_DATABASE_INITIAL_VERSION == oldVersion if (METADATA_DATABASE_INITIAL_VERSION == oldVersion
&& METADATA_DATABASE_VERSION_WITH_CLIENTID == newVersion) { && METADATA_DATABASE_VERSION_WITH_CLIENTID <= newVersion
&& CURRENT_METADATA_DATABASE_VERSION >= newVersion) {
// Upgrade from version METADATA_DATABASE_INITIAL_VERSION to version // Upgrade from version METADATA_DATABASE_INITIAL_VERSION to version
// METADATA_DATABASE_VERSION_WITH_CLIENT_ID // METADATA_DATABASE_VERSION_WITH_CLIENT_ID
// Only the default database should contain the client table, so we test for mClientId.
if (TextUtils.isEmpty(mClientId)) { if (TextUtils.isEmpty(mClientId)) {
// Only the default database should contain the client table. // Anyway in version 3 only the default table existed so the emptiness
// Anyway in version 3 only the default table existed so the emptyness
// test should always be true, but better check to be sure. // test should always be true, but better check to be sure.
createClientTable(db); createClientTable(db);
} }
} else if (METADATA_DATABASE_VERSION_WITH_CLIENTID < newVersion
&& CURRENT_METADATA_DATABASE_VERSION >= newVersion) {
// Here we drop the client table, so that all clients send us their information again.
// The client table contains the URL to hit to update the available dictionaries list,
// but the info about the dictionaries themselves is stored in the table called
// METADATA_TABLE_NAME and we want to keep it, so we only drop the client table.
db.execSQL("DROP TABLE IF EXISTS " + CLIENT_TABLE_NAME);
// Only the default database should contain the client table, so we test for mClientId.
if (TextUtils.isEmpty(mClientId)) {
createClientTable(db);
}
} else { } else {
// Version 3 was the earliest version, so we should never come here. If we do, we // If we're not in the above case, either we are upgrading from an earlier versionCode
// have no idea what this database is, so we'd better wipe it off. // and we should wipe the database, or we are handling a version we never heard about
// (can only be a bug) so it's safer to wipe the database.
db.execSQL("DROP TABLE IF EXISTS " + METADATA_TABLE_NAME); db.execSQL("DROP TABLE IF EXISTS " + METADATA_TABLE_NAME);
db.execSQL("DROP TABLE IF EXISTS " + CLIENT_TABLE_NAME); db.execSQL("DROP TABLE IF EXISTS " + CLIENT_TABLE_NAME);
onCreate(db); onCreate(db);