Skip to content

Commit 750cbaa

Browse files
authored
Update AuthDatabase.java
1 parent 10ffdc9 commit 750cbaa

File tree

1 file changed

+20
-29
lines changed
  • android/walletlib/src/main/java/com/solana/mobilewalletadapter/walletlib/authorization

1 file changed

+20
-29
lines changed

android/walletlib/src/main/java/com/solana/mobilewalletadapter/walletlib/authorization/AuthDatabase.java

Lines changed: 20 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -38,14 +38,15 @@ public void onConfigure(SQLiteDatabase db) {
3838

3939
@Override
4040
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
41-
if (oldVersion < 5) {
41+
if (oldVersion > newVersion) {
42+
// The documentation for this method does not explicitly state this cannot occur
43+
// (newVersion < oldVersion). We cannot downgrade so recreate the database instead.
44+
Log.w(TAG, "Database downgrade detected; DB downgrade is not supported");
45+
recreateDatabase(db);
46+
} else if (oldVersion < 5) {
4247
Log.w(TAG, "Old database schema detected; pre-v1.0.0, no DB schema backward compatibility is implemented");
43-
db.execSQL("DROP TABLE IF EXISTS " + IdentityRecordSchema.TABLE_IDENTITIES);
44-
db.execSQL("DROP TABLE IF EXISTS " + AuthorizationsSchema.TABLE_AUTHORIZATIONS);
45-
db.execSQL("DROP TABLE IF EXISTS " + PublicKeysSchema.TABLE_PUBLIC_KEYS);
46-
db.execSQL("DROP TABLE IF EXISTS " + WalletUriBaseSchema.TABLE_WALLET_URI_BASE);
47-
onCreate(db);
48-
} else {
48+
recreateDatabase(db);
49+
} else try {
4950
// first migrate from public keys to accounts if necessary
5051
if (oldVersion == 5) {
5152
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) {
8889
// migrate to multi account structure
8990
Log.w(TAG, "Old database schema detected; pre-v2.1.0, migrating to multi account structure");
9091

91-
// try (final Cursor cursor = db.rawQuery("SELECT " +
92-
// AuthorizationsSchema.TABLE_AUTHORIZATIONS + '.' + AuthorizationsSchema.COLUMN_AUTHORIZATIONS_ID +
93-
// ", " + AuthorizationsSchema.TABLE_AUTHORIZATIONS + '.' + AuthorizationsSchema.COLUMN_AUTHORIZATIONS_ACCOUNT_ID +
94-
// ", " + AccountRecordsSchema.TABLE_ACCOUNTS + '.' + AccountRecordsSchema.COLUMN_ACCOUNTS_ID +
95-
// ", " + AccountRecordsSchema.TABLE_ACCOUNTS + '.' + AccountRecordsSchema.COLUMN_ACCOUNTS_PUBLIC_KEY_RAW +
96-
// ", " + AccountRecordsSchema.TABLE_ACCOUNTS + '.' + AccountRecordsSchema.COLUMN_ACCOUNTS_LABEL +
97-
// " FROM " + AuthorizationsSchema.TABLE_AUTHORIZATIONS +
98-
// " INNER JOIN " + AccountRecordsSchema.TABLE_ACCOUNTS +
99-
// " ON " + AuthorizationsSchema.TABLE_AUTHORIZATIONS + '.' + AuthorizationsSchema.COLUMN_AUTHORIZATIONS_ACCOUNT_ID +
100-
// " = " + AccountRecordsSchema.TABLE_ACCOUNTS + '.' + AccountRecordsSchema.COLUMN_ACCOUNTS_ID,
101-
// null)) {
102-
// AccountRecordsDao accountRecordsDao = new AccountRecordsDao(db);
103-
// while (cursor.moveToNext()) {
104-
// final int parentId = cursor.getInt(0);
105-
// final int accountId = cursor.getInt(1);
106-
// ContentValues values = new ContentValues();
107-
// values.put(AccountRecordsSchema.COLUMN_ACCOUNTS_PARENT_ID, parentId);
108-
// accountRecordsDao.update(AccountRecordsSchema.TABLE_ACCOUNTS, values,
109-
// AccountRecordsSchema.COLUMN_ACCOUNTS_ID + "=" + accountId, null);
110-
// }
111-
// }
112-
11392
// migrate to multi account structure
11493
// first add parent id column to accounts table
11594
// add parent ids to accounts table
@@ -165,9 +144,21 @@ public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
165144
authorizationMigrationTable);
166145

167146
db.execSQL("DROP TABLE IF EXISTS " + authorizationMigrationTable);
147+
} catch (Throwable ignored) {
148+
Log.w(TAG, "Database migration failed, recreating database");
149+
recreateDatabase(db);
168150
}
169151
}
170152

153+
private void recreateDatabase(SQLiteDatabase db) {
154+
db.execSQL("DROP TABLE IF EXISTS " + IdentityRecordSchema.TABLE_IDENTITIES);
155+
db.execSQL("DROP TABLE IF EXISTS " + AuthorizationsSchema.TABLE_AUTHORIZATIONS);
156+
db.execSQL("DROP TABLE IF EXISTS " + PublicKeysSchema.TABLE_PUBLIC_KEYS);
157+
db.execSQL("DROP TABLE IF EXISTS " + WalletUriBaseSchema.TABLE_WALLET_URI_BASE);
158+
db.execSQL("DROP TABLE IF EXISTS " + AccountRecordsSchema.TABLE_ACCOUNTS);
159+
onCreate(db);
160+
}
161+
171162
@NonNull
172163
public static String getDatabaseName(@NonNull AuthIssuerConfig authIssuerConfig) {
173164
return authIssuerConfig.name + DATABASE_NAME_SUFFIX;

0 commit comments

Comments
 (0)