Merge "Recreate recoverablekeystore database if upgrade fails." into udc-dev
This commit is contained in:
committed by
Android (Google) Code Review
commit
d5c227a71f
@@ -18,6 +18,7 @@ package com.android.server.locksettings.recoverablekeystore.storage;
|
|||||||
|
|
||||||
import android.content.Context;
|
import android.content.Context;
|
||||||
import android.database.sqlite.SQLiteDatabase;
|
import android.database.sqlite.SQLiteDatabase;
|
||||||
|
import android.database.sqlite.SQLiteException;
|
||||||
import android.database.sqlite.SQLiteOpenHelper;
|
import android.database.sqlite.SQLiteOpenHelper;
|
||||||
import android.util.Log;
|
import android.util.Log;
|
||||||
|
|
||||||
@@ -122,14 +123,14 @@ class RecoverableKeyStoreDbHelper extends SQLiteOpenHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
private static int getDbVersion(Context context) {
|
private static int getDbVersion(Context context) {
|
||||||
// TODO(b/254335492): Check flag
|
// TODO(b/254335492): Update to version 7 and clean up code.
|
||||||
return DATABASE_VERSION;
|
return DATABASE_VERSION;
|
||||||
}
|
}
|
||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onCreate(SQLiteDatabase db) {
|
public void onCreate(SQLiteDatabase db) {
|
||||||
db.execSQL(SQL_CREATE_KEYS_ENTRY);
|
db.execSQL(SQL_CREATE_KEYS_ENTRY);
|
||||||
if (db.getVersion() == 6) {
|
if (db.getVersion() == 6) { // always false
|
||||||
db.execSQL(SQL_CREATE_USER_METADATA_ENTRY);
|
db.execSQL(SQL_CREATE_USER_METADATA_ENTRY);
|
||||||
} else {
|
} else {
|
||||||
db.execSQL(SQL_CREATE_USER_METADATA_ENTRY_FOR_V7);
|
db.execSQL(SQL_CREATE_USER_METADATA_ENTRY_FOR_V7);
|
||||||
@@ -147,6 +148,7 @@ class RecoverableKeyStoreDbHelper extends SQLiteOpenHelper {
|
|||||||
|
|
||||||
@Override
|
@Override
|
||||||
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
|
||||||
|
try {
|
||||||
if (oldVersion < 2) {
|
if (oldVersion < 2) {
|
||||||
dropAllKnownTables(db); // Wipe database.
|
dropAllKnownTables(db); // Wipe database.
|
||||||
onCreate(db);
|
onCreate(db);
|
||||||
@@ -174,10 +176,19 @@ class RecoverableKeyStoreDbHelper extends SQLiteOpenHelper {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (oldVersion < 7 && newVersion >= 7) {
|
if (oldVersion < 7 && newVersion >= 7) {
|
||||||
|
try {
|
||||||
upgradeDbForVersion7(db);
|
upgradeDbForVersion7(db);
|
||||||
|
} catch (SQLiteException e) {
|
||||||
|
Log.w(TAG, "Column was added without version update - ignore error", e);
|
||||||
|
}
|
||||||
oldVersion = 7;
|
oldVersion = 7;
|
||||||
}
|
}
|
||||||
|
} catch (SQLiteException e) {
|
||||||
|
Log.e(TAG, "Recreating recoverablekeystore after unexpected upgrade error.", e);
|
||||||
|
dropAllKnownTables(db); // Wipe database.
|
||||||
|
onCreate(db);
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (oldVersion != newVersion) {
|
if (oldVersion != newVersion) {
|
||||||
Log.e(TAG, "Failed to update recoverablekeystore database to the most recent version");
|
Log.e(TAG, "Failed to update recoverablekeystore database to the most recent version");
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -157,6 +157,20 @@ public class RecoverableKeyStoreDbHelperTest {
|
|||||||
checkAllColumns_latest();
|
checkAllColumns_latest();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onUpgradeToV7_ignoresDuplicateColumnError() throws Exception {
|
||||||
|
mDatabaseHelper.onCreate(mDatabase);
|
||||||
|
mDatabaseHelper.onUpgrade(mDatabase, 6, 7);
|
||||||
|
checkAllColumns_latest();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Test
|
||||||
|
public void onUpgradeToV7_recreatesDatabaseAfterFailure() throws Exception {
|
||||||
|
mDatabaseHelper.onCreate(mDatabase);
|
||||||
|
mDatabaseHelper.onUpgrade(mDatabase, 1, 7);
|
||||||
|
checkAllColumns_latest();
|
||||||
|
}
|
||||||
|
|
||||||
private boolean isRootOfTrustTableAvailable() {
|
private boolean isRootOfTrustTableAvailable() {
|
||||||
ContentValues values = new ContentValues();
|
ContentValues values = new ContentValues();
|
||||||
values.put(RootOfTrustEntry.COLUMN_NAME_USER_ID, TEST_USER_ID);
|
values.put(RootOfTrustEntry.COLUMN_NAME_USER_ID, TEST_USER_ID);
|
||||||
|
|||||||
Reference in New Issue
Block a user