From 750cbaa2543398dee751fb5cfdee336fdc26d225 Mon Sep 17 00:00:00 2001 From: Marco Martinez Date: Mon, 2 Dec 2024 18:56:13 -0700 Subject: [PATCH] Update AuthDatabase.java --- .../walletlib/authorization/AuthDatabase.java | 49 ++++++++----------- 1 file changed, 20 insertions(+), 29 deletions(-) diff --git a/android/walletlib/src/main/java/com/solana/mobilewalletadapter/walletlib/authorization/AuthDatabase.java b/android/walletlib/src/main/java/com/solana/mobilewalletadapter/walletlib/authorization/AuthDatabase.java index 6575b0fc8..6c5560201 100644 --- a/android/walletlib/src/main/java/com/solana/mobilewalletadapter/walletlib/authorization/AuthDatabase.java +++ b/android/walletlib/src/main/java/com/solana/mobilewalletadapter/walletlib/authorization/AuthDatabase.java @@ -38,14 +38,15 @@ public void onConfigure(SQLiteDatabase db) { @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { - if (oldVersion < 5) { + if (oldVersion > newVersion) { + // The documentation for this method does not explicitly state this cannot occur + // (newVersion < oldVersion). We cannot downgrade so recreate the database instead. + Log.w(TAG, "Database downgrade detected; DB downgrade is not supported"); + recreateDatabase(db); + } else if (oldVersion < 5) { Log.w(TAG, "Old database schema detected; pre-v1.0.0, no DB schema backward compatibility is implemented"); - db.execSQL("DROP TABLE IF EXISTS " + IdentityRecordSchema.TABLE_IDENTITIES); - db.execSQL("DROP TABLE IF EXISTS " + AuthorizationsSchema.TABLE_AUTHORIZATIONS); - db.execSQL("DROP TABLE IF EXISTS " + PublicKeysSchema.TABLE_PUBLIC_KEYS); - db.execSQL("DROP TABLE IF EXISTS " + WalletUriBaseSchema.TABLE_WALLET_URI_BASE); - onCreate(db); - } else { + recreateDatabase(db); + } else try { // first migrate from public keys to accounts if necessary if (oldVersion == 5) { Log.w(TAG, "Old database schema detected; pre-v2.0.0, migrating public keys to account records"); @@ -88,28 +89,6 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { // migrate to multi account structure Log.w(TAG, "Old database schema detected; pre-v2.1.0, migrating to multi account structure"); -// try (final Cursor cursor = db.rawQuery("SELECT " + -// AuthorizationsSchema.TABLE_AUTHORIZATIONS + '.' + AuthorizationsSchema.COLUMN_AUTHORIZATIONS_ID + -// ", " + AuthorizationsSchema.TABLE_AUTHORIZATIONS + '.' + AuthorizationsSchema.COLUMN_AUTHORIZATIONS_ACCOUNT_ID + -// ", " + AccountRecordsSchema.TABLE_ACCOUNTS + '.' + AccountRecordsSchema.COLUMN_ACCOUNTS_ID + -// ", " + AccountRecordsSchema.TABLE_ACCOUNTS + '.' + AccountRecordsSchema.COLUMN_ACCOUNTS_PUBLIC_KEY_RAW + -// ", " + AccountRecordsSchema.TABLE_ACCOUNTS + '.' + AccountRecordsSchema.COLUMN_ACCOUNTS_LABEL + -// " FROM " + AuthorizationsSchema.TABLE_AUTHORIZATIONS + -// " INNER JOIN " + AccountRecordsSchema.TABLE_ACCOUNTS + -// " ON " + AuthorizationsSchema.TABLE_AUTHORIZATIONS + '.' + AuthorizationsSchema.COLUMN_AUTHORIZATIONS_ACCOUNT_ID + -// " = " + AccountRecordsSchema.TABLE_ACCOUNTS + '.' + AccountRecordsSchema.COLUMN_ACCOUNTS_ID, -// null)) { -// AccountRecordsDao accountRecordsDao = new AccountRecordsDao(db); -// while (cursor.moveToNext()) { -// final int parentId = cursor.getInt(0); -// final int accountId = cursor.getInt(1); -// ContentValues values = new ContentValues(); -// values.put(AccountRecordsSchema.COLUMN_ACCOUNTS_PARENT_ID, parentId); -// accountRecordsDao.update(AccountRecordsSchema.TABLE_ACCOUNTS, values, -// AccountRecordsSchema.COLUMN_ACCOUNTS_ID + "=" + accountId, null); -// } -// } - // migrate to multi account structure // first add parent id column to accounts table // add parent ids to accounts table @@ -165,9 +144,21 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { authorizationMigrationTable); db.execSQL("DROP TABLE IF EXISTS " + authorizationMigrationTable); + } catch (Throwable ignored) { + Log.w(TAG, "Database migration failed, recreating database"); + recreateDatabase(db); } } + private void recreateDatabase(SQLiteDatabase db) { + db.execSQL("DROP TABLE IF EXISTS " + IdentityRecordSchema.TABLE_IDENTITIES); + db.execSQL("DROP TABLE IF EXISTS " + AuthorizationsSchema.TABLE_AUTHORIZATIONS); + db.execSQL("DROP TABLE IF EXISTS " + PublicKeysSchema.TABLE_PUBLIC_KEYS); + db.execSQL("DROP TABLE IF EXISTS " + WalletUriBaseSchema.TABLE_WALLET_URI_BASE); + db.execSQL("DROP TABLE IF EXISTS " + AccountRecordsSchema.TABLE_ACCOUNTS); + onCreate(db); + } + @NonNull public static String getDatabaseName(@NonNull AuthIssuerConfig authIssuerConfig) { return authIssuerConfig.name + DATABASE_NAME_SUFFIX;